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

Update chunk #65

Merged
merged 2 commits into from
Sep 27, 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
67 changes: 25 additions & 42 deletions Adafruit_I2CDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ bool Adafruit_I2CDevice::detected(void) {
bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,
const uint8_t *prefix_buffer,
size_t prefix_len) {
if ((len + prefix_len) > maxBufferSize()) {
// currently not guaranteed to work if more than 32 bytes!
// we will need to find out if some platforms have larger
// I2C buffer sizes :/
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println(F("\tI2CDevice could not write such a large buffer"));
#endif
return false;
}

_wire->beginTransmission(_addr);

// Write the prefix data (usually an address)
// This is required to be less than _maxBufferSize, so no need to chunkify
if ((prefix_len != 0) && (prefix_buffer != NULL)) {
if (_wire->write(prefix_buffer, prefix_len) != prefix_len) {
#ifdef DEBUG_SERIAL
Expand All @@ -90,32 +99,14 @@ bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,
}
}

// Write the data itself, chunkify if needed
size_t bufferSize = maxBufferSize();
if (bufferSize >= len) {
// can just write
if (_wire->write(buffer, len) != len) {
// Write the data itself
if (_wire->write(buffer, len) != len) {
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
#endif
return false;
}
} else {
// must chunkify
size_t pos = 0;
uint8_t write_buffer[bufferSize];
while (pos < len) {
size_t write_len = len - pos > bufferSize ? bufferSize : len - pos;
for (size_t i = 0; i < write_len; i++)
write_buffer[i] = buffer[pos++];
if (_wire->write(write_buffer, write_len) != write_len) {
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
#endif
return false;
}
}
return false;
}

#ifdef DEBUG_SERIAL

DEBUG_SERIAL.print(F("\tI2CWRITE @ 0x"));
Expand Down Expand Up @@ -146,7 +137,7 @@ bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,

if (_wire->endTransmission(stop) == 0) {
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("Sent!");
// DEBUG_SERIAL.println("Sent!");
#endif
return true;
} else {
Expand All @@ -166,24 +157,16 @@ bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,
* @return True if read was successful, otherwise false.
*/
bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) {
size_t bufferSize = maxBufferSize();
if (bufferSize >= len) {
// can just read
return _read(buffer, len, stop);
} else {
// must chunkify
size_t pos = 0;
uint8_t read_buffer[bufferSize];
while (pos < len) {
size_t read_len = len - pos > bufferSize ? bufferSize : len - pos;
if (!_read(read_buffer, read_len, false)) {
return false;
}
for (size_t i = 0; i < read_len; i++)
buffer[pos++] = read_buffer[i];
}
return true;
size_t pos = 0;
while (pos < len) {
size_t read_len =
((len - pos) > maxBufferSize()) ? maxBufferSize() : (len - pos);
bool read_stop = (pos < (len - read_len)) ? false : stop;
if (!_read(buffer + pos, read_len, read_stop))
return false;
pos += read_len;
}
return true;
}

bool Adafruit_I2CDevice::_read(uint8_t *buffer, size_t len, bool stop) {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit BusIO
version=1.9.1
version=1.9.2
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=This is a library for abstracting away UART, I2C and SPI interfacing
Expand Down