diff --git a/pycardium/modules/py/bme680.py b/pycardium/modules/py/bme680.py index bbb6481de9bb0cf780fce35ca161cea8ccaf077b..b50b05624348a8f85460f510619fd265dbc4402a 100644 --- a/pycardium/modules/py/bme680.py +++ b/pycardium/modules/py/bme680.py @@ -8,6 +8,19 @@ Bme680Data = ucollections.namedtuple( "Bme680Data", ["temperature", "humidity", "pressure", "gas_resistance"] ) +BSECData = ucollections.namedtuple( + "BSECData", + [ + "temperature", + "humidity", + "pressure", + "gas_resistance", + "iaq", + "iaq_accuracy", + "eco2", + ], +) + class Bme680: """ @@ -58,6 +71,13 @@ class Bme680: - ``pressure``: Barometric pressure in *hPa* - ``gas_resistance``: Gas resistance in *Ω* + If the Bosch BSEC libary is active the following extra fields will + be returned: + + - ``iaq``: Indoor air quality indication + - ``iaq_accuracy``: Accuracy of indoor air quality + - ``eco2``: Equivalent CO2 content in *ppm* + **Example**: .. code-block:: python @@ -70,7 +90,11 @@ class Bme680: print("T: {}".format(data.temperature)) print("H: {}".format(data.humidity)) """ - return Bme680Data(*sys_bme680.get_data()) + + try: + return BSECData(*sys_bme680.bsec_get_data()) + except: + return Bme680Data(*sys_bme680.get_data()) def close(self): """ @@ -132,3 +156,53 @@ class Bme680: print(str(environment.gas_resistance())) """ return self.get_data().gas_resistance + + def iaq(self): + """ + Retrieve indoor air quality as defined by the Bosch BSEC libaray. + + BSEC needs to be enable in ``card10.cfg``. Otherwise this method + will raise an ``OSError`` exception. + + **Example**: + + .. code-block:: python + + environment = bme680.Bme680() + print(str(environment.iaq())) + """ + return BSECData(*sys_bme680.bsec_get_data()).iaq + + def iaq_accuracy(self): + """ + Retrieve indoor air quality accuracy as defined by the Bosch BSEC libaray. + + BSEC needs to be enable in ``card10.cfg``. Otherwise this method + will raise an ``OSError`` exception. + + **Example**: + + .. code-block:: python + + environment = bme680.Bme680() + print(str(environment.iaq_accuracy())) + """ + + return BSECData(*sys_bme680.bsec_get_data()).iaq_accuracy + + def eco2(self): + """ + Retrieve equivalant CO2 as defined by the Bosch BSEC libaray. + + BSEC needs to be enable in ``card10.cfg``. Otherwise this method + will raise an ``OSError`` exception. + + **Example**: + + .. code-block:: python + + environment = bme680.Bme680() + print(str(environment.eco2())) + """ + + return BSECData(*sys_bme680.bsec_get_data()).eco2 diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index f804089f3c66b8abf03e408b0b7deeedaf5228e0..2f144702f7cf929453306eb6e92a132c0394cb84 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -116,6 +116,7 @@ Q(sys_bme680) Q(init) Q(deinit) Q(get_data) +Q(bsec_get_data) /* file */ Q(__del__) diff --git a/pycardium/modules/sys_bme680.c b/pycardium/modules/sys_bme680.c index f6899c77c187c4d0d536fb41c14428d449bf62be..ef3abc9fa52816231ee7a40d068e3a57f95716d1 100644 --- a/pycardium/modules/sys_bme680.c +++ b/pycardium/modules/sys_bme680.c @@ -45,11 +45,34 @@ static mp_obj_t mp_bme680_get_data() } static MP_DEFINE_CONST_FUN_OBJ_0(bme680_get_data_obj, mp_bme680_get_data); +static mp_obj_t mp_bsec_get_data() +{ + struct bsec_sensor_data data; + int ret = epic_bsec_read_sensors(&data); + + if (ret < 0) { + mp_raise_OSError(-ret); + } + + mp_obj_t values_list[] = { + mp_obj_new_float(data.temperature), + mp_obj_new_float(data.humidity), + mp_obj_new_float(data.pressure), + mp_obj_new_float(data.gas_resistance), + mp_obj_new_int(data.indoor_air_quality), + mp_obj_new_int(data.accuracy), + mp_obj_new_float(data.co2_equivalent), + }; + return mp_obj_new_tuple(7, values_list); +} +static MP_DEFINE_CONST_FUN_OBJ_0(bsec_get_data_obj, mp_bsec_get_data); + static const mp_rom_map_elem_t bme680_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys_bme680) }, { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&bme680_init_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bme680_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_get_data), MP_ROM_PTR(&bme680_get_data_obj) }, + { MP_ROM_QSTR(MP_QSTR_bsec_get_data), MP_ROM_PTR(&bsec_get_data_obj) }, }; static MP_DEFINE_CONST_DICT(bme680_module_globals, bme680_module_globals_table);