From 48773132199636f1e43c0773393896b9f3148eed Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Wed, 29 Apr 2020 14:37:40 +0200
Subject: [PATCH] feat(bsec): Cleanup.

---
 epicardium/modules/bsec.c | 114 +++++++++++++++++---------------------
 1 file changed, 52 insertions(+), 62 deletions(-)

diff --git a/epicardium/modules/bsec.c b/epicardium/modules/bsec.c
index deb996e2..abda8a79 100644
--- a/epicardium/modules/bsec.c
+++ b/epicardium/modules/bsec.c
@@ -2,6 +2,7 @@
 
 #include "card10.h"
 #include "bosch.h"
+#include "bsec_integration.h"
 
 #include "epicardium.h"
 #include "modules.h"
@@ -20,6 +21,7 @@ TaskHandle_t bsec_task_id;
 static struct bme680_sensor_data last_bme680_data;
 static int64_t last_bme680_timestamp;
 static bool active;
+#define ULP 0
 
 // From generic_18v_3s_4d/bsec_serialized_configurations_iaq.c
 static const uint8_t bsec_config_generic_18v_3s_4d[454] = {
@@ -58,17 +60,6 @@ static const uint8_t bsec_config_generic_18v_3s_4d[454] = {
 	0,   0,   52,  233, 0,   0
 };
 
-/**********************************************************************************************************************/
-/* header files */
-/**********************************************************************************************************************/
-/* card10: ULP example specific: */
-/* BSEC configuration files are available in the config/ folder of the release package. Please chose a configuration file with 3s maximum time between `bsec_sensor_control()` calls */
-#include "bsec_integration.h"
-
-/**********************************************************************************************************************/
-/* functions */
-/**********************************************************************************************************************/
-
 /*!
  * @brief           Capture the system time in microseconds
  *
@@ -257,37 +248,34 @@ uint32_t config_load(uint8_t *config_buffer, uint32_t n_buffer)
 	return len;
 }
 
-#if 0
-/* card10: ULP example specific: */
-// Attach a button (or other) interrupt here to the ulp_plus_button_press() handler function to
-// enable this interrupt to trigger a ULP plus
-
-/*!
- * @brief           Interrupt handler for press of a ULP plus button
- *
- * @return          none
- */
-void ulp_plus_button_press()
+#if ULP
+void ulp_plus_trigger_iaq()
 {
-    /* We call bsec_update_subscription() in order to instruct BSEC to perform an extra measurement at the next
+	/* We call bsec_update_subscription() in order to instruct BSEC to perform an extra measurement at the next
      * possible time slot
      */
 
-    bsec_sensor_configuration_t requested_virtual_sensors[1];
-    uint8_t n_requested_virtual_sensors = 1;
-    bsec_sensor_configuration_t required_sensor_settings[BSEC_MAX_PHYSICAL_SENSOR];
-    uint8_t n_required_sensor_settings = BSEC_MAX_PHYSICAL_SENSOR;
-    bsec_library_return_t status = BSEC_OK;
-
-    /* To trigger a ULP plus, we request the IAQ virtual sensor with a specific sample rate code */
-    requested_virtual_sensors[0].sensor_id = BSEC_OUTPUT_IAQ;
-    requested_virtual_sensors[0].sample_rate = BSEC_SAMPLE_RATE_ULP_MEASUREMENT_ON_DEMAND;
-
-    /* Call bsec_update_subscription() to enable/disable the requested virtual sensors */
-    status = bsec_update_subscription(requested_virtual_sensors, n_requested_virtual_sensors, required_sensor_settings,
-         &n_required_sensor_settings);
+	bsec_sensor_configuration_t requested_virtual_sensors[1];
+	uint8_t n_requested_virtual_sensors = 1;
+	bsec_sensor_configuration_t
+		required_sensor_settings[BSEC_MAX_PHYSICAL_SENSOR];
+	uint8_t n_required_sensor_settings = BSEC_MAX_PHYSICAL_SENSOR;
+	bsec_library_return_t status       = BSEC_OK;
+
+	/* To trigger a ULP plus, we request the IAQ virtual sensor with a specific sample rate code */
+	requested_virtual_sensors[0].sensor_id = BSEC_OUTPUT_IAQ;
+	requested_virtual_sensors[0].sample_rate =
+		BSEC_SAMPLE_RATE_ULP_MEASUREMENT_ON_DEMAND;
+
+	/* Call bsec_update_subscription() to enable/disable the requested virtual sensors */
+	status = bsec_update_subscription(
+		requested_virtual_sensors,
+		n_requested_virtual_sensors,
+		required_sensor_settings,
+		&n_required_sensor_settings
+	);
 
-    /* The status code would tell is if the request was accepted. It will be rejected if the sensor is not already in
+	/* The status code would tell is if the request was accepted. It will be rejected if the sensor is not already in
      * ULP mode, or if the time difference between requests is too short, for example. */
 }
 #endif
@@ -319,21 +307,27 @@ void vBSECTask(void *pvParameters)
 	active       = true;
 	bsec_task_id = xTaskGetCurrentTaskHandle();
 
-	/* Call to the function which initializes the BSEC library */
-#if 0
-    /* Switch on ultra_low-power mode and provide no temperature offset */
-    ret = bsec_iot_init(BSEC_SAMPLE_RATE_ULP, 0.0f, card10_bosch_i2c_write, card10_bosch_i2c_read, card10_bosch_delay, state_load, config_load);
+#if ULP
+	float sample_rate = BSEC_SAMPLE_RATE_ULP;
+	/* State is saved every 100 samples, which means every 100 * 300 secs = 500 minutes  */
+	const int stat_save_interval = 100;
 #else
-	ret = bsec_iot_init(
-		BSEC_SAMPLE_RATE_LP,
-		0.0f,
-		i2c_write,
-		i2c_read,
-		delay,
-		state_load,
-		config_load
-	);
+	float sample_rate = BSEC_SAMPLE_RATE_LP;
+	/* State is saved every 10.000 samples, which means every 10.000 * 3 secs = 500 minutes  */
+	const int stat_save_interval = 10000;
 #endif
+
+	float temperature_offset = 0.0;
+	ret                      = bsec_iot_init(
+                sample_rate,
+                temperature_offset,
+                i2c_write,
+                i2c_read,
+                delay,
+                state_load,
+                config_load
+	);
+
 	if (ret.bme680_status) {
 		printf("bme680 init failed\n");
 		/* Could not intialize BME680 */
@@ -345,17 +339,13 @@ void vBSECTask(void *pvParameters)
 		while (1)
 			;
 	}
-	/* Call to endless loop function which reads and processes data based on sensor settings */
-#if 0
-    /* State is saved every 10.000 samples, which means every 100 * 300 secs = 500 minutes  */
-    bsec_iot_loop(sleep, get_timestamp_us, output_ready, state_save, 100);
-#else
-	/* State is saved every 10.000 samples, which means every 10.000 * 3 secs = 500 minutes  */
-	//bsec_iot_loop(card10_bosch_delay, get_timestamp_us, output_ready, state_save, 10000);
 
-	/* State is saved every 1200 samples, which means every 1200 * 3 secs = 60 minutes  */
-	bsec_iot_loop(delay, get_timestamp_us, output_ready, state_save, 1200);
-#endif
-	while (1)
-		;
+	/* Call to endless loop function which reads and processes data based on sensor settings */
+	bsec_iot_loop(
+		delay,
+		get_timestamp_us,
+		output_ready,
+		state_save,
+		stat_save_interval
+	);
 }
-- 
GitLab