From e7660b0af6c7256df2bb7856908067eba2dde2c6 Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Sun, 5 Dec 2021 14:00:33 -0500 Subject: [PATCH 1/2] Enable I2C_BUFFER_LENGTH definition for Wire lib Based on https://github.com/paclema/arduino-esp32/commit/375a89ed58f341a8aae26dab505713f3ef0edb3d We should have been very careful when #defining things to not use a name that could conflict with the user's own code, so this marks that old define as deprecated. If you opt-into the new behavior, you do not get the old BUFFER_LENGTH constant. As a bonus, changing these uint8_ts to size_t both reduces the code size & improves performance. --- libraries/Wire/Wire.cpp | 10 +++++----- libraries/Wire/Wire.h | 5 ++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 3aa3604ab0..548aa35abe 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -40,12 +40,12 @@ extern "C" { // Initialize Class Variables ////////////////////////////////////////////////// -uint8_t TwoWire::rxBuffer[BUFFER_LENGTH]; +uint8_t TwoWire::rxBuffer[I2C_BUFFER_LENGTH]; uint8_t TwoWire::rxBufferIndex = 0; uint8_t TwoWire::rxBufferLength = 0; uint8_t TwoWire::txAddress = 0; -uint8_t TwoWire::txBuffer[BUFFER_LENGTH]; +uint8_t TwoWire::txBuffer[I2C_BUFFER_LENGTH]; uint8_t TwoWire::txBufferIndex = 0; uint8_t TwoWire::txBufferLength = 0; @@ -122,9 +122,9 @@ void TwoWire::setClockStretchLimit(uint32_t limit) size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop) { - if (size > BUFFER_LENGTH) + if (size > I2C_BUFFER_LENGTH) { - size = BUFFER_LENGTH; + size = I2C_BUFFER_LENGTH; } size_t read = (twi_readFrom(address, rxBuffer, size, sendStop) == 0) ? size : 0; rxBufferIndex = 0; @@ -183,7 +183,7 @@ size_t TwoWire::write(uint8_t data) { if (transmitting) { - if (txBufferLength >= BUFFER_LENGTH) + if (txBufferLength >= I2C_BUFFER_LENGTH) { setWriteError(); return 0; diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index bcdbb1214e..40581e9fbe 100644 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -28,11 +28,10 @@ #include "Stream.h" - #ifndef I2C_BUFFER_LENGTH +// DEPRECATED: Do not use BUFFER_LENGTH, prefer I2C_BUFFER_LENGTH #define BUFFER_LENGTH 128 -#else -#define BUFFER_LENGTH I2C_BUFFER_LENGTH +#define I2C_BUFFER_LENGTH BUFFER_LENGTH #endif class TwoWire : public Stream From 733eef373bf5ca5d9be7ccc5d8edd77ee6e55fee Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Sun, 5 Dec 2021 14:39:50 -0500 Subject: [PATCH 2/2] Increase buffer indexing variable size I looked over the users of these variables and they should be fine with no additional changes. The existing methods already have an option to use size_t rather than uint8_t. There's a few methods which return int instead of size_t, which isn't great from a portability perspective but will be fine since this only is designed to run on the ESP8266. --- libraries/Wire/Wire.cpp | 8 ++++---- libraries/Wire/Wire.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 548aa35abe..e9fc796bbf 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -41,13 +41,13 @@ extern "C" { // Initialize Class Variables ////////////////////////////////////////////////// uint8_t TwoWire::rxBuffer[I2C_BUFFER_LENGTH]; -uint8_t TwoWire::rxBufferIndex = 0; -uint8_t TwoWire::rxBufferLength = 0; +size_t TwoWire::rxBufferIndex = 0; +size_t TwoWire::rxBufferLength = 0; uint8_t TwoWire::txAddress = 0; uint8_t TwoWire::txBuffer[I2C_BUFFER_LENGTH]; -uint8_t TwoWire::txBufferIndex = 0; -uint8_t TwoWire::txBufferLength = 0; +size_t TwoWire::txBufferIndex = 0; +size_t TwoWire::txBufferLength = 0; uint8_t TwoWire::transmitting = 0; void (*TwoWire::user_onRequest)(void); diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index 40581e9fbe..be73f10fe6 100644 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -38,13 +38,13 @@ class TwoWire : public Stream { private: static uint8_t rxBuffer[]; - static uint8_t rxBufferIndex; - static uint8_t rxBufferLength; + static size_t rxBufferIndex; + static size_t rxBufferLength; static uint8_t txAddress; static uint8_t txBuffer[]; - static uint8_t txBufferIndex; - static uint8_t txBufferLength; + static size_t txBufferIndex; + static size_t txBufferLength; static uint8_t transmitting; static void (*user_onRequest)(void);