Skip to content

Commit

Permalink
Merge pull request #21 from adafruit/busio
Browse files Browse the repository at this point in the history
Busio
  • Loading branch information
ladyada authored Sep 28, 2020
2 parents 1e22b98 + 4ead0f0 commit 267c88c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 55 deletions.
62 changes: 18 additions & 44 deletions Adafruit_SGP30.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
*
*/

#include "Arduino.h"

#include "Adafruit_SGP30.h"
//#define I2C_DEBUG
#include "Arduino.h"

/*!
* @brief Instantiates a new SGP30 class
Expand All @@ -47,10 +45,15 @@ Adafruit_SGP30::Adafruit_SGP30() {}
* @return True if SGP30 found on I2C, False if something went wrong!
*/
boolean Adafruit_SGP30::begin(TwoWire *theWire, boolean initSensor) {
_i2caddr = SGP30_I2CADDR_DEFAULT;
_i2c = theWire;
if (i2c_dev) {
delete i2c_dev; // remove old interface
}

_i2c->begin();
i2c_dev = new Adafruit_I2CDevice(SGP30_I2CADDR_DEFAULT, theWire);

if (!i2c_dev->begin()) {
return false;
}

uint8_t command[2];
command[0] = 0x36;
Expand Down Expand Up @@ -217,55 +220,26 @@ boolean Adafruit_SGP30::setHumidity(uint32_t absolute_humidity) {
* @brief I2C low level interfacing
*/

boolean Adafruit_SGP30::readWordFromCommand(uint8_t command[],
uint8_t commandLength,
uint16_t delayms,
uint16_t *readdata,
uint8_t readlen) {
bool Adafruit_SGP30::readWordFromCommand(uint8_t command[],
uint8_t commandLength,
uint16_t delayms, uint16_t *readdata,
uint8_t readlen) {

_i2c->beginTransmission(_i2caddr);

#ifdef I2C_DEBUG
Serial.print("\t\t-> ");
#endif

for (uint8_t i = 0; i < commandLength; i++) {
_i2c->write(command[i]);
#ifdef I2C_DEBUG
Serial.print("0x");
Serial.print(command[i], HEX);
Serial.print(", ");
#endif
if (!i2c_dev->write(command, commandLength)) {
return false;
}
#ifdef I2C_DEBUG
Serial.println();
#endif
_i2c->endTransmission();

delay(delayms);

if (readlen == 0)
return true;

uint8_t replylen = readlen * (SGP30_WORD_LEN + 1);
if (_i2c->requestFrom(_i2caddr, replylen) != replylen)
return false;
uint8_t replybuffer[replylen];
#ifdef I2C_DEBUG
Serial.print("\t\t<- ");
#endif
for (uint8_t i = 0; i < replylen; i++) {
replybuffer[i] = _i2c->read();
#ifdef I2C_DEBUG
Serial.print("0x");
Serial.print(replybuffer[i], HEX);
Serial.print(", ");
#endif
}

#ifdef I2C_DEBUG
Serial.println();
#endif
if (!i2c_dev->read(replybuffer, replylen)) {
return false;
}

for (uint8_t i = 0; i < readlen; i++) {
uint8_t crc = generateCRC(replybuffer + i * 3, 2);
Expand Down
13 changes: 6 additions & 7 deletions Adafruit_SGP30.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#define ADAFRUIT_SGP30_H

#include "Arduino.h"
#include <Wire.h>
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>

// the i2c address
#define SGP30_I2CADDR_DEFAULT 0x58 ///< SGP30 has only one I2C address
Expand Down Expand Up @@ -71,14 +72,12 @@ class Adafruit_SGP30 {
uint16_t serialnumber[3];

private:
TwoWire *_i2c;
uint8_t _i2caddr;

Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
void write(uint8_t address, uint8_t *data, uint8_t n);
void read(uint8_t address, uint8_t *data, uint8_t n);
boolean readWordFromCommand(uint8_t command[], uint8_t commandLength,
uint16_t delay, uint16_t *readdata = NULL,
uint8_t readlen = 0);
bool readWordFromCommand(uint8_t command[], uint8_t commandLength,
uint16_t delay, uint16_t *readdata = NULL,
uint8_t readlen = 0);
uint8_t generateCRC(uint8_t data[], uint8_t datalen);
};
#endif // ndef ADAFRUIT_SGP30_H
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ Adafruit invests time and resources providing this open source code, please supp
To install, use the Arduino Library Manager and search for "Adafruit SGP30" and install the library.

## Dependencies
* [Adafruit ILI9341](https://github.com/adafruit/Adafruit_ILI9341)
* [Adafruit GFX Library](https://github.com/adafruit/Adafruit-GFX-Library)
* [Adafruit Bus IO](https://github.com/adafruit/Adafruit_BusIO)

# Contributing

Expand Down
4 changes: 3 additions & 1 deletion examples/sgp30test/sgp30test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ uint32_t getAbsoluteHumidity(float temperature, float humidity) {
}

void setup() {
Serial.begin(9600);
Serial.begin(115200);
while (!Serial) { delay(10); } // Wait for serial console to open!

Serial.println("SGP30 test");

if (! sgp.begin()){
Expand Down
3 changes: 2 additions & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name=Adafruit SGP30 Sensor
version=1.2.2
version=2.0.0
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=This is an Arduino library for the Adafruit SGP30 Gas / Air Quality Sensor
paragraph=This is an Arduino library for the Adafruit SGP30 Gas / Air Quality Sensor
category=Sensors
url=https://github.com/adafruit/Adafruit_SGP30
architectures=*
depends=Adafruit BusIO

0 comments on commit 267c88c

Please sign in to comment.