diff --git a/Adafruit_TSL2591.cpp b/Adafruit_TSL2591.cpp index 62ed241..2358e33 100644 --- a/Adafruit_TSL2591.cpp +++ b/Adafruit_TSL2591.cpp @@ -74,9 +74,9 @@ Adafruit_TSL2591::Adafruit_TSL2591(int32_t sensorID) { */ /**************************************************************************/ boolean Adafruit_TSL2591::begin(TwoWire *theWire, uint8_t addr) { - _i2c = theWire; - _i2c->begin(); - _addr = addr; + i2c_dev = new Adafruit_I2CDevice(addr, theWire); + if (!i2c_dev->begin()) + return false; /* for (uint8_t i=0; i<0x20; i++) @@ -476,44 +476,28 @@ void Adafruit_TSL2591::getSensor(sensor_t *sensor) { /*******************************************************/ uint8_t Adafruit_TSL2591::read8(uint8_t reg) { - uint8_t x; - - _i2c->beginTransmission(_addr); - _i2c->write(reg); - _i2c->endTransmission(); - - _i2c->requestFrom(_addr, 1); - x = _i2c->read(); - - return x; + uint8_t buffer[1]; + buffer[0] = reg; + i2c_dev->write_then_read(buffer, 1, buffer, 1); + return buffer[0]; } uint16_t Adafruit_TSL2591::read16(uint8_t reg) { - uint16_t x; - uint16_t t; - - _i2c->beginTransmission(_addr); - _i2c->write(reg); - _i2c->endTransmission(); - - _i2c->requestFrom(_addr, 2); - t = _i2c->read(); - x = _i2c->read(); - - x <<= 8; - x |= t; - return x; + uint8_t buffer[2]; + buffer[0] = reg; + i2c_dev->write_then_read(buffer, 1, buffer, 2); + return uint16_t(buffer[1]) << 8 | uint16_t(buffer[0]); } void Adafruit_TSL2591::write8(uint8_t reg, uint8_t value) { - _i2c->beginTransmission(_addr); - _i2c->write(reg); - _i2c->write(value); - _i2c->endTransmission(); + uint8_t buffer[2]; + buffer[0] = reg; + buffer[1] = value; + i2c_dev->write(buffer, 2); } void Adafruit_TSL2591::write8(uint8_t reg) { - _i2c->beginTransmission(_addr); - _i2c->write(reg); - _i2c->endTransmission(); + uint8_t buffer[1]; + buffer[0] = reg; + i2c_dev->write(buffer, 1); } diff --git a/Adafruit_TSL2591.h b/Adafruit_TSL2591.h index b7f2664..1a9e480 100644 --- a/Adafruit_TSL2591.h +++ b/Adafruit_TSL2591.h @@ -19,9 +19,9 @@ #ifndef _TSL2591_H_ #define _TSL2591_H_ +#include #include #include -#include #define TSL2591_VISIBLE (2) ///< (channel 0) - (channel 1) #define TSL2591_INFRARED (1) ///< channel 1 @@ -157,7 +157,7 @@ class Adafruit_TSL2591 : public Adafruit_Sensor { void getSensor(sensor_t *); private: - TwoWire *_i2c; + Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface void write8(uint8_t r); void write8(uint8_t r, uint8_t v); diff --git a/library.properties b/library.properties index 409eca2..d061bd7 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit TSL2591 Library -version=1.3.1 +version=1.4.0 author=Adafruit maintainer=Adafruit sentence=Library for the TSL2591 digital luminosity (light) sensors. @@ -7,4 +7,4 @@ paragraph=Library for the TSL2591 digital luminosity (light) sensors. category=Sensors url=https://github.com/adafruit/Adafruit_TSL2591_Library architectures=* -depends=Adafruit Unified Sensor +depends=Adafruit Unified Sensor, Adafruit BusIO