|
|
|
@ -23,6 +23,7 @@ static const char *TAG = "voc"; |
|
|
|
|
#include "bme68x_defs.h" |
|
|
|
|
#include "bsec2.h" |
|
|
|
|
#include "periph_init.h" |
|
|
|
|
#include "settings.h" |
|
|
|
|
|
|
|
|
|
struct sensor_itf { |
|
|
|
|
uint8_t dev_addr; |
|
|
|
@ -30,7 +31,7 @@ struct sensor_itf { |
|
|
|
|
|
|
|
|
|
static struct sensor_itf gas_sensor_intf = {}; |
|
|
|
|
static struct bme68x_dev gas_sensor = {}; |
|
|
|
|
static struct bsec2 gas_sensor_bsec = {}; |
|
|
|
|
static struct bsec2 hBsec = {}; |
|
|
|
|
|
|
|
|
|
void bsec2_errormsg(const char *msg) { |
|
|
|
|
ESP_LOGE("BSEC", "%s", msg); |
|
|
|
@ -103,7 +104,7 @@ static esp_err_t voc_init(void) { |
|
|
|
|
int8_t rslt; |
|
|
|
|
gas_sensor_intf.dev_addr = BME68X_I2C_ADDR_LOW; |
|
|
|
|
gas_sensor.intf_ptr = &gas_sensor_intf; |
|
|
|
|
gas_sensor.amb_temp = 25; // TODO set this from senseair!
|
|
|
|
|
gas_sensor.amb_temp = 25; |
|
|
|
|
gas_sensor.intf = BME68X_I2C_INTF; |
|
|
|
|
gas_sensor.read = user_i2c_read; |
|
|
|
|
gas_sensor.write = user_i2c_write; |
|
|
|
@ -243,7 +244,7 @@ void voc_read_task(void *param) { |
|
|
|
|
goto abort; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int rv = bsec2_init(&gas_sensor_bsec, &gas_sensor); |
|
|
|
|
int rv = bsec2_init(&hBsec, &gas_sensor); |
|
|
|
|
if (rv != 0) { |
|
|
|
|
ESP_LOGE(TAG, "Error in bsec init: %d", rv); |
|
|
|
|
goto abort; |
|
|
|
@ -267,7 +268,7 @@ void voc_read_task(void *param) { |
|
|
|
|
BSEC_OUTPUT_RUN_IN_STATUS, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
rv = bsec2_updateSubscription(&gas_sensor_bsec, |
|
|
|
|
rv = bsec2_updateSubscription(&hBsec, |
|
|
|
|
sensorList, |
|
|
|
|
sizeof(sensorList) / sizeof(bsecSensor), |
|
|
|
|
BSEC_SAMPLE_RATE_LP); |
|
|
|
@ -277,21 +278,35 @@ void voc_read_task(void *param) { |
|
|
|
|
goto abort; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bsec2_attachCallback(&gas_sensor_bsec, new_data_callback); |
|
|
|
|
bsec2_attachCallback(&hBsec, new_data_callback); |
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "BSEC library version %d.%d.%d.%d", |
|
|
|
|
gas_sensor_bsec.version.major, |
|
|
|
|
gas_sensor_bsec.version.minor, |
|
|
|
|
gas_sensor_bsec.version.major_bugfix, |
|
|
|
|
gas_sensor_bsec.version.minor_bugfix); |
|
|
|
|
hBsec.version.major, |
|
|
|
|
hBsec.version.minor, |
|
|
|
|
hBsec.version.major_bugfix, |
|
|
|
|
hBsec.version.minor_bugfix); |
|
|
|
|
|
|
|
|
|
// TODO add bsec state persistence to NVS at some landmark intervals, e.g. 1 day
|
|
|
|
|
// TODO periodic updating of sensor ambient temp
|
|
|
|
|
const uint32_t state_persist_time_ticks = 12 * 3600 * 1000; |
|
|
|
|
_Static_assert(configTICK_RATE_HZ == 1000, "1kHz tick"); |
|
|
|
|
uint32_t last_state_persist = xTaskGetTickCount(); |
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Restore BSEC state"); |
|
|
|
|
bsec2_setState(&hBsec, g_Settings.bsec_state); |
|
|
|
|
|
|
|
|
|
while (1) { |
|
|
|
|
rv = bsec2_run(&gas_sensor_bsec); |
|
|
|
|
rv = bsec2_run(&hBsec); |
|
|
|
|
if (false == rv) { |
|
|
|
|
ESP_LOGE(TAG, "Error in bsec run!"); |
|
|
|
|
} else { |
|
|
|
|
uint32_t tickNow = xTaskGetTickCount(); |
|
|
|
|
uint32_t elapsed = tickNow - last_state_persist; |
|
|
|
|
if (elapsed > state_persist_time_ticks) { |
|
|
|
|
ESP_LOGI(TAG, "Read & persist BSEC state"); |
|
|
|
|
last_state_persist = tickNow; |
|
|
|
|
if (bsec2_getState(&hBsec, g_Settings.bsec_state)) { |
|
|
|
|
settings_persist(SETTINGS_bsec_state); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100)); |
|
|
|
|
} |
|
|
|
|