Files
Smart-flower-system/components/ui/screens.c
Wang Beihong bec31c01ce 在主应用中更新显示驱动和 MQTT 主题
- 将显示驱动从 lvgl_st7735s_use 改为 lvgl_st7789_use,在 CMakeLists.txt 和 main.c 中。
- 将 MQTT 警报主题从“topic/alert/esp32_iothome_001”更新为“topic/alert/esp32_iothome_002”。
- 重构 UI 任务,去除周期性屏幕切换逻辑,专注于界面刷新。
- 调整错误处理,使新的显示驱动程序用于错误信息。
2026-03-14 15:44:01 +08:00

312 lines
11 KiB
C

#include <string.h>
#include "screens.h"
#include "images.h"
#include "fonts.h"
#include "actions.h"
#include "vars.h"
#include "styles.h"
#include "ui.h"
#include <string.h>
objects_t objects;
//
// Event handlers
//
lv_obj_t *tick_value_change_obj;
//
// Screens
//
void create_screen_main() {
lv_obj_t *obj = lv_obj_create(0);
objects.main = obj;
lv_obj_set_pos(obj, 0, 0);
lv_obj_set_size(obj, 240, 240);
lv_obj_set_style_bg_color(obj, lv_color_hex(0xff000000), LV_PART_MAIN | LV_STATE_DEFAULT);
{
lv_obj_t *parent_obj = obj;
{
lv_obj_t *obj = lv_label_create(parent_obj);
objects.obj0 = obj;
lv_obj_set_pos(obj, 16, 0);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_text_font(obj, &ui_font_source_han_sans_sc_medium_22, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_label_set_text(obj, "空气温度");
}
{
lv_obj_t *obj = lv_label_create(parent_obj);
objects.obj1 = obj;
lv_obj_set_pos(obj, 129, 2);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_text_font(obj, &ui_font_source_han_sans_sc_medium_22, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_label_set_text(obj, "空气湿度");
}
{
lv_obj_t *obj = lv_label_create(parent_obj);
objects.obj2 = obj;
lv_obj_set_pos(obj, 16, 77);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_text_font(obj, &ui_font_source_han_sans_sc_medium_22, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_label_set_text(obj, "土壤湿度");
}
{
lv_obj_t *obj = lv_label_create(parent_obj);
objects.obj3 = obj;
lv_obj_set_pos(obj, 129, 79);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_text_font(obj, &ui_font_source_han_sans_sc_medium_22, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_label_set_text(obj, "光照强度");
}
{
lv_obj_t *obj = lv_label_create(parent_obj);
objects.obj4 = obj;
lv_obj_set_pos(obj, 16, 38);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_text_font(obj, &ui_font_source_han_sans_sc_medium_22, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_label_set_text(obj, "");
}
{
lv_obj_t *obj = lv_label_create(parent_obj);
objects.obj5 = obj;
lv_obj_set_pos(obj, 129, 38);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_text_font(obj, &ui_font_source_han_sans_sc_medium_22, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_label_set_text(obj, "");
}
{
lv_obj_t *obj = lv_label_create(parent_obj);
objects.obj6 = obj;
lv_obj_set_pos(obj, 16, 116);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_text_font(obj, &ui_font_source_han_sans_sc_medium_22, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_label_set_text(obj, "");
}
{
lv_obj_t *obj = lv_label_create(parent_obj);
objects.obj7 = obj;
lv_obj_set_pos(obj, 129, 116);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_text_font(obj, &ui_font_source_han_sans_sc_medium_22, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_label_set_text(obj, "");
}
{
lv_obj_t *obj = lv_image_create(parent_obj);
lv_obj_set_pos(obj, 72, 27);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_image_set_src(obj, &img_temp);
}
{
lv_obj_t *obj = lv_image_create(parent_obj);
lv_obj_set_pos(obj, 192, 27);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_image_set_src(obj, &img_humi);
}
{
lv_obj_t *obj = lv_image_create(parent_obj);
lv_obj_set_pos(obj, 72, 105);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_image_set_src(obj, &img_mois);
}
{
lv_obj_t *obj = lv_image_create(parent_obj);
lv_obj_set_pos(obj, 192, 105);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_image_set_src(obj, &img_light);
}
{
lv_obj_t *obj = lv_line_create(parent_obj);
objects.obj8 = obj;
lv_obj_set_pos(obj, 0, 75);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
static lv_point_precise_t line_points[] = {
{ 0, 0 },
{ 240, 0 }
};
lv_line_set_points(obj, line_points, 2);
lv_obj_set_style_line_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
}
{
lv_obj_t *obj = lv_line_create(parent_obj);
objects.obj9 = obj;
lv_obj_set_pos(obj, 0, 160);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
static lv_point_precise_t line_points[] = {
{ 0, 0 },
{ 240, 0 }
};
lv_line_set_points(obj, line_points, 2);
lv_obj_set_style_line_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
}
{
lv_obj_t *obj = lv_line_create(parent_obj);
objects.obj10 = obj;
lv_obj_set_pos(obj, 120, 0);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
static lv_point_precise_t line_points[] = {
{ 0, 0 },
{ 0, 160 }
};
lv_line_set_points(obj, line_points, 2);
lv_obj_set_style_line_color(obj, lv_color_hex(0xffffffff), LV_PART_MAIN | LV_STATE_DEFAULT);
}
}
tick_screen_main();
}
void tick_screen_main() {
{
const char *new_val = get_var_air_temperature();
const char *cur_val = lv_label_get_text(objects.obj4);
if (strcmp(new_val, cur_val) != 0) {
tick_value_change_obj = objects.obj4;
lv_label_set_text(objects.obj4, new_val);
tick_value_change_obj = NULL;
}
}
{
const char *new_val = get_var_air_humidity();
const char *cur_val = lv_label_get_text(objects.obj5);
if (strcmp(new_val, cur_val) != 0) {
tick_value_change_obj = objects.obj5;
lv_label_set_text(objects.obj5, new_val);
tick_value_change_obj = NULL;
}
}
{
const char *new_val = get_var_soil_moisture();
const char *cur_val = lv_label_get_text(objects.obj6);
if (strcmp(new_val, cur_val) != 0) {
tick_value_change_obj = objects.obj6;
lv_label_set_text(objects.obj6, new_val);
tick_value_change_obj = NULL;
}
}
{
const char *new_val = get_var_light_intensity();
const char *cur_val = lv_label_get_text(objects.obj7);
if (strcmp(new_val, cur_val) != 0) {
tick_value_change_obj = objects.obj7;
lv_label_set_text(objects.obj7, new_val);
tick_value_change_obj = NULL;
}
}
}
typedef void (*tick_screen_func_t)();
tick_screen_func_t tick_screen_funcs[] = {
tick_screen_main,
};
void tick_screen(int screen_index) {
tick_screen_funcs[screen_index]();
}
void tick_screen_by_id(enum ScreensEnum screenId) {
tick_screen_funcs[screenId - 1]();
}
//
// Fonts
//
ext_font_desc_t fonts[] = {
{ "SourceHanSansSC-Medium_22", &ui_font_source_han_sans_sc_medium_22 },
{ "SourceHanSansSC-Medium_36", &ui_font_source_han_sans_sc_medium_36 },
#if LV_FONT_MONTSERRAT_8
{ "MONTSERRAT_8", &lv_font_montserrat_8 },
#endif
#if LV_FONT_MONTSERRAT_10
{ "MONTSERRAT_10", &lv_font_montserrat_10 },
#endif
#if LV_FONT_MONTSERRAT_12
{ "MONTSERRAT_12", &lv_font_montserrat_12 },
#endif
#if LV_FONT_MONTSERRAT_14
{ "MONTSERRAT_14", &lv_font_montserrat_14 },
#endif
#if LV_FONT_MONTSERRAT_16
{ "MONTSERRAT_16", &lv_font_montserrat_16 },
#endif
#if LV_FONT_MONTSERRAT_18
{ "MONTSERRAT_18", &lv_font_montserrat_18 },
#endif
#if LV_FONT_MONTSERRAT_20
{ "MONTSERRAT_20", &lv_font_montserrat_20 },
#endif
#if LV_FONT_MONTSERRAT_22
{ "MONTSERRAT_22", &lv_font_montserrat_22 },
#endif
#if LV_FONT_MONTSERRAT_24
{ "MONTSERRAT_24", &lv_font_montserrat_24 },
#endif
#if LV_FONT_MONTSERRAT_26
{ "MONTSERRAT_26", &lv_font_montserrat_26 },
#endif
#if LV_FONT_MONTSERRAT_28
{ "MONTSERRAT_28", &lv_font_montserrat_28 },
#endif
#if LV_FONT_MONTSERRAT_30
{ "MONTSERRAT_30", &lv_font_montserrat_30 },
#endif
#if LV_FONT_MONTSERRAT_32
{ "MONTSERRAT_32", &lv_font_montserrat_32 },
#endif
#if LV_FONT_MONTSERRAT_34
{ "MONTSERRAT_34", &lv_font_montserrat_34 },
#endif
#if LV_FONT_MONTSERRAT_36
{ "MONTSERRAT_36", &lv_font_montserrat_36 },
#endif
#if LV_FONT_MONTSERRAT_38
{ "MONTSERRAT_38", &lv_font_montserrat_38 },
#endif
#if LV_FONT_MONTSERRAT_40
{ "MONTSERRAT_40", &lv_font_montserrat_40 },
#endif
#if LV_FONT_MONTSERRAT_42
{ "MONTSERRAT_42", &lv_font_montserrat_42 },
#endif
#if LV_FONT_MONTSERRAT_44
{ "MONTSERRAT_44", &lv_font_montserrat_44 },
#endif
#if LV_FONT_MONTSERRAT_46
{ "MONTSERRAT_46", &lv_font_montserrat_46 },
#endif
#if LV_FONT_MONTSERRAT_48
{ "MONTSERRAT_48", &lv_font_montserrat_48 },
#endif
};
//
// Color themes
//
uint32_t active_theme_index = 0;
//
//
//
void create_screens() {
// Set default LVGL theme
lv_display_t *dispp = lv_display_get_default();
lv_theme_t *theme = lv_theme_default_init(dispp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), false, LV_FONT_DEFAULT);
lv_display_set_theme(dispp, theme);
// Initialize screens
// Create screens
create_screen_main();
}