From 553653ff251aa107e18d89a7f5432cfc39e40d58 Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 10 Aug 2021 10:57:17 -0700 Subject: [PATCH 1/4] convert to busio --- Adafruit_CCS811.cpp | 53 ++++++++++++++++++++++----------------------- Adafruit_CCS811.h | 7 +++--- library.properties | 4 ++-- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/Adafruit_CCS811.cpp b/Adafruit_CCS811.cpp index 250b8ba..e0ea11b 100644 --- a/Adafruit_CCS811.cpp +++ b/Adafruit_CCS811.cpp @@ -8,10 +8,11 @@ @returns True if device is set up, false on any failure */ /**************************************************************************/ -bool Adafruit_CCS811::begin(uint8_t addr) { - _i2caddr = addr; - - _i2c_init(); +bool Adafruit_CCS811::begin(uint8_t addr, TwoWire *theWire) { + i2c_dev = new Adafruit_I2CDevice(addr, theWire); + if (!i2c_dev->begin()) { + return false; + } SWReset(); delay(100); @@ -266,35 +267,33 @@ uint8_t Adafruit_CCS811::read8(byte reg) { return ret; } -void Adafruit_CCS811::_i2c_init() { - Wire.begin(); -#ifdef ESP8266 - Wire.setClockStretchLimit(500); -#endif -} - void Adafruit_CCS811::read(uint8_t reg, uint8_t *buf, uint8_t num) { + uint8_t buffer[1]; + size_t chunkSize = i2c_dev->maxBufferSize(); uint8_t pos = 0; - // on arduino we need to read in 32 byte chunks - while (pos < num) { - - uint8_t read_now = min((uint8_t)32, (uint8_t)(num - pos)); - Wire.beginTransmission((uint8_t)_i2caddr); - Wire.write((uint8_t)reg + pos); - Wire.endTransmission(); - Wire.requestFrom((uint8_t)_i2caddr, read_now); - - for (int i = 0; i < read_now; i++) { - buf[pos] = Wire.read(); - pos++; + if (chunkSize > num) { + // can just read + buffer[0] = reg; + i2c_dev->write(buffer, 1); + i2c_dev->read(buf, num); + } else { + // must read in chunks + uint8_t read_buffer[chunkSize]; + while (pos < num) { + buffer[0] = reg + pos; + i2c_dev->write(buffer, 1); + uint8_t read_now = min(uint8_t(chunkSize), (uint8_t)(num - pos)); + i2c_dev->read(read_buffer, read_now); + for (uint8_t i = 0; i < read_now; i++) { + buf[pos] = read_buffer[i]; + pos++; + } } } } void Adafruit_CCS811::write(uint8_t reg, uint8_t *buf, uint8_t num) { - Wire.beginTransmission((uint8_t)_i2caddr); - Wire.write((uint8_t)reg); - Wire.write((uint8_t *)buf, num); - Wire.endTransmission(); + uint8_t prefix[1] = {reg}; + i2c_dev->write(buf, num, true, prefix, 1); } diff --git a/Adafruit_CCS811.h b/Adafruit_CCS811.h index 8f3ddcb..94e3e55 100644 --- a/Adafruit_CCS811.h +++ b/Adafruit_CCS811.h @@ -7,7 +7,7 @@ #include "WProgram.h" #endif -#include +#include /*========================================================================= I2C ADDRESS/BITS @@ -69,7 +69,7 @@ class Adafruit_CCS811 { Adafruit_CCS811(void){}; ~Adafruit_CCS811(void){}; - bool begin(uint8_t addr = CCS811_ADDRESS); + bool begin(uint8_t addr = CCS811_ADDRESS, TwoWire *theWire = &Wire); void setEnvironmentalData(float humidity, float temperature); @@ -140,7 +140,7 @@ class Adafruit_CCS811 { bool checkError(); private: - uint8_t _i2caddr; + Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface float _tempOffset; uint16_t _TVOC; @@ -155,7 +155,6 @@ class Adafruit_CCS811 { void read(uint8_t reg, uint8_t *buf, uint8_t num); void write(uint8_t reg, uint8_t *buf, uint8_t num); - void _i2c_init(); /*========================================================================= REGISTER BITFIELDS diff --git a/library.properties b/library.properties index 61865ab..2ce9c8d 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit CCS811 Library -version=1.0.5 +version=1.1.0 author=Adafruit maintainer=Adafruit sentence=This is a library for the Adafruit CCS811 I2C gas sensor breakout. @@ -7,4 +7,4 @@ paragraph=CCS811 is a gas sensor that can detect a wide range of Volatile Organi category=Sensors url=https://github.com/adafruit/Adafruit_CCS811 architectures=* -depends=Adafruit SSD1306, Adafruit GFX Library +depends=Adafruit SSD1306, Adafruit GFX Library, Adafruit BusIO From ca4b3c983cc5b5f2ef0d8f121c55cf54870fc73f Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 10 Aug 2021 11:25:08 -0700 Subject: [PATCH 2/4] dox and add back esp8266 clock stretch --- Adafruit_CCS811.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Adafruit_CCS811.cpp b/Adafruit_CCS811.cpp index e0ea11b..c5c9c58 100644 --- a/Adafruit_CCS811.cpp +++ b/Adafruit_CCS811.cpp @@ -5,14 +5,19 @@ @brief Setups the I2C interface and hardware and checks for communication. @param addr Optional I2C address the sensor can be found on. Default is 0x5A + @param theWire Optional pointer to I2C interface, &Wire is used by default @returns True if device is set up, false on any failure */ /**************************************************************************/ bool Adafruit_CCS811::begin(uint8_t addr, TwoWire *theWire) { + i2c_dev = new Adafruit_I2CDevice(addr, theWire); if (!i2c_dev->begin()) { return false; } +#ifdef ESP8266 + Wire.setClockStretchLimit(500); +#endif SWReset(); delay(100); From 46afc07747d7c0a0e8c89f76186a2801e1c543d7 Mon Sep 17 00:00:00 2001 From: caternuson Date: Wed, 11 Aug 2021 08:41:55 -0700 Subject: [PATCH 3/4] remove chunk --- Adafruit_CCS811.cpp | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/Adafruit_CCS811.cpp b/Adafruit_CCS811.cpp index c5c9c58..0954e03 100644 --- a/Adafruit_CCS811.cpp +++ b/Adafruit_CCS811.cpp @@ -273,29 +273,8 @@ uint8_t Adafruit_CCS811::read8(byte reg) { } void Adafruit_CCS811::read(uint8_t reg, uint8_t *buf, uint8_t num) { - uint8_t buffer[1]; - size_t chunkSize = i2c_dev->maxBufferSize(); - uint8_t pos = 0; - - if (chunkSize > num) { - // can just read - buffer[0] = reg; - i2c_dev->write(buffer, 1); - i2c_dev->read(buf, num); - } else { - // must read in chunks - uint8_t read_buffer[chunkSize]; - while (pos < num) { - buffer[0] = reg + pos; - i2c_dev->write(buffer, 1); - uint8_t read_now = min(uint8_t(chunkSize), (uint8_t)(num - pos)); - i2c_dev->read(read_buffer, read_now); - for (uint8_t i = 0; i < read_now; i++) { - buf[pos] = read_buffer[i]; - pos++; - } - } - } + uint8_t buffer[1] = {reg}; + i2c_dev->write_then_read(buffer, 1, buf, num); } void Adafruit_CCS811::write(uint8_t reg, uint8_t *buf, uint8_t num) { From 4f50ac3a1c79cb3919511561cf9508405ec3c286 Mon Sep 17 00:00:00 2001 From: caternuson Date: Wed, 11 Aug 2021 09:03:16 -0700 Subject: [PATCH 4/4] change to instance --- Adafruit_CCS811.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_CCS811.cpp b/Adafruit_CCS811.cpp index 0954e03..42a0266 100644 --- a/Adafruit_CCS811.cpp +++ b/Adafruit_CCS811.cpp @@ -16,7 +16,7 @@ bool Adafruit_CCS811::begin(uint8_t addr, TwoWire *theWire) { return false; } #ifdef ESP8266 - Wire.setClockStretchLimit(500); + theWire->setClockStretchLimit(500); #endif SWReset();