Skip to content

Commit

Permalink
Trigger alerts based on settings values
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyrkan committed Apr 29, 2023
1 parent f2e0ee7 commit e97d278
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 92 deletions.
14 changes: 14 additions & 0 deletions include/K40/alerts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef K40_ALERTS_H
#define K40_ALERTS_H

enum AlertType {
ALERT_TYPE_VOLTAGE = 0x01,
ALERT_TYPE_COOLING = 0x02,
ALERT_TYPE_LIDS = 0x04,
ALERT_TYPE_FLAME_SENSOR = 0x08,
};

void alerts_toggle_alert(AlertType type, bool enable);
uint8_t alerts_get_current_alerts();

#endif
5 changes: 0 additions & 5 deletions include/K40/cooling.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
#define COOLING_THERMISTOR_VOLTAGE_DIVIDER_RESISTANCE 10000
#define COOLING_THERMISTOR_BUFFER_LENGTH 50

#define COOLING_TEMP_MIN_VALUE 5
#define COOLING_TEMP_MAX_VALUE 30
#define COOLING_FLOW_MIN_VALUE 1
#define COOLING_FLOW_MAX_VALUE 99

#define COOLING_FLOW_UPDATE_INTERVAL 500 // ms

enum CoolingPin {
Expand Down
8 changes: 0 additions & 8 deletions include/K40/voltage_probes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@

#define VOLTAGE_PROBES_UPDATE_INTERVAL 1000 // ms

#define VOLTAGE_PROBE_1_MIN_VALUE 4.0
#define VOLTAGE_PROBE_2_MIN_VALUE 11.0
#define VOLTAGE_PROBE_3_MIN_VALUE 24.0

#define VOLTAGE_PROBE_1_MAX_VALUE 5.5
#define VOLTAGE_PROBE_2_MAX_VALUE 12.5
#define VOLTAGE_PROBE_3_MAX_VALUE 25.5

enum VoltageProbePin {
PIN_VOLTAGE_PROBE_1 = 34,
PIN_VOLTAGE_PROBE_2 = 39,
Expand Down
12 changes: 0 additions & 12 deletions include/UI/alerts.h

This file was deleted.

9 changes: 9 additions & 0 deletions include/UI/overlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef UI_ALERTS_H
#define UI_ALERTS_H

#include <lvgl.h>

void ui_overlay_init();
void ui_overlay_update();

#endif
31 changes: 31 additions & 0 deletions src/K40/alerts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>

#include "K40/alerts.h"
#include "queues.h"

static uint8_t alerts_statuses = 0;
static SemaphoreHandle_t alerts_statuses_mutex = xSemaphoreCreateMutex();

void alerts_toggle_alert(AlertType type, bool enable) {
while (xSemaphoreTake(alerts_statuses_mutex, portMAX_DELAY) != pdPASS)
;

if (enable) {
alerts_statuses |= type;
} else {
alerts_statuses &= ~type;
}

xSemaphoreGive(alerts_statuses_mutex);
}

uint8_t alerts_get_current_alerts() {
while (xSemaphoreTake(alerts_statuses_mutex, portMAX_DELAY) != pdPASS)
;

uint8_t result = alerts_statuses;
xSemaphoreGive(alerts_statuses_mutex);

return result;
}
12 changes: 12 additions & 0 deletions src/K40/cooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include <math.h>
#include "esp_adc_cal.h"

#include "K40/alerts.h"
#include "K40/cooling.h"
#include "UI/screens/status.h"
#include "queues.h"
#include "settings.h"

static CoolingValues cooling_values;

Expand Down Expand Up @@ -71,6 +73,16 @@ void cooling_update_status(esp_adc_cal_characteristics_t *adc_chars) {

// Send update to the queue
if (cooling_values_updated) {
// Change alert state
bool enable_alert =
(cooling_values.temperature < probes_settings.cooling_temp_min ||
cooling_values.temperature > probes_settings.cooling_temp_max ||
cooling_values.flow < probes_settings.cooling_flow_min ||
cooling_values.flow > probes_settings.cooling_temp_max);

alerts_toggle_alert(ALERT_TYPE_COOLING, enable_alert);

// Notify UI of new values
xQueueOverwrite(cooling_current_status_queue, &cooling_values);
ui_status_notify_update(STATUS_UPDATE_PROBE_COOLING);
}
Expand Down
6 changes: 6 additions & 0 deletions src/K40/flame_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <freertos/queue.h>
#include "esp_adc_cal.h"

#include "K40/alerts.h"
#include "K40/flame_sensor.h"
#include "UI/screens/status.h"
#include "queues.h"
Expand All @@ -16,6 +17,11 @@ void flame_sensor_update_status() {
if (first_update || (updated_sensor_value != flame_sensor_triggered)) {
first_update = false;
flame_sensor_triggered = updated_sensor_value;

// Change alert state
alerts_toggle_alert(ALERT_TYPE_FLAME_SENSOR, flame_sensor_triggered);

// Notify UI of new value
xQueueOverwrite(flame_sensor_current_status_queue, &flame_sensor_triggered);
ui_status_notify_update(STATUS_UPDATE_PROBE_FLAME_SENSOR);
}
Expand Down
7 changes: 7 additions & 0 deletions src/K40/lids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>

#include "K40/alerts.h"
#include "K40/lids.h"
#include "UI/screens/status.h"
#include "queues.h"
Expand All @@ -17,6 +18,12 @@ void lids_update_status() {
first_update = false;
lids_states.front_opened = front_lid_opened;
lids_states.back_opened = back_lid_opened;

// Change alert state
bool enable_alert = front_lid_opened || back_lid_opened;
alerts_toggle_alert(ALERT_TYPE_LIDS, enable_alert);

// Notify UI of new values
xQueueOverwrite(lids_current_status_queue, &lids_states);
ui_status_notify_update(STATUS_UPDATE_PROBE_LIDS);
}
Expand Down
15 changes: 15 additions & 0 deletions src/K40/voltage_probes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include <math.h>
#include "esp_adc_cal.h"

#include "K40/alerts.h"
#include "K40/voltage_probes.h"
#include "UI/screens/status.h"
#include "queues.h"
#include "settings.h"

static VoltageProbesValues voltage_probes_values;

Expand Down Expand Up @@ -41,6 +43,19 @@ void voltage_probes_update_status(esp_adc_cal_characteristics_t *adc_chars) {
voltage_probes_values.probe2 = voltage_probes_get_value(PIN_VOLTAGE_PROBE_2, adc_chars);
voltage_probes_values.probe3 = voltage_probes_get_value(PIN_VOLTAGE_PROBE_3, adc_chars);
xQueueOverwrite(voltage_current_status_queue, &voltage_probes_values);

// Change alert state
bool enable_alert =
(voltage_probes_values.probe1 < probes_settings.voltage_probe_v1_min ||
voltage_probes_values.probe1 > probes_settings.voltage_probe_v1_max ||
voltage_probes_values.probe1 < probes_settings.voltage_probe_v2_min ||
voltage_probes_values.probe1 > probes_settings.voltage_probe_v2_max ||
voltage_probes_values.probe2 < probes_settings.voltage_probe_v3_min ||
voltage_probes_values.probe2 > probes_settings.voltage_probe_v3_max);

alerts_toggle_alert(ALERT_TYPE_VOLTAGE, enable_alert);

// Notify UI of new values
ui_status_notify_update(STATUS_UPDATE_PROBE_VOLTAGE);

// Reset timer
Expand Down
49 changes: 0 additions & 49 deletions src/UI/alerts.cpp

This file was deleted.

49 changes: 49 additions & 0 deletions src/UI/overlay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <Arduino.h>
#include <lvgl.h>

#include "UI/images.h"
#include "UI/overlay.h"
#include "UI/screens/controls.h"
#include "queues.h"

static lv_obj_t *ui_overlay_laser_enabled_label;

void ui_overlay_init() {
ui_overlay_laser_enabled_label = lv_label_create(lv_layer_top());
lv_obj_set_width(ui_overlay_laser_enabled_label, LV_SIZE_CONTENT);
lv_obj_set_height(ui_overlay_laser_enabled_label, LV_SIZE_CONTENT);
lv_obj_set_align(ui_overlay_laser_enabled_label, LV_ALIGN_BOTTOM_RIGHT);
lv_obj_set_x(ui_overlay_laser_enabled_label, -25);
lv_obj_set_y(ui_overlay_laser_enabled_label, -20);
lv_label_set_recolor(ui_overlay_laser_enabled_label, true);
lv_label_set_text(ui_overlay_laser_enabled_label, "#FF0000 " LV_SYMBOL_WARNING "# Laser enabled");
lv_obj_set_style_text_color(ui_overlay_laser_enabled_label, lv_color_black(), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_font(ui_overlay_laser_enabled_label, &font_default_14, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_radius(ui_overlay_laser_enabled_label, 6, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_bg_color(ui_overlay_laser_enabled_label, lv_color_white(), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_bg_opa(ui_overlay_laser_enabled_label, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_pad_left(ui_overlay_laser_enabled_label, 10, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_pad_right(ui_overlay_laser_enabled_label, 10, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_pad_top(ui_overlay_laser_enabled_label, 6, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_pad_bottom(ui_overlay_laser_enabled_label, 6, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_border_color(ui_overlay_laser_enabled_label, lv_color_black(), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_border_opa(ui_overlay_laser_enabled_label, 25, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_border_width(ui_overlay_laser_enabled_label, 1, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_shadow_color(ui_overlay_laser_enabled_label, lv_color_black(), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_shadow_opa(ui_overlay_laser_enabled_label, 15, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_shadow_width(ui_overlay_laser_enabled_label, 4, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_shadow_ofs_x(ui_overlay_laser_enabled_label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_shadow_ofs_y(ui_overlay_laser_enabled_label, 4, LV_PART_MAIN | LV_STATE_DEFAULT);
}

void ui_overlay_update() {
if (lv_obj_has_state(ui_controls_laser_switch, LV_STATE_CHECKED)) {
if (lv_obj_has_flag(ui_overlay_laser_enabled_label, LV_OBJ_FLAG_HIDDEN)) {
lv_obj_clear_flag(ui_overlay_laser_enabled_label, LV_OBJ_FLAG_HIDDEN);
}
} else {
if (!lv_obj_has_flag(ui_overlay_laser_enabled_label, LV_OBJ_FLAG_HIDDEN)) {
lv_obj_add_flag(ui_overlay_laser_enabled_label, LV_OBJ_FLAG_HIDDEN);
}
}
}
22 changes: 7 additions & 15 deletions src/UI/screens/status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <freertos/queue.h>
#include <freertos/event_groups.h>

#include "K40/alerts.h"
#include "K40/cooling.h"
#include "K40/lids.h"
#include "K40/voltage_probes.h"
Expand Down Expand Up @@ -313,6 +314,8 @@ void ui_status_update() {
bool pending_lids_update = (pending_updates & STATUS_UPDATE_PROBE_LIDS) != 0;
bool pending_flame_sensor_update = (pending_updates & STATUS_UPDATE_PROBE_FLAME_SENSOR) != 0;

uint8_t alerts_statuses = alerts_get_current_alerts();

// Update voltage probes widgets
if (pending_voltage_update && xQueuePeek(voltage_current_status_queue, &voltage_probes_values, 0) == pdTRUE) {
// Probe 1
Expand All @@ -330,14 +333,7 @@ void ui_status_update() {
lv_label_set_text(ui_status_voltages_v3_value, voltage_probe3_formatted_value);
lv_bar_set_value(ui_status_voltages_v3_bar, (int)voltage_probes_values.probe3, LV_ANIM_ON);

updateWarningIcon(
ui_status_voltages_icon_warning,
voltage_probes_values.probe1 < VOLTAGE_PROBE_1_MIN_VALUE ||
voltage_probes_values.probe1 > VOLTAGE_PROBE_1_MAX_VALUE ||
voltage_probes_values.probe2 < VOLTAGE_PROBE_2_MIN_VALUE ||
voltage_probes_values.probe2 > VOLTAGE_PROBE_2_MAX_VALUE ||
voltage_probes_values.probe3 < VOLTAGE_PROBE_3_MIN_VALUE ||
voltage_probes_values.probe3 > VOLTAGE_PROBE_3_MAX_VALUE);
updateWarningIcon(ui_status_voltages_icon_warning, (alerts_statuses & ALERT_TYPE_VOLTAGE) != 0);
}

// Update cooling widgets
Expand All @@ -352,25 +348,21 @@ void ui_status_update() {
lv_label_set_text(ui_status_cooling_temp_value, cooling_temp_formatted_value);
lv_bar_set_value(ui_status_cooling_temp_bar, (int)cooling_values.temperature, LV_ANIM_ON);

updateWarningIcon(
ui_status_cooling_icon_warning,
cooling_values.flow < COOLING_FLOW_MIN_VALUE || cooling_values.flow > COOLING_FLOW_MAX_VALUE ||
cooling_values.temperature < COOLING_TEMP_MIN_VALUE ||
cooling_values.temperature > COOLING_TEMP_MAX_VALUE);
updateWarningIcon(ui_status_cooling_icon_warning, (alerts_statuses & ALERT_TYPE_COOLING) != 0);
}

// Update lids widgets
if (pending_lids_update && xQueuePeek(lids_current_status_queue, &lids_states, 0) == pdTRUE) {
lv_label_set_text(ui_status_lid_front_value, lids_states.front_opened ? "Opened" : "Closed");
lv_label_set_text(ui_status_lid_back_value, lids_states.back_opened ? "Opened" : "Closed");
updateWarningIcon(ui_status_lid_icon_warning, lids_states.front_opened || lids_states.back_opened);
updateWarningIcon(ui_status_lid_icon_warning, (alerts_statuses & ALERT_TYPE_LIDS) != 0);
}

// Update flame sensor widgets
if (pending_flame_sensor_update &&
xQueuePeek(flame_sensor_current_status_queue, &flame_sensor_triggered, 0) == pdTRUE) {
lv_label_set_text(ui_status_fire_value, flame_sensor_triggered ? "Triggered" : "OK");
updateWarningIcon(ui_status_fire_icon_warning, flame_sensor_triggered);
updateWarningIcon(ui_status_fire_icon_warning, (alerts_statuses & ALERT_TYPE_FLAME_SENSOR) != 0);
}

// Clear pending updates bits
Expand Down
6 changes: 3 additions & 3 deletions src/UI/ui.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "UI/alerts.h"
#include "UI/menu.h"
#include "UI/overlay.h"
#include "UI/ui.h"
#include "UI/screens/bed.h"
#include "UI/screens/controls.h"
Expand All @@ -19,7 +19,7 @@ void ui_init() {
lv_disp_set_theme(default_display, theme);

// Initialize alerts overlay
ui_alerts_init();
ui_overlay_init();

// Initialize screens
ui_splashscreen_init();
Expand All @@ -33,7 +33,7 @@ void ui_init() {
}

void ui_update() {
ui_alerts_update();
ui_overlay_update();
ui_menu_indicators_update();
ui_status_update();
ui_controls_update();
Expand Down

0 comments on commit e97d278

Please sign in to comment.