aboutsummaryrefslogtreecommitdiff
path: root/control/application.c
diff options
context:
space:
mode:
Diffstat (limited to 'control/application.c')
-rw-r--r--control/application.c96
1 files changed, 11 insertions, 85 deletions
diff --git a/control/application.c b/control/application.c
index c304a84..06d6ada 100644
--- a/control/application.c
+++ b/control/application.c
@@ -3,61 +3,21 @@
#include <bc_spi.h>
#include <bc_dice.h>
#include "ledctl.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
-
-#define COLOR_BLACK true
-
-
-typedef struct {
- uint8_t number;
- float value;
- bc_tick_t next_pub;
-} event_param_t;
+#include "sensors.h"
+#include "gui.h"
bc_led_t led;
bc_led_t led_lcd_red;
bc_led_t led_lcd_blue;
-// Thermometer instance
-bc_tmp112_t tmp112;
-event_param_t temperature_event_param = { .next_pub = 0, .value = NAN };
-float temperature_on_display = 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;
-
- if ((fabsf(param->value - temperature_on_display) >= 0.1) || isnan(temperature_on_display))
- bc_scheduler_plan_now(APPLICATION_TASK_ID);
-}
-
void lcd_button_left_event_handler(bc_button_t *self, bc_button_event_t event, void *event_param) {
if (event == BC_BUTTON_EVENT_CLICK) {
click_left();
+ update_gui();
bc_led_pulse(&led_lcd_blue, 30);
} else if (event == BC_BUTTON_EVENT_HOLD) {
hold_left();
+ update_gui();
bc_led_pulse(&led_lcd_blue, 500);
}
}
@@ -65,27 +25,18 @@ void lcd_button_left_event_handler(bc_button_t *self, bc_button_event_t event, v
void lcd_button_right_event_handler(bc_button_t *self, bc_button_event_t event, void *event_param) {
if (event == BC_BUTTON_EVENT_CLICK) {
click_right();
+ update_gui();
bc_led_pulse(&led_lcd_red, 30);
} else if (event == BC_BUTTON_EVENT_HOLD) {
hold_right();
+ update_gui();
bc_led_pulse(&led_lcd_red, 500);
}
}
-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 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 application_init(void) {
+ bc_log_init(BC_LOG_LEVEL_DEBUG, BC_LOG_TIMESTAMP_ABS);
+
// Initialize LED on core module
bc_led_init(&led, BC_GPIO_LED, false, false);
bc_led_set_mode(&led, BC_LED_MODE_OFF);
@@ -94,14 +45,10 @@ void application_init(void) {
bc_radio_init(BC_RADIO_MODE_NODE_SLEEPING);
// Initialize battery
- bc_module_battery_init();
- bc_module_battery_set_event_handler(battery_event_handler, NULL);
- bc_module_battery_set_update_interval(BATTERY_UPDATE_INTERVAL);
+ init_battery();
// Initialize thermometer sensor on core module
- 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);
+ init_temperature();
// Initialize LCD
bc_module_lcd_init();
@@ -122,28 +69,7 @@ void application_init(void) {
bc_radio_pairing_request("lcd-thermostat", VERSION);
- bc_scheduler_register(switch_to_normal_mode_task, NULL, SERVICE_INTERVAL_INTERVAL);
-
bc_led_pulse(&led, 2000);
-}
-
-void application_task(void) {
- static char str_temperature[10];
- if (!bc_module_lcd_is_ready())
- return;
-
- bc_system_pll_enable();
- bc_module_lcd_clear();
-
- bc_module_lcd_set_font(&bc_font_ubuntu_33);
- snprintf(str_temperature, sizeof(str_temperature), "%.1f ", temperature_event_param.value);
- int x = bc_module_lcd_draw_string(20, 20, str_temperature, COLOR_BLACK);
- temperature_on_display = temperature_event_param.value;
-
- bc_module_lcd_set_font(&bc_font_ubuntu_24);
- bc_module_lcd_draw_string(x - 20, 25, "\xb0" "C ", COLOR_BLACK);
-
- bc_module_lcd_update();
- bc_system_pll_disable();
+ update_gui();
}