| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- /**
- ******************************************************************************
- * @file Ui_temperature.c
- * @brief 实时温度页面 (UI 页面层)
- *
- * @note 标题 + 左右箭头固定不动; 中间通道面板 (s_temp_box) 为独立容器,
- * 通道翻页由左右箭头按钮瞬时重建完成
- ******************************************************************************
- */
- #include <stdio.h>
- #include "Ui_temperature.h"
- #include "Ui.h"
- #include "Ui_theme.h"
- #include "Logic_temperature.h"
- /* 每页显示的通道数 = 3 行 x 2 列 = 6 条 */
- #define UI_TEMP_PAGE_NUM 6
- #define UI_TEMP_ROW_CNT 3
- #define UI_TEMP_COL_CNT 2
- /* 实时刷新周期 (ms) */
- #define UI_TEMP_REFRESH_MS 500
- /* 左右箭头导航按钮 (与数据卡片等高) */
- #define UI_TEMP_NAV_X 10
- #define UI_TEMP_NAV_W 70
- #define UI_TEMP_NAV_Y UI_TEMP_PANEL_Y
- #define UI_TEMP_NAV_H UI_TEMP_PANEL_H
- /* 导航按钮与数据卡片之间的间隔 (左右一致) */
- #define UI_TEMP_GAP 20
- /* 数据卡片位置 (左右留白一致, 与导航按钮间隔一致) */
- #define UI_TEMP_PANEL_X (UI_TEMP_NAV_X + UI_TEMP_NAV_W + UI_TEMP_GAP) /* 100 */
- #define UI_TEMP_PANEL_Y 44
- #define UI_TEMP_PANEL_W 430
- #define UI_TEMP_PANEL_H 388
- #define UI_TEMP_NAV_RIGHT_X (UI_TEMP_PANEL_X + UI_TEMP_PANEL_W + UI_TEMP_GAP) /* 550 */
- /* 数据区行列位置 (相对数据卡片) */
- #define UI_TEMP_SW_X1 18
- #define UI_TEMP_SW_X2 220
- #define UI_TEMP_SW_Y_BASE 50
- #define UI_TEMP_SW_H 100
- /* 色卡尺寸 */
- #define UI_TEMP_SW_SIZE 26
- /* 卡片底部: 分隔线 + 页码指示条 */
- #define UI_TEMP_FOOTER_Y 348
- #define UI_TEMP_CHIP_W 60
- #define UI_TEMP_CHIP_H 28
- /* 全局句柄 */
- static lv_obj_t * s_temp_value[UI_TEMP_PAGE_NUM]; /* 6 个动态数值标签 (温度) */
- static lv_obj_t * s_temp_swatch[UI_TEMP_PAGE_NUM]; /* 6 个色卡 */
- static lv_obj_t * s_page_label = NULL; /* 页码指示 (如 1/4) */
- static lv_timer_t * s_temp_timer = NULL; /* 实时刷新定时器 */
- static uint8_t s_page_start = 0; /* 当前页起始通道 (0/6/12/18) */
- static lv_obj_t * s_temp_box = NULL; /* 通道面板容器 */
- /* 前向声明: 箭头翻页需重建面板 (定义在下方) */
- static void ui_temp_build_panel(lv_obj_t * box, uint8_t start);
- /* 数值标签的静态缓冲区 (lv_label_set_text_static 不重新分配内存) */
- static char s_value_buf[UI_TEMP_PAGE_NUM][12];
- #define UI_TEMP_PAGE_STEP 6 /* 翻页步长: 每页 6 个通道 */
- #define UI_TEMP_PAGE_MAX (LOGIC_TEMP_TOTAL_CH - UI_TEMP_PAGE_NUM) /* 18 = 末页起始通道 */
- /* 每通道固定且互不相同的色相 (HSV -> RGB) */
- static lv_color_t ui_temp_ch_color(uint8_t ch)
- {
- uint16_t hue = (uint16_t)(ch * (360U / LOGIC_TEMP_TOTAL_CH));
- return lv_color_hsv_to_rgb(hue, 100, 100);
- }
- /* 刷新当前页的温度数值与色卡, 并更新页码指示 */
- static void ui_temp_refresh(void)
- {
- uint8_t i;
- for (i = 0; i < UI_TEMP_PAGE_NUM; i++)
- {
- uint8_t ch = s_page_start + i;
- int8_t t = Logic_temperature_get(ch);
- if (t == LOGIC_TEMP_INVALID)
- {
- snprintf(s_value_buf[i], sizeof(s_value_buf[i]), "--\xC2\xB0"
- "C");
- }
- else
- {
- snprintf(s_value_buf[i], sizeof(s_value_buf[i]), "%d\xC2\xB0"
- "C", (int)t);
- }
- lv_label_set_text_static(s_temp_value[i], s_value_buf[i]);
- lv_obj_set_style_bg_color(s_temp_swatch[i], ui_temp_ch_color(ch), LV_STATE_DEFAULT);
- }
- if (s_page_label != NULL)
- {
- char page_buf[16];
- snprintf(page_buf, sizeof(page_buf), "%d/%d",
- (int)(s_page_start / UI_TEMP_PAGE_STEP) + 1,
- (int)(LOGIC_TEMP_TOTAL_CH / UI_TEMP_PAGE_NUM));
- lv_label_set_text(s_page_label, page_buf);
- }
- }
- static void ui_temp_timer_cb(lv_timer_t * t)
- {
- (void)t;
- ui_temp_refresh();
- }
- /* 面板销毁时释放定时器, 防止悬挂指针 */
- static void ui_temp_delete_cb(lv_event_t * e)
- {
- (void)e;
- s_temp_box = NULL;
- if (s_temp_timer != NULL)
- {
- lv_timer_del(s_temp_timer);
- s_temp_timer = NULL;
- }
- }
- /* 重建面板内容: 描述标签 "CPUx CHn:" 是静态文字,
- * 翻页时只刷数值会导致描述永远停在第一页, 必须整面板重建 */
- static void ui_temp_rebuild_panel(void)
- {
- if (s_temp_box == NULL)
- {
- return;
- }
- lv_obj_clean(s_temp_box);
- ui_temp_build_panel(s_temp_box, s_page_start);
- }
- /* 左箭头: 上一页 (到首页后跳到最后页) */
- static void ui_temp_prev_cb(lv_event_t * e)
- {
- (void)e;
- if (s_page_start >= UI_TEMP_PAGE_STEP)
- {
- s_page_start -= UI_TEMP_PAGE_STEP;
- }
- else
- {
- s_page_start = UI_TEMP_PAGE_MAX;
- }
- ui_temp_rebuild_panel();
- }
- /* 右箭头: 下一页 (到末页后回到首页) */
- static void ui_temp_next_cb(lv_event_t * e)
- {
- (void)e;
- if (s_page_start < UI_TEMP_PAGE_MAX)
- {
- s_page_start += UI_TEMP_PAGE_STEP;
- }
- else
- {
- s_page_start = 0;
- }
- ui_temp_rebuild_panel();
- }
- static void ui_temp_build_nav_btn(lv_obj_t * parent, const char * ch, lv_coord_t x,
- lv_event_cb_t cb)
- {
- lv_obj_t * btn = lv_btn_create(parent);
- lv_obj_set_size(btn, UI_TEMP_NAV_W, UI_TEMP_NAV_H);
- lv_obj_set_pos(btn, x, UI_TEMP_NAV_Y);
- lv_obj_add_event_cb(btn, cb, LV_EVENT_CLICKED, NULL);
- Ui_theme_style_btn(btn);
- lv_obj_t * lbl = lv_label_create(btn);
- lv_label_set_text(lbl, ch);
- lv_obj_set_style_text_font(lbl, &lv_font_montserrat_20, LV_STATE_DEFAULT);
- lv_obj_set_style_text_color(lbl, UI_CLR_TEXT, LV_STATE_DEFAULT);
- lv_obj_set_style_text_color(lbl, UI_CLR_ACCENT_DARK, LV_STATE_PRESSED);
- lv_obj_center(lbl);
- }
- /* 构建面板内容: 3 行 x 2 列共 6 条温度数据 + 底部页码 (全部创建在 box 内) */
- static void ui_temp_build_panel(lv_obj_t * box, uint8_t start)
- {
- uint8_t r, c, i = 0;
- s_page_start = start;
- for (r = 0; r < UI_TEMP_ROW_CNT; r++)
- {
- for (c = 0; c < UI_TEMP_COL_CNT; c++)
- {
- lv_coord_t x = (c == 0) ? UI_TEMP_SW_X1 : UI_TEMP_SW_X2;
- lv_coord_t y = UI_TEMP_SW_Y_BASE + r * UI_TEMP_SW_H;
- uint8_t ch = s_page_start + i;
- int8_t cpu = Logic_temperature_get_cpu(ch);
- uint8_t ch_in = (uint8_t)(ch % LOGIC_TEMP_CPU_CH_COUNT) + 1;
- char desc[16];
- /* 色卡 (固定通道色) */
- lv_obj_t * sw = lv_obj_create(box);
- lv_obj_set_size(sw, UI_TEMP_SW_SIZE, UI_TEMP_SW_SIZE);
- lv_obj_set_pos(sw, x, y);
- lv_obj_set_style_radius(sw, LV_RADIUS_CIRCLE, LV_STATE_DEFAULT);
- lv_obj_set_style_bg_opa(sw, LV_OPA_COVER, LV_STATE_DEFAULT);
- Ui_theme_clear_visuals(sw);
- s_temp_swatch[i] = sw;
- /* 静态描述 "CPUx CHn:" */
- snprintf(desc, sizeof(desc), "CPU%d CH%d:", (int)cpu, (int)ch_in);
- lv_obj_t * d = lv_label_create(box);
- lv_label_set_text(d, desc);
- lv_obj_align_to(d, sw, LV_ALIGN_OUT_RIGHT_MID, 12, 0);
- lv_obj_set_style_text_font(d, &lv_font_montserrat_16, LV_STATE_DEFAULT);
- lv_obj_set_style_text_color(d, UI_CLR_TEXT_DIM, LV_STATE_DEFAULT);
- /* 动态数值 (实时刷新) */
- lv_obj_t * v = lv_label_create(box);
- lv_obj_set_style_text_font(v, &lv_font_montserrat_20, LV_STATE_DEFAULT);
- lv_obj_set_style_text_color(v, UI_CLR_ACCENT, LV_STATE_DEFAULT);
- lv_obj_align_to(v, d, LV_ALIGN_OUT_RIGHT_MID, 8, 0);
- s_temp_value[i] = v;
- i++;
- }
- }
- /* 底部: 分隔线 */
- lv_obj_t * footer_line = lv_obj_create(box);
- lv_obj_set_pos(footer_line, 14, UI_TEMP_FOOTER_Y);
- lv_obj_set_size(footer_line, UI_TEMP_PANEL_W - 28, 1);
- Ui_theme_clear_visuals(footer_line);
- lv_obj_set_style_bg_color(footer_line, UI_CLR_LINE, LV_STATE_DEFAULT);
- lv_obj_set_style_bg_opa(footer_line, LV_OPA_COVER, LV_STATE_DEFAULT);
- /* 页码指示条 */
- lv_obj_t * page_chip = lv_obj_create(box);
- lv_obj_set_size(page_chip, UI_TEMP_CHIP_W, UI_TEMP_CHIP_H);
- lv_obj_align(page_chip, LV_ALIGN_BOTTOM_MID, 0, -8);
- Ui_theme_style_chip(page_chip);
- s_page_label = lv_label_create(page_chip);
- lv_obj_set_style_text_font(s_page_label, &lv_font_montserrat_16, LV_STATE_DEFAULT);
- lv_obj_set_style_text_color(s_page_label, UI_CLR_ACCENT, LV_STATE_DEFAULT);
- lv_obj_center(s_page_label);
- ui_temp_refresh();
- }
- /**
- * @brief 创建"实时温度"页面 (标题/箭头固定, 通道面板由左右箭头按钮翻页)
- */
- void Ui_temperature_create(lv_obj_t * parent)
- {
- Ui_theme_add_title(parent, "Real-time Temperature", 10);
- ui_temp_build_nav_btn(parent, LV_SYMBOL_LEFT, UI_TEMP_NAV_X, ui_temp_prev_cb);
- ui_temp_build_nav_btn(parent, LV_SYMBOL_RIGHT, UI_TEMP_NAV_RIGHT_X, ui_temp_next_cb);
- /* 中间通道面板 */
- s_temp_box = lv_obj_create(parent);
- lv_obj_set_pos(s_temp_box, UI_TEMP_PANEL_X, UI_TEMP_PANEL_Y);
- lv_obj_set_size(s_temp_box, UI_TEMP_PANEL_W, UI_TEMP_PANEL_H);
- Ui_theme_style_panel(s_temp_box);
- lv_obj_add_event_cb(s_temp_box, ui_temp_delete_cb, LV_EVENT_DELETE, NULL);
- ui_temp_build_panel(s_temp_box, 0);
- /* 启动实时刷新定时器 (500ms) */
- s_temp_timer = lv_timer_create(ui_temp_timer_cb, UI_TEMP_REFRESH_MS, NULL);
- lv_timer_set_repeat_count(s_temp_timer, -1);
- }
|