Skip to content
Snippets Groups Projects
Commit 04e56e1a authored by chris007's avatar chris007
Browse files

Split init and get_data in separate methods

parent d2c90d04
No related branches found
No related tags found
No related merge requests found
Pipeline #2616 failed
``bme680`` - 4-in-1 Sensor ``bme680`` - 4-in-1 Sensor
========================== ==========================
.. py:function:: bme680.get_bme_data() .. py:function:: bme680.init()
Before being able to read data from the sensor, you have to call init().
.. py:function:: bme680.get_data()
Does a single measurement to get environmental data. Does a single measurement to get environmental data.
......
...@@ -106,7 +106,8 @@ typedef _Bool bool; ...@@ -106,7 +106,8 @@ typedef _Bool bool;
#define API_PERSONAL_STATE_GET 0xc1 #define API_PERSONAL_STATE_GET 0xc1
#define API_PERSONAL_STATE_IS_PERSISTENT 0xc2 #define API_PERSONAL_STATE_IS_PERSISTENT 0xc2
#define API_BME680_GET_DATA 0xD0 #define API_BME680_INIT 0xD0
#define API_BME680_GET_DATA 0xD1
/* clang-format on */ /* clang-format on */
...@@ -683,6 +684,20 @@ struct bme680_sensor_data { ...@@ -683,6 +684,20 @@ struct bme680_sensor_data {
float gas_resistance; float gas_resistance;
}; };
/**
* Initialize the BM680 sensor.
*
* :param data: Where to store the environmental data.
* :return: 0 on success or ``-Exxx`` on error. The following
* errors might occur:
*
* - ``-EFAULT``: On NULL-pointer.
* - ``-EINVAL``: Invalid configuration.
* - ``-EIO``: Communication with the device failed.
* - ``-ENODEV``: Device was not found.
*/
API(API_BME680_INIT, int epic_bme680_init());
/** /**
* Get the current BME680 data. * Get the current BME680 data.
* *
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
static bool initialized; static bool initialized;
static struct bme680_dev bme; static struct bme680_dev bme;
uint16_t settings_sel;
static int convert_error(int8_t error) static int convert_error(int8_t error)
{ {
...@@ -32,10 +33,8 @@ static int convert_error(int8_t error) ...@@ -32,10 +33,8 @@ static int convert_error(int8_t error)
} }
} }
int epic_bme680_read_sensors(struct bme680_sensor_data *data) int epic_bme680_init() {
{
int8_t result = BME680_OK; int8_t result = BME680_OK;
uint16_t settings_sel;
if (__builtin_expect(!initialized, 0)) { if (__builtin_expect(!initialized, 0)) {
...@@ -84,7 +83,17 @@ int epic_bme680_read_sensors(struct bme680_sensor_data *data) ...@@ -84,7 +83,17 @@ int epic_bme680_read_sensors(struct bme680_sensor_data *data)
initialized = true; initialized = true;
} }
return 0;
}
int epic_bme680_read_sensors(struct bme680_sensor_data *data)
{
if(!initialized) {
LOG_ERR("bme680", "bme680 sensor not initialized");
return -1;
}
int8_t result = BME680_OK;
struct bme680_field_data raw_data; struct bme680_field_data raw_data;
uint16_t profile_dur = 0; uint16_t profile_dur = 0;
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "epicardium.h" #include "epicardium.h"
static mp_obj_t mp_get_bme_data() static mp_obj_t mp_bme680_get_data()
{ {
struct bme680_sensor_data data; struct bme680_sensor_data data;
int ret = epic_bme680_read_sensors(&data); int ret = epic_bme680_read_sensors(&data);
...@@ -21,19 +21,32 @@ static mp_obj_t mp_get_bme_data() ...@@ -21,19 +21,32 @@ static mp_obj_t mp_get_bme_data()
}; };
return mp_obj_new_tuple(4, values_list); return mp_obj_new_tuple(4, values_list);
} }
static MP_DEFINE_CONST_FUN_OBJ_0(bme_get_bme_data_obj, mp_get_bme_data); static MP_DEFINE_CONST_FUN_OBJ_0(bme680_get_data_obj, mp_bme680_get_data);
static const mp_rom_map_elem_t bme_module_globals_table[] = { static mp_obj_t mp_bme680_init()
{
int ret = epic_bme680_init();
if (ret < 0) {
mp_raise_OSError(-ret);
}
return 0;
}
static MP_DEFINE_CONST_FUN_OBJ_0(bme680_init_obj, mp_bme680_init);
static const mp_rom_map_elem_t bme680_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bme680) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bme680) },
{ MP_ROM_QSTR(MP_QSTR_get_bme_data), MP_ROM_PTR(&bme_get_bme_data_obj) }, { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&bme680_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_data), MP_ROM_PTR(&bme680_get_data_obj) },
}; };
static MP_DEFINE_CONST_DICT(bme_module_globals, bme_module_globals_table); static MP_DEFINE_CONST_DICT(bme680_module_globals, bme680_module_globals_table);
const mp_obj_module_t bme_module = { const mp_obj_module_t bme680_module = {
.base = { &mp_type_module }, .base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&bme_module_globals, .globals = (mp_obj_dict_t *)&bme680_module_globals,
}; };
/* Register the module to make it available in Python */ /* Register the module to make it available in Python */
MP_REGISTER_MODULE(MP_QSTR_bme680, bme_module, MODULE_BME680_ENABLED); MP_REGISTER_MODULE(MP_QSTR_bme680, bme680_module, MODULE_BME680_ENABLED);
...@@ -76,7 +76,8 @@ Q(stop) ...@@ -76,7 +76,8 @@ Q(stop)
/* bme680 */ /* bme680 */
Q(bme680) Q(bme680)
Q(get_bme_data) Q(init)
Q(get_data)
/* file */ /* file */
Q(__del__) Q(__del__)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment