TinyWireM.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. TinyWireM.h - a wrapper(+) class for TWI/I2C Master library for the ATtiny on Arduino
  3. 1/21/2011 BroHogan - brohoganx10 at gmail dot com
  4. Thanks to 'jkl' for the gcc version of Atmel's USI_TWI_Master code
  5. http://www.cs.cmu.edu/~dst/ARTSI/Create/PC%20Comm/
  6. I added Atmel's original Device dependant defines section back into USI_TWI_Master.h
  7. NOTE! - It's very important to use pullups on the SDA & SCL lines! More so than with the Wire lib.
  8. USAGE is modeled after the standard Wire library . . .
  9. Put in setup():
  10. TinyWireM.begin(){ // initialize I2C lib
  11. To Send:
  12. TinyWireM.beginTransmission(uint8_t slaveAddr){ // setup slave's address (7 bit address - same as Wire)
  13. TinyWireM.send(uint8_t data){ // buffer up bytes to send - can be called multiple times
  14. someByte = TinyWireM.endTransmission(){ // actually send the bytes in the buffer
  15. // returns (optional) 0 = sucess or see USI_TWI_Master.h for error codes
  16. To Receive:
  17. someByte = TinyWireM.requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // reads 'numBytes' from slave's address
  18. // (usage optional) returns 0= success or see USI_TWI_Master.h for error codes
  19. someByte = TinyWireM.receive(){ // returns the next byte in the received buffer - called multiple times
  20. someByte = TinyWireM.available(){ // returns the number of unread bytes in the received buffer
  21. TODO: (by others!)
  22. - merge this class with TinyWireS for master & slave support in one library
  23. This library is free software; you can redistribute it and/or modify it under the
  24. terms of the GNU General Public License as published by the Free Software
  25. Foundation; either version 2.1 of the License, or any later version.
  26. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  27. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  28. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  29. */
  30. #ifndef TinyWireM_h
  31. #define TinyWireM_h
  32. #include <inttypes.h>
  33. #include "Arduino.h"
  34. #define USI_SEND 0 // indicates sending to TWI
  35. #define USI_RCVE 1 // indicates receiving from TWI
  36. #define USI_BUF_SIZE 18 // bytes in message buffer
  37. //class USI_TWI : public Stream
  38. class USI_TWI
  39. {
  40. private:
  41. static uint8_t USI_Buf[]; // holds I2C send and receive data
  42. static uint8_t USI_BufIdx; // current number of bytes in the send buff
  43. static uint8_t USI_LastRead; // number of bytes read so far
  44. static uint8_t USI_BytesAvail; // number of bytes requested but not read
  45. public:
  46. USI_TWI();
  47. void begin();
  48. void beginTransmission(uint8_t);
  49. size_t write(uint8_t);
  50. inline size_t write(uint8_t* d, uint8_t n) { uint16_t i; for (i = 0; i < n; i++) write(d[i]); return (size_t)n; }
  51. inline size_t write(unsigned long n) { return write((uint8_t)n); }
  52. inline size_t write(long n) { return write((uint8_t)n); }
  53. inline size_t write(unsigned int n) { return write((uint8_t)n); }
  54. inline size_t write(int n) { return write((uint8_t)n); }
  55. void send(uint8_t b) { write(b); }
  56. void send(uint8_t *d, uint8_t n) { write(d, n); }
  57. void send(int n) { write((uint8_t)n); }
  58. uint8_t endTransmission();
  59. uint8_t endTransmission(uint8_t);
  60. uint8_t requestFrom(uint8_t, uint8_t);
  61. int read();
  62. int available();
  63. int peek(void);
  64. void flush(void);
  65. uint8_t receive(void) {
  66. int c = read();
  67. if (c < 0) return 0;
  68. return c;
  69. }
  70. };
  71. extern USI_TWI TinyWireM;
  72. #endif