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