Ui_temperature.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. ******************************************************************************
  3. * @file Ui_temperature.c
  4. * @brief 实时温度页面 (UI 页面层)
  5. *
  6. * @note 标题 + 左右箭头固定不动; 中间通道面板 (s_temp_box) 为独立容器,
  7. * 通道翻页由左右箭头按钮瞬时重建完成
  8. ******************************************************************************
  9. */
  10. #include <stdio.h>
  11. #include "Ui_temperature.h"
  12. #include "Ui.h"
  13. #include "Ui_theme.h"
  14. #include "Logic_temperature.h"
  15. /* 每页显示的通道数 = 3 行 x 2 列 = 6 条 */
  16. #define UI_TEMP_PAGE_NUM 6
  17. #define UI_TEMP_ROW_CNT 3
  18. #define UI_TEMP_COL_CNT 2
  19. /* 实时刷新周期 (ms) */
  20. #define UI_TEMP_REFRESH_MS 500
  21. /* 左右箭头导航按钮 (与数据卡片等高) */
  22. #define UI_TEMP_NAV_X 10
  23. #define UI_TEMP_NAV_W 70
  24. #define UI_TEMP_NAV_Y UI_TEMP_PANEL_Y
  25. #define UI_TEMP_NAV_H UI_TEMP_PANEL_H
  26. /* 导航按钮与数据卡片之间的间隔 (左右一致) */
  27. #define UI_TEMP_GAP 20
  28. /* 数据卡片位置 (左右留白一致, 与导航按钮间隔一致) */
  29. #define UI_TEMP_PANEL_X (UI_TEMP_NAV_X + UI_TEMP_NAV_W + UI_TEMP_GAP) /* 100 */
  30. #define UI_TEMP_PANEL_Y 44
  31. #define UI_TEMP_PANEL_W 430
  32. #define UI_TEMP_PANEL_H 388
  33. #define UI_TEMP_NAV_RIGHT_X (UI_TEMP_PANEL_X + UI_TEMP_PANEL_W + UI_TEMP_GAP) /* 550 */
  34. /* 数据区行列位置 (相对数据卡片) */
  35. #define UI_TEMP_SW_X1 18
  36. #define UI_TEMP_SW_X2 220
  37. #define UI_TEMP_SW_Y_BASE 50
  38. #define UI_TEMP_SW_H 100
  39. /* 色卡尺寸 */
  40. #define UI_TEMP_SW_SIZE 26
  41. /* 卡片底部: 分隔线 + 页码指示条 */
  42. #define UI_TEMP_FOOTER_Y 348
  43. #define UI_TEMP_CHIP_W 60
  44. #define UI_TEMP_CHIP_H 28
  45. /* 全局句柄 */
  46. static lv_obj_t * s_temp_value[UI_TEMP_PAGE_NUM]; /* 6 个动态数值标签 (温度) */
  47. static lv_obj_t * s_temp_swatch[UI_TEMP_PAGE_NUM]; /* 6 个色卡 */
  48. static lv_obj_t * s_page_label = NULL; /* 页码指示 (如 1/4) */
  49. static lv_timer_t * s_temp_timer = NULL; /* 实时刷新定时器 */
  50. static uint8_t s_page_start = 0; /* 当前页起始通道 (0/6/12/18) */
  51. static lv_obj_t * s_temp_box = NULL; /* 通道面板容器 */
  52. /* 前向声明: 箭头翻页需重建面板 (定义在下方) */
  53. static void ui_temp_build_panel(lv_obj_t * box, uint8_t start);
  54. /* 数值标签的静态缓冲区 (lv_label_set_text_static 不重新分配内存) */
  55. static char s_value_buf[UI_TEMP_PAGE_NUM][12];
  56. #define UI_TEMP_PAGE_STEP 6 /* 翻页步长: 每页 6 个通道 */
  57. #define UI_TEMP_PAGE_MAX (LOGIC_TEMP_TOTAL_CH - UI_TEMP_PAGE_NUM) /* 18 = 末页起始通道 */
  58. /* 每通道固定且互不相同的色相 (HSV -> RGB) */
  59. static lv_color_t ui_temp_ch_color(uint8_t ch)
  60. {
  61. uint16_t hue = (uint16_t)(ch * (360U / LOGIC_TEMP_TOTAL_CH));
  62. return lv_color_hsv_to_rgb(hue, 100, 100);
  63. }
  64. /* 刷新当前页的温度数值与色卡, 并更新页码指示 */
  65. static void ui_temp_refresh(void)
  66. {
  67. uint8_t i;
  68. for (i = 0; i < UI_TEMP_PAGE_NUM; i++)
  69. {
  70. uint8_t ch = s_page_start + i;
  71. int8_t t = Logic_temperature_get(ch);
  72. if (t == LOGIC_TEMP_INVALID)
  73. {
  74. snprintf(s_value_buf[i], sizeof(s_value_buf[i]), "--\xC2\xB0"
  75. "C");
  76. }
  77. else
  78. {
  79. snprintf(s_value_buf[i], sizeof(s_value_buf[i]), "%d\xC2\xB0"
  80. "C", (int)t);
  81. }
  82. lv_label_set_text_static(s_temp_value[i], s_value_buf[i]);
  83. lv_obj_set_style_bg_color(s_temp_swatch[i], ui_temp_ch_color(ch), LV_STATE_DEFAULT);
  84. }
  85. if (s_page_label != NULL)
  86. {
  87. char page_buf[16];
  88. snprintf(page_buf, sizeof(page_buf), "%d/%d",
  89. (int)(s_page_start / UI_TEMP_PAGE_STEP) + 1,
  90. (int)(LOGIC_TEMP_TOTAL_CH / UI_TEMP_PAGE_NUM));
  91. lv_label_set_text(s_page_label, page_buf);
  92. }
  93. }
  94. static void ui_temp_timer_cb(lv_timer_t * t)
  95. {
  96. (void)t;
  97. ui_temp_refresh();
  98. }
  99. /* 面板销毁时释放定时器, 防止悬挂指针 */
  100. static void ui_temp_delete_cb(lv_event_t * e)
  101. {
  102. (void)e;
  103. s_temp_box = NULL;
  104. if (s_temp_timer != NULL)
  105. {
  106. lv_timer_del(s_temp_timer);
  107. s_temp_timer = NULL;
  108. }
  109. }
  110. /* 重建面板内容: 描述标签 "CPUx CHn:" 是静态文字,
  111. * 翻页时只刷数值会导致描述永远停在第一页, 必须整面板重建 */
  112. static void ui_temp_rebuild_panel(void)
  113. {
  114. if (s_temp_box == NULL)
  115. {
  116. return;
  117. }
  118. lv_obj_clean(s_temp_box);
  119. ui_temp_build_panel(s_temp_box, s_page_start);
  120. }
  121. /* 左箭头: 上一页 (到首页后跳到最后页) */
  122. static void ui_temp_prev_cb(lv_event_t * e)
  123. {
  124. (void)e;
  125. if (s_page_start >= UI_TEMP_PAGE_STEP)
  126. {
  127. s_page_start -= UI_TEMP_PAGE_STEP;
  128. }
  129. else
  130. {
  131. s_page_start = UI_TEMP_PAGE_MAX;
  132. }
  133. ui_temp_rebuild_panel();
  134. }
  135. /* 右箭头: 下一页 (到末页后回到首页) */
  136. static void ui_temp_next_cb(lv_event_t * e)
  137. {
  138. (void)e;
  139. if (s_page_start < UI_TEMP_PAGE_MAX)
  140. {
  141. s_page_start += UI_TEMP_PAGE_STEP;
  142. }
  143. else
  144. {
  145. s_page_start = 0;
  146. }
  147. ui_temp_rebuild_panel();
  148. }
  149. static void ui_temp_build_nav_btn(lv_obj_t * parent, const char * ch, lv_coord_t x,
  150. lv_event_cb_t cb)
  151. {
  152. lv_obj_t * btn = lv_btn_create(parent);
  153. lv_obj_set_size(btn, UI_TEMP_NAV_W, UI_TEMP_NAV_H);
  154. lv_obj_set_pos(btn, x, UI_TEMP_NAV_Y);
  155. lv_obj_add_event_cb(btn, cb, LV_EVENT_CLICKED, NULL);
  156. Ui_theme_style_btn(btn);
  157. lv_obj_t * lbl = lv_label_create(btn);
  158. lv_label_set_text(lbl, ch);
  159. lv_obj_set_style_text_font(lbl, &lv_font_montserrat_20, LV_STATE_DEFAULT);
  160. lv_obj_set_style_text_color(lbl, UI_CLR_TEXT, LV_STATE_DEFAULT);
  161. lv_obj_set_style_text_color(lbl, UI_CLR_ACCENT_DARK, LV_STATE_PRESSED);
  162. lv_obj_center(lbl);
  163. }
  164. /* 构建面板内容: 3 行 x 2 列共 6 条温度数据 + 底部页码 (全部创建在 box 内) */
  165. static void ui_temp_build_panel(lv_obj_t * box, uint8_t start)
  166. {
  167. uint8_t r, c, i = 0;
  168. s_page_start = start;
  169. for (r = 0; r < UI_TEMP_ROW_CNT; r++)
  170. {
  171. for (c = 0; c < UI_TEMP_COL_CNT; c++)
  172. {
  173. lv_coord_t x = (c == 0) ? UI_TEMP_SW_X1 : UI_TEMP_SW_X2;
  174. lv_coord_t y = UI_TEMP_SW_Y_BASE + r * UI_TEMP_SW_H;
  175. uint8_t ch = s_page_start + i;
  176. int8_t cpu = Logic_temperature_get_cpu(ch);
  177. uint8_t ch_in = (uint8_t)(ch % LOGIC_TEMP_CPU_CH_COUNT) + 1;
  178. char desc[16];
  179. /* 色卡 (固定通道色) */
  180. lv_obj_t * sw = lv_obj_create(box);
  181. lv_obj_set_size(sw, UI_TEMP_SW_SIZE, UI_TEMP_SW_SIZE);
  182. lv_obj_set_pos(sw, x, y);
  183. lv_obj_set_style_radius(sw, LV_RADIUS_CIRCLE, LV_STATE_DEFAULT);
  184. lv_obj_set_style_bg_opa(sw, LV_OPA_COVER, LV_STATE_DEFAULT);
  185. Ui_theme_clear_visuals(sw);
  186. s_temp_swatch[i] = sw;
  187. /* 静态描述 "CPUx CHn:" */
  188. snprintf(desc, sizeof(desc), "CPU%d CH%d:", (int)cpu, (int)ch_in);
  189. lv_obj_t * d = lv_label_create(box);
  190. lv_label_set_text(d, desc);
  191. lv_obj_align_to(d, sw, LV_ALIGN_OUT_RIGHT_MID, 12, 0);
  192. lv_obj_set_style_text_font(d, &lv_font_montserrat_16, LV_STATE_DEFAULT);
  193. lv_obj_set_style_text_color(d, UI_CLR_TEXT_DIM, LV_STATE_DEFAULT);
  194. /* 动态数值 (实时刷新) */
  195. lv_obj_t * v = lv_label_create(box);
  196. lv_obj_set_style_text_font(v, &lv_font_montserrat_20, LV_STATE_DEFAULT);
  197. lv_obj_set_style_text_color(v, UI_CLR_ACCENT, LV_STATE_DEFAULT);
  198. lv_obj_align_to(v, d, LV_ALIGN_OUT_RIGHT_MID, 8, 0);
  199. s_temp_value[i] = v;
  200. i++;
  201. }
  202. }
  203. /* 底部: 分隔线 */
  204. lv_obj_t * footer_line = lv_obj_create(box);
  205. lv_obj_set_pos(footer_line, 14, UI_TEMP_FOOTER_Y);
  206. lv_obj_set_size(footer_line, UI_TEMP_PANEL_W - 28, 1);
  207. Ui_theme_clear_visuals(footer_line);
  208. lv_obj_set_style_bg_color(footer_line, UI_CLR_LINE, LV_STATE_DEFAULT);
  209. lv_obj_set_style_bg_opa(footer_line, LV_OPA_COVER, LV_STATE_DEFAULT);
  210. /* 页码指示条 */
  211. lv_obj_t * page_chip = lv_obj_create(box);
  212. lv_obj_set_size(page_chip, UI_TEMP_CHIP_W, UI_TEMP_CHIP_H);
  213. lv_obj_align(page_chip, LV_ALIGN_BOTTOM_MID, 0, -8);
  214. Ui_theme_style_chip(page_chip);
  215. s_page_label = lv_label_create(page_chip);
  216. lv_obj_set_style_text_font(s_page_label, &lv_font_montserrat_16, LV_STATE_DEFAULT);
  217. lv_obj_set_style_text_color(s_page_label, UI_CLR_ACCENT, LV_STATE_DEFAULT);
  218. lv_obj_center(s_page_label);
  219. ui_temp_refresh();
  220. }
  221. /**
  222. * @brief 创建"实时温度"页面 (标题/箭头固定, 通道面板由左右箭头按钮翻页)
  223. */
  224. void Ui_temperature_create(lv_obj_t * parent)
  225. {
  226. Ui_theme_add_title(parent, "Real-time Temperature", 10);
  227. ui_temp_build_nav_btn(parent, LV_SYMBOL_LEFT, UI_TEMP_NAV_X, ui_temp_prev_cb);
  228. ui_temp_build_nav_btn(parent, LV_SYMBOL_RIGHT, UI_TEMP_NAV_RIGHT_X, ui_temp_next_cb);
  229. /* 中间通道面板 */
  230. s_temp_box = lv_obj_create(parent);
  231. lv_obj_set_pos(s_temp_box, UI_TEMP_PANEL_X, UI_TEMP_PANEL_Y);
  232. lv_obj_set_size(s_temp_box, UI_TEMP_PANEL_W, UI_TEMP_PANEL_H);
  233. Ui_theme_style_panel(s_temp_box);
  234. lv_obj_add_event_cb(s_temp_box, ui_temp_delete_cb, LV_EVENT_DELETE, NULL);
  235. ui_temp_build_panel(s_temp_box, 0);
  236. /* 启动实时刷新定时器 (500ms) */
  237. s_temp_timer = lv_timer_create(ui_temp_timer_cb, UI_TEMP_REFRESH_MS, NULL);
  238. lv_timer_set_repeat_count(s_temp_timer, -1);
  239. }