Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire buffer length improvments. #8398

Merged
merged 2 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ extern "C" {

// Initialize Class Variables //////////////////////////////////////////////////

uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
uint8_t TwoWire::rxBufferIndex = 0;
uint8_t TwoWire::rxBufferLength = 0;
uint8_t TwoWire::rxBuffer[I2C_BUFFER_LENGTH];
size_t TwoWire::rxBufferIndex = 0;
size_t TwoWire::rxBufferLength = 0;

uint8_t TwoWire::txAddress = 0;
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
uint8_t TwoWire::txBufferIndex = 0;
uint8_t TwoWire::txBufferLength = 0;
uint8_t TwoWire::txBuffer[I2C_BUFFER_LENGTH];
size_t TwoWire::txBufferIndex = 0;
size_t TwoWire::txBufferLength = 0;

uint8_t TwoWire::transmitting = 0;
void (*TwoWire::user_onRequest)(void);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 6 additions & 7 deletions libraries/Wire/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,23 @@
#include "Stream.h"



#ifndef I2C_BUFFER_LENGTH
// DEPRECATED: Do not use BUFFER_LENGTH, prefer I2C_BUFFER_LENGTH
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect this may be a bit controversial. My idea is to use the new I2C_BUFFER_LENGTH functionality as a carrot to convince people to switch to the new define.

#define BUFFER_LENGTH 128
#else
#define BUFFER_LENGTH I2C_BUFFER_LENGTH
#define I2C_BUFFER_LENGTH BUFFER_LENGTH
#endif

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);
Expand Down