From 91f55207047a8402c34d1df48ba6b9599a7e4fca Mon Sep 17 00:00:00 2001 From: ladyada Date: Thu, 7 Mar 2019 15:13:03 -0500 Subject: [PATCH] write, read and write_then_read --- Adafruit_I2CDevice.cpp | 89 ++++++++++++++++++++++++ Adafruit_I2CDevice.h | 4 ++ examples/i2c_readwrite/i2c_readwrite.ino | 41 +++++++++++ 3 files changed, 134 insertions(+) create mode 100644 examples/i2c_readwrite/i2c_readwrite.ino diff --git a/Adafruit_I2CDevice.cpp b/Adafruit_I2CDevice.cpp index e0e6afc..fee01ec 100644 --- a/Adafruit_I2CDevice.cpp +++ b/Adafruit_I2CDevice.cpp @@ -1,6 +1,8 @@ #include #include +//#define DEBUG_SERIAL Serial + Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire) { _addr = addr; _wire = theWire; @@ -28,6 +30,93 @@ bool Adafruit_I2CDevice::detected(void) { return false; } +bool Adafruit_I2CDevice::write(uint8_t *buffer, size_t len, bool stop) { + if (len > 32) { + // 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); + + if (_wire->write(buffer, len) != len) { +#ifdef DEBUG_SERIAL + DEBUG_SERIAL.println(F("\tI2CDevice failed to write")); +#endif + return false; + } + +#ifdef DEBUG_SERIAL + DEBUG_SERIAL.print(F("\tI2CDevice Wrote: ")); + for (int i=0; i endTransmission(stop) == 0); +} + +bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) { + if (len > 32) { + // 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 read such a large buffer")); +#endif + return false; + } + + if (_wire->requestFrom(_addr, (uint8_t)len, stop) != len) { + // Not enough data available to fulfill our obligation! +#ifdef DEBUG_SERIAL + DEBUG_SERIAL.println(F("\tI2CDevice did not receive enough data")); +#endif + return false; + } + + for (uint16_t i=0; iread(); + } + +#ifdef DEBUG_SERIAL + DEBUG_SERIAL.print(F("\tI2CDevice Read: ")); + for (int i=0; i + +#define I2C_ADDRESS 0x60 +Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS); + + +void setup() { + while (!Serial) { delay(10); } + Serial.begin(115200); + Serial.println("I2C device read and write test"); + + if (!i2c_dev.begin()) { + Serial.print("Did not find device at 0x"); + Serial.println(i2c_dev.address(), HEX); + while (1); + } + Serial.print("Device found on address 0x"); + Serial.println(i2c_dev.address(), HEX); + + uint8_t buffer[32]; + // Try to read 32 bytes + i2c_dev.read(buffer, 32); + Serial.print("Read: "); + for (uint8_t i=0; i<32; i++) { + Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", "); + } + Serial.println(); + + // read a register by writing first, then reading + buffer[0] = 0x0C; // we'll reuse the same buffer + i2c_dev.write_then_read(buffer, 1, buffer, 2, false); + Serial.print("Write then Read: "); + for (uint8_t i=0; i<2; i++) { + Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", "); + } + Serial.println(); +} + +void loop() { + +}