| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- #include "App_freertosTask.h"
- #include <stdio.h>
- #include "lvgl.h"
- #include "lv_port_disp.h"
- #define TASK_CPLD_PRIORITY 4
- #define TASK_CPLD_STACK_SIZE 512
- #define TASK_LVGL_PRIORITY 3
- #define TASK_LVGL_STACK_SIZE 2048
- #define TASK_TEST_PRIORITY 1
- #define TASK_TEST_STACK_SIZE 512
- /* 测试1(顺序测试)条件编译开关:1=运行;0=静态红底+文字页 */
- #define ENABLE_TEST1 1
- #if ENABLE_TEST1
- /**
- * @brief 测试1 状态机
- *
- * 顺序:10→1 倒数(局部文字刷新)→ 背景三原色切换(各 1s)→ 3x3 九宫格 9 色
- */
- typedef enum
- {
- TEST1_PHASE_COUNTDOWN, /* 10→1 倒数 */
- TEST1_PHASE_BG, /* 三原色背景切换 */
- TEST1_PHASE_GRID /* 三原色九宫格 */
- } test1_phase_t;
- typedef struct
- {
- test1_phase_t phase;
- uint32_t last_ms;
- int32_t count; /* 倒数值 10..1 */
- uint32_t bg_idx; /* 当前背景色索引 */
- lv_obj_t * scr;
- lv_obj_t * lbl;
- } test1_state_t;
- static const uint32_t test1_bg_hex[3] = {
- 0xFF0000, /* 红 */
- 0x00FF00, /* 绿 */
- 0x0000FF /* 蓝 */
- };
- static const uint32_t test1_grid_hex[9] = {
- 0xFF0000, /* 红 */
- 0x00FF00, /* 绿 */
- 0x0000FF, /* 蓝 */
- 0xFFFF00, /* 黄 */
- 0x00FFFF, /* 青 */
- 0xFF00FF, /* 紫 */
- 0xFF8000, /* 橙 */
- 0x80FF00, /* 草绿 */
- 0x8000FF /* 品红 */
- };
- /* 九宫格列宽:800 = 266 + 267 + 267;行高:480 = 160 * 3 */
- static const lv_coord_t test1_grid_col_w[3] = { 266, 267, 267 };
- static const lv_coord_t test1_grid_col_x[3] = { 0, 266, 533 };
- static const lv_coord_t test1_grid_row_h = 160;
- static void Test1_Init(test1_state_t * st)
- {
- st->phase = TEST1_PHASE_COUNTDOWN;
- st->last_ms = lv_tick_get();
- st->count = 10;
- st->bg_idx = 0;
- st->scr = lv_scr_act();
- lv_obj_set_style_bg_color(st->scr, lv_color_hex(test1_bg_hex[0]), LV_STATE_DEFAULT);
- st->lbl = lv_label_create(st->scr);
- lv_label_set_text_fmt(st->lbl, "%d", st->count);
- lv_obj_center(st->lbl);
- }
- static void Test1_Tick(test1_state_t * st)
- {
- uint32_t now = lv_tick_get();
- if ((uint32_t)(now - st->last_ms) < 1000)
- {
- return;
- }
- st->last_ms = now;
- switch (st->phase)
- {
- case TEST1_PHASE_COUNTDOWN:
- {
- st->count--;
- if (st->count >= 1)
- {
- lv_label_set_text_fmt(st->lbl, "%d", st->count);
- }
- else
- {
- /* 倒数结束:删除文字,进入背景切换 */
- if (st->lbl != NULL)
- {
- lv_obj_del(st->lbl);
- st->lbl = NULL;
- }
- st->phase = TEST1_PHASE_BG;
- lv_obj_set_style_bg_color(st->scr, lv_color_hex(test1_bg_hex[0]), LV_STATE_DEFAULT);
- }
- break;
- }
- case TEST1_PHASE_BG:
- {
- st->bg_idx++;
- if (st->bg_idx < 3)
- {
- lv_obj_set_style_bg_color(st->scr, lv_color_hex(test1_bg_hex[st->bg_idx]), LV_STATE_DEFAULT);
- }
- else
- {
- /* 三原色切换完毕:创建 3x3 九宫格 */
- uint32_t i;
- for (i = 0; i < 9; i++)
- {
- lv_obj_t * blk = lv_obj_create(st->scr);
- lv_obj_set_size(blk, test1_grid_col_w[i % 3], test1_grid_row_h);
- lv_obj_set_pos(blk, test1_grid_col_x[i % 3], (i / 3) * test1_grid_row_h);
- lv_obj_set_style_bg_color(blk, lv_color_hex(test1_grid_hex[i]), LV_STATE_DEFAULT);
- lv_obj_set_style_border_width(blk, 0, LV_STATE_DEFAULT);
- lv_obj_set_style_radius(blk, 0, LV_STATE_DEFAULT);
- }
- st->phase = TEST1_PHASE_GRID;
- }
- break;
- }
- default:
- break;
- }
- }
- #endif /* ENABLE_TEST1 */
- TaskHandle_t xCPLDProcessTaskHandle = NULL;
- TaskHandle_t xLVGLDisplayTaskHandle = NULL;
- TaskHandle_t xTestTaskHandle = NULL;
- /**
- * @brief 创建各个任务
- *
- */
- void App_Task_Create(void)
- {
- BaseType_t ret;
- ret = xTaskCreate(Task_CPLD_Process, "cpldProcessTask", TASK_CPLD_STACK_SIZE, NULL, TASK_CPLD_PRIORITY, &xCPLDProcessTaskHandle);
- if (ret != pdPASS)
- {
- printf("[ERR] CPLD process task create failed\r\n");
- }
- ret = xTaskCreate(Task_LVGL_Display, "lvglDisplayTask", TASK_LVGL_STACK_SIZE, NULL, TASK_LVGL_PRIORITY, &xLVGLDisplayTaskHandle);
- if (ret != pdPASS)
- {
- printf("[ERR] LVGL display task create failed\r\n");
- }
- ret = xTaskCreate(Task_Test, "testTask", TASK_TEST_STACK_SIZE, NULL, TASK_TEST_PRIORITY, &xTestTaskHandle);
- if (ret != pdPASS)
- {
- printf("[ERR] Test task create failed\r\n");
- }
- }
- /**
- * @brief 处理 CPLD 通信的任务
- *
- * @param argument
- */
- void Task_CPLD_Process(void *argument)
- {
- (void)argument;
- printf("CPLD process task running...\r\n");
- while (1)
- {
- printf("CPLD\r\n");
- /* TODO: CPLD 通信处理 */
- vTaskDelay(pdMS_TO_TICKS(1000));
- }
- }
- /**
- * @brief 处理 LVGL 显示的任务
- *
- * @param argument
- */
- void Task_LVGL_Display(void *argument)
- {
- (void)argument;
- printf("LVGL display task running...\r\n");
- /* LVGL 内核初始化 */
- lv_init();
- /* 注册显示驱动:LTDC + SDRAM 静态显存 + 双全屏缓冲 */
- lv_port_disp_init();
- #if ENABLE_TEST1
- test1_state_t test1;
- Test1_Init(&test1);
- #else
- /* 验证界面:红底 + 居中文字 */
- lv_obj_t *scr = lv_scr_act();
- lv_obj_set_style_bg_color(scr, lv_color_hex(0xFF0000), LV_STATE_DEFAULT);
- lv_obj_t *lbl = lv_label_create(scr);
- lv_label_set_text(lbl, "LVGL Test");
- lv_obj_center(lbl);
- #endif
- while (1)
- {
- /* 周期驱动 LVGL(渲染/动画/输入),配合 LV_DISP_DEF_REFR_PERIOD 30ms */
- lv_timer_handler();
- #if ENABLE_TEST1
- Test1_Tick(&test1);
- #endif
- vTaskDelay(pdMS_TO_TICKS(5));
- }
- }
- /**
- * @brief 处理测试的任务
- *
- * @param argument
- */
- void Task_Test(void *argument)
- {
- (void)argument;
- printf("Test task running...\r\n");
- while (1)
- {
- printf("TEST\r\n");
- /* TODO: 测试 */
- vTaskDelay(pdMS_TO_TICKS(5000));
- }
- }
|