-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_functions.c
462 lines (419 loc) · 17.1 KB
/
internal_functions.c
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#define _XOPEN_SOURCE 700
#define TEMP_OFFSET 0.0f
#define BSEC
#include "internal_functions.h"
struct bme68x_data data[3];
uint32_t del_period;
uint32_t time_ms;
uint8_t n_fields;
static int8_t rslt;
uint16_t
get_max(uint16_t array[], int8_t len)
{
uint16_t max = 0;
for (int i = 0; i < (ssize_t)len; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}
void pi3g_delay_us(uint32_t duration_us, void *intf_ptr)
{
struct timespec ts;
ts.tv_sec = duration_us / 1000000;
ts.tv_nsec = (duration_us % 1000000) * 1000;
nanosleep(&ts, NULL);
}
int8_t pi3g_read(uint8_t regAddr, uint8_t *regData, uint32_t len, void *intf_ptr)
{
rslt = BME68X_OK;
if (write(*((uint8_t *)intf_ptr), ®Addr, 1) != 1)
{
perror("pi3g_read register");
rslt = -1;
}
if (read(*((uint8_t *)intf_ptr), regData, len) != len)
{
perror("pi3g_read data");
rslt = -1;
}
return rslt;
}
int8_t pi3g_write(uint8_t regAddr, const uint8_t *regData, uint32_t len, void *intf_ptr)
{
rslt = BME68X_OK;
uint8_t reg[len + 1];
reg[0] = regAddr;
for (uint32_t i = 1; i < len + 1; i++)
reg[i] = regData[i - 1];
if (write(*((uint8_t *)intf_ptr), reg, len + 1) != len + 1)
{
perror("pi3g_write");
rslt = -1;
}
return rslt;
}
int8_t pi3g_set_conf(uint8_t os_hum, uint8_t os_pres, uint8_t os_temp, uint8_t filter, uint8_t odr, struct bme68x_conf *conf, struct bme68x_dev *bme, uint8_t debug_mode)
{
int8_t rslt = BME68X_OK;
rslt = bme68x_get_conf(conf, bme);
if (rslt < 0)
{
perror("bme68x_get_conf");
}
conf->os_hum = os_hum;
conf->os_pres = os_pres;
conf->os_temp = os_temp;
conf->filter = filter;
conf->odr = odr;
rslt = bme68x_set_conf(conf, bme);
if (rslt != BME68X_OK)
{
perror("bme68x_set_conf");
}
if (debug_mode == 1)
{
printf("SET BME68X CONFIG\n");
}
return rslt;
}
int8_t pi3g_set_heater_conf_fm(uint8_t enable, uint16_t heatr_temp, uint16_t heatr_dur, struct bme68x_heatr_conf *heatr_conf, struct bme68x_dev *bme, uint8_t debug_mode)
{
int8_t rslt = BME68X_OK;
heatr_conf->enable = enable;
heatr_conf->heatr_temp = heatr_temp;
heatr_conf->heatr_dur = heatr_dur;
rslt = bme68x_set_heatr_conf(BME68X_FORCED_MODE, heatr_conf, bme);
if (rslt != BME68X_OK)
{
perror("bme68x_set_heatr_conf");
}
if (debug_mode == 1)
{
printf("SET HEATER CONFIG (FORCED MODE)\n");
}
return rslt;
}
int8_t pi3g_set_heater_conf_pm(uint8_t enable, uint16_t temp_prof[], uint16_t dur_prof[], uint8_t profile_len, struct bme68x_conf *conf, struct bme68x_heatr_conf *heatr_conf, struct bme68x_dev *bme, uint8_t debug_mode)
{
int8_t rslt = BME68X_OK;
heatr_conf->enable = enable;
heatr_conf->heatr_temp_prof = temp_prof;
heatr_conf->heatr_dur_prof = dur_prof;
heatr_conf->shared_heatr_dur = 140 - (bme68x_get_meas_dur(BME68X_PARALLEL_MODE, conf, bme) / 1000);
heatr_conf->profile_len = profile_len;
rslt = bme68x_set_heatr_conf(BME68X_PARALLEL_MODE, heatr_conf, bme);
if (rslt != BME68X_OK)
{
perror("bme68x_set_heatr_conf");
}
rslt = bme68x_set_op_mode(BME68X_PARALLEL_MODE, bme);
if (rslt != BME68X_OK)
{
perror("bme68x_set_op_mode");
}
if (debug_mode == 1)
{
printf("SET HEATER CONFIG (PARALLEL MODE)\n");
}
return rslt;
}
int8_t pi3g_set_heater_conf_sm(uint8_t enable, uint16_t temp_prof[], uint16_t dur_prof[], uint8_t profile_len, struct bme68x_heatr_conf *heatr_conf, struct bme68x_dev *bme, uint8_t debug_mode)
{
int8_t rslt = BME68X_OK;
heatr_conf->enable = enable;
heatr_conf->heatr_temp_prof = temp_prof;
heatr_conf->heatr_dur_prof = dur_prof;
heatr_conf->profile_len = profile_len;
rslt = bme68x_set_heatr_conf(BME68X_SEQUENTIAL_MODE, heatr_conf, bme);
if (rslt != BME68X_OK)
{
perror("bme68x_set_heatr_conf");
}
rslt = bme68x_set_op_mode(BME68X_SEQUENTIAL_MODE, bme);
if (rslt != BME68X_OK)
{
perror("bme68x_set_op_mode");
}
if (debug_mode == 1)
{
printf("SET HEATER CONFIG (SEQUENTIAL MODE)\n");
}
return rslt;
}
int64_t pi3g_timestamp_ns()
{
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
int64_t time_ns = (int64_t)(spec.tv_sec) * (int64_t)1000000000 + (int64_t)(spec.tv_nsec);
return time_ns;
}
uint32_t pi3g_timestamp_us()
{
return (uint32_t)(pi3g_timestamp_ns() / 1000);
}
uint32_t pi3g_timestamp_ms()
{
return (uint32_t)(pi3g_timestamp_us() / 1000);
}
#ifdef BSEC
bsec_library_return_t bsec_set_sample_rate(float sample_rate)
{
uint8_t n_requested_virtual_sensors;
n_requested_virtual_sensors = 13;
bsec_sensor_configuration_t requested_virtual_sensors[n_requested_virtual_sensors];
bsec_sensor_configuration_t required_sensor_settings[BSEC_MAX_PHYSICAL_SENSOR];
uint8_t n_required_sensor_settings = BSEC_MAX_PHYSICAL_SENSOR;
requested_virtual_sensors[0].sensor_id = BSEC_OUTPUT_IAQ;
requested_virtual_sensors[0].sample_rate = sample_rate;
requested_virtual_sensors[1].sensor_id = BSEC_OUTPUT_STATIC_IAQ;
requested_virtual_sensors[1].sample_rate = sample_rate;
requested_virtual_sensors[2].sensor_id = BSEC_OUTPUT_CO2_EQUIVALENT;
requested_virtual_sensors[2].sample_rate = sample_rate;
requested_virtual_sensors[3].sensor_id = BSEC_OUTPUT_BREATH_VOC_EQUIVALENT;
requested_virtual_sensors[3].sample_rate = sample_rate;
requested_virtual_sensors[4].sensor_id = BSEC_OUTPUT_RAW_TEMPERATURE;
requested_virtual_sensors[4].sample_rate = sample_rate;
requested_virtual_sensors[5].sensor_id = BSEC_OUTPUT_RAW_PRESSURE;
requested_virtual_sensors[5].sample_rate = sample_rate;
requested_virtual_sensors[6].sensor_id = BSEC_OUTPUT_RAW_HUMIDITY;
requested_virtual_sensors[6].sample_rate = sample_rate;
requested_virtual_sensors[7].sensor_id = BSEC_OUTPUT_RAW_GAS;
requested_virtual_sensors[7].sample_rate = sample_rate;
requested_virtual_sensors[8].sensor_id = BSEC_OUTPUT_STABILIZATION_STATUS;
requested_virtual_sensors[8].sample_rate = sample_rate;
requested_virtual_sensors[9].sensor_id = BSEC_OUTPUT_RUN_IN_STATUS;
requested_virtual_sensors[9].sample_rate = sample_rate;
requested_virtual_sensors[10].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE;
requested_virtual_sensors[10].sample_rate = sample_rate;
requested_virtual_sensors[11].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY;
requested_virtual_sensors[11].sample_rate = sample_rate;
requested_virtual_sensors[12].sensor_id = BSEC_OUTPUT_GAS_PERCENTAGE;
requested_virtual_sensors[12].sample_rate = sample_rate;
return bsec_update_subscription(requested_virtual_sensors, n_requested_virtual_sensors, required_sensor_settings, &n_required_sensor_settings);
}
bsec_library_return_t bsec_set_sample_rate_ai(uint8_t variant_id, struct bme68x_heatr_conf bme68x_heatr_conf, uint8_t num_ai_classes)
{
if (variant_id == BME68X_VARIANT_GAS_LOW)
{
perror("bsec_set_sample_rate_ai");
printf("AI features are not available for BME680\n");
return BSEC_OK;
}
uint8_t n_requested_virtual_sensors;
n_requested_virtual_sensors = 12;
bsec_sensor_configuration_t requested_virtual_sensors[n_requested_virtual_sensors];
bsec_sensor_configuration_t required_sensor_settings[BSEC_MAX_PHYSICAL_SENSOR];
uint8_t n_required_sensor_settings = BSEC_MAX_PHYSICAL_SENSOR;
printf("SHARED HEATR DUR IN SET SAMPLE RATE AI %d\n", bme68x_heatr_conf.shared_heatr_dur);
requested_virtual_sensors[0].sensor_id = BSEC_OUTPUT_GAS_ESTIMATE_1;
requested_virtual_sensors[0].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[1].sensor_id = BSEC_OUTPUT_GAS_ESTIMATE_2;
requested_virtual_sensors[1].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[2].sensor_id = BSEC_OUTPUT_GAS_ESTIMATE_3;
requested_virtual_sensors[2].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[3].sensor_id = BSEC_OUTPUT_GAS_ESTIMATE_4;
requested_virtual_sensors[3].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[4].sensor_id = BSEC_OUTPUT_RAW_TEMPERATURE;
requested_virtual_sensors[4].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[5].sensor_id = BSEC_OUTPUT_RAW_PRESSURE;
requested_virtual_sensors[5].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[6].sensor_id = BSEC_OUTPUT_RAW_HUMIDITY;
requested_virtual_sensors[6].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[7].sensor_id = BSEC_OUTPUT_RAW_GAS;
requested_virtual_sensors[7].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[8].sensor_id = BSEC_OUTPUT_RAW_GAS_INDEX;
requested_virtual_sensors[8].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[9].sensor_id = BSEC_OUTPUT_IAQ;
requested_virtual_sensors[9].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[10].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE;
requested_virtual_sensors[10].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
requested_virtual_sensors[11].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY;
requested_virtual_sensors[11].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
/*
float SEL = ((float)1000) / ((float)bme68x_heatr_conf.shared_heatr_dur);
printf("SEL %.6f\n", SEL);
for (uint8_t i = 0; i < 4; i++)
{
if (i < num_ai_classes)
{
requested_virtual_sensors[i].sensor_id = BSEC_OUTPUT_GAS_ESTIMATE_1 + i;
requested_virtual_sensors[i].sample_rate = BSEC_SAMPLE_RATE_SCAN ;
}
}
float HTR = ((float)1000) / ((float)bme68x_heatr_conf.heatr_dur_prof[0]);
requested_virtual_sensors[4].sensor_id = BSEC_OUTPUT_RAW_GAS_INDEX;
requested_virtual_sensors[4].sample_rate = HTR;
*/
bsec_library_return_t rslt = bsec_update_subscription(requested_virtual_sensors, n_requested_virtual_sensors, required_sensor_settings, &n_required_sensor_settings);
printf("SET_SAMPLE_RATE_AI %d\n", rslt);
return rslt;
}
bsec_library_return_t bsec_read_data(struct bme68x_data *data, int64_t time_stamp, bsec_input_t *inputs, uint8_t *n_bsec_inputs, int32_t bsec_process_data, uint8_t op_mode, struct bme68x_dev *bme, int8_t temp_offset)
{
if (bsec_process_data)
{
/* Pressure to be processed by BSEC */
if (bsec_process_data & BSEC_PROCESS_PRESSURE)
{
printf("PRESSURE %f\n", data->pressure);
/* Place presssure sample into input struct */
inputs[*n_bsec_inputs].sensor_id = BSEC_INPUT_PRESSURE;
inputs[*n_bsec_inputs].signal = data->pressure;
inputs[*n_bsec_inputs].time_stamp = time_stamp;
(*n_bsec_inputs)++;
}
/* Temperature to be processed by BSEC */
if (bsec_process_data & BSEC_PROCESS_TEMPERATURE)
{
printf("TEMPERATURE %f\n", data->temperature);
/* Place temperature sample into input struct */
inputs[*n_bsec_inputs].sensor_id = BSEC_INPUT_TEMPERATURE;
#ifdef BME68X_FLOAT_POINT_COMPENSATION
inputs[*n_bsec_inputs].signal = data->temperature;
#else
inputs[*n_bsec_inputs].signal = data->temperature / 100.0f;
#endif
inputs[*n_bsec_inputs].time_stamp = time_stamp;
(*n_bsec_inputs)++;
/* Also add optional heatsource input which will be subtracted from the temperature reading to
* compensate for device-specific self-heating (supported in BSEC IAQ solution)*/
inputs[*n_bsec_inputs].sensor_id = BSEC_INPUT_HEATSOURCE;
inputs[*n_bsec_inputs].signal = temp_offset;
inputs[*n_bsec_inputs].time_stamp = time_stamp;
(*n_bsec_inputs)++;
}
/* Humidity to be processed by BSEC */
if (bsec_process_data & BSEC_PROCESS_HUMIDITY)
{
printf("HUMIDITY %f\n",data->humidity);
/* Place humidity sample into input struct */
inputs[*n_bsec_inputs].sensor_id = BSEC_INPUT_HUMIDITY;
#ifdef BME68X_FLOAT_POINT_COMPENSATION
inputs[*n_bsec_inputs].signal = data->humidity;
#else
inputs[*n_bsec_inputs].signal = data->humidity / 1000.0f;
#endif
inputs[*n_bsec_inputs].time_stamp = time_stamp;
(*n_bsec_inputs)++;
}
/* Gas to be processed by BSEC */
if (bsec_process_data & BSEC_PROCESS_GAS)
{
printf("GAS_RESISTANCE %f\n", data->gas_resistance);
/* Check whether gas_valid flag is set */
if (data->status & BME68X_GASM_VALID_MSK)
{
/* Place sample into input struct */
inputs[*n_bsec_inputs].sensor_id = BSEC_INPUT_GASRESISTOR;
inputs[*n_bsec_inputs].signal = data->gas_resistance;
inputs[*n_bsec_inputs].time_stamp = time_stamp;
(*n_bsec_inputs)++;
}
}
/* Profile part */
if (op_mode == BME68X_PARALLEL_MODE || op_mode == BME68X_SEQUENTIAL_MODE)
{
printf("PROFILE_PART %d\n", data->gas_index);
inputs[*n_bsec_inputs].sensor_id = BSEC_INPUT_PROFILE_PART;
inputs[*n_bsec_inputs].signal = data->gas_index;
inputs[*n_bsec_inputs].time_stamp = time_stamp;
(*n_bsec_inputs)++;
}
}
return BSEC_OK;
}
bsec_library_return_t bsec_process_data(bsec_input_t *bsec_inputs, uint8_t num_bsec_inputs)
{
/* Output buffer set to the maximum virtual sensor outputs supported */
bsec_output_t bsec_outputs[BSEC_NUMBER_OUTPUTS];
uint8_t num_bsec_outputs = 0;
uint8_t index = 0;
bsec_library_return_t bsec_status = BSEC_OK;
int64_t timestamp = 0;
float iaq = 0.0f;
uint8_t iaq_accuracy = 0;
float temp = 0.0f;
float raw_temp = 0.0f;
float raw_pressure = 0.0f;
float humidity = 0.0f;
float raw_humidity = 0.0f;
float raw_gas = 0.0f;
float static_iaq = 0.0f;
uint8_t static_iaq_accuracy = 0;
float co2_equivalent = 0.0f;
uint8_t co2_accuracy = 0;
float breath_voc_equivalent = 0.0f;
uint8_t breath_voc_accuracy = 0;
float comp_gas_value = 0.0f;
uint8_t comp_gas_accuracy = 0;
float gas_percentage = 0.0f;
uint8_t gas_percentage_accuracy = 0;
/* Check if something should be processed by BSEC */
if (num_bsec_inputs > 0)
{
/* Set number of outputs to the size of the allocated buffer */
/* BSEC_NUMBER_OUTPUTS to be defined */
num_bsec_outputs = BSEC_NUMBER_OUTPUTS;
/* Perform processing of the data by BSEC
Note:
* The number of outputs you get depends on what you asked for during bsec_update_subscription(). This is
handled under bme680_bsec_update_subscription() function in this example file.
* The number of actual outputs that are returned is written to num_bsec_outputs. */
bsec_status = bsec_do_steps(bsec_inputs, num_bsec_inputs, bsec_outputs, &num_bsec_outputs);
/* Iterate through the outputs and extract the relevant ones. */
for (index = 0; index < num_bsec_outputs; index++)
{
switch (bsec_outputs[index].sensor_id)
{
case BSEC_OUTPUT_IAQ:
iaq = bsec_outputs[index].signal;
iaq_accuracy = bsec_outputs[index].accuracy;
break;
case BSEC_OUTPUT_STATIC_IAQ:
static_iaq = bsec_outputs[index].signal;
static_iaq_accuracy = bsec_outputs[index].accuracy;
break;
case BSEC_OUTPUT_CO2_EQUIVALENT:
co2_equivalent = bsec_outputs[index].signal;
co2_accuracy = bsec_outputs[index].accuracy;
break;
case BSEC_OUTPUT_BREATH_VOC_EQUIVALENT:
breath_voc_equivalent = bsec_outputs[index].signal;
breath_voc_accuracy = bsec_outputs[index].accuracy;
break;
case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE:
temp = bsec_outputs[index].signal;
break;
case BSEC_OUTPUT_RAW_PRESSURE:
raw_pressure = bsec_outputs[index].signal;
break;
case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY:
humidity = bsec_outputs[index].signal;
break;
case BSEC_OUTPUT_RAW_GAS:
raw_gas = bsec_outputs[index].signal;
break;
case BSEC_OUTPUT_RAW_TEMPERATURE:
raw_temp = bsec_outputs[index].signal;
break;
case BSEC_OUTPUT_RAW_HUMIDITY:
raw_humidity = bsec_outputs[index].signal;
break;
case BSEC_OUTPUT_GAS_PERCENTAGE:
gas_percentage = bsec_outputs[index].signal;
gas_percentage_accuracy = bsec_outputs[index].accuracy;
break;
default:
continue;
}
/* Assume that all the returned timestamps are the same */
timestamp = bsec_outputs[index].time_stamp;
}
}
return bsec_status;
}
#endif