Skip to content

Commit a87cc4c

Browse files
committed
Wire: Fix write() returning incorrect byte count on buffer overflow (see issue #597)
1 parent 6d00783 commit a87cc4c

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

libraries/Wire/src/Wire.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,20 @@ size_t TwoWire::write(uint8_t data)
264264
// or after beginTransmission(address)
265265
size_t TwoWire::write(const uint8_t *data, size_t quantity)
266266
{
267+
// number of bytes succefully added to the buffer
268+
uint8_t bytesSent = 0;
267269
if(transmitting){
268270
// in master transmitter mode
269271
for(size_t i = 0; i < quantity; ++i){
270-
write(data[i]);
272+
if (write(data[i]) == 1) // if a byte was successfully added to the buffer
273+
bytesSent++;
271274
}
272275
}else{
273276
// in slave send mode
274277
// reply to master
275278
twi_transmit(data, quantity);
276279
}
277-
return quantity;
280+
return bytesSent;
278281
}
279282

280283
// must be called in:

0 commit comments

Comments
 (0)