From 77f1d1f69514d8669619f8a68072e17f74c2b3f1 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sun, 3 May 2020 01:47:55 +0200
Subject: [PATCH] feat(bsec): Add config options

---
 Documentation/card10-cfg.rst |  4 ++++
 epicardium/epicardium.h      | 13 ++++++++----
 epicardium/modules/bsec.c    | 38 +++++++++++++++++++++++-------------
 3 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/Documentation/card10-cfg.rst b/Documentation/card10-cfg.rst
index d37bedca..0c992b15 100644
--- a/Documentation/card10-cfg.rst
+++ b/Documentation/card10-cfg.rst
@@ -56,4 +56,8 @@ Option name        Type       Description
 ``long_press_ms``  Integer    Defines the timespan for a long key press in milliseconds.
 ------------------ ---------- -----------
 ``retrigger_ms``   Integer    Defines the timespan for repeating key presses when a key is hold in milliseconds.
+------------------ ---------- -----------
+``bsec_enable``    Boolean    Activate the Bosch :ref:`BSEC` binary blob to compute an Indoor Air Quality indication.
+------------------ ---------- -----------
+``bsec_debug``     Boolean    Turn on debug output of the BSEC system. Prints each meaurement on the console.
 ================== ========== ===========
diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h
index d8161824..5a980443 100644
--- a/epicardium/epicardium.h
+++ b/epicardium/epicardium.h
@@ -920,7 +920,7 @@ API(API_BME680_GET_DATA, int epic_bme680_read_sensors(
 ));
 
 /**
- * BME680 Sensor Data
+ * BSEC Sensor Data
  */
 struct bsec_sensor_data {
 	/** Compensated temperature in degree celsius */
@@ -995,17 +995,22 @@ struct bsec_sensor_data {
 	int32_t static_indoor_air_quality;
 	/** Estimation of equivalant CO2 content in the air in ppm. */
 	float co2_equivalent;
-	/** Estimation of equivalant brath VOC content in the air in ppm. */
+	/** Estimation of equivalant breath VOC content in the air in ppm. */
 	float breath_voc_equivalent;
 };
 
 /**
+ *
+ * .. _BSEC:
  * Get the current BME680 data filtered by Bosch BSEC library
  *
  * The Bosch BSEC libary allows to compute an indoor air
  * qualtiy (IAQ)metric as well as CO2 and VOC content
  * equivalents using the gas sensor of the BME680.
  *
+ * As it is a proprietary binary blob, it has to be enabled using
+ * the ``bsec_enabled`` configuration option (see <link to config>).
+ *
  * The sample rate is currently fixed to one sample every 3 seconds.
  * Querying the sensor more often will return cached data.
  *
@@ -1019,10 +1024,10 @@ struct bsec_sensor_data {
  * status of the sensor. Please take it into consideration when
  * using / displaying the IAQ.
  *
- * Please refer to the description of ``bsec_sensor_data`` for more
+ * Please refer to the description of :c:type:`bsec_sensor_data` for more
  * information about how to interpret its content.
  *
- * .. versionadded:: 1.
+ * .. versionadded:: 1.x
  *
  * :param data: Where to store the environmental data.
  * :return: 0 on success or ``-Exxx`` on error.  The following
diff --git a/epicardium/modules/bsec.c b/epicardium/modules/bsec.c
index e257708a..3230dbb7 100644
--- a/epicardium/modules/bsec.c
+++ b/epicardium/modules/bsec.c
@@ -6,6 +6,7 @@
 
 #include "epicardium.h"
 #include "modules.h"
+#include "config.h"
 #include "modules/log.h"
 
 #include "FreeRTOS.h"
@@ -20,6 +21,7 @@
 TaskHandle_t bsec_task_id;
 static int64_t last_bme680_timestamp;
 static bool bsec_task_active;
+static bool debug;
 static struct bsec_sensor_data last_bsec_data;
 #define ULP 0
 
@@ -116,20 +118,22 @@ void output_ready(
 	__sync_synchronize();
 
 	last_bme680_timestamp           = timestamp;
-	return;
-	printf("bosch data time: %u, iaq: %u, iaq_a: %u, temp10: %u, hum10: %u, pres: %u, raw_temp10: %u, raw_hum10: %u, gas: %u, static_iaq: %u, co21e3: %u, breath_voc1e3: %u\n",
-	       (unsigned int)(timestamp / 1e6),
-	       (unsigned int)(iaq),
-	       (unsigned int)(iaq_accuracy),
-	       (unsigned int)(temperature * 10),
-	       (unsigned int)(humidity * 10),
-	       (unsigned int)(pressure),
-	       (unsigned int)(raw_temperature * 10),
-	       (unsigned int)(raw_humidity * 10),
-	       (unsigned int)(gas),
-	       (unsigned int)(static_iaq),
-	       (unsigned int)(co2_equivalent * 1e3),
-	       (unsigned int)(breath_voc_equivalent * 1e3));
+
+	if(debug) {
+		printf("BSEC time[ms]: %u, IAQ: %u, IAQ ACC[0-3]: %u, T[.1C]: %u, Hum[.1%%]: %u, P[Pa]: %u, Raw T[.1C]: %u, Raw Hum[.1%%]: %u, Gas[Ohm]: %u, Static IAQ: %u, CO2[ppm]: %u, Breath VOC[ppb]: %u\n",
+			(unsigned int)(timestamp / 1e6),
+			(unsigned int)(iaq),
+			(unsigned int)(iaq_accuracy),
+			(unsigned int)(temperature * 10),
+			(unsigned int)(humidity * 10),
+			(unsigned int)(pressure),
+			(unsigned int)(raw_temperature * 10),
+			(unsigned int)(raw_humidity * 10),
+			(unsigned int)(gas),
+			(unsigned int)(static_iaq),
+			(unsigned int)(co2_equivalent),
+			(unsigned int)(breath_voc_equivalent * 1e3));
+	}
 }
 
 int epic_bsec_read_sensors(struct bsec_sensor_data *data)
@@ -361,6 +365,12 @@ int bsec_activate(void)
 
 	float temperature_offset = 0.0;
 
+	bool bsec_enabled = config_get_boolean_with_default("bsec_enabled", false);
+	if(!bsec_enabled) {
+		return -1;
+	}
+
+	debug = config_get_boolean_with_default("bsec_debug", false);
 	/* Puts AT LEAST 2 * #BSEC_MAX_PROPERTY_BLOB_SIZE = 2 * 454 = 908 bytes onto the stack */
 	ret = bsec_iot_init(
 		sample_rate,
-- 
GitLab