Skip to content

Commit 42aa0e6

Browse files
authored
Wire buffer length improvments. (#8398)
* Enable I2C_BUFFER_LENGTH definition for Wire lib Based on paclema/arduino-esp32@375a89e 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. * 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.
1 parent 55ef3e7 commit 42aa0e6

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

libraries/Wire/Wire.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ extern "C" {
4040

4141
// Initialize Class Variables //////////////////////////////////////////////////
4242

43-
uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
44-
uint8_t TwoWire::rxBufferIndex = 0;
45-
uint8_t TwoWire::rxBufferLength = 0;
43+
uint8_t TwoWire::rxBuffer[I2C_BUFFER_LENGTH];
44+
size_t TwoWire::rxBufferIndex = 0;
45+
size_t TwoWire::rxBufferLength = 0;
4646

4747
uint8_t TwoWire::txAddress = 0;
48-
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
49-
uint8_t TwoWire::txBufferIndex = 0;
50-
uint8_t TwoWire::txBufferLength = 0;
48+
uint8_t TwoWire::txBuffer[I2C_BUFFER_LENGTH];
49+
size_t TwoWire::txBufferIndex = 0;
50+
size_t TwoWire::txBufferLength = 0;
5151

5252
uint8_t TwoWire::transmitting = 0;
5353
void (*TwoWire::user_onRequest)(void);
@@ -122,9 +122,9 @@ void TwoWire::setClockStretchLimit(uint32_t limit)
122122

123123
size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop)
124124
{
125-
if (size > BUFFER_LENGTH)
125+
if (size > I2C_BUFFER_LENGTH)
126126
{
127-
size = BUFFER_LENGTH;
127+
size = I2C_BUFFER_LENGTH;
128128
}
129129
size_t read = (twi_readFrom(address, rxBuffer, size, sendStop) == 0) ? size : 0;
130130
rxBufferIndex = 0;
@@ -183,7 +183,7 @@ size_t TwoWire::write(uint8_t data)
183183
{
184184
if (transmitting)
185185
{
186-
if (txBufferLength >= BUFFER_LENGTH)
186+
if (txBufferLength >= I2C_BUFFER_LENGTH)
187187
{
188188
setWriteError();
189189
return 0;

libraries/Wire/Wire.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,23 @@
2828
#include "Stream.h"
2929

3030

31-
3231
#ifndef I2C_BUFFER_LENGTH
32+
// DEPRECATED: Do not use BUFFER_LENGTH, prefer I2C_BUFFER_LENGTH
3333
#define BUFFER_LENGTH 128
34-
#else
35-
#define BUFFER_LENGTH I2C_BUFFER_LENGTH
34+
#define I2C_BUFFER_LENGTH BUFFER_LENGTH
3635
#endif
3736

3837
class TwoWire : public Stream
3938
{
4039
private:
4140
static uint8_t rxBuffer[];
42-
static uint8_t rxBufferIndex;
43-
static uint8_t rxBufferLength;
41+
static size_t rxBufferIndex;
42+
static size_t rxBufferLength;
4443

4544
static uint8_t txAddress;
4645
static uint8_t txBuffer[];
47-
static uint8_t txBufferIndex;
48-
static uint8_t txBufferLength;
46+
static size_t txBufferIndex;
47+
static size_t txBufferLength;
4948

5049
static uint8_t transmitting;
5150
static void (*user_onRequest)(void);

0 commit comments

Comments
 (0)