Skip to content
Snippets Groups Projects
Commit dcc5c745 authored by schneider's avatar schneider
Browse files

feat(bsec): Add micropython access

parent f7dd7f3e
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -116,6 +116,7 @@ Q(sys_bme680)
Q(init)
Q(deinit)
Q(get_data)
Q(bsec_get_data)
/* file */
Q(__del__)
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment