pse-firmware/Core/Src/home_screen.c
2023-08-09 14:58:55 +02:00

131 lines
4.0 KiB
C

/*
* home_screen.c
*
* Created on: Aug 7, 2023
* Author: leo
*/
#include <stdint.h>
#include <stdio.h>
#include "lvgl.h"
#include "PSE_unit.h"
static void run_handler(lv_event_t * e){
lv_event_code_t code = lv_event_get_code(e);
if(code == LV_EVENT_VALUE_CHANGED) {
lv_obj_t* button = lv_event_get_current_target(e);
lv_obj_t* label = lv_obj_get_child(button, 0);
lv_state_t state = lv_obj_get_state(button);
if(state & LV_STATE_CHECKED){
lv_label_set_text(label, LV_SYMBOL_PAUSE);
}
else{
lv_label_set_text(label, LV_SYMBOL_PLAY);
}
}
}
static lv_coord_t units_col_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
static lv_coord_t units_row_dsc[] = {LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
static void unit_widget_enabled_handler(lv_event_t * e){
lv_event_code_t code = lv_event_get_code(e);
if(code == LV_EVENT_VALUE_CHANGED) {
lv_obj_t* button = lv_event_get_current_target(e);
lv_obj_t* label = lv_obj_get_child(button, 0);
lv_state_t state = lv_obj_get_state(button);
if(state & LV_STATE_CHECKED){
lv_label_set_text(label, LV_SYMBOL_CLOSE);
}
else{
lv_label_set_text(label, LV_SYMBOL_OK);
}
}
}
static lv_obj_t* PSE_unit_widget(lv_obj_t* parent, pse_unit* pse_unit){
// The main container
lv_obj_t* cont = lv_obj_create(parent);
lv_obj_set_size(cont, lv_pct(24), lv_pct(100));
lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
// flow setting
lv_obj_t* flow = lv_label_create(cont);
lv_obj_set_width(flow, lv_pct(100));
lv_obj_set_flex_grow(flow, 1);
lv_label_set_text_fmt(flow, "%d.%d\nmL/mn", pse_unit->flow / 1000, pse_unit->flow % 1000);
// volume delivered readout
lv_obj_t* vol = lv_label_create(cont);
lv_obj_set_width(vol, lv_pct(100));
lv_obj_set_flex_grow(vol, 1);
lv_label_set_text_fmt(vol, "%d.%d\nmL", pse_unit->volume / 1000, pse_unit->volume % 1000);
// home button
lv_obj_t* home = lv_btn_create(cont);
lv_obj_t* home_label = lv_label_create(home);
lv_obj_set_flex_grow(home, 1);
lv_obj_center(home_label);
lv_label_set_text(home_label, LV_SYMBOL_HOME);
// enabled button
lv_obj_t* enabled = lv_btn_create(cont);
lv_obj_t* enabled_label = lv_label_create(enabled);
lv_obj_set_width(enabled, lv_pct(100));
lv_obj_add_flag(enabled, LV_OBJ_FLAG_CHECKABLE);
lv_obj_add_event_cb(enabled, unit_widget_enabled_handler, LV_EVENT_ALL, NULL);
lv_obj_set_flex_grow(enabled, 1);
lv_obj_center(enabled_label);
lv_label_set_text(enabled_label, LV_SYMBOL_OK);
if(!pse_unit->enabled){
lv_obj_add_state(enabled, LV_STATE_CHECKED);
lv_event_send(enabled, LV_EVENT_VALUE_CHANGED, NULL);
}
return cont;
}
void Home_Screen_Gen(pse_unit* pse_units, uint8_t pse_unit_num){
// Create a new screen
lv_obj_t* scr = lv_obj_create(NULL);
// create the top menu on the top 20%
lv_obj_t* top_menu = lv_obj_create(scr);
lv_obj_set_align(top_menu, LV_ALIGN_TOP_LEFT);
lv_obj_set_size(top_menu, lv_pct(100), lv_pct(20));
lv_obj_set_style_pad_all(top_menu, 5, 0);
// add a run button
lv_obj_t* run_button = lv_btn_create(top_menu);
lv_obj_set_size(run_button, lv_pct(10), lv_pct(100));
lv_obj_set_align(run_button, LV_ALIGN_TOP_LEFT);
lv_obj_add_flag(run_button, LV_OBJ_FLAG_CHECKABLE);
lv_obj_add_event_cb(run_button, run_handler, LV_EVENT_ALL, NULL);
// add the start label
lv_obj_t* run_label = lv_label_create(run_button);
lv_label_set_text(run_label, LV_SYMBOL_PLAY);
lv_obj_center(run_label);
// grid layout for the 4 differents units
lv_obj_t* units_grid = lv_obj_create(scr);
lv_obj_align_to(units_grid, top_menu, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
lv_obj_set_size(units_grid, lv_pct(100), lv_pct(80));
lv_obj_set_style_pad_all(units_grid, 5, 0);
lv_obj_set_layout(units_grid, LV_LAYOUT_GRID);
lv_obj_set_grid_dsc_array(units_grid, units_col_dsc, units_row_dsc);
for(int i = 0; i < pse_unit_num; i++){
lv_obj_t* obj = PSE_unit_widget(units_grid, &pse_units[i]);
lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_CENTER, i, 1, LV_GRID_ALIGN_CENTER, 0, 1);
}
// fade in the new screen
lv_scr_load_anim(scr, LV_SCR_LOAD_ANIM_FADE_ON, 100, 100, false);
}