App_freertosTask.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "App_freertosTask.h"
  2. #include <stdio.h>
  3. #include "lvgl.h"
  4. #include "lv_port_disp.h"
  5. #define TASK_CPLD_PRIORITY 4
  6. #define TASK_CPLD_STACK_SIZE 512
  7. #define TASK_LVGL_PRIORITY 3
  8. #define TASK_LVGL_STACK_SIZE 2048
  9. #define TASK_TEST_PRIORITY 1
  10. #define TASK_TEST_STACK_SIZE 512
  11. /* 测试1(顺序测试)条件编译开关:1=运行;0=静态红底+文字页 */
  12. #define ENABLE_TEST1 1
  13. #if ENABLE_TEST1
  14. /**
  15. * @brief 测试1 状态机
  16. *
  17. * 顺序:10→1 倒数(局部文字刷新)→ 背景三原色切换(各 1s)→ 3x3 九宫格 9 色
  18. */
  19. typedef enum
  20. {
  21. TEST1_PHASE_COUNTDOWN, /* 10→1 倒数 */
  22. TEST1_PHASE_BG, /* 三原色背景切换 */
  23. TEST1_PHASE_GRID /* 三原色九宫格 */
  24. } test1_phase_t;
  25. typedef struct
  26. {
  27. test1_phase_t phase;
  28. uint32_t last_ms;
  29. int32_t count; /* 倒数值 10..1 */
  30. uint32_t bg_idx; /* 当前背景色索引 */
  31. lv_obj_t * scr;
  32. lv_obj_t * lbl;
  33. } test1_state_t;
  34. static const uint32_t test1_bg_hex[3] = {
  35. 0xFF0000, /* 红 */
  36. 0x00FF00, /* 绿 */
  37. 0x0000FF /* 蓝 */
  38. };
  39. static const uint32_t test1_grid_hex[9] = {
  40. 0xFF0000, /* 红 */
  41. 0x00FF00, /* 绿 */
  42. 0x0000FF, /* 蓝 */
  43. 0xFFFF00, /* 黄 */
  44. 0x00FFFF, /* 青 */
  45. 0xFF00FF, /* 紫 */
  46. 0xFF8000, /* 橙 */
  47. 0x80FF00, /* 草绿 */
  48. 0x8000FF /* 品红 */
  49. };
  50. /* 九宫格列宽:800 = 266 + 267 + 267;行高:480 = 160 * 3 */
  51. static const lv_coord_t test1_grid_col_w[3] = { 266, 267, 267 };
  52. static const lv_coord_t test1_grid_col_x[3] = { 0, 266, 533 };
  53. static const lv_coord_t test1_grid_row_h = 160;
  54. static void Test1_Init(test1_state_t * st)
  55. {
  56. st->phase = TEST1_PHASE_COUNTDOWN;
  57. st->last_ms = lv_tick_get();
  58. st->count = 10;
  59. st->bg_idx = 0;
  60. st->scr = lv_scr_act();
  61. lv_obj_set_style_bg_color(st->scr, lv_color_hex(test1_bg_hex[0]), LV_STATE_DEFAULT);
  62. st->lbl = lv_label_create(st->scr);
  63. lv_label_set_text_fmt(st->lbl, "%d", st->count);
  64. lv_obj_center(st->lbl);
  65. }
  66. static void Test1_Tick(test1_state_t * st)
  67. {
  68. uint32_t now = lv_tick_get();
  69. if ((uint32_t)(now - st->last_ms) < 1000)
  70. {
  71. return;
  72. }
  73. st->last_ms = now;
  74. switch (st->phase)
  75. {
  76. case TEST1_PHASE_COUNTDOWN:
  77. {
  78. st->count--;
  79. if (st->count >= 1)
  80. {
  81. lv_label_set_text_fmt(st->lbl, "%d", st->count);
  82. }
  83. else
  84. {
  85. /* 倒数结束:删除文字,进入背景切换 */
  86. if (st->lbl != NULL)
  87. {
  88. lv_obj_del(st->lbl);
  89. st->lbl = NULL;
  90. }
  91. st->phase = TEST1_PHASE_BG;
  92. lv_obj_set_style_bg_color(st->scr, lv_color_hex(test1_bg_hex[0]), LV_STATE_DEFAULT);
  93. }
  94. break;
  95. }
  96. case TEST1_PHASE_BG:
  97. {
  98. st->bg_idx++;
  99. if (st->bg_idx < 3)
  100. {
  101. lv_obj_set_style_bg_color(st->scr, lv_color_hex(test1_bg_hex[st->bg_idx]), LV_STATE_DEFAULT);
  102. }
  103. else
  104. {
  105. /* 三原色切换完毕:创建 3x3 九宫格 */
  106. uint32_t i;
  107. for (i = 0; i < 9; i++)
  108. {
  109. lv_obj_t * blk = lv_obj_create(st->scr);
  110. lv_obj_set_size(blk, test1_grid_col_w[i % 3], test1_grid_row_h);
  111. lv_obj_set_pos(blk, test1_grid_col_x[i % 3], (i / 3) * test1_grid_row_h);
  112. lv_obj_set_style_bg_color(blk, lv_color_hex(test1_grid_hex[i]), LV_STATE_DEFAULT);
  113. lv_obj_set_style_border_width(blk, 0, LV_STATE_DEFAULT);
  114. lv_obj_set_style_radius(blk, 0, LV_STATE_DEFAULT);
  115. }
  116. st->phase = TEST1_PHASE_GRID;
  117. }
  118. break;
  119. }
  120. default:
  121. break;
  122. }
  123. }
  124. #endif /* ENABLE_TEST1 */
  125. TaskHandle_t xCPLDProcessTaskHandle = NULL;
  126. TaskHandle_t xLVGLDisplayTaskHandle = NULL;
  127. TaskHandle_t xTestTaskHandle = NULL;
  128. /**
  129. * @brief 创建各个任务
  130. *
  131. */
  132. void App_Task_Create(void)
  133. {
  134. BaseType_t ret;
  135. ret = xTaskCreate(Task_CPLD_Process, "cpldProcessTask", TASK_CPLD_STACK_SIZE, NULL, TASK_CPLD_PRIORITY, &xCPLDProcessTaskHandle);
  136. if (ret != pdPASS)
  137. {
  138. printf("[ERR] CPLD process task create failed\r\n");
  139. }
  140. ret = xTaskCreate(Task_LVGL_Display, "lvglDisplayTask", TASK_LVGL_STACK_SIZE, NULL, TASK_LVGL_PRIORITY, &xLVGLDisplayTaskHandle);
  141. if (ret != pdPASS)
  142. {
  143. printf("[ERR] LVGL display task create failed\r\n");
  144. }
  145. ret = xTaskCreate(Task_Test, "testTask", TASK_TEST_STACK_SIZE, NULL, TASK_TEST_PRIORITY, &xTestTaskHandle);
  146. if (ret != pdPASS)
  147. {
  148. printf("[ERR] Test task create failed\r\n");
  149. }
  150. }
  151. /**
  152. * @brief 处理 CPLD 通信的任务
  153. *
  154. * @param argument
  155. */
  156. void Task_CPLD_Process(void *argument)
  157. {
  158. (void)argument;
  159. printf("CPLD process task running...\r\n");
  160. while (1)
  161. {
  162. printf("CPLD\r\n");
  163. /* TODO: CPLD 通信处理 */
  164. vTaskDelay(pdMS_TO_TICKS(1000));
  165. }
  166. }
  167. /**
  168. * @brief 处理 LVGL 显示的任务
  169. *
  170. * @param argument
  171. */
  172. void Task_LVGL_Display(void *argument)
  173. {
  174. (void)argument;
  175. printf("LVGL display task running...\r\n");
  176. /* LVGL 内核初始化 */
  177. lv_init();
  178. /* 注册显示驱动:LTDC + SDRAM 静态显存 + 双全屏缓冲 */
  179. lv_port_disp_init();
  180. #if ENABLE_TEST1
  181. test1_state_t test1;
  182. Test1_Init(&test1);
  183. #else
  184. /* 验证界面:红底 + 居中文字 */
  185. lv_obj_t *scr = lv_scr_act();
  186. lv_obj_set_style_bg_color(scr, lv_color_hex(0xFF0000), LV_STATE_DEFAULT);
  187. lv_obj_t *lbl = lv_label_create(scr);
  188. lv_label_set_text(lbl, "LVGL Test");
  189. lv_obj_center(lbl);
  190. #endif
  191. while (1)
  192. {
  193. /* 周期驱动 LVGL(渲染/动画/输入),配合 LV_DISP_DEF_REFR_PERIOD 30ms */
  194. lv_timer_handler();
  195. #if ENABLE_TEST1
  196. Test1_Tick(&test1);
  197. #endif
  198. vTaskDelay(pdMS_TO_TICKS(5));
  199. }
  200. }
  201. /**
  202. * @brief 处理测试的任务
  203. *
  204. * @param argument
  205. */
  206. void Task_Test(void *argument)
  207. {
  208. (void)argument;
  209. printf("Test task running...\r\n");
  210. while (1)
  211. {
  212. printf("TEST\r\n");
  213. /* TODO: 测试 */
  214. vTaskDelay(pdMS_TO_TICKS(5000));
  215. }
  216. }