@@ -27,22 +27,6 @@ extern "C" {
2727
2828#include " Wire.h"
2929
30- // Initialize Class Variables //////////////////////////////////////////////////
31- uint8_t *TwoWire::rxBuffer = nullptr ;
32- uint8_t TwoWire::rxBufferAllocated = 0 ;
33- uint8_t TwoWire::rxBufferIndex = 0 ;
34- uint8_t TwoWire::rxBufferLength = 0 ;
35-
36- uint8_t TwoWire::txAddress = 0 ;
37- uint8_t *TwoWire::txBuffer = nullptr ;
38- uint8_t TwoWire::txBufferAllocated = 0 ;
39- uint8_t TwoWire::txBufferIndex = 0 ;
40- uint8_t TwoWire::txBufferLength = 0 ;
41-
42- uint8_t TwoWire::transmitting = 0 ;
43- void (*TwoWire::user_onRequest)(void );
44- void (*TwoWire::user_onReceive)(int );
45-
4630// Constructors ////////////////////////////////////////////////////////////////
4731
4832TwoWire::TwoWire ()
@@ -81,6 +65,13 @@ void TwoWire::begin(uint8_t address, bool generalCall)
8165 txBufferLength = 0 ;
8266 resetTxBuffer ();
8367
68+ rxBuffer = nullptr ;
69+ rxBufferAllocated = 0 ;
70+ txAddress = 0 ;
71+ txBuffer = nullptr ;
72+ txBufferAllocated = 0 ;
73+ _i2c.__this = (void *)this ;
74+ user_onRequest = NULL ;
8475 transmitting = 0 ;
8576
8677 ownAddress = address << 1 ;
@@ -403,44 +394,51 @@ void TwoWire::flush(void)
403394}
404395
405396// behind the scenes function that is called when data is received
406- void TwoWire::onReceiveService (uint8_t *inBytes, int numBytes )
397+ void TwoWire::onReceiveService (i2c_t *obj )
407398{
399+ uint8_t *inBytes = (uint8_t *) obj->i2cTxRxBuffer ;
400+ int numBytes = obj->slaveRxNbData ;
401+
402+ TwoWire *TW = (TwoWire *)(obj->__this );
403+
408404 // don't bother if user hasn't registered a callback
409- if (user_onReceive) {
405+ if (TW-> user_onReceive ) {
410406 // don't bother if rx buffer is in use by a master requestFrom() op
411407 // i know this drops data, but it allows for slight stupidity
412408 // meaning, they may not have read all the master requestFrom() data yet
413- if (rxBufferIndex >= rxBufferLength) {
409+ if (TW-> rxBufferIndex >= TW-> rxBufferLength ) {
414410
415- allocateRxBuffer (numBytes);
411+ TW-> allocateRxBuffer (numBytes);
416412 // error if no memory block available to allocate the buffer
417- if (rxBuffer == nullptr ) {
413+ if (TW-> rxBuffer == nullptr ) {
418414 Error_Handler ();
419415 }
420416
421417 // copy twi rx buffer into local read buffer
422418 // this enables new reads to happen in parallel
423- memcpy (rxBuffer, inBytes, numBytes);
419+ memcpy (TW-> rxBuffer , inBytes, numBytes);
424420 // set rx iterator vars
425- rxBufferIndex = 0 ;
426- rxBufferLength = numBytes;
421+ TW-> rxBufferIndex = 0 ;
422+ TW-> rxBufferLength = numBytes;
427423 // alert user program
428- user_onReceive (numBytes);
424+ TW-> user_onReceive (numBytes);
429425 }
430426 }
431427}
432428
433429// behind the scenes function that is called when data is requested
434- void TwoWire::onRequestService (void )
430+ void TwoWire::onRequestService (i2c_t *obj )
435431{
432+ TwoWire *TW = (TwoWire *)(obj->__this );
433+
436434 // don't bother if user hasn't registered a callback
437- if (user_onRequest) {
435+ if (TW-> user_onRequest ) {
438436 // reset tx buffer iterator vars
439437 // !!! this will kill any pending pre-master sendTo() activity
440- txBufferIndex = 0 ;
441- txBufferLength = 0 ;
438+ TW-> txBufferIndex = 0 ;
439+ TW-> txBufferLength = 0 ;
442440 // alert user program
443- user_onRequest ();
441+ TW-> user_onRequest ();
444442 }
445443}
446444
0 commit comments