|
-
-
- #if defined(__AVR__)
-
- #include "Wire.h"
-
- extern "C" {
- #include <stdlib.h>
- #include <string.h>
- #include <inttypes.h>
- #include "twi.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::end()
- {
- TWCR &= ~(_BV(TWEN) | _BV(TWIE) | _BV(TWEA));
- digitalWrite(SDA, 0);
- digitalWrite(SCL, 0);
- }
-
- void TwoWire::setClock(uint32_t frequency)
- {
- TWBR = ((F_CPU / frequency) - 16) / 2;
- }
-
- void TwoWire::setSDA(uint8_t pin)
- {
- }
-
- void TwoWire::setSCL(uint8_t pin)
- {
- }
-
- 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();
-
- #endif
|