Wire.h
1 /* 2 TwoWire.h - TWI/I2C library for Arduino & Wiring 3 Copyright (c) 2006 Nicholas Zambetti. All right reserved. 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 19 Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts 20 */ 21 22 #ifdef __AVR_ATtiny85__ 23 24 #include "TinyWireM.h" 25 26 #else 27 28 #ifndef TwoWire_h 29 #define TwoWire_h 30 31 #include <inttypes.h> 32 #include "Stream.h" 33 34 #define BUFFER_LENGTH 32 35 36 // WIRE_HAS_END means Wire has end() 37 #define WIRE_HAS_END 1 38 39 class TwoWire : public Stream 40 { 41 private: 42 static uint8_t rxBuffer[]; 43 static uint8_t rxBufferIndex; 44 static uint8_t rxBufferLength; 45 46 static uint8_t txAddress; 47 static uint8_t txBuffer[]; 48 static uint8_t txBufferIndex; 49 static uint8_t txBufferLength; 50 51 static uint8_t transmitting; 52 static void (*user_onRequest)(void); 53 static void (*user_onReceive)(int); 54 static void onRequestService(void); 55 static void onReceiveService(uint8_t*, int); 56 public: 57 TwoWire(); 58 void begin(); 59 void begin(uint8_t); 60 void begin(int); 61 void end(); 62 void setClock(uint32_t); 63 void beginTransmission(uint8_t); 64 void beginTransmission(int); 65 uint8_t endTransmission(void); 66 uint8_t endTransmission(uint8_t); 67 uint8_t requestFrom(uint8_t, uint8_t); 68 uint8_t requestFrom(uint8_t, uint8_t, uint8_t); 69 uint8_t requestFrom(int, int); 70 uint8_t requestFrom(int, int, int); 71 virtual size_t write(uint8_t); 72 virtual size_t write(const uint8_t *, size_t); 73 virtual int available(void); 74 virtual int read(void); 75 virtual int peek(void); 76 virtual void flush(void); 77 void onReceive( void (*)(int) ); 78 void onRequest( void (*)(void) ); 79 80 inline size_t write(unsigned long n) { return write((uint8_t)n); } 81 inline size_t write(long n) { return write((uint8_t)n); } 82 inline size_t write(unsigned int n) { return write((uint8_t)n); } 83 inline size_t write(int n) { return write((uint8_t)n); } 84 using Print::write; 85 }; 86 87 extern TwoWire Wire; 88 89 #endif 90 91 #endif //attiny85