From 5cf01233151569a73476a889a871b3c9d833a845 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 19 Sep 2023 16:53:58 +0100 Subject: [PATCH 1/3] Initial ENS160 code --- library.properties | 2 +- platformio.ini | 1 + src/components/i2c/WipperSnapper_I2C.cpp | 11 ++ src/components/i2c/WipperSnapper_I2C.h | 2 + .../drivers/WipperSnapper_I2C_Driver_ENS160.h | 135 ++++++++++++++++++ 5 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h diff --git a/library.properties b/library.properties index d87d48413..4bc8aec3d 100644 --- a/library.properties +++ b/library.properties @@ -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 diff --git a/platformio.ini b/platformio.ini index b26f9d857..ee82e4029 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 884d5cfdd..6fd26e519 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -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()) { diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index f97e455b7..f6371d22a 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -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" @@ -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; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h new file mode 100644 index 000000000..cf160d3b9 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h @@ -0,0 +1,135 @@ +/*! + * @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 + +#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(_i2c, _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 \ No newline at end of file From 9d60392fd12ec1a81ee1c687fd6bc274deaf520a Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 19 Sep 2023 17:18:22 +0100 Subject: [PATCH 2/3] Correct case sensitivity + clang format --- src/components/i2c/WipperSnapper_I2C.h | 2 +- .../i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index f6371d22a..3b9ac0125 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -106,7 +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_ENS160 *_ens160 = nullptr; WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr; WipperSnapper_I2C_Driver_BH1750 *_bh1750 = nullptr; WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h index cf160d3b9..28998abc8 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h @@ -17,7 +17,7 @@ #define WipperSnapper_I2C_Driver_ENS160_H #include "WipperSnapper_I2C_Driver.h" -#include +#include #define SEALEVELPRESSURE_HPA (1013.25) ///< Default sea level pressure, in hPa @@ -64,7 +64,7 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver { // attempt to initialize ENS160 if (!_ens160->begin()) return false; - + // Set the mode to standard return _ens160->setMode(ENS160_OPMODE_STD); } @@ -75,7 +75,9 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver { @returns True if the reading succeeded, False otherwise. */ /*******************************************************************************/ - bool ensPerformReading() { return _ens160->available() && _ens160->measure(true); } + bool ensPerformReading() { + return _ens160->available() && _ens160->measure(true); + } /*******************************************************************************/ /*! @@ -93,7 +95,6 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver { return true; } - /*******************************************************************************/ /*! @brief Reads the ENS160's TVOC sensor into an event. @@ -110,7 +111,6 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver { return true; } - /*******************************************************************************/ /*! @brief Reads the ENS160's AQI value into an event. @@ -127,7 +127,6 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver { return true; } - protected: ScioSense_ENS160 *_ens160; ///< ENS160 object }; From 164b457867a5916d97d6a6eb22de81a258763074 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 19 Sep 2023 22:40:27 +0100 Subject: [PATCH 3/3] fix: typecast twowire and i2c address arguments --- src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h index 28998abc8..49015b745 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h @@ -59,7 +59,7 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool begin() { - _ens160 = new ScioSense_ENS160(_i2c, _sensorAddress); + _ens160 = new ScioSense_ENS160((TwoWire *)_i2c, (uint8_t)_sensorAddress); // attempt to initialize ENS160 if (!_ens160->begin())