From a1b6ce78a67d08b4e8b658a64ae8c668ade47890 Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 2 Sep 2021 11:31:51 -0700 Subject: [PATCH] convert to busio --- Adafruit_SI1145.cpp | 52 ++++++++++++++++++++++----------------------- Adafruit_SI1145.h | 6 +++--- library.properties | 3 ++- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/Adafruit_SI1145.cpp b/Adafruit_SI1145.cpp index 3454afc..62d372d 100644 --- a/Adafruit_SI1145.cpp +++ b/Adafruit_SI1145.cpp @@ -16,11 +16,20 @@ ****************************************************/ #include "Adafruit_SI1145.h" + +/** + * @brief Destructor + * + */ +Adafruit_SI1145::~Adafruit_SI1145() { + if (i2c_dev) + delete i2c_dev; +} /** * @brief Construct a new Adafruit_SI1145::Adafruit_SI1145 object * */ -Adafruit_SI1145::Adafruit_SI1145() : m_pBus(&Wire), _addr(SI1145_ADDR) {} +Adafruit_SI1145::Adafruit_SI1145() {} /** * @brief Initize the driver, specifying the `TwoWire` bus to use * @@ -38,10 +47,13 @@ boolean Adafruit_SI1145::begin(TwoWire *pBus) { * @return boolean true: success false: failure to initize the sensor */ boolean Adafruit_SI1145::begin(uint8_t addr, TwoWire *pBus) { + if (i2c_dev) + delete i2c_dev; + i2c_dev = new Adafruit_I2CDevice(addr, pBus); + if (!i2c_dev->begin()) { + return false; + } - _addr = addr; - m_pBus = pBus; - m_pBus->begin(); uint8_t id = read8(SI1145_REG_PARTID); if (id != 0x45) return false; // look for SI1145 @@ -174,32 +186,18 @@ uint8_t Adafruit_SI1145::readParam(uint8_t p) { /*********************************************************************/ uint8_t Adafruit_SI1145::read8(uint8_t reg) { - m_pBus->beginTransmission(_addr); - m_pBus->write((uint8_t)reg); - m_pBus->endTransmission(); - - m_pBus->requestFrom((uint8_t)_addr, (uint8_t)1); - return m_pBus->read(); + uint8_t buffer[1] = {reg}; + i2c_dev->write_then_read(buffer, 1, buffer, 1); + return buffer[0]; } uint16_t Adafruit_SI1145::read16(uint8_t a) { - uint16_t ret; - - m_pBus->beginTransmission(_addr); // start transmission to device - m_pBus->write(a); // sends register address to read from - m_pBus->endTransmission(); // end transmission - - m_pBus->requestFrom(_addr, (uint8_t)2); // send data n-bytes read - ret = m_pBus->read(); // receive DATA - ret |= (uint16_t)m_pBus->read() << 8; // receive DATA - - return ret; + uint8_t buffer[2] = {a, 0}; + i2c_dev->write_then_read(buffer, 1, buffer, 2); + return ((uint16_t)buffer[0]) | ((uint16_t)buffer[1] << 8); } void Adafruit_SI1145::write8(uint8_t reg, uint8_t val) { - - m_pBus->beginTransmission(_addr); // start transmission to device - m_pBus->write(reg); // sends register address to write - m_pBus->write(val); // sends value - m_pBus->endTransmission(); // end transmission -} + uint8_t buffer[2] = {reg, val}; + i2c_dev->write(buffer, 2); +} \ No newline at end of file diff --git a/Adafruit_SI1145.h b/Adafruit_SI1145.h index d1e9ed8..5551171 100644 --- a/Adafruit_SI1145.h +++ b/Adafruit_SI1145.h @@ -22,7 +22,7 @@ #else #include "WProgram.h" #endif -#include +#include /* COMMANDS */ #define SI1145_PARAM_QUERY 0x80 @@ -150,6 +150,7 @@ class Adafruit_SI1145 { public: Adafruit_SI1145(void); + ~Adafruit_SI1145(); boolean begin(uint8_t addr = SI1145_ADDR, TwoWire *pBus = &Wire); boolean begin(TwoWire *pBus); void reset(); @@ -165,7 +166,6 @@ class Adafruit_SI1145 { void write8(uint8_t reg, uint8_t val); uint8_t readParam(uint8_t p); uint8_t writeParam(uint8_t p, uint8_t v); - TwoWire *m_pBus; - uint8_t _addr; + Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface }; #endif diff --git a/library.properties b/library.properties index 8452eb6..4e727ea 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit SI1145 Library -version=1.1.1 +version=1.2.0 author=Adafruit maintainer=Adafruit sentence=Arduino library for the SI1145 sensors in the Adafruit shop @@ -7,3 +7,4 @@ paragraph=Arduino library for the SI1145 sensors in the Adafruit shop category=Sensors url=https://github.com/adafruit/Adafruit_SI1145_Library architectures=* +depends=Adafruit BusIO