Wire library (TWI)

One problem with this request is that, in the existing library at least, the buffer is copied 5 times.

Wire.cpp:

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

uint8_t TwoWire::txAddress = 0;
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];

Twi.c:

static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
static volatile uint8_t twi_masterBufferIndex;
static volatile uint8_t twi_masterBufferLength;

static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
static volatile uint8_t twi_txBufferIndex;
static volatile uint8_t twi_txBufferLength;

static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];

So a 32-byte buffer is already taking up 160 bytes (out of the 2048 you have on a Uno). To double that would take 320 bytes.

You only have to edit two defines to changes its length to suit you.

It would be nice if they had done a malloc to allocate the buffers in the first place, and taken the buffer length as an argument, but no, that wasn't done, and it might be a bit late now.


TwoWire(int speed=100, uint8_t buffersize=32)  // max bufsize 255 ? or do we need an uint16_t for that? (MEGA / DUE?)

Your proposal might work with the defaults taken like that. Personally I think 255 is probably enough, once you start using int arithmetic the code gets larger.