123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- extern "C" {
- #include <stdlib.h>
- #include <string.h>
- #include <inttypes.h>
- #include "twi.h"
- }
- #include "Wire.h"
- uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
- uint8_t TwoWire::rxBufferIndex = 0;
- uint8_t TwoWire::rxBufferLength = 0;
- uint8_t TwoWire::txAddress = 0;
- uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
- uint8_t TwoWire::txBufferIndex = 0;
- uint8_t TwoWire::txBufferLength = 0;
- uint8_t TwoWire::transmitting = 0;
- void (*TwoWire::user_onRequest)(void);
- void (*TwoWire::user_onReceive)(int);
- TwoWire::TwoWire()
- {
- }
- void TwoWire::begin(void)
- {
- rxBufferIndex = 0;
- rxBufferLength = 0;
- txBufferIndex = 0;
- txBufferLength = 0;
- twi_init();
- }
- void TwoWire::begin(uint8_t address)
- {
- twi_setAddress(address);
- twi_attachSlaveTxEvent(onRequestService);
- twi_attachSlaveRxEvent(onReceiveService);
- begin();
- }
- void TwoWire::begin(int address)
- {
- begin((uint8_t)address);
- }
- void TwoWire::setClock(uint32_t frequency)
- {
- TWBR = ((F_CPU / frequency) - 16) / 2;
- }
- uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
- {
-
- if(quantity > BUFFER_LENGTH){
- quantity = BUFFER_LENGTH;
- }
-
- uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
-
- rxBufferIndex = 0;
- rxBufferLength = read;
- return read;
- }
- uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
- {
- return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
- }
- uint8_t TwoWire::requestFrom(int address, int quantity)
- {
- return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
- }
- uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
- {
- return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
- }
- void TwoWire::beginTransmission(uint8_t address)
- {
-
- transmitting = 1;
-
- txAddress = address;
-
- txBufferIndex = 0;
- txBufferLength = 0;
- }
- void TwoWire::beginTransmission(int address)
- {
- beginTransmission((uint8_t)address);
- }
- uint8_t TwoWire::endTransmission(uint8_t sendStop)
- {
-
- int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
-
- txBufferIndex = 0;
- txBufferLength = 0;
-
- transmitting = 0;
- return ret;
- }
- uint8_t TwoWire::endTransmission(void)
- {
- return endTransmission(true);
- }
- size_t TwoWire::write(uint8_t data)
- {
- if(transmitting){
-
-
- if(txBufferLength >= BUFFER_LENGTH){
- setWriteError();
- return 0;
- }
-
- txBuffer[txBufferIndex] = data;
- ++txBufferIndex;
-
- txBufferLength = txBufferIndex;
- }else{
-
-
- twi_transmit(&data, 1);
- }
- return 1;
- }
- size_t TwoWire::write(const uint8_t *data, size_t quantity)
- {
- if(transmitting){
-
- for(size_t i = 0; i < quantity; ++i){
- write(data[i]);
- }
- }else{
-
-
- twi_transmit(data, quantity);
- }
- return quantity;
- }
- int TwoWire::available(void)
- {
- return rxBufferLength - rxBufferIndex;
- }
- int TwoWire::read(void)
- {
- int value = -1;
-
-
- if(rxBufferIndex < rxBufferLength){
- value = rxBuffer[rxBufferIndex];
- ++rxBufferIndex;
- }
- return value;
- }
- int TwoWire::peek(void)
- {
- int value = -1;
-
- if(rxBufferIndex < rxBufferLength){
- value = rxBuffer[rxBufferIndex];
- }
- return value;
- }
- void TwoWire::flush(void)
- {
-
- }
- void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
- {
-
- if(!user_onReceive){
- return;
- }
-
-
-
- if(rxBufferIndex < rxBufferLength){
- return;
- }
-
-
- for(uint8_t i = 0; i < numBytes; ++i){
- rxBuffer[i] = inBytes[i];
- }
-
- rxBufferIndex = 0;
- rxBufferLength = numBytes;
-
- user_onReceive(numBytes);
- }
- void TwoWire::onRequestService(void)
- {
-
- if(!user_onRequest){
- return;
- }
-
-
- txBufferIndex = 0;
- txBufferLength = 0;
-
- user_onRequest();
- }
- void TwoWire::onReceive( void (*function)(int) )
- {
- user_onReceive = function;
- }
- void TwoWire::onRequest( void (*function)(void) )
- {
- user_onRequest = function;
- }
- TwoWire Wire = TwoWire();
|