diff options
Diffstat (limited to 'control/sensors.c')
-rw-r--r-- | control/sensors.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/control/sensors.c b/control/sensors.c new file mode 100644 index 0000000..0e7d9f8 --- /dev/null +++ b/control/sensors.c @@ -0,0 +1,68 @@ +#include "sensors.h" +#include <bcl.h> + +#define SERVICE_INTERVAL_INTERVAL (60 * 60 * 1000) +#define BATTERY_UPDATE_INTERVAL (60 * 60 * 1000) + +#define TEMPERATURE_TAG_PUB_NO_CHANGE_INTEVAL (15 * 60 * 1000) +#define TEMPERATURE_TAG_PUB_VALUE_CHANGE 0.2f +#define TEMPERATURE_UPDATE_SERVICE_INTERVAL (5 * 1000) +#define TEMPERATURE_UPDATE_NORMAL_INTERVAL (10 * 1000) + +#define APPLICATION_TASK_ID 0 + + +typedef struct { + uint8_t number; + float value; + bc_tick_t next_pub; +} event_param_t; + +// Thermometer instance +bc_tmp112_t tmp112; +event_param_t temperature_event_param = { .next_pub = 0, .value = NAN }; + +void tmp112_event_handler(bc_tmp112_t *self, bc_tmp112_event_t event, void *event_param) { + float value; + event_param_t *param = (event_param_t *)event_param; + + if (event != BC_TMP112_EVENT_UPDATE) + return; + + if (bc_tmp112_get_temperature_celsius(self, &value)) { + if ((fabsf(value - param->value) >= TEMPERATURE_TAG_PUB_VALUE_CHANGE) || (param->next_pub < bc_scheduler_get_spin_tick())) { + bc_radio_pub_temperature(BC_RADIO_PUB_CHANNEL_R1_I2C0_ADDRESS_ALTERNATE, &value); + param->value = value; + param->next_pub = bc_scheduler_get_spin_tick() + TEMPERATURE_TAG_PUB_NO_CHANGE_INTEVAL; + } + } else + param->value = NAN; +} + +void switch_to_normal_mode_task(void *param) { + bc_tmp112_set_update_interval(&tmp112, TEMPERATURE_UPDATE_NORMAL_INTERVAL); + bc_scheduler_unregister(bc_scheduler_get_current_task_id()); +} + +void init_temperature() { + bc_tmp112_init(&tmp112, BC_I2C_I2C0, 0x49); + bc_tmp112_set_event_handler(&tmp112, tmp112_event_handler, &temperature_event_param); + bc_tmp112_set_update_interval(&tmp112, TEMPERATURE_UPDATE_SERVICE_INTERVAL); + + bc_scheduler_register(switch_to_normal_mode_task, NULL, SERVICE_INTERVAL_INTERVAL); +} + + +void battery_event_handler(bc_module_battery_event_t event, void *event_param) { + if (event != BC_MODULE_BATTERY_EVENT_UPDATE) + return; + float voltage; + if (bc_module_battery_get_voltage(&voltage)) + bc_radio_pub_battery(&voltage); +} + +void init_battery() { + bc_module_battery_init(); + bc_module_battery_set_event_handler(battery_event_handler, NULL); + bc_module_battery_set_update_interval(BATTERY_UPDATE_INTERVAL); +} |