1 #ifndef EAGLE_EBOARD_HELPLIB_TWOWIRE 2 #define EAGLE_EBOARD_HELPLIB_TWOWIRE 19 #define BUFFER_LENGTH 32 87 void begin(uint8_t address);
93 void begin(
int address);
134 uint8_t
requestFrom(uint8_t address, uint8_t quantity);
142 uint8_t
requestFrom(uint8_t address , uint8_t quantity, uint8_t sendStop);
159 uint8_t
requestFrom(
int address,
int quantity,
int sendStop);
168 virtual size_t write(uint8_t data);
176 virtual size_t write(
const uint8_t *data,
size_t quantity);
187 virtual int read(
void);
193 virtual int peek(
void);
207 void onRequest(
void (*
function)(
void) );
221 #include <inttypes.h> 233 uint8_t TwoWire::rxBufferIndex = 0;
234 uint8_t TwoWire::rxBufferLength = 0;
236 uint8_t TwoWire::txAddress = 0;
238 uint8_t TwoWire::txBufferIndex = 0;
239 uint8_t TwoWire::txBufferLength = 0;
241 uint8_t TwoWire::transmitting = 0;
242 void (*TwoWire::user_onRequest)(void);
243 void (*TwoWire::user_onReceive)(int);
245 TwoWire::TwoWire() {}
247 void TwoWire::begin(
void) {
257 void TwoWire::begin(uint8_t address) {
264 void TwoWire::begin(
int address) {
265 begin((uint8_t)address);
268 uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {
272 uint8_t read =
twi_readFrom(address, rxBuffer, quantity, sendStop);
274 rxBufferLength = read;
279 uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
280 return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)
true);
283 uint8_t TwoWire::requestFrom(
int address,
int quantity) {
284 return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)
true);
287 uint8_t TwoWire::requestFrom(
int address,
int quantity,
int sendStop) {
288 return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
291 void TwoWire::beginTransmission(uint8_t address) {
298 void TwoWire::beginTransmission(
int address) {
299 beginTransmission((uint8_t)address);
302 uint8_t TwoWire::endTransmission(uint8_t sendStop) {
303 int8_t ret =
twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
310 uint8_t TwoWire::endTransmission(
void){
311 return endTransmission(
true);
314 size_t TwoWire::write(uint8_t data) {
320 txBuffer[txBufferIndex] = data;
322 txBufferLength = txBufferIndex;
329 size_t TwoWire::write(
const uint8_t *data,
size_t quantity) {
331 for(
size_t i = 0; i < quantity; ++i) {
340 int TwoWire::available(
void) {
341 return rxBufferLength - rxBufferIndex;
344 int TwoWire::read(
void) {
347 if(rxBufferIndex < rxBufferLength) {
348 value = rxBuffer[rxBufferIndex];
355 int TwoWire::peek(
void) {
358 if(rxBufferIndex < rxBufferLength) {
359 value = rxBuffer[rxBufferIndex];
365 void TwoWire::onReceiveService(uint8_t* inBytes,
int numBytes) {
370 if(rxBufferIndex < rxBufferLength) {
374 for(uint8_t i = 0; i < numBytes; ++i) {
375 rxBuffer[i] = inBytes[i];
378 rxBufferLength = numBytes;
379 user_onReceive(numBytes);
382 void TwoWire::onRequestService(
void) {
383 if(!user_onRequest)
return;
390 void TwoWire::onReceive(
void (*
function)(
int) ) {
391 user_onReceive =
function;
394 void TwoWire::onRequest(
void (*
function)(
void) ) {
395 user_onRequest =
function;
void onRequest(void(*function)(void))
this will set the user_onRequest method
virtual size_t write(uint8_t data)
this will write a single unsigned 8-bit value to address
static void(* user_onReceive)(int numBytes)
twi slave [Rx]receive-event user def handler
void twi_setAddress(uint8_t)
static uint8_t rxBufferIndex
this defines the rxBuffer Index - current position in rxBuffer array
static uint8_t txBufferIndex
this defines the txBuffer Index - current position in txBuffer array
static void onRequestService(void)
twi slave [Tx]transmitting-event handler
[I2C] This is used to avoid path resolving issues and defines the common known Arduino Wire-Interface...
static uint8_t transmitting
'boolean' value. Set to 1 if transmitting => in master write mode
uint8_t twi_transmit(const uint8_t *, uint8_t)
uint8_t twi_readFrom(uint8_t, uint8_t *, uint8_t, uint8_t)
static uint8_t rxBufferLength
this defines the length of rxBuffer
static uint8_t txAddress
this defines the txAddress the transmitting Dta
static uint8_t txBuffer[]
this defines the txBuffer used to enable delayed read
this namespace contains all the don't use manually classes ;)
virtual int read(void)
this will read a single byte from rxBuffer and increment the Index
uint8_t twi_writeTo(uint8_t, uint8_t *, uint8_t, uint8_t, uint8_t)
uint8_t requestFrom(uint8_t address, uint8_t quantity)
this will read a specific quantity of bytes from a specific address
static void onReceiveService(uint8_t *inBytes, int numBytes)
twi slave [Rx]receive-event handler
void begin()
begin the TwoWire communcation without any data set
TwoWire()
The constructor of the TwoWire class.
void beginTransmission(uint8_t address)
this will start a new transmission to a specific address => master mode
virtual int peek(void)
this will read a single byte from rxBuffer without increment the Index
void twi_attachSlaveRxEvent(void(*)(uint8_t *, int))
void onReceive(void(*function)(int))
this will set the user_onReceive method
static uint8_t txBufferLength
this defines the length of txBuffer
TwoWire Wire
this is the well-known Arduino Wire Interface, just a little bit 'modified' ;P
uint8_t endTransmission(void)
this will end the transmission and send the STOP-sequence
virtual int available(void)
this will return the amount of rxBuffer left
static void(* user_onRequest)(void)
twi slave [Tx]transmitting-event user def handler
static uint8_t rxBuffer[]
this defines the rxBuffer used to enable delayed read
void twi_attachSlaveTxEvent(void(*)(void))