1
+ /* !
2
+ * @file WipperSnapper_I2C_Driver_ENS160.h
3
+ *
4
+ * Device driver for a ENS160 MOX Gas Sensor.
5
+ *
6
+ * Adafruit invests time and resources providing this open source code,
7
+ * please support Adafruit and open-source hardware by purchasing
8
+ * products from Adafruit!
9
+ *
10
+ * Copyright (c) Tyeth Gundry 2023 for Adafruit Industries.
11
+ *
12
+ * MIT license, all text here must be included in any redistribution.
13
+ *
14
+ */
15
+
16
+ #ifndef WipperSnapper_I2C_Driver_ENS160_H
17
+ #define WipperSnapper_I2C_Driver_ENS160_H
18
+
19
+ #include " WipperSnapper_I2C_Driver.h"
20
+ #include < ScioSense_ENS160.h>
21
+
22
+ #define SEALEVELPRESSURE_HPA (1013.25 ) // /< Default sea level pressure, in hPa
23
+
24
+ /* *************************************************************************/
25
+ /* !
26
+ @brief Class that provides a sensor driver for the ENS160 temperature
27
+ and humidity sensor.
28
+ */
29
+ /* *************************************************************************/
30
+ class WipperSnapper_I2C_Driver_ENS160 : public WipperSnapper_I2C_Driver {
31
+
32
+ public:
33
+ /* ******************************************************************************/
34
+ /* !
35
+ @brief Constructor for an ENS160 sensor.
36
+ @param i2c
37
+ The I2C interface.
38
+ @param sensorAddress
39
+ 7-bit device address.
40
+ */
41
+ /* ******************************************************************************/
42
+ WipperSnapper_I2C_Driver_ENS160 (TwoWire *i2c, uint16_t sensorAddress)
43
+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
44
+ _i2c = i2c;
45
+ _sensorAddress = sensorAddress;
46
+ }
47
+
48
+ /* ******************************************************************************/
49
+ /* !
50
+ @brief Destructor for an ENS160 sensor.
51
+ */
52
+ /* ******************************************************************************/
53
+ ~WipperSnapper_I2C_Driver_ENS160 () { delete _ens160; }
54
+
55
+ /* ******************************************************************************/
56
+ /* !
57
+ @brief Initializes the ENS160 sensor and begins I2C.
58
+ @returns True if initialized successfully, False otherwise.
59
+ */
60
+ /* ******************************************************************************/
61
+ bool begin () {
62
+ _ens160 = new ScioSense_ENS160 ((TwoWire *)_i2c, (uint8_t )_sensorAddress);
63
+
64
+ // attempt to initialize ENS160
65
+ if (!_ens160->begin ())
66
+ return false ;
67
+
68
+ // Set the mode to standard
69
+ return _ens160->setMode (ENS160_OPMODE_STD);
70
+ }
71
+
72
+ /* ******************************************************************************/
73
+ /* !
74
+ @brief Performs a reading in blocking mode.
75
+ @returns True if the reading succeeded, False otherwise.
76
+ */
77
+ /* ******************************************************************************/
78
+ bool ensPerformReading () {
79
+ return _ens160->available () && _ens160->measure (true );
80
+ }
81
+
82
+ /* ******************************************************************************/
83
+ /* !
84
+ @brief Reads the ENS160's eCO2 sensor into an event.
85
+ @param eco2Event
86
+ Pointer to an adafruit sensor event.
87
+ @returns True if the sensor event was obtained successfully, False
88
+ otherwise.
89
+ */
90
+ /* ******************************************************************************/
91
+ bool getEventECO2 (sensors_event_t *eco2Event) {
92
+ if (!ensPerformReading ())
93
+ return false ;
94
+ eco2Event->eCO2 = (float )_ens160->geteCO2 ();
95
+ return true ;
96
+ }
97
+
98
+ /* ******************************************************************************/
99
+ /* !
100
+ @brief Reads the ENS160's TVOC sensor into an event.
101
+ @param tvocEvent
102
+ Pointer to an adafruit sensor event.
103
+ @returns True if the sensor event was obtained successfully, False
104
+ otherwise.
105
+ */
106
+ /* ******************************************************************************/
107
+ bool getEventTVOC (sensors_event_t *tvocEvent) {
108
+ if (!ensPerformReading ())
109
+ return false ;
110
+ tvocEvent->tvoc = (float )_ens160->getTVOC ();
111
+ return true ;
112
+ }
113
+
114
+ /* ******************************************************************************/
115
+ /* !
116
+ @brief Reads the ENS160's AQI value into an event.
117
+ @param rawEvent
118
+ Pointer to an adafruit sensor event.
119
+ @returns True if the sensor event was obtained successfully, False
120
+ otherwise.
121
+ */
122
+ /* ******************************************************************************/
123
+ bool getEventRaw (sensors_event_t *rawEvent) {
124
+ if (!ensPerformReading ())
125
+ return false ;
126
+ rawEvent->data [0 ] = (float )_ens160->getAQI ();
127
+ return true ;
128
+ }
129
+
130
+ protected:
131
+ ScioSense_ENS160 *_ens160; // /< ENS160 object
132
+ };
133
+
134
+ #endif // WipperSnapper_I2C_Driver_ENS160
0 commit comments