| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- #include "App_freertosTask.h"
- #include <stdio.h>
- #include "lvgl.h"
- #include "lv_port_disp.h"
- #include "lv_port_indev.h"
- #include "Int_gt1151.h"
- #include "Int_lcd.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
- /* 触摸验证(可点击按钮)条件编译开关:1=运行触摸验证界面;0=不运行 */
- #define ENABLE_TOUCH_TEST 1
- #if ENABLE_TEST1
- /**
- * @brief 测试1 状态机
- *
- * 顺序:10s 倒数(局部文字刷新)→ 背景三原色切换(1s)→ 3x3 九宫格
- */
- typedef enum
- {
- TEST1_PHASE_COUNTDOWN, /* 10s 倒数 */
- 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 */
- #if ENABLE_TOUCH_TEST
- /**
- * @brief 触摸画板: 配合电容触摸, 在屏幕上用手指绘制线条
- *
- * 实现方式:
- * - 全屏 lv_canvas (RGB565), 白底黑笔。
- * - 画布数据缓冲放在 SDRAM 第三块 (非缓存区, 无缓存一致性问题)。
- * - 主循环每帧轮询按压状态: 按下则用 Bresenham 从上一触点连线到当前触点;
- * 松开则重置上一触点, 下次按下从新起点开始 (不连线)。
- * - 右上角提供 "CLEAR" 清屏按钮。
- */
- /* 画板常量 */
- #define PAD_BUF_ADDR (INT_LCD_FRAME_BUF_ADDR + 2 * INT_LCD_FRAME_BUF_SIZE) /* SDRAM 3rd block */
- #define PAD_WIDTH INT_LCD_WIDTH
- #define PAD_HEIGHT INT_LCD_HEIGHT
- /* 画板句柄 */
- static lv_obj_t * s_pad_canvas = NULL;
- static lv_coord_t s_pad_last_x;
- static lv_coord_t s_pad_last_y;
- static uint8_t s_pad_drawing; /* 是否正在连线中 */
- /**
- * @brief Bresenham 画线: 在 canvas 上从 (x0,y0) 到 (x1,y1) 逐点涂色
- */
- static void Pad_DrawLine(lv_coord_t x0, lv_coord_t y0, lv_coord_t x1, lv_coord_t y1)
- {
- int dx = (x1 > x0) ? (x1 - x0) : (x0 - x1);
- int dy = (y1 > y0) ? (y1 - y0) : (y0 - y1);
- int sx = (x1 > x0) ? 1 : -1;
- int sy = (y1 > y0) ? 1 : -1;
- int err = dx - dy;
- lv_color_t c = lv_color_black();
- while (1)
- {
- if (x0 >= 0 && x0 < PAD_WIDTH && y0 >= 0 && y0 < PAD_HEIGHT)
- {
- lv_canvas_set_px(s_pad_canvas, x0, y0, c);
- }
- if (x0 == x1 && y0 == y1) break;
- int e2 = 2 * err;
- if (e2 > -dy) { err -= dy; x0 += sx; }
- if (e2 < dx) { err += dx; y0 += sy; }
- }
- }
- /**
- * @brief 清屏按钮事件回调
- */
- static void Pad_ClearCb(lv_event_t * e)
- {
- if (lv_event_get_code(e) == LV_EVENT_CLICKED && s_pad_canvas != NULL)
- {
- lv_canvas_fill_bg(s_pad_canvas, lv_color_white(), LV_OPA_COVER);
- lv_obj_invalidate(s_pad_canvas);
- s_pad_drawing = 0;
- }
- }
- /**
- * @brief 画板主循环动作: 每帧调用, 根据按压状态绘制
- */
- static void TouchTest_Tick(void)
- {
- uint16_t px = 0;
- uint16_t py = 0;
- if (s_pad_canvas == NULL) return;
- if (Int_gt1151_pressed())
- {
- Int_gt1151_get_point(&px, &py);
- if (s_pad_drawing)
- {
- /* 上一触点 -> 当前触点连线 */
- Pad_DrawLine(s_pad_last_x, s_pad_last_y, (lv_coord_t)px, (lv_coord_t)py);
- }
- else
- {
- /* 刚按下: 画一个起始点 */
- if (px < PAD_WIDTH && py < PAD_HEIGHT)
- {
- lv_canvas_set_px(s_pad_canvas, (lv_coord_t)px, (lv_coord_t)py, lv_color_black());
- }
- s_pad_drawing = 1;
- }
- s_pad_last_x = (lv_coord_t)px;
- s_pad_last_y = (lv_coord_t)py;
- /* 通知 LVGL 重绘画布, 将新画笔迹刷到显示 */
- lv_obj_invalidate(s_pad_canvas);
- }
- else
- {
- s_pad_drawing = 0; /* 松开, 下次按下重新起笔 */
- }
- }
- /**
- * @brief 初始化触摸画板
- */
- static void TouchTest_Init(lv_obj_t * scr)
- {
- lv_obj_set_style_bg_color(scr, lv_color_hex(0x000000), LV_STATE_DEFAULT);
- /* 创建全屏画布 */
- s_pad_canvas = lv_canvas_create(scr);
- /* 画布缓冲使用 SDRAM 固定地址 */
- lv_canvas_set_buffer(s_pad_canvas, (void *)PAD_BUF_ADDR, PAD_WIDTH, PAD_HEIGHT, LV_IMG_CF_TRUE_COLOR);
- lv_obj_center(s_pad_canvas);
- lv_canvas_fill_bg(s_pad_canvas, lv_color_white(), LV_OPA_COVER);
- s_pad_drawing = 0;
- s_pad_last_x = 0;
- s_pad_last_y = 0;
- /* 右上角清屏按钮 */
- lv_obj_t * btn = lv_btn_create(scr);
- lv_obj_set_size(btn, 96, 40);
- lv_obj_align(btn, LV_ALIGN_TOP_RIGHT, -10, 10);
- lv_obj_add_event_cb(btn, Pad_ClearCb, LV_EVENT_CLICKED, NULL);
- lv_obj_t * lbl = lv_label_create(btn);
- lv_label_set_text(lbl, "CLEAR");
- lv_obj_center(lbl);
- }
- #endif /* ENABLE_TOUCH_TEST */
- 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)
- {
- /* 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();
- /* 初始化 GT1151Q 电容触摸芯片 */
- if (Int_gt1151_init() == 0)
- {
- printf("GT1151 Init OK\r\n");
- /* 注册触摸输入设备 (注意: 需在 lv_init 之后、主循环 lv_timer_handler 之前) */
- lv_port_indev_init();
- }
- else
- {
- printf("[ERR] GT1151 Init failed\r\n");
- }
- #if ENABLE_TOUCH_TEST
- /* 触摸验证界面:可点击按钮 + 实时坐标显示 */
- {
- lv_obj_t *scr = lv_scr_act();
- TouchTest_Init(scr);
- }
- #elif 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_TOUCH_TEST
- TouchTest_Tick();
- #elif 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)
- {
- /* TODO: 测试 */
- vTaskDelay(pdMS_TO_TICKS(5000));
- }
- }
|