@@ -67,8 +67,7 @@ void TwoWire::begin(uint8_t address, bool generalCall)
6767 rxBufferAllocated = 0 ;
6868 resetRxBuffer ();
6969
70- txBufferIndex = 0 ;
71- txBufferLength = 0 ;
70+ txDataSize = 0 ;
7271 txAddress = 0 ;
7372 txBuffer = nullptr ;
7473 txBufferAllocated = 0 ;
@@ -202,9 +201,8 @@ void TwoWire::beginTransmission(uint8_t address)
202201 transmitting = 1 ;
203202 // set address of targeted slave
204203 txAddress = address << 1 ;
205- // reset tx buffer iterator vars
206- txBufferIndex = 0 ;
207- txBufferLength = 0 ;
204+ // reset tx data size
205+ txDataSize = 0 ;
208206}
209207
210208void TwoWire::beginTransmission (int address)
@@ -242,7 +240,7 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
242240
243241 if (_i2c.isMaster == 1 ) {
244242 // transmit buffer (blocking)
245- switch (i2c_master_write (&_i2c, txAddress, txBuffer, txBufferLength )) {
243+ switch (i2c_master_write (&_i2c, txAddress, txBuffer, txDataSize )) {
246244 case I2C_OK :
247245 ret = 0 ; // Success
248246 break ;
@@ -266,9 +264,8 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
266264 // reset Tx buffer
267265 resetTxBuffer ();
268266
269- // reset tx buffer iterator vars
270- txBufferIndex = 0 ;
271- txBufferLength = 0 ;
267+ // reset tx buffer data size
268+ txDataSize = 0 ;
272269
273270 // indicate that we are done transmitting
274271 transmitting = 0 ;
@@ -292,17 +289,16 @@ size_t TwoWire::write(uint8_t data)
292289 size_t ret = 1 ;
293290 if (transmitting) {
294291 // in master transmitter mode
295- allocateTxBuffer (txBufferLength + 1 );
292+ allocateTxBuffer (txDataSize + 1 );
296293 // error if no memory block available to allocate the buffer
297294 if (txBuffer == nullptr ) {
298295 setWriteError ();
299296 ret = 0 ;
300297 } else {
301298 // put byte in tx buffer
302- txBuffer[txBufferIndex] = data;
303- ++txBufferIndex;
299+ txBuffer[txDataSize] = data;
304300 // update amount in buffer
305- txBufferLength = txBufferIndex ;
301+ txDataSize++ ;
306302 }
307303 } else {
308304 // in slave send mode
@@ -327,17 +323,17 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)
327323
328324 if (transmitting) {
329325 // in master transmitter mode
330- allocateTxBuffer (txBufferLength + quantity);
326+ allocateTxBuffer (txDataSize + quantity);
331327 // error if no memory block available to allocate the buffer
332328 if (txBuffer == nullptr ) {
333329 setWriteError ();
334330 ret = 0 ;
335331 } else {
336332 // put bytes in tx buffer
337- memcpy (&(txBuffer[txBufferIndex ]), data, quantity);
338- txBufferIndex = txBufferIndex + quantity;
333+ memcpy (&(txBuffer[txDataSize ]), data, quantity);
334+
339335 // update amount in buffer
340- txBufferLength = txBufferIndex ;
336+ txDataSize += quantity ;
341337 }
342338 } else {
343339 // in slave send mode
@@ -397,8 +393,7 @@ void TwoWire::flush(void)
397393 rxBufferIndex = 0 ;
398394 rxBufferLength = 0 ;
399395 resetRxBuffer ();
400- txBufferIndex = 0 ;
401- txBufferLength = 0 ;
396+ txDataSize = 0 ;
402397 resetTxBuffer ();
403398}
404399
@@ -442,10 +437,9 @@ void TwoWire::onRequestService(i2c_t *obj)
442437
443438 // don't bother if user hasn't registered a callback
444439 if (TW->user_onRequest ) {
445- // reset tx buffer iterator vars
440+ // reset tx data size
446441 // !!! this will kill any pending pre-master sendTo() activity
447- TW->txBufferIndex = 0 ;
448- TW->txBufferLength = 0 ;
442+ TW->txDataSize = 0 ;
449443 // alert user program
450444 TW->user_onRequest ();
451445 }
0 commit comments