Skip to content
Snippets Groups Projects
Commit 1b7a655c authored by chris007's avatar chris007
Browse files

[#33] Taking settings from Democode

parent be8fb28a
Branches
No related tags found
No related merge requests found
Pipeline #2588 failed
``bme680`` - 4-in-1 Sensor
==========================
.. py:function:: bme680.get_bme_data()
Does a single measurement to get environmental data.
:return: Tuple containing ``temperature`` (°C), ``humidity`` (% r.h.),
``pressure`` (hPa) and ``gas resistance`` (Ohm).
...@@ -675,10 +675,10 @@ API(API_LEDS_CLEAR_ALL, void epic_leds_clear_all(uint8_t r, uint8_t g, uint8_t b ...@@ -675,10 +675,10 @@ API(API_LEDS_CLEAR_ALL, void epic_leds_clear_all(uint8_t r, uint8_t g, uint8_t b
struct bme_sensor_data { struct bme_sensor_data {
/*! Temperature in degree celsius */ /*! Temperature in degree celsius */
float temperature; float temperature;
/*! Humidity in % relative humidity */
float humidity;
/*! Pressure in Pascal */ /*! Pressure in Pascal */
float pressure; float pressure;
/*! Humidity in % relative humidity x1000 */
float humidity;
/*! Gas resistance in Ohms */ /*! Gas resistance in Ohms */
float gas_resistance; float gas_resistance;
}; };
......
...@@ -8,10 +8,8 @@ ...@@ -8,10 +8,8 @@
#include "epicardium.h" #include "epicardium.h"
#define HEATR_DUR 2000 #define HEATR_TEMP 320
#define N_MEAS 6 #define HEATR_DUR 150
#define LOW_TEMP 150
#define HIGH_TEMP 350
static bool initialized; static bool initialized;
static struct bme680_dev bme; static struct bme680_dev bme;
...@@ -38,14 +36,17 @@ int epic_bme_get_data(struct bme_sensor_data *data) ...@@ -38,14 +36,17 @@ int epic_bme_get_data(struct bme_sensor_data *data)
uint16_t settings_sel; uint16_t settings_sel;
if (__builtin_expect(!initialized, 0)) { if (__builtin_expect(!initialized, 0)) {
/*
bma.intf_ptr = NULL; bme.dev_id = BME680_I2C_ADDR_PRIMARY;
bma.delay_ms = card10_bosch_delay; bme.intf = BME680_I2C_INTF;
bma.dev_id = BMA400_I2C_ADDRESS_SDO_LOW; bme.read = card10_bosch_i2c_read;
bma.read = card10_bosch_i2c_read_ex; bme.write = card10_bosch_i2c_write;
bma.write = card10_bosch_i2c_write_ex; bme.delay_ms = card10_bosch_delay;
bma.intf = BMA400_I2C_INTF; /* amb_temp can be set to 25 prior to configuring the gas sensor
* or by performing a few temperature readings without operating the gas sensor.
*/ */
bme.amb_temp = 25;
result = bme680_init(&bme); result = bme680_init(&bme);
if (result != BME680_OK) { if (result != BME680_OK) {
printf("bme680_init error: %d\n", result); printf("bme680_init error: %d\n", result);
...@@ -56,40 +57,37 @@ int epic_bme_get_data(struct bme_sensor_data *data) ...@@ -56,40 +57,37 @@ int epic_bme_get_data(struct bme_sensor_data *data)
/* Must be set before writing the sensor configuration */ /* Must be set before writing the sensor configuration */
bme.power_mode = BME680_FORCED_MODE; bme.power_mode = BME680_FORCED_MODE;
/* Set the temperature, pressure and humidity & filter settings */ /* Set the temperature, pressure and humidity settings */
bme.tph_sett.os_hum = BME680_OS_1X; bme.tph_sett.os_hum = BME680_OS_2X;
bme.tph_sett.os_pres = BME680_OS_16X; bme.tph_sett.os_pres = BME680_OS_4X;
bme.tph_sett.os_temp = BME680_OS_2X; bme.tph_sett.os_temp = BME680_OS_8X;
bme.tph_sett.filter = BME680_FILTER_SIZE_3;
/* Set the remaining gas sensor settings and link the heating profile */ /* Set the remaining gas sensor settings and link the heating profile */
bme.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; bme.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
bme.gas_sett.heatr_dur = HEATR_DUR; /* Create a ramp heat waveform in 3 steps */
bme.gas_sett.heatr_temp = HEATR_TEMP; /* degree Celsius */
bme.gas_sett.heatr_dur = HEATR_DUR; /* milliseconds */
/* Set the required sensor settings needed */
settings_sel = BME680_OST_SEL | BME680_OSP_SEL |
BME680_OSH_SEL | BME680_FILTER_SEL |
BME680_GAS_SENSOR_SEL;
settings_sel = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL; result = bme680_set_sensor_settings(settings_sel, &bme);
if (result != BME680_OK) {
printf("bme680_set_sensor_settings error: %d\n", result);
return -convert_error(result);
}
initialized = true; initialized = true;
} }
struct bme680_field_data raw_data[N_MEAS]; struct bme680_field_data raw_data;
uint16_t profile_dur = 0; uint16_t profile_dur = 0;
bme680_get_profile_dur(&profile_dur, &bme); bme680_get_profile_dur(&profile_dur, &bme);
uint8_t i = 0;
while ((result == BME680_OK) && (i < N_MEAS)) {
if (result == BME680_OK) {
if (i % 2 == 0)
bme.gas_sett.heatr_temp = HIGH_TEMP; /* Higher temperature */
else
bme.gas_sett.heatr_temp = LOW_TEMP; /* Lower temperature */
result = bme680_set_sensor_settings(settings_sel, &bme);
if (result != BME680_OK) {
printf("bme680_set_sensor_settings error: %d\n", result);
return -convert_error(result);
}
if (result == BME680_OK) { if (result == BME680_OK) {
result = bme680_set_sensor_mode(&bme); /* Trigger a measurement */ result = bme680_set_sensor_mode(&bme); /* Trigger a measurement */
...@@ -100,21 +98,17 @@ int epic_bme_get_data(struct bme_sensor_data *data) ...@@ -100,21 +98,17 @@ int epic_bme_get_data(struct bme_sensor_data *data)
bme.delay_ms(profile_dur); /* Wait for the measurement to complete */ bme.delay_ms(profile_dur); /* Wait for the measurement to complete */
result = bme680_get_sensor_data(&raw_data[i], &bme); result = bme680_get_sensor_data(&raw_data, &bme);
if (result != BME680_OK) { if (result != BME680_OK) {
printf("bme680_get_sensor_data error: %d\n", result); printf("bme680_get_sensor_data error: %d\n", result);
return -convert_error(result); return -convert_error(result);
} }
} }
}
i++;
}
data->temperature = raw_data[0].temperature; data->temperature = raw_data.temperature / 100.0l;
data->humidity = raw_data[0].humidity; data->humidity = raw_data.humidity / 1000.0l;
data->pressure = raw_data[0].pressure; data->pressure = raw_data.pressure / 100.0l;
data->gas_resistance = raw_data[0].gas_resistance; data->gas_resistance = raw_data.gas_resistance;
return 0; return 0;
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "epicardium.h" #include "epicardium.h"
static mp_obj_t mp_bme_get_data() static mp_obj_t mp_get_bme_data()
{ {
struct bme_sensor_data data; struct bme_sensor_data data;
int ret = epic_bme_get_data(&data); int ret = epic_bme_get_data(&data);
...@@ -17,10 +17,11 @@ static mp_obj_t mp_bme_get_data() ...@@ -17,10 +17,11 @@ static mp_obj_t mp_bme_get_data()
mp_obj_new_float(data.temperature), mp_obj_new_float(data.temperature),
mp_obj_new_float(data.humidity), mp_obj_new_float(data.humidity),
mp_obj_new_float(data.pressure), mp_obj_new_float(data.pressure),
mp_obj_new_float(data.gas_resistance)
}; };
return mp_obj_new_tuple(3, values_list); return mp_obj_new_tuple(4, values_list);
} }
static MP_DEFINE_CONST_FUN_OBJ_0(bme_get_bme_data_obj, mp_bme_get_data); static MP_DEFINE_CONST_FUN_OBJ_0(bme_get_bme_data_obj, mp_get_bme_data);
static const mp_rom_map_elem_t bme_module_globals_table[] = { static const mp_rom_map_elem_t bme_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) },
...@@ -33,5 +34,6 @@ const mp_obj_module_t bme_module = { ...@@ -33,5 +34,6 @@ const mp_obj_module_t bme_module = {
.globals = (mp_obj_dict_t *)&bme_module_globals, .globals = (mp_obj_dict_t *)&bme_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_BME_ENABLED); MP_REGISTER_MODULE(MP_QSTR_bme680, bme_module, MODULE_BME680_ENABLED);
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment