From dcdc3d2f5f0dbec755848ce708da16d1bd585a3e Mon Sep 17 00:00:00 2001 From: Tranomial Date: Sat, 22 Nov 2025 23:07:30 +0200 Subject: [PATCH] Wire: Fix write() returning incorrect byte count on buffer overflow (see issue #597) fixed typo Fixed Wire.write() always returning 0 in slave send mode --- libraries/Wire/src/Wire.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 001d924df..8749a4221 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -265,16 +265,20 @@ size_t TwoWire::write(uint8_t data) size_t TwoWire::write(const uint8_t *data, size_t quantity) { if(transmitting){ - // in master transmitter mode + // in master transmitter mode + uint8_t bytesSent = 0; + // number of bytes successfully added to the buffer for(size_t i = 0; i < quantity; ++i){ - write(data[i]); - } + if (write(data[i]) == 1) // if a byte was successfully added to the buffer + bytesSent++; + } + return bytesSent; }else{ // in slave send mode // reply to master twi_transmit(data, quantity); + return quantity; } - return quantity; } // must be called in: