|
| 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 | +#ifndef TwoWire_h |
| 23 | +#define TwoWire_h |
| 24 | + |
| 25 | +#include <inttypes.h> |
| 26 | +#include "Stream.h" |
| 27 | +#include "Arduino.h" |
| 28 | + |
| 29 | +#define BUFFER_LENGTH 32 |
| 30 | + |
| 31 | +#define MASTER_ADDRESS 0x33 |
| 32 | + |
| 33 | +// WIRE_HAS_END means Wire has end() |
| 34 | +#define WIRE_HAS_END 1 |
| 35 | + |
| 36 | +class TwoWire : public Stream |
| 37 | +{ |
| 38 | + private: |
| 39 | + static uint8_t *rxBuffer; |
| 40 | + static uint8_t rxBufferAllocated; |
| 41 | + static uint8_t rxBufferIndex; |
| 42 | + static uint8_t rxBufferLength; |
| 43 | + |
| 44 | + static uint8_t txAddress; |
| 45 | + static uint8_t *txBuffer; |
| 46 | + static uint8_t txBufferAllocated; |
| 47 | + static uint8_t txBufferIndex; |
| 48 | + static uint8_t txBufferLength; |
| 49 | + |
| 50 | + static uint8_t transmitting; |
| 51 | + |
| 52 | + uint8_t ownAddress; |
| 53 | + bool master; |
| 54 | + i2c_t _i2c; |
| 55 | + |
| 56 | + static void (*user_onRequest)(void); |
| 57 | + static void (*user_onReceive)(int); |
| 58 | + static void onRequestService(void); |
| 59 | + static void onReceiveService(uint8_t*, int); |
| 60 | + |
| 61 | + static void allocateRxBuffer(size_t length); |
| 62 | + void allocateTxBuffer(size_t length); |
| 63 | + |
| 64 | + void resetRxBuffer(void); |
| 65 | + void resetTxBuffer(void); |
| 66 | + |
| 67 | + public: |
| 68 | + TwoWire(); |
| 69 | + TwoWire(uint8_t sda, uint8_t scl); |
| 70 | + // setSCL/SDA have to be called before begin() |
| 71 | + void setSCL(uint32_t scl) { _i2c.scl = digitalPinToPinName(scl); }; |
| 72 | + void setSDA(uint32_t sda) { _i2c.sda = digitalPinToPinName(sda); }; |
| 73 | + void setSCL(PinName scl) { _i2c.scl = scl; }; |
| 74 | + void setSDA(PinName sda) { _i2c.sda = sda; }; |
| 75 | + void begin(); |
| 76 | + void begin(uint8_t); |
| 77 | + void begin(int); |
| 78 | + void end(); |
| 79 | + void setClock(uint32_t); |
| 80 | + void beginTransmission(uint8_t); |
| 81 | + void beginTransmission(int); |
| 82 | + uint8_t endTransmission(void); |
| 83 | + uint8_t endTransmission(uint8_t); |
| 84 | + uint8_t requestFrom(uint8_t, uint8_t); |
| 85 | + uint8_t requestFrom(uint8_t, uint8_t, uint8_t); |
| 86 | + uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t); |
| 87 | + uint8_t requestFrom(int, int); |
| 88 | + uint8_t requestFrom(int, int, int); |
| 89 | + virtual size_t write(uint8_t); |
| 90 | + virtual size_t write(const uint8_t *, size_t); |
| 91 | + virtual int available(void); |
| 92 | + virtual int read(void); |
| 93 | + virtual int peek(void); |
| 94 | + virtual void flush(void); |
| 95 | + void onReceive( void (*)(int) ); |
| 96 | + void onRequest( void (*)(void) ); |
| 97 | + |
| 98 | + inline size_t write(unsigned long n) { return write((uint8_t)n); } |
| 99 | + inline size_t write(long n) { return write((uint8_t)n); } |
| 100 | + inline size_t write(unsigned int n) { return write((uint8_t)n); } |
| 101 | + inline size_t write(int n) { return write((uint8_t)n); } |
| 102 | + using Print::write; |
| 103 | +}; |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +extern TwoWire Wire; |
| 108 | + |
| 109 | +#endif |
0 commit comments