Wire.h 2.5 KB

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