@@ -67,8 +67,7 @@ void TwoWire::begin(uint8_t address, bool generalCall)
67
67
rxBufferAllocated = 0 ;
68
68
resetRxBuffer ();
69
69
70
- txBufferIndex = 0 ;
71
- txBufferLength = 0 ;
70
+ txDataSize = 0 ;
72
71
txAddress = 0 ;
73
72
txBuffer = nullptr ;
74
73
txBufferAllocated = 0 ;
@@ -202,9 +201,8 @@ void TwoWire::beginTransmission(uint8_t address)
202
201
transmitting = 1 ;
203
202
// set address of targeted slave
204
203
txAddress = address << 1 ;
205
- // reset tx buffer iterator vars
206
- txBufferIndex = 0 ;
207
- txBufferLength = 0 ;
204
+ // reset tx data size
205
+ txDataSize = 0 ;
208
206
}
209
207
210
208
void TwoWire::beginTransmission (int address)
@@ -242,7 +240,7 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
242
240
243
241
if (_i2c.isMaster == 1 ) {
244
242
// transmit buffer (blocking)
245
- switch (i2c_master_write (&_i2c, txAddress, txBuffer, txBufferLength )) {
243
+ switch (i2c_master_write (&_i2c, txAddress, txBuffer, txDataSize )) {
246
244
case I2C_OK :
247
245
ret = 0 ; // Success
248
246
break ;
@@ -266,9 +264,8 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
266
264
// reset Tx buffer
267
265
resetTxBuffer ();
268
266
269
- // reset tx buffer iterator vars
270
- txBufferIndex = 0 ;
271
- txBufferLength = 0 ;
267
+ // reset tx buffer data size
268
+ txDataSize = 0 ;
272
269
273
270
// indicate that we are done transmitting
274
271
transmitting = 0 ;
@@ -292,17 +289,16 @@ size_t TwoWire::write(uint8_t data)
292
289
size_t ret = 1 ;
293
290
if (transmitting) {
294
291
// in master transmitter mode
295
- allocateTxBuffer (txBufferLength + 1 );
292
+ allocateTxBuffer (txDataSize + 1 );
296
293
// error if no memory block available to allocate the buffer
297
294
if (txBuffer == nullptr ) {
298
295
setWriteError ();
299
296
ret = 0 ;
300
297
} else {
301
298
// put byte in tx buffer
302
- txBuffer[txBufferIndex] = data;
303
- ++txBufferIndex;
299
+ txBuffer[txDataSize] = data;
304
300
// update amount in buffer
305
- txBufferLength = txBufferIndex ;
301
+ txDataSize++ ;
306
302
}
307
303
} else {
308
304
// in slave send mode
@@ -327,17 +323,17 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)
327
323
328
324
if (transmitting) {
329
325
// in master transmitter mode
330
- allocateTxBuffer (txBufferLength + quantity);
326
+ allocateTxBuffer (txDataSize + quantity);
331
327
// error if no memory block available to allocate the buffer
332
328
if (txBuffer == nullptr ) {
333
329
setWriteError ();
334
330
ret = 0 ;
335
331
} else {
336
332
// put bytes in tx buffer
337
- memcpy (&(txBuffer[txBufferIndex ]), data, quantity);
338
- txBufferIndex = txBufferIndex + quantity;
333
+ memcpy (&(txBuffer[txDataSize ]), data, quantity);
334
+
339
335
// update amount in buffer
340
- txBufferLength = txBufferIndex ;
336
+ txDataSize += quantity ;
341
337
}
342
338
} else {
343
339
// in slave send mode
@@ -397,8 +393,7 @@ void TwoWire::flush(void)
397
393
rxBufferIndex = 0 ;
398
394
rxBufferLength = 0 ;
399
395
resetRxBuffer ();
400
- txBufferIndex = 0 ;
401
- txBufferLength = 0 ;
396
+ txDataSize = 0 ;
402
397
resetTxBuffer ();
403
398
}
404
399
@@ -442,10 +437,9 @@ void TwoWire::onRequestService(i2c_t *obj)
442
437
443
438
// don't bother if user hasn't registered a callback
444
439
if (TW->user_onRequest ) {
445
- // reset tx buffer iterator vars
440
+ // reset tx data size
446
441
// !!! this will kill any pending pre-master sendTo() activity
447
- TW->txBufferIndex = 0 ;
448
- TW->txBufferLength = 0 ;
442
+ TW->txDataSize = 0 ;
449
443
// alert user program
450
444
TW->user_onRequest ();
451
445
}
0 commit comments