Skip to content

Commit

Permalink
Merge pull request #491 from tyeth/add-ENS160
Browse files Browse the repository at this point in the history
Initial ENS160 code
  • Loading branch information
brentru authored Sep 20, 2023
2 parents f146157 + 164b457 commit 9eaeae9
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino client for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_IO_Arduino
architectures=*
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit PCT2075, hp_BH1750
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ lib_deps =
adafruit/Adafruit Zero DMA Library
adafruit/Adafruit SPIFlash
adafruit/Adafruit DotStar
adafruit/ENS160 - Adafruit Fork
adafruit/Adafruit SleepyDog Library
adafruit/Adafruit AHTX0
adafruit/Adafruit BME280 Library
Expand Down
11 changes: 11 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_dps310->configureDriver(msgDeviceInitReq);
drivers.push_back(_dps310);
WS_DEBUG_PRINTLN("DPS310 Initialized Successfully!");
} else if (strcmp("ens160", msgDeviceInitReq->i2c_device_name) == 0) {
_ens160 = new WipperSnapper_I2C_Driver_ENS160(this->_i2c, i2cAddress);
if (!_ens160->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize ENS160!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_ens160->configureDriver(msgDeviceInitReq);
drivers.push_back(_ens160);
WS_DEBUG_PRINTLN("ENS160 Initialized Successfully!");
} else if (strcmp("hts221", msgDeviceInitReq->i2c_device_name) == 0) {
_hts221 = new WipperSnapper_I2C_Driver_HTS221(this->_i2c, i2cAddress);
if (!_hts221->begin()) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "drivers/WipperSnapper_I2C_Driver_BMP280.h"
#include "drivers/WipperSnapper_I2C_Driver_BMP3XX.h"
#include "drivers/WipperSnapper_I2C_Driver_DPS310.h"
#include "drivers/WipperSnapper_I2C_Driver_ENS160.h"
#include "drivers/WipperSnapper_I2C_Driver_HTS221.h"
#include "drivers/WipperSnapper_I2C_Driver_INA219.h"
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
Expand Down Expand Up @@ -105,6 +106,7 @@ class WipperSnapper_Component_I2C {
// Sensor driver objects
WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr;
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
WipperSnapper_I2C_Driver_ENS160 *_ens160 = nullptr;
WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr;
WipperSnapper_I2C_Driver_BH1750 *_bh1750 = nullptr;
WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr;
Expand Down
134 changes: 134 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*!
* @file WipperSnapper_I2C_Driver_ENS160.h
*
* Device driver for a ENS160 MOX Gas Sensor.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2023 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/

#ifndef WipperSnapper_I2C_Driver_ENS160_H
#define WipperSnapper_I2C_Driver_ENS160_H

#include "WipperSnapper_I2C_Driver.h"
#include <ScioSense_ENS160.h>

#define SEALEVELPRESSURE_HPA (1013.25) ///< Default sea level pressure, in hPa

/**************************************************************************/
/*!
@brief Class that provides a sensor driver for the ENS160 temperature
and humidity sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver {

public:
/*******************************************************************************/
/*!
@brief Constructor for an ENS160 sensor.
@param i2c
The I2C interface.
@param sensorAddress
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_ENS160(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an ENS160 sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_ENS160() { delete _ens160; }

/*******************************************************************************/
/*!
@brief Initializes the ENS160 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_ens160 = new ScioSense_ENS160((TwoWire *)_i2c, (uint8_t)_sensorAddress);

// attempt to initialize ENS160
if (!_ens160->begin())
return false;

// Set the mode to standard
return _ens160->setMode(ENS160_OPMODE_STD);
}

/*******************************************************************************/
/*!
@brief Performs a reading in blocking mode.
@returns True if the reading succeeded, False otherwise.
*/
/*******************************************************************************/
bool ensPerformReading() {
return _ens160->available() && _ens160->measure(true);
}

/*******************************************************************************/
/*!
@brief Reads the ENS160's eCO2 sensor into an event.
@param eco2Event
Pointer to an adafruit sensor event.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventECO2(sensors_event_t *eco2Event) {
if (!ensPerformReading())
return false;
eco2Event->eCO2 = (float)_ens160->geteCO2();
return true;
}

/*******************************************************************************/
/*!
@brief Reads the ENS160's TVOC sensor into an event.
@param tvocEvent
Pointer to an adafruit sensor event.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventTVOC(sensors_event_t *tvocEvent) {
if (!ensPerformReading())
return false;
tvocEvent->tvoc = (float)_ens160->getTVOC();
return true;
}

/*******************************************************************************/
/*!
@brief Reads the ENS160's AQI value into an event.
@param rawEvent
Pointer to an adafruit sensor event.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventRaw(sensors_event_t *rawEvent) {
if (!ensPerformReading())
return false;
rawEvent->data[0] = (float)_ens160->getAQI();
return true;
}

protected:
ScioSense_ENS160 *_ens160; ///< ENS160 object
};

#endif // WipperSnapper_I2C_Driver_ENS160

0 comments on commit 9eaeae9

Please sign in to comment.