App_freertosTask.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "App_freertosTask.h"
  2. #include <stdio.h>
  3. #include "lvgl.h"
  4. #include "lv_port_disp.h"
  5. #include "lv_port_indev.h"
  6. #include "Int_gt1151.h"
  7. #include "Int_lcd.h"
  8. #define TASK_CPLD_PRIORITY 4
  9. #define TASK_CPLD_STACK_SIZE 512
  10. #define TASK_LVGL_PRIORITY 3
  11. #define TASK_LVGL_STACK_SIZE 2048
  12. #define TASK_TEST_PRIORITY 1
  13. #define TASK_TEST_STACK_SIZE 512
  14. /* 测试1(顺序测试)条件编译开关:1=运行;0=静态红底+文字页 */
  15. #define ENABLE_TEST1 1
  16. /* 触摸验证(可点击按钮)条件编译开关:1=运行触摸验证界面;0=不运行 */
  17. #define ENABLE_TOUCH_TEST 1
  18. #if ENABLE_TEST1
  19. /**
  20. * @brief 测试1 状态机
  21. *
  22. * 顺序:10s 倒数(局部文字刷新)→ 背景三原色切换(1s)→ 3x3 九宫格
  23. */
  24. typedef enum
  25. {
  26. TEST1_PHASE_COUNTDOWN, /* 10s 倒数 */
  27. TEST1_PHASE_BG, /* 三原色背景切换 */
  28. TEST1_PHASE_GRID /* 三原色九宫格 */
  29. } test1_phase_t;
  30. typedef struct
  31. {
  32. test1_phase_t phase;
  33. uint32_t last_ms;
  34. int32_t count; /* 倒数 10..1 */
  35. uint32_t bg_idx; /* 当前背景色索引 */
  36. lv_obj_t * scr;
  37. lv_obj_t * lbl;
  38. } test1_state_t;
  39. static const uint32_t test1_bg_hex[3] = {
  40. 0xFF0000, /* 红 */
  41. 0x00FF00, /* 绿 */
  42. 0x0000FF /* 蓝 */
  43. };
  44. static const uint32_t test1_grid_hex[9] = {
  45. 0xFF0000, /* 红 */
  46. 0x00FF00, /* 绿 */
  47. 0x0000FF, /* 蓝 */
  48. 0xFFFF00, /* 黄 */
  49. 0x00FFFF, /* 青 */
  50. 0xFF00FF, /* 品红 */
  51. 0xFF8000, /* 橙 */
  52. 0x80FF00, /* 草绿 */
  53. 0x8000FF /* 品蓝 */
  54. };
  55. /* 九宫格列宽:800 = 266 + 267 + 267;行高:480 = 160 * 3 */
  56. static const lv_coord_t test1_grid_col_w[3] = { 266, 267, 267 };
  57. static const lv_coord_t test1_grid_col_x[3] = { 0, 266, 533 };
  58. static const lv_coord_t test1_grid_row_h = 160;
  59. static void Test1_Init(test1_state_t * st)
  60. {
  61. st->phase = TEST1_PHASE_COUNTDOWN;
  62. st->last_ms = lv_tick_get();
  63. st->count = 10;
  64. st->bg_idx = 0;
  65. st->scr = lv_scr_act();
  66. lv_obj_set_style_bg_color(st->scr, lv_color_hex(test1_bg_hex[0]), LV_STATE_DEFAULT);
  67. st->lbl = lv_label_create(st->scr);
  68. lv_label_set_text_fmt(st->lbl, "%d", st->count);
  69. lv_obj_center(st->lbl);
  70. }
  71. static void Test1_Tick(test1_state_t * st)
  72. {
  73. uint32_t now = lv_tick_get();
  74. if ((uint32_t)(now - st->last_ms) < 1000)
  75. {
  76. return;
  77. }
  78. st->last_ms = now;
  79. switch (st->phase)
  80. {
  81. case TEST1_PHASE_COUNTDOWN:
  82. {
  83. st->count--;
  84. if (st->count >= 1)
  85. {
  86. lv_label_set_text_fmt(st->lbl, "%d", st->count);
  87. }
  88. else
  89. {
  90. /* 倒数结束:删除文字,进入背景切换 */
  91. if (st->lbl != NULL)
  92. {
  93. lv_obj_del(st->lbl);
  94. st->lbl = NULL;
  95. }
  96. st->phase = TEST1_PHASE_BG;
  97. lv_obj_set_style_bg_color(st->scr, lv_color_hex(test1_bg_hex[0]), LV_STATE_DEFAULT);
  98. }
  99. break;
  100. }
  101. case TEST1_PHASE_BG:
  102. {
  103. st->bg_idx++;
  104. if (st->bg_idx < 3)
  105. {
  106. lv_obj_set_style_bg_color(st->scr, lv_color_hex(test1_bg_hex[st->bg_idx]), LV_STATE_DEFAULT);
  107. }
  108. else
  109. {
  110. /* 三原色切换完毕:创建 3x3 九宫格 */
  111. uint32_t i;
  112. for (i = 0; i < 9; i++)
  113. {
  114. lv_obj_t * blk = lv_obj_create(st->scr);
  115. lv_obj_set_size(blk, test1_grid_col_w[i % 3], test1_grid_row_h);
  116. lv_obj_set_pos(blk, test1_grid_col_x[i % 3], (i / 3) * test1_grid_row_h);
  117. lv_obj_set_style_bg_color(blk, lv_color_hex(test1_grid_hex[i]), LV_STATE_DEFAULT);
  118. lv_obj_set_style_border_width(blk, 0, LV_STATE_DEFAULT);
  119. lv_obj_set_style_radius(blk, 0, LV_STATE_DEFAULT);
  120. }
  121. st->phase = TEST1_PHASE_GRID;
  122. }
  123. break;
  124. }
  125. default:
  126. break;
  127. }
  128. }
  129. #endif /* ENABLE_TEST1 */
  130. #if ENABLE_TOUCH_TEST
  131. /**
  132. * @brief 触摸画板: 配合电容触摸, 在屏幕上用手指绘制线条
  133. *
  134. * 实现方式:
  135. * - 全屏 lv_canvas (RGB565), 白底黑笔。
  136. * - 画布数据缓冲放在 SDRAM 第三块 (非缓存区, 无缓存一致性问题)。
  137. * - 主循环每帧轮询按压状态: 按下则用 Bresenham 从上一触点连线到当前触点;
  138. * 松开则重置上一触点, 下次按下从新起点开始 (不连线)。
  139. * - 右上角提供 "CLEAR" 清屏按钮。
  140. */
  141. /* 画板常量 */
  142. #define PAD_BUF_ADDR (INT_LCD_FRAME_BUF_ADDR + 2 * INT_LCD_FRAME_BUF_SIZE) /* SDRAM 3rd block */
  143. #define PAD_WIDTH INT_LCD_WIDTH
  144. #define PAD_HEIGHT INT_LCD_HEIGHT
  145. /* 画板句柄 */
  146. static lv_obj_t * s_pad_canvas = NULL;
  147. static lv_coord_t s_pad_last_x;
  148. static lv_coord_t s_pad_last_y;
  149. static uint8_t s_pad_drawing; /* 是否正在连线中 */
  150. /**
  151. * @brief Bresenham 画线: 在 canvas 上从 (x0,y0) 到 (x1,y1) 逐点涂色
  152. */
  153. static void Pad_DrawLine(lv_coord_t x0, lv_coord_t y0, lv_coord_t x1, lv_coord_t y1)
  154. {
  155. int dx = (x1 > x0) ? (x1 - x0) : (x0 - x1);
  156. int dy = (y1 > y0) ? (y1 - y0) : (y0 - y1);
  157. int sx = (x1 > x0) ? 1 : -1;
  158. int sy = (y1 > y0) ? 1 : -1;
  159. int err = dx - dy;
  160. lv_color_t c = lv_color_black();
  161. while (1)
  162. {
  163. if (x0 >= 0 && x0 < PAD_WIDTH && y0 >= 0 && y0 < PAD_HEIGHT)
  164. {
  165. lv_canvas_set_px(s_pad_canvas, x0, y0, c);
  166. }
  167. if (x0 == x1 && y0 == y1) break;
  168. int e2 = 2 * err;
  169. if (e2 > -dy) { err -= dy; x0 += sx; }
  170. if (e2 < dx) { err += dx; y0 += sy; }
  171. }
  172. }
  173. /**
  174. * @brief 清屏按钮事件回调
  175. */
  176. static void Pad_ClearCb(lv_event_t * e)
  177. {
  178. if (lv_event_get_code(e) == LV_EVENT_CLICKED && s_pad_canvas != NULL)
  179. {
  180. lv_canvas_fill_bg(s_pad_canvas, lv_color_white(), LV_OPA_COVER);
  181. lv_obj_invalidate(s_pad_canvas);
  182. s_pad_drawing = 0;
  183. }
  184. }
  185. /**
  186. * @brief 画板主循环动作: 每帧调用, 根据按压状态绘制
  187. */
  188. static void TouchTest_Tick(void)
  189. {
  190. uint16_t px = 0;
  191. uint16_t py = 0;
  192. if (s_pad_canvas == NULL) return;
  193. if (Int_gt1151_pressed())
  194. {
  195. Int_gt1151_get_point(&px, &py);
  196. if (s_pad_drawing)
  197. {
  198. /* 上一触点 -> 当前触点连线 */
  199. Pad_DrawLine(s_pad_last_x, s_pad_last_y, (lv_coord_t)px, (lv_coord_t)py);
  200. }
  201. else
  202. {
  203. /* 刚按下: 画一个起始点 */
  204. if (px < PAD_WIDTH && py < PAD_HEIGHT)
  205. {
  206. lv_canvas_set_px(s_pad_canvas, (lv_coord_t)px, (lv_coord_t)py, lv_color_black());
  207. }
  208. s_pad_drawing = 1;
  209. }
  210. s_pad_last_x = (lv_coord_t)px;
  211. s_pad_last_y = (lv_coord_t)py;
  212. /* 通知 LVGL 重绘画布, 将新画笔迹刷到显示 */
  213. lv_obj_invalidate(s_pad_canvas);
  214. }
  215. else
  216. {
  217. s_pad_drawing = 0; /* 松开, 下次按下重新起笔 */
  218. }
  219. }
  220. /**
  221. * @brief 初始化触摸画板
  222. */
  223. static void TouchTest_Init(lv_obj_t * scr)
  224. {
  225. lv_obj_set_style_bg_color(scr, lv_color_hex(0x000000), LV_STATE_DEFAULT);
  226. /* 创建全屏画布 */
  227. s_pad_canvas = lv_canvas_create(scr);
  228. /* 画布缓冲使用 SDRAM 固定地址 */
  229. lv_canvas_set_buffer(s_pad_canvas, (void *)PAD_BUF_ADDR, PAD_WIDTH, PAD_HEIGHT, LV_IMG_CF_TRUE_COLOR);
  230. lv_obj_center(s_pad_canvas);
  231. lv_canvas_fill_bg(s_pad_canvas, lv_color_white(), LV_OPA_COVER);
  232. s_pad_drawing = 0;
  233. s_pad_last_x = 0;
  234. s_pad_last_y = 0;
  235. /* 右上角清屏按钮 */
  236. lv_obj_t * btn = lv_btn_create(scr);
  237. lv_obj_set_size(btn, 96, 40);
  238. lv_obj_align(btn, LV_ALIGN_TOP_RIGHT, -10, 10);
  239. lv_obj_add_event_cb(btn, Pad_ClearCb, LV_EVENT_CLICKED, NULL);
  240. lv_obj_t * lbl = lv_label_create(btn);
  241. lv_label_set_text(lbl, "CLEAR");
  242. lv_obj_center(lbl);
  243. }
  244. #endif /* ENABLE_TOUCH_TEST */
  245. TaskHandle_t xCPLDProcessTaskHandle = NULL;
  246. TaskHandle_t xLVGLDisplayTaskHandle = NULL;
  247. TaskHandle_t xTestTaskHandle = NULL;
  248. /**
  249. * @brief 创建各个任务
  250. *
  251. */
  252. void App_Task_Create(void)
  253. {
  254. BaseType_t ret;
  255. ret = xTaskCreate(Task_CPLD_Process, "cpldProcessTask", TASK_CPLD_STACK_SIZE, NULL, TASK_CPLD_PRIORITY, &xCPLDProcessTaskHandle);
  256. if (ret != pdPASS)
  257. {
  258. printf("[ERR] CPLD process task create failed\r\n");
  259. }
  260. ret = xTaskCreate(Task_LVGL_Display, "lvglDisplayTask", TASK_LVGL_STACK_SIZE, NULL, TASK_LVGL_PRIORITY, &xLVGLDisplayTaskHandle);
  261. if (ret != pdPASS)
  262. {
  263. printf("[ERR] LVGL display task create failed\r\n");
  264. }
  265. ret = xTaskCreate(Task_Test, "testTask", TASK_TEST_STACK_SIZE, NULL, TASK_TEST_PRIORITY, &xTestTaskHandle);
  266. if (ret != pdPASS)
  267. {
  268. printf("[ERR] Test task create failed\r\n");
  269. }
  270. }
  271. /**
  272. * @brief 处理 CPLD 通信的任务
  273. *
  274. * @param argument
  275. */
  276. void Task_CPLD_Process(void *argument)
  277. {
  278. (void)argument;
  279. printf("CPLD process task running...\r\n");
  280. while (1)
  281. {
  282. /* TODO: CPLD 通信处理 */
  283. vTaskDelay(pdMS_TO_TICKS(1000));
  284. }
  285. }
  286. /**
  287. * @brief 处理 LVGL 显示的任务
  288. *
  289. * @param argument
  290. */
  291. void Task_LVGL_Display(void *argument)
  292. {
  293. (void)argument;
  294. printf("LVGL display task running...\r\n");
  295. /* LVGL 内核初始化 */
  296. lv_init();
  297. /* 注册显示驱动:LTDC + SDRAM 静态显存 + 双全屏缓冲 */
  298. lv_port_disp_init();
  299. /* 初始化 GT1151Q 电容触摸芯片 */
  300. if (Int_gt1151_init() == 0)
  301. {
  302. printf("GT1151 Init OK\r\n");
  303. /* 注册触摸输入设备 (注意: 需在 lv_init 之后、主循环 lv_timer_handler 之前) */
  304. lv_port_indev_init();
  305. }
  306. else
  307. {
  308. printf("[ERR] GT1151 Init failed\r\n");
  309. }
  310. #if ENABLE_TOUCH_TEST
  311. /* 触摸验证界面:可点击按钮 + 实时坐标显示 */
  312. {
  313. lv_obj_t *scr = lv_scr_act();
  314. TouchTest_Init(scr);
  315. }
  316. #elif ENABLE_TEST1
  317. test1_state_t test1;
  318. Test1_Init(&test1);
  319. #else
  320. /* 验证界面:红底 + 居中文字 */
  321. lv_obj_t *scr = lv_scr_act();
  322. lv_obj_set_style_bg_color(scr, lv_color_hex(0xFF0000), LV_STATE_DEFAULT);
  323. lv_obj_t *lbl = lv_label_create(scr);
  324. lv_label_set_text(lbl, "LVGL Test");
  325. lv_obj_center(lbl);
  326. #endif
  327. while (1)
  328. {
  329. /* 周期驱动 LVGL(渲染/动画/输入),配合 LV_DISP_DEF_REFR_PERIOD 30ms */
  330. lv_timer_handler();
  331. #if ENABLE_TOUCH_TEST
  332. TouchTest_Tick();
  333. #elif ENABLE_TEST1
  334. Test1_Tick(&test1);
  335. #endif
  336. vTaskDelay(pdMS_TO_TICKS(5));
  337. }
  338. }
  339. /**
  340. * @brief 处理测试的任务
  341. *
  342. * @param argument
  343. */
  344. void Task_Test(void *argument)
  345. {
  346. (void)argument;
  347. printf("Test task running...\r\n");
  348. while (1)
  349. {
  350. /* TODO: 测试 */
  351. vTaskDelay(pdMS_TO_TICKS(5000));
  352. }
  353. }