00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 extern "C" {
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <inttypes.h>
00024 #include "twi.h"
00025 }
00026
00027 #include "Wire.h"
00028
00029
00030
00031 uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
00032 uint8_t TwoWire::rxBufferIndex = 0;
00033 uint8_t TwoWire::rxBufferLength = 0;
00034
00035 uint8_t TwoWire::txAddress = 0;
00036 uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
00037 uint8_t TwoWire::txBufferIndex = 0;
00038 uint8_t TwoWire::txBufferLength = 0;
00039
00040 uint8_t TwoWire::transmitting = 0;
00041 void (*TwoWire::user_onRequest)(void);
00042 void (*TwoWire::user_onReceive)(int);
00043
00044
00045
00046 TwoWire::TwoWire()
00047 {
00048 }
00049
00050
00051
00052 void TwoWire::begin(void)
00053 {
00054 rxBufferIndex = 0;
00055 rxBufferLength = 0;
00056
00057 txBufferIndex = 0;
00058 txBufferLength = 0;
00059
00060 twi_init();
00061 }
00062
00063 void TwoWire::begin(uint8_t address)
00064 {
00065 twi_setAddress(address);
00066 twi_attachSlaveTxEvent(onRequestService);
00067 twi_attachSlaveRxEvent(onReceiveService);
00068 begin();
00069 }
00070
00071 void TwoWire::begin(int address)
00072 {
00073 begin((uint8_t)address);
00074 }
00075
00076 uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
00077 {
00078
00079 if(quantity > BUFFER_LENGTH){
00080 quantity = BUFFER_LENGTH;
00081 }
00082
00083 uint8_t read = twi_readFrom(address, rxBuffer, quantity);
00084
00085 rxBufferIndex = 0;
00086 rxBufferLength = read;
00087
00088 return read;
00089 }
00090
00091 uint8_t TwoWire::requestFrom(int address, int quantity)
00092 {
00093 return requestFrom((uint8_t)address, (uint8_t)quantity);
00094 }
00095
00096 void TwoWire::beginTransmission(uint8_t address)
00097 {
00098
00099 transmitting = 1;
00100
00101 txAddress = address;
00102
00103 txBufferIndex = 0;
00104 txBufferLength = 0;
00105 }
00106
00107 void TwoWire::beginTransmission(int address)
00108 {
00109 beginTransmission((uint8_t)address);
00110 }
00111
00112 uint8_t TwoWire::endTransmission(void)
00113 {
00114
00115 int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1);
00116
00117 txBufferIndex = 0;
00118 txBufferLength = 0;
00119
00120 transmitting = 0;
00121 return ret;
00122 }
00123
00124
00125
00126
00127 void TwoWire::send(uint8_t data)
00128 {
00129 if(transmitting){
00130
00131
00132 if(txBufferLength >= BUFFER_LENGTH){
00133 return;
00134 }
00135
00136 txBuffer[txBufferIndex] = data;
00137 ++txBufferIndex;
00138
00139 txBufferLength = txBufferIndex;
00140 }else{
00141
00142
00143 twi_transmit(&data, 1);
00144 }
00145 }
00146
00147
00148
00149
00150 void TwoWire::send(uint8_t* data, uint8_t quantity)
00151 {
00152 if(transmitting){
00153
00154 for(uint8_t i = 0; i < quantity; ++i){
00155 send(data[i]);
00156 }
00157 }else{
00158
00159
00160 twi_transmit(data, quantity);
00161 }
00162 }
00163
00164
00165
00166
00167 void TwoWire::send(char* data)
00168 {
00169 send((uint8_t*)data, strlen(data));
00170 }
00171
00172
00173
00174
00175 void TwoWire::send(int data)
00176 {
00177 send((uint8_t)data);
00178 }
00179
00180
00181
00182
00183 uint8_t TwoWire::available(void)
00184 {
00185 return rxBufferLength - rxBufferIndex;
00186 }
00187
00188
00189
00190
00191 uint8_t TwoWire::receive(void)
00192 {
00193
00194
00195 uint8_t value = '\0';
00196
00197
00198 if(rxBufferIndex < rxBufferLength){
00199 value = rxBuffer[rxBufferIndex];
00200 ++rxBufferIndex;
00201 }
00202
00203 return value;
00204 }
00205
00206
00207 void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
00208 {
00209
00210 if(!user_onReceive){
00211 return;
00212 }
00213
00214
00215
00216 if(rxBufferIndex < rxBufferLength){
00217 return;
00218 }
00219
00220
00221 for(uint8_t i = 0; i < numBytes; ++i){
00222 rxBuffer[i] = inBytes[i];
00223 }
00224
00225 rxBufferIndex = 0;
00226 rxBufferLength = numBytes;
00227
00228 user_onReceive(numBytes);
00229 }
00230
00231
00232 void TwoWire::onRequestService(void)
00233 {
00234
00235 if(!user_onRequest){
00236 return;
00237 }
00238
00239
00240 txBufferIndex = 0;
00241 txBufferLength = 0;
00242
00243 user_onRequest();
00244 }
00245
00246
00247 void TwoWire::onReceive( void (*function)(int) )
00248 {
00249 user_onReceive = function;
00250 }
00251
00252
00253 void TwoWire::onRequest( void (*function)(void) )
00254 {
00255 user_onRequest = function;
00256 }
00257
00258
00259
00260 TwoWire Wire = TwoWire();
00261