| 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 ([email protected]) to implement repeated starts |
| 20 |
Modified 2020 by Greyson Christoforo ([email protected]) to implement timeouts |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef TwoWire_h |
| 24 |
#define TwoWire_h |
| 25 |
|
| 26 |
#include <inttypes.h> |
| 27 |
#include "Stream.h" |
| 28 |
|
| 29 |
#define BUFFER_LENGTH 32 |
| 30 |
|
| 31 |
// WIRE_HAS_END means Wire has end() |
| 32 |
#define WIRE_HAS_END 1 |
| 33 |
|
| 34 |
class TwoWire : public Stream |
| 35 |
{ |
| 36 |
private: |
| 37 |
static uint8_t rxBuffer[]; |
| 38 |
static uint8_t rxBufferIndex; |
| 39 |
static uint8_t rxBufferLength; |
| 40 |
|
| 41 |
static uint8_t txAddress; |
| 42 |
static uint8_t txBuffer[]; |
| 43 |
static uint8_t txBufferIndex; |
| 44 |
static uint8_t txBufferLength; |
| 45 |
|
| 46 |
static uint8_t transmitting; |
| 47 |
static void (*user_onRequest)(void); |
| 48 |
static void (*user_onReceive)(int); |
| 49 |
static void onRequestService(void); |
| 50 |
static void onReceiveService(uint8_t*, int); |
| 51 |
public: |
| 52 |
TwoWire(); |
| 53 |
void begin(); |
| 54 |
void begin(uint8_t); |
| 55 |
void begin(int); |
| 56 |
void end(); |
| 57 |
void setClock(uint32_t); |
| 58 |
void setWireTimeout(uint32_t timeout = 25000, bool reset_with_timeout = false); |
| 59 |
bool getWireTimeoutFlag(void); |
| 60 |
void clearWireTimeoutFlag(void); |
| 61 |
void beginTransmission(uint8_t); |
| 62 |
void beginTransmission(int); |
| 63 |
uint8_t endTransmission(void); |
| 64 |
uint8_t endTransmission(uint8_t); |
| 65 |
uint8_t requestFrom(uint8_t, uint8_t); |
| 66 |
uint8_t requestFrom(uint8_t, uint8_t, uint8_t); |
| 67 |
uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t); |
| 68 |
uint8_t requestFrom(int, int); |
| 69 |
uint8_t requestFrom(int, int, int); |
| 70 |
virtual size_t write(uint8_t); |
| 71 |
virtual size_t write(const uint8_t *, size_t); |
| 72 |
virtual int available(void); |
| 73 |
virtual int read(void); |
| 74 |
virtual int peek(void); |
| 75 |
virtual void flush(void); |
| 76 |
void onReceive( void (*)(int) ); |
| 77 |
void onRequest( void (*)(void) ); |
| 78 |
|
| 79 |
inline size_t write(unsigned long n) { return write((uint8_t)n); } |
| 80 |
inline size_t write(long n) { return write((uint8_t)n); } |
| 81 |
inline size_t write(unsigned int n) { return write((uint8_t)n); } |
| 82 |
inline size_t write(int n) { return write((uint8_t)n); } |
| 83 |
using Print::write; |
| 84 |
}; |
| 85 |
|
| 86 |
extern TwoWire Wire; |
| 87 |
|
| 88 |
#endif |
| 89 |
|