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