Skip to content

Commit 11c5678

Browse files
authored
Merge pull request #865 from adafruit/ens161
add(component): ENS161 support, mention low power option
2 parents e52f1db + f9b7eb6 commit 11c5678

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,17 +350,18 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
350350
_ds2484->configureDriver(msgDeviceInitReq);
351351
drivers.push_back(_ds2484);
352352
WS_DEBUG_PRINTLN("DS2484 Initialized Successfully!");
353-
} else if (strcmp("ens160", msgDeviceInitReq->i2c_device_name) == 0) {
354-
_ens160 = new WipperSnapper_I2C_Driver_ENS160(this->_i2c, i2cAddress);
355-
if (!_ens160->begin()) {
356-
WS_DEBUG_PRINTLN("ERROR: Failed to initialize ENS160!");
353+
} else if ((strcmp("ens160", msgDeviceInitReq->i2c_device_name) == 0) ||
354+
(strcmp("ens161", msgDeviceInitReq->i2c_device_name) == 0)) {
355+
_ens16x = new WipperSnapper_I2C_Driver_ENS16x(this->_i2c, i2cAddress);
356+
if (!_ens16x->begin()) {
357+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize ENS16x!");
357358
_busStatusResponse =
358359
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
359360
return false;
360361
}
361-
_ens160->configureDriver(msgDeviceInitReq);
362-
drivers.push_back(_ens160);
363-
WS_DEBUG_PRINTLN("ENS160 Initialized Successfully!");
362+
_ens16x->configureDriver(msgDeviceInitReq);
363+
drivers.push_back(_ens16x);
364+
WS_DEBUG_PRINTLN("ENS16x Initialized Successfully!");
364365
} else if (strcmp("hdc302x", msgDeviceInitReq->i2c_device_name) == 0) {
365366
_hdc302x = new WipperSnapper_I2C_Driver_HDC302X(this->_i2c, i2cAddress);
366367
if (!_hdc302x->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "drivers/WipperSnapper_I2C_Driver_D6T1A.h"
3333
#include "drivers/WipperSnapper_I2C_Driver_DPS310.h"
3434
#include "drivers/WipperSnapper_I2C_Driver_DS2484.h"
35-
#include "drivers/WipperSnapper_I2C_Driver_ENS160.h"
35+
#include "drivers/WipperSnapper_I2C_Driver_ENS16X.h"
3636
#include "drivers/WipperSnapper_I2C_Driver_HDC302X.h"
3737
#include "drivers/WipperSnapper_I2C_Driver_HTS221.h"
3838
#include "drivers/WipperSnapper_I2C_Driver_HTU21D.h"
@@ -167,7 +167,7 @@ class WipperSnapper_Component_I2C {
167167
WipperSnapper_I2C_Driver_D6T1A *_d6t1a = nullptr;
168168
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
169169
WipperSnapper_I2C_Driver_DS2484 *_ds2484 = nullptr;
170-
WipperSnapper_I2C_Driver_ENS160 *_ens160 = nullptr;
170+
WipperSnapper_I2C_Driver_ENS16x *_ens16x = nullptr;
171171
WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr;
172172
WipperSnapper_I2C_Driver_BH1750 *_bh1750 = nullptr;
173173
WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr;

src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS160.h renamed to src/components/i2c/drivers/WipperSnapper_I2C_Driver_ENS16X.h

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
2-
* @file WipperSnapper_I2C_Driver_ENS160.h
2+
* @file WipperSnapper_I2C_Driver_ENS16X.h
33
*
4-
* Device driver for a ENS160 MOX Gas Sensor.
4+
* Device driver for a ENS160/ENS161 MOX Gas Sensor.
55
*
66
* Adafruit invests time and resources providing this open source code,
77
* please support Adafruit and open-source hardware by purchasing
@@ -13,8 +13,8 @@
1313
*
1414
*/
1515

16-
#ifndef WipperSnapper_I2C_Driver_ENS160_H
17-
#define WipperSnapper_I2C_Driver_ENS160_H
16+
#ifndef WipperSnapper_I2C_Driver_ENS16X_H
17+
#define WipperSnapper_I2C_Driver_ENS16X_H
1818

1919
#include "WipperSnapper_I2C_Driver.h"
2020
#include <ScioSense_ENS160.h>
@@ -23,50 +23,53 @@
2323

2424
/**************************************************************************/
2525
/*!
26-
@brief Class that provides a sensor driver for the ENS160 temperature
27-
and humidity sensor.
26+
@brief Class that provides a sensor driver for the ENS16x temperature
27+
and humidity sensors.
2828
*/
2929
/**************************************************************************/
30-
class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver {
30+
class WipperSnapper_I2C_Driver_ENS16x : public WipperSnapper_I2C_Driver {
3131

3232
public:
3333
/*******************************************************************************/
3434
/*!
35-
@brief Constructor for an ENS160 sensor.
35+
@brief Constructor for an ENS16x sensor.
3636
@param i2c
3737
The I2C interface.
3838
@param sensorAddress
3939
7-bit device address.
4040
*/
4141
/*******************************************************************************/
42-
WipperSnapper_I2C_Driver_ENS160(TwoWire *i2c, uint16_t sensorAddress)
42+
WipperSnapper_I2C_Driver_ENS16x(TwoWire *i2c, uint16_t sensorAddress)
4343
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
4444
_i2c = i2c;
4545
_sensorAddress = sensorAddress;
4646
}
4747

4848
/*******************************************************************************/
4949
/*!
50-
@brief Destructor for an ENS160 sensor.
50+
@brief Destructor for an ENS16x sensor.
5151
*/
5252
/*******************************************************************************/
53-
~WipperSnapper_I2C_Driver_ENS160() { delete _ens160; }
53+
~WipperSnapper_I2C_Driver_ENS16x() { delete _ens16x; }
5454

5555
/*******************************************************************************/
5656
/*!
57-
@brief Initializes the ENS160 sensor and begins I2C.
57+
@brief Initializes the ENS16x sensor and begins I2C.
5858
@returns True if initialized successfully, False otherwise.
5959
*/
6060
/*******************************************************************************/
6161
bool begin() {
62-
_ens160 = new ScioSense_ENS160((TwoWire *)_i2c, (uint8_t)_sensorAddress);
62+
_ens16x = new ScioSense_ENS160((TwoWire *)_i2c, (uint8_t)_sensorAddress);
6363

64-
// attempt to initialize ENS160
65-
if (!_ens160->begin())
64+
// attempt to initialize ENS16x, verify chip id
65+
if (!_ens16x->begin() || !_ens16x->available())
6666
return false;
6767

68-
// Set the mode to standard
69-
return _ens160->setMode(ENS160_OPMODE_STD);
68+
/* In future set the mode to ulp for 161 (need to add to adafruit lib), see
69+
* https://github.com/sciosense/ens16x-arduino/blob/d09d25dd0912b729a21366e58b55393a49afc256/src/lib/ens16x/ScioSense_Ens161.h#L10-L22
70+
* _ens16x->revENS16x() == 0 ? ENS160_OPMODE_STD : ENS160_OPMODE_LP/ULP
71+
*/
72+
return _ens16x->setMode(ENS160_OPMODE_STD);
7073
}
7174

7275
/*******************************************************************************/
@@ -76,12 +79,12 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver {
7679
*/
7780
/*******************************************************************************/
7881
bool ensPerformReading() {
79-
return _ens160->available() && _ens160->measure(true);
82+
return _ens16x->available() && _ens16x->measure(true);
8083
}
8184

8285
/*******************************************************************************/
8386
/*!
84-
@brief Reads the ENS160's eCO2 sensor into an event.
87+
@brief Reads the ENS16x's eCO2 sensor into an event.
8588
@param eco2Event
8689
Pointer to an adafruit sensor event.
8790
@returns True if the sensor event was obtained successfully, False
@@ -91,13 +94,13 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver {
9194
bool getEventECO2(sensors_event_t *eco2Event) {
9295
if (!ensPerformReading())
9396
return false;
94-
eco2Event->eCO2 = (float)_ens160->geteCO2();
97+
eco2Event->eCO2 = (float)_ens16x->geteCO2();
9598
return true;
9699
}
97100

98101
/*******************************************************************************/
99102
/*!
100-
@brief Reads the ENS160's TVOC sensor into an event.
103+
@brief Reads the ENS16x's TVOC sensor into an event.
101104
@param tvocEvent
102105
Pointer to an adafruit sensor event.
103106
@returns True if the sensor event was obtained successfully, False
@@ -107,13 +110,13 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver {
107110
bool getEventTVOC(sensors_event_t *tvocEvent) {
108111
if (!ensPerformReading())
109112
return false;
110-
tvocEvent->tvoc = (float)_ens160->getTVOC();
113+
tvocEvent->tvoc = (float)_ens16x->getTVOC();
111114
return true;
112115
}
113116

114117
/*******************************************************************************/
115118
/*!
116-
@brief Reads the ENS160's AQI value into an event.
119+
@brief Reads the ENS16x's AQI value into an event.
117120
@param rawEvent
118121
Pointer to an adafruit sensor event.
119122
@returns True if the sensor event was obtained successfully, False
@@ -123,12 +126,12 @@ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver {
123126
bool getEventRaw(sensors_event_t *rawEvent) {
124127
if (!ensPerformReading())
125128
return false;
126-
rawEvent->data[0] = (float)_ens160->getAQI();
129+
rawEvent->data[0] = (float)_ens16x->getAQI();
127130
return true;
128131
}
129132

130133
protected:
131-
ScioSense_ENS160 *_ens160; ///< ENS160 object
134+
ScioSense_ENS160 *_ens16x; ///< ENS16x object
132135
};
133136

134-
#endif // WipperSnapper_I2C_Driver_ENS160
137+
#endif // WipperSnapper_I2C_Driver_ENS16X_H

0 commit comments

Comments
 (0)