1
+ /* !
2
+ * @file WipperSnapper_I2C_Driver_BME680.h
3
+ *
4
+ * Device driver for an AHT Humidity and Temperature 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) Brent Rubell 2021 for Adafruit Industries.
11
+ *
12
+ * MIT license, all text here must be included in any redistribution.
13
+ *
14
+ */
15
+
16
+ #ifndef WipperSnapper_I2C_Driver_BME680_H
17
+ #define WipperSnapper_I2C_Driver_BME680_H
18
+
19
+ #include " WipperSnapper_I2C_Driver.h"
20
+ #include < Adafruit_BME680.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 BME680 temperature
27
+ and humidity sensor.
28
+ */
29
+ /* *************************************************************************/
30
+ class WipperSnapper_I2C_Driver_BME680 : public WipperSnapper_I2C_Driver {
31
+
32
+ public:
33
+ /* ******************************************************************************/
34
+ /* !
35
+ @brief Constructor for an BME680 sensor.
36
+ @param i2c
37
+ The I2C interface.
38
+ @param sensorAddress
39
+ 7-bit device address.
40
+ */
41
+ /* ******************************************************************************/
42
+ WipperSnapper_I2C_Driver_BME680 (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 BME680 sensor.
51
+ */
52
+ /* ******************************************************************************/
53
+ ~WipperSnapper_I2C_Driver_BME680 () { delete _bme; }
54
+
55
+ /* ******************************************************************************/
56
+ /* !
57
+ @brief Initializes the BME680 sensor and begins I2C.
58
+ @returns True if initialized successfully, False otherwise.
59
+ */
60
+ /* ******************************************************************************/
61
+ bool begin () {
62
+ _bme = new Adafruit_BME680 (_i2c);
63
+
64
+ // attempt to initialize BME680
65
+ if (!_bme->begin ())
66
+ return false ;
67
+
68
+ // Set up oversampling and filter initialization
69
+ // defaults from
70
+ // https://github.com/adafruit/Adafruit_BME680/blob/master/examples/bme680test/bme680test.ino
71
+ _bme->setTemperatureOversampling (BME680_OS_8X);
72
+ _bme->setHumidityOversampling (BME680_OS_2X);
73
+ _bme->setPressureOversampling (BME680_OS_4X);
74
+ _bme->setIIRFilterSize (BME680_FILTER_SIZE_3);
75
+ _bme->setGasHeater (320 , 150 ); // 320*C for 150 ms
76
+
77
+ return true ;
78
+ }
79
+
80
+ /* ******************************************************************************/
81
+ /* !
82
+ @brief Performs a reading in blocking mode.
83
+ @returns True if the reading succeeded, False otherwise.
84
+ */
85
+ /* ******************************************************************************/
86
+ bool bmePerformReading () { return _bme->performReading (); }
87
+
88
+ /* ******************************************************************************/
89
+ /* !
90
+ @brief Gets the BME680's current temperature.
91
+ @param tempEvent
92
+ Pointer to an Adafruit_Sensor event.
93
+ @returns True if the temperature was obtained successfully, False
94
+ otherwise.
95
+ */
96
+ /* ******************************************************************************/
97
+ bool getEventAmbientTemp (sensors_event_t *tempEvent) {
98
+ if (!bmePerformReading ())
99
+ return false ;
100
+ tempEvent->temperature = _bme->temperature ;
101
+ return true ;
102
+ }
103
+
104
+ /* ******************************************************************************/
105
+ /* !
106
+ @brief Gets the BME680's current relative humidity reading.
107
+ @param humidEvent
108
+ Pointer to an Adafruit_Sensor event.
109
+ @returns True if the humidity was obtained successfully, False
110
+ otherwise.
111
+ */
112
+ /* ******************************************************************************/
113
+ bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
114
+ if (!bmePerformReading ())
115
+ return false ;
116
+ humidEvent->relative_humidity = _bme->humidity ;
117
+ return true ;
118
+ }
119
+
120
+ /* ******************************************************************************/
121
+ /* !
122
+ @brief Reads a pressure sensor and converts
123
+ the reading into the expected SI unit.
124
+ @param pressureEvent
125
+ Pointer to an Adafruit_Sensor event.
126
+ @returns True if the sensor event was obtained successfully, False
127
+ otherwise.
128
+ */
129
+ /* ******************************************************************************/
130
+ bool getEventPressure (sensors_event_t *pressureEvent) {
131
+ if (!bmePerformReading ())
132
+ return false ;
133
+ pressureEvent->pressure = (float )_bme->pressure ;
134
+ return true ;
135
+ }
136
+
137
+ /* ******************************************************************************/
138
+ /* !
139
+ @brief Reads a the BME680's altitude sensor into an event.
140
+ @param altitudeEvent
141
+ Pointer to an adafruit sensor event.
142
+ @returns True if the sensor event was obtained successfully, False
143
+ otherwise.
144
+ */
145
+ /* ******************************************************************************/
146
+ bool getEventAltitude (sensors_event_t *altitudeEvent) {
147
+ if (!bmePerformReading ())
148
+ return false ;
149
+ // NOTE: This is hacked onto Adafruit_Sensor and should eventually be
150
+ // removed
151
+ altitudeEvent->data [0 ] = (float )_bme->readAltitude (SEALEVELPRESSURE_HPA);
152
+ return true ;
153
+ }
154
+
155
+ /* ******************************************************************************/
156
+ /* !
157
+ @brief Base implementation - Reads a gas resistance sensor and converts
158
+ the reading into the expected SI unit.
159
+ @param gasEvent
160
+ gas resistance sensor reading, in ohms.
161
+ @returns True if the sensor event was obtained successfully, False
162
+ otherwise.
163
+ */
164
+ /* ******************************************************************************/
165
+ virtual bool getEventGasResistance (sensors_event_t *gasEvent) {
166
+ if (!bmePerformReading ())
167
+ return false ;
168
+ // NOTE: This is hacked onto Adafruit_Sensor and should eventually be
169
+ // removed
170
+ gasEvent->data [0 ] = (float )_bme->gas_resistance ;
171
+ return true ;
172
+ }
173
+
174
+ protected:
175
+ Adafruit_BME680 *_bme; // /< BME680 object
176
+ };
177
+
178
+ #endif // WipperSnapper_I2C_Driver_BME680
0 commit comments