Ui_status.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. ******************************************************************************
  3. * @file Ui_status.c
  4. * @brief 设备状态页面 (UI 页面层)
  5. *
  6. * @note 面板1: Status/Power/Comm 三行
  7. * Power 恒显示 OK (上电即正常);
  8. * Comm 与 CPLD 通讯正常显示 OK, 否则 NO (红);
  9. * Status 两者全 OK 显示 Running (绿), 任一异常显示 Fault (红);
  10. * 面板2: TC 温度控制状态 (PID 运行中 Running 绿 / 否则 Not Running 红).
  11. * 页面内 500ms 定时器实时刷新 (进入页面时取最新值)。
  12. ******************************************************************************
  13. */
  14. #include <stdio.h>
  15. #include "Ui_status.h"
  16. #include "Ui.h"
  17. #include "Ui_theme.h"
  18. #include "App_cpld.h"
  19. #include "App_pidControl.h"
  20. /* 状态卡片位置 */
  21. #define UI_STATUS_PANEL_X 10
  22. #define UI_STATUS_PANEL_Y 48
  23. #define UI_STATUS_PANEL_W 610
  24. #define UI_STATUS_PANEL_H 200
  25. /* TC 控制状态卡片 (位于状态卡片下方) */
  26. #define UI_STATUS_TC_PANEL_Y (UI_STATUS_PANEL_Y + UI_STATUS_PANEL_H + 12)
  27. #define UI_STATUS_TC_PANEL_H 84
  28. /* 行内坐标 */
  29. #define UI_STATUS_KEY_X 16
  30. #define UI_STATUS_VALUE_OFF_X -16
  31. #define UI_STATUS_ROW_Y0 14 /* 首行 y */
  32. #define UI_STATUS_ROW_GAP 52 /* 行间距 */
  33. #define UI_STATUS_LINE_X 16 /* 分隔线 */
  34. #define UI_STATUS_LINE_W (UI_STATUS_PANEL_W - 32)
  35. /* 实时刷新周期 (ms) */
  36. #define UI_STATUS_REFRESH_MS 500
  37. /* 全局句柄 (页面销毁时置空) */
  38. static lv_obj_t * s_status_panel = NULL;
  39. static lv_timer_t * s_status_timer = NULL;
  40. static lv_obj_t * s_status_value = NULL; /* Status 值标签 */
  41. static lv_obj_t * s_comm_value = NULL; /* Comm 值标签 */
  42. static lv_obj_t * s_tc_value = NULL; /* TC 值标签 */
  43. static lv_obj_t * ui_status_add_item(lv_obj_t * parent, const char * key, const char * value,
  44. lv_coord_t y, lv_color_t value_color)
  45. {
  46. lv_obj_t * k = lv_label_create(parent);
  47. lv_label_set_text(k, key);
  48. lv_obj_set_style_text_font(k, &lv_font_montserrat_16, LV_STATE_DEFAULT);
  49. lv_obj_set_style_text_color(k, UI_CLR_TEXT_DIM, LV_STATE_DEFAULT);
  50. lv_obj_set_pos(k, UI_STATUS_KEY_X, y);
  51. lv_obj_t * v = lv_label_create(parent);
  52. lv_label_set_text(v, value);
  53. lv_obj_set_style_text_font(v, &lv_font_montserrat_16, LV_STATE_DEFAULT);
  54. lv_obj_set_style_text_color(v, value_color, LV_STATE_DEFAULT);
  55. lv_obj_align(v, LV_ALIGN_TOP_RIGHT, UI_STATUS_VALUE_OFF_X, y);
  56. return v;
  57. }
  58. /* 刷新各行动态状态 */
  59. static void ui_status_refresh(void)
  60. {
  61. uint8_t comm_ok = App_cpld_comm_alive();
  62. uint8_t pid_run = App_pidControl_is_running();
  63. if (s_status_value != NULL)
  64. {
  65. lv_label_set_text_static(s_status_value, comm_ok ? "Running" : "Fault");
  66. lv_obj_set_style_text_color(s_status_value, comm_ok ? UI_CLR_OK : UI_CLR_ERR, LV_STATE_DEFAULT);
  67. }
  68. if (s_comm_value != NULL)
  69. {
  70. lv_label_set_text_static(s_comm_value, comm_ok ? "OK" : "NO");
  71. lv_obj_set_style_text_color(s_comm_value, comm_ok ? UI_CLR_OK : UI_CLR_ERR, LV_STATE_DEFAULT);
  72. }
  73. if (s_tc_value != NULL)
  74. {
  75. lv_label_set_text_static(s_tc_value, pid_run ? "Running" : "Not Running");
  76. lv_obj_set_style_text_color(s_tc_value, pid_run ? UI_CLR_OK : UI_CLR_ERR, LV_STATE_DEFAULT);
  77. }
  78. }
  79. static void ui_status_timer_cb(lv_timer_t * t)
  80. {
  81. (void)t;
  82. ui_status_refresh();
  83. }
  84. /* 页面销毁时释放定时器, 防止悬挂指针 */
  85. static void ui_status_delete_cb(lv_event_t * e)
  86. {
  87. (void)e;
  88. s_status_panel = NULL;
  89. if (s_status_timer != NULL)
  90. {
  91. lv_timer_del(s_status_timer);
  92. s_status_timer = NULL;
  93. }
  94. }
  95. void Ui_status_create(lv_obj_t * parent)
  96. {
  97. Ui_theme_add_title(parent, "Device Status", 10);
  98. /* 状态信息卡片 */
  99. s_status_panel = lv_obj_create(parent);
  100. lv_obj_set_pos(s_status_panel, UI_STATUS_PANEL_X, UI_STATUS_PANEL_Y);
  101. lv_obj_set_size(s_status_panel, UI_STATUS_PANEL_W, UI_STATUS_PANEL_H);
  102. Ui_theme_style_panel(s_status_panel);
  103. lv_obj_add_event_cb(s_status_panel, ui_status_delete_cb, LV_EVENT_DELETE, NULL);
  104. s_status_value = ui_status_add_item(s_status_panel, "Status:", "Fault", UI_STATUS_ROW_Y0 + 0 * UI_STATUS_ROW_GAP, UI_CLR_ERR);
  105. ui_status_add_item(s_status_panel, "Power:", "OK", UI_STATUS_ROW_Y0 + 1 * UI_STATUS_ROW_GAP, UI_CLR_OK);
  106. s_comm_value = ui_status_add_item(s_status_panel, "Comm:", "NO", UI_STATUS_ROW_Y0 + 2 * UI_STATUS_ROW_GAP, UI_CLR_ERR);
  107. /* 行间分隔线 (前两行下方) */
  108. uint8_t i;
  109. for (i = 0; i < 2; i++)
  110. {
  111. lv_obj_t * line = lv_obj_create(s_status_panel);
  112. lv_obj_set_pos(line, UI_STATUS_LINE_X, UI_STATUS_ROW_Y0 + (i + 1) * UI_STATUS_ROW_GAP - 18);
  113. lv_obj_set_size(line, UI_STATUS_LINE_W, 1);
  114. Ui_theme_clear_visuals(line);
  115. lv_obj_set_style_bg_color(line, UI_CLR_LINE, LV_STATE_DEFAULT);
  116. lv_obj_set_style_bg_opa(line, LV_OPA_COVER, LV_STATE_DEFAULT);
  117. }
  118. /* TC 温度控制状态卡片 */
  119. lv_obj_t * tc_panel = lv_obj_create(parent);
  120. lv_obj_set_pos(tc_panel, UI_STATUS_PANEL_X, UI_STATUS_TC_PANEL_Y);
  121. lv_obj_set_size(tc_panel, UI_STATUS_PANEL_W, UI_STATUS_TC_PANEL_H);
  122. Ui_theme_style_panel(tc_panel);
  123. s_tc_value = ui_status_add_item(tc_panel, "Temperature Control:", "Not Running", 14, UI_CLR_ERR);
  124. ui_status_refresh();
  125. /* 启动实时刷新定时器 (500ms) */
  126. s_status_timer = lv_timer_create(ui_status_timer_cb, UI_STATUS_REFRESH_MS, NULL);
  127. lv_timer_set_repeat_count(s_status_timer, -1);
  128. }