Skip to content

Commit

Permalink
Merge pull request #30 from caternuson/iss29_busio
Browse files Browse the repository at this point in the history
Convert to BusIO
  • Loading branch information
ladyada authored Aug 13, 2021
2 parents 8063146 + 4f50ac3 commit 15b521f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
43 changes: 13 additions & 30 deletions Adafruit_CCS811.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +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) {
_i2caddr = addr;
bool Adafruit_CCS811::begin(uint8_t addr, TwoWire *theWire) {

_i2c_init();
i2c_dev = new Adafruit_I2CDevice(addr, theWire);
if (!i2c_dev->begin()) {
return false;
}
#ifdef ESP8266
theWire->setClockStretchLimit(500);
#endif

SWReset();
delay(100);
Expand Down Expand Up @@ -266,35 +272,12 @@ 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 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++;
}
}
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) {
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);
}
7 changes: 3 additions & 4 deletions Adafruit_CCS811.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "WProgram.h"
#endif

#include <Wire.h>
#include <Adafruit_I2CDevice.h>

/*=========================================================================
I2C ADDRESS/BITS
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=Adafruit CCS811 Library
version=1.0.5
version=1.1.0
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=This is a library for the Adafruit CCS811 I2C gas sensor breakout.
paragraph=CCS811 is a gas sensor that can detect a wide range of Volatile Organic Compounds (VOCs) and is intended for indoor air quality monitoring.
category=Sensors
url=https://github.com/adafruit/Adafruit_CCS811
architectures=*
depends=Adafruit SSD1306, Adafruit GFX Library
depends=Adafruit SSD1306, Adafruit GFX Library, Adafruit BusIO

0 comments on commit 15b521f

Please sign in to comment.