forked from sensidev/BME680
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbed_bme680.cpp
353 lines (279 loc) · 8.87 KB
/
mbed_bme680.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "mbed_bme680.h"
BME680::BME680() {
_filterEnabled = _tempEnabled = _humEnabled = _presEnabled = _gasEnabled = false;
}
bool BME680::begin() {
int8_t result;
gas_sensor.dev_id = 0;
gas_sensor.intf = BME680_SPI_INTF;
gas_sensor.read = &BME680::spi_read;
gas_sensor.write = &BME680::spi_write;
gas_sensor.delay_ms = BME680::delay_msec;
setHumidityOversampling(BME680_OS_2X);
setPressureOversampling(BME680_OS_4X);
setTemperatureOversampling(BME680_OS_8X);
setIIRFilterSize(BME680_FILTER_SIZE_3);
setGasHeater(320, 150); // 320*C for 150 ms
cs.write(1);
result = bme680_init(&gas_sensor);
if (result != BME680_OK)
return false;
return true;
}
/**
* Performs a full reading of all 4 sensors in the BME680.
* Assigns the internal BME680#temperature, BME680#pressure, BME680#humidity and BME680#gas_resistance member variables
* @return True on success, False on failure
*/
bool BME680::performReading(void) {
uint8_t set_required_settings = 0;
int8_t result;
/* Select the power mode */
/* Must be set before writing the sensor configuration */
gas_sensor.power_mode = BME680_FORCED_MODE;
/* Set the required sensor settings needed */
if (_tempEnabled)
set_required_settings |= BME680_OST_SEL;
if (_humEnabled)
set_required_settings |= BME680_OSH_SEL;
if (_presEnabled)
set_required_settings |= BME680_OSP_SEL;
if (_filterEnabled)
set_required_settings |= BME680_FILTER_SEL;
if (_gasEnabled)
set_required_settings |= BME680_GAS_SENSOR_SEL;
/* Set the desired sensor configuration */
result = bme680_set_sensor_settings(set_required_settings, &gas_sensor);
log("Set settings, result %d \r\n", result);
if (result != BME680_OK)
return false;
/* Set the power mode */
result = bme680_set_sensor_mode(&gas_sensor);
log("Set power mode, result %d \r\n", result);
if (result != BME680_OK)
return false;
/* Get the total measurement duration so as to sleep or wait till the
* measurement is complete */
uint16_t meas_period;
bme680_get_profile_dur(&meas_period, &gas_sensor);
/* Delay till the measurement is ready */
delay_msec(meas_period);
result = bme680_get_sensor_data(&data, &gas_sensor);
log("Get sensor data, result %d \r\n", result);
if (result != BME680_OK)
return false;
return true;
}
bool BME680::isGasHeatingSetupStable() {
if (data.status & BME680_HEAT_STAB_MSK) {
return true;
}
return false;
}
int16_t BME680::getRawTemperature() {
return data.temperature;
}
uint32_t BME680::getRawPressure() {
return data.pressure;
}
uint32_t BME680::getRawHumidity() {
return data.humidity;
}
uint32_t BME680::getRawGasResistance() {
return data.gas_resistance;
}
/**
* Get last read temperature
* @return Temperature in degree celsius
*/
float BME680::getTemperature() {
float temperature = NAN;
if (_tempEnabled) {
temperature = data.temperature / 100.0;
log("Temperature Raw Data %d \r\n", temperature);
}
return temperature;
}
/**
* Get last read humidity
* @return Humidity in % relative humidity
*/
float BME680::getHumidity() {
float humidity = NAN;
if (_humEnabled) {
humidity = data.humidity / 1000.0;
log("Humidity Raw Data %d \r\n", humidity);
}
return humidity;
}
/**
* Get last read pressure
* @return Pressure in Pascal
*/
float BME680::getPressure() {
float pressure = NAN;
if (_presEnabled) {
pressure = data.pressure;
log("Pressure Raw Data %d \r\n", pressure);
}
return pressure;
}
/**
* Get last read gas resistance
* @return Gas resistance in Ohms
*/
float BME680::getGasResistance() {
float gas_resistance = 0;
if (_gasEnabled) {
if (this->isGasHeatingSetupStable()) {
gas_resistance = data.gas_resistance;
log("Gas Resistance Raw Data %d \r\n", gas_resistance);
} else {
log("Gas reading unstable \r\n");
}
}
return gas_resistance;
}
/**
* Enable and configure gas reading + heater
* @param heaterTemp Desired temperature in degrees Centigrade
* @param heaterTime Time to keep heater on in milliseconds
* @return True on success, False on failure
*/
bool BME680::setGasHeater(uint16_t heaterTemp, uint16_t heaterTime) {
gas_sensor.gas_sett.heatr_temp = heaterTemp;
gas_sensor.gas_sett.heatr_dur = heaterTime;
if ((heaterTemp == 0) || (heaterTime == 0)) {
// disabled!
gas_sensor.gas_sett.run_gas = BME680_DISABLE_GAS_MEAS;
_gasEnabled = false;
} else {
gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
_gasEnabled = true;
}
return true;
}
/**
* Setter for Temperature oversampling
* @param oversample Oversampling setting, can be BME680_OS_NONE (turn off Temperature reading),
* BME680_OS_1X, BME680_OS_2X, BME680_OS_4X, BME680_OS_8X or BME680_OS_16X
* @return True on success, False on failure
*/
bool BME680::setTemperatureOversampling(uint8_t oversample) {
if (oversample > BME680_OS_16X) return false;
gas_sensor.tph_sett.os_temp = oversample;
if (oversample == BME680_OS_NONE)
_tempEnabled = false;
else
_tempEnabled = true;
return true;
}
/**
* Setter for Humidity oversampling
* @param oversample Oversampling setting, can be BME680_OS_NONE (turn off Humidity reading),
* BME680_OS_1X, BME680_OS_2X, BME680_OS_4X, BME680_OS_8X or BME680_OS_16X
* @return True on success, False on failure
*/
bool BME680::setHumidityOversampling(uint8_t oversample) {
if (oversample > BME680_OS_16X) return false;
gas_sensor.tph_sett.os_hum = oversample;
if (oversample == BME680_OS_NONE)
_humEnabled = false;
else
_humEnabled = true;
return true;
}
/**
* Setter for Pressure oversampling
* @param oversample Oversampling setting, can be BME680_OS_NONE (turn off Humidity reading),
* BME680_OS_1X, BME680_OS_2X, BME680_OS_4X, BME680_OS_8X or BME680_OS_16X
* @return True on success, False on failure
*/
bool BME680::setPressureOversampling(uint8_t oversample) {
if (oversample > BME680_OS_16X) return false;
gas_sensor.tph_sett.os_pres = oversample;
if (oversample == BME680_OS_NONE)
_presEnabled = false;
else
_presEnabled = true;
return true;
}
/**
* Setter for IIR filter.
* @param filter_seize Size of the filter (in samples).
* Can be BME680_FILTER_SIZE_0 (no filtering), BME680_FILTER_SIZE_1, BME680_FILTER_SIZE_3, BME680_FILTER_SIZE_7,
* BME680_FILTER_SIZE_15, BME680_FILTER_SIZE_31, BME680_FILTER_SIZE_63, BME680_FILTER_SIZE_127
* @return True on success, False on failure
*/
bool BME680::setIIRFilterSize(uint8_t filter_seize) {
if (filter_seize > BME680_FILTER_SIZE_127) return false;
gas_sensor.tph_sett.filter = filter_seize;
if (filter_seize == BME680_FILTER_SIZE_0)
_filterEnabled = false;
else
_filterEnabled = true;
return true;
}
/**
* Reads 8 bit values over SPI
* @param dev_id Device ID (8 bits I2C address)
* @param reg_addr Register address to read from
* @param reg_data Read data buffer
* @param len Number of bytes to read
* @return 0 on success, non-zero for failure
*/
int8_t BME680::spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) {
int8_t result;
char data[1];
data[0] = (char) reg_addr;
log("[0x%X] SPI $%X => ", dev_id >> 1, data[0]);
cs.write(0);
spi.write(data, 1, (char *) reg_data, 0);
result = spi.write(data, 0, (char *) reg_data, len);
cs.write(1);
log("[W: %d] ", result);
for (uint8_t i = 0; i < len; i++) log("0x%X ", reg_data[i]);
log("[R: %d, L: %d] \r\n", result, len);
if (result != 0) {
return 0;
}
return 1;
}
/**
* Writes 8 bit values over SPI
* @param dev_id Device ID (8 bits I2C address)
* @param reg_addr Register address to write to
* @param reg_data Write data buffer
* @param len Number of bytes to write
* @return 0 on success, non-zero for failure
*/
int8_t BME680::spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) {
int8_t result;
char data[len + 1];
data[0] = (char) reg_addr;
for (uint8_t i = 1; i < len + 1; i++) {
data[i] = (char) reg_data[i - 1];
}
log("[0x%X] SPI $%X <= ", dev_id >> 1, data[0]);
cs.write(0);
result = spi.write(data, len + 1, (char *) reg_data, 0);
cs.write(1);
for (uint8_t i = 1; i < len + 1; i++) log("0x%X ", data[i]);
log("[W: %d, L: %d] \r\n", result, len);
if (result != 0) {
return 0;
}
return 1;
}
void BME680::delay_msec(uint32_t ms) {
log(" * wait %d ms ... \r\n", ms);
wait_us(ms * 1000);
}
void BME680::log(const char *format, ...) {
#ifdef BME680_DEBUG_MODE
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
#endif
}