feat(touch): 实现多点触控显示,5 点独立轨迹和坐标
- touch.h: 添加 TP_IS_ACTIVE / TP_ACTIVE_COUNT 辅助宏 - test_touch() 重写为多点触控模式: - 5 种颜色区分不同触控点 (黄/青/紫/绿/红) - LCD 左侧 5 行实时坐标显示 - 5 条独立轨迹线 + 圆点追踪 - 手指抬起自动清除对应圆点并显示 released - UART 输出全部活跃点坐标
This commit is contained in:
+82
-40
@@ -352,7 +352,7 @@ static void concurrent_loop(void)
|
||||
|
||||
static void test_touch(void)
|
||||
{
|
||||
printf("\r\n===== Touch 测试 (中断驱动) =====\r\n");
|
||||
printf("\r\n===== Touch 测试 (多点触控) =====\r\n");
|
||||
|
||||
uint8_t ret = GT1151_Init();
|
||||
if (ret != 0)
|
||||
@@ -362,20 +362,41 @@ static void concurrent_loop(void)
|
||||
}
|
||||
printf(" Touch 初始化成功,等待触摸中断...\r\n");
|
||||
|
||||
LCD_Clear(0x0841);
|
||||
static const uint16_t TP_COLORS[5] = {
|
||||
0xFFE0, // YELLOW - point 0
|
||||
0x07FF, // CYAN - point 1
|
||||
0xF81F, // MAGENTA - point 2
|
||||
0x07E0, // GREEN - point 3
|
||||
0xF800 // RED - point 4
|
||||
};
|
||||
|
||||
const uint16_t BG_COLOR = 0x0841;
|
||||
|
||||
LCD_Clear(BG_COLOR);
|
||||
POINT_COLOR = CYAN;
|
||||
LCD_ShowString(10, 10, 460, 24, 16, 0, (uint8_t *)"=== Touch Test (IRQ) ===");
|
||||
LCD_ShowString(10, 10, 460, 24, 16, 0, (uint8_t *)"=== Multi-Touch (5pt) ===");
|
||||
POINT_COLOR = WHITE;
|
||||
LCD_ShowString(10, 35, 460, 24, 16, 0, (uint8_t *)"Touch screen now");
|
||||
LCD_ShowString(10, 55, 300, 24, 16, 0, (uint8_t *)"IRQ driven, watch UART");
|
||||
LCD_ShowString(10, 75, 200, 24, 16, 0, (uint8_t *)"X:0 Y:0");
|
||||
LCD_ShowString(10, 30, 460, 24, 16, 0, (uint8_t *)"Touch with up to 5 fingers");
|
||||
|
||||
for (uint8_t i = 0; i < CT_MAX_TOUCH; i++)
|
||||
{
|
||||
POINT_COLOR = TP_COLORS[i];
|
||||
char lbl[8];
|
||||
sprintf(lbl, "P%d: --", i);
|
||||
LCD_ShowString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)lbl);
|
||||
}
|
||||
|
||||
uint16_t last_x[5];
|
||||
uint16_t last_y[5];
|
||||
bool was_active[5];
|
||||
for (uint8_t i = 0; i < CT_MAX_TOUCH; i++)
|
||||
{
|
||||
last_x[i] = 0xFFFF;
|
||||
last_y[i] = 0xFFFF;
|
||||
was_active[i] = false;
|
||||
}
|
||||
|
||||
uint16_t last_x = 0xFFFF;
|
||||
uint16_t last_y = 0xFFFF;
|
||||
uint32_t idle_counter = 0;
|
||||
bool was_touching = false;
|
||||
uint32_t iter = 0;
|
||||
|
||||
g_touch_irq_flag = 0;
|
||||
|
||||
while (1)
|
||||
@@ -387,46 +408,67 @@ static void concurrent_loop(void)
|
||||
|
||||
GT1151_Scan(0);
|
||||
|
||||
if (tp_dev.sta & TP_PRES_DOWN)
|
||||
idle_counter = 0;
|
||||
|
||||
uint8_t active_cnt = TP_ACTIVE_COUNT(tp_dev.sta);
|
||||
uint8_t mask = tp_dev.sta & 0x1F;
|
||||
|
||||
for (uint8_t i = 0; i < CT_MAX_TOUCH; i++)
|
||||
{
|
||||
idle_counter = 0;
|
||||
uint16_t x = tp_dev.x[0];
|
||||
uint16_t y = tp_dev.y[0];
|
||||
bool active = (mask & (1 << i)) != 0;
|
||||
|
||||
printf("[TOUCH] iter=%lu sta=0x%02X x=%u y=%u\r\n",
|
||||
(unsigned long)iter, tp_dev.sta, x, y);
|
||||
|
||||
if (x < lcddev.width && y < lcddev.height)
|
||||
if (active)
|
||||
{
|
||||
POINT_COLOR = BLACK;
|
||||
LCD_FillRectangle(10, 75, 200, 95);
|
||||
POINT_COLOR = GREEN;
|
||||
char buf[24];
|
||||
sprintf(buf, "X:%-3d Y:%-3d", x, y);
|
||||
LCD_ShowString(10, 75, 200, 24, 16, 0, (uint8_t *)buf);
|
||||
uint16_t x = tp_dev.x[i];
|
||||
uint16_t y = tp_dev.y[i];
|
||||
|
||||
if (was_touching && last_x != 0xFFFF)
|
||||
if (x < lcddev.width && y < lcddev.height)
|
||||
{
|
||||
POINT_COLOR = MAGENTA;
|
||||
LCD_DrawLine(last_x, last_y, x, y);
|
||||
POINT_COLOR = TP_COLORS[i];
|
||||
char buf[20];
|
||||
sprintf(buf, "P%d: X:%-3d Y:%-3d", i, x, y);
|
||||
LCD_FillRectangle(10, 52 + i * 18, 200, 52 + i * 18 + 17);
|
||||
LCD_ShowString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)buf);
|
||||
|
||||
if (was_active[i] && last_x[i] != 0xFFFF)
|
||||
{
|
||||
POINT_COLOR = TP_COLORS[i];
|
||||
LCD_DrawLine(last_x[i], last_y[i], x, y);
|
||||
}
|
||||
|
||||
POINT_COLOR = TP_COLORS[i];
|
||||
LCD_FillCircle(x, y, 5);
|
||||
|
||||
last_x[i] = x;
|
||||
last_y[i] = y;
|
||||
was_active[i] = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (was_active[i])
|
||||
{
|
||||
POINT_COLOR = BG_COLOR;
|
||||
LCD_FillCircle(last_x[i], last_y[i], 6);
|
||||
|
||||
POINT_COLOR = YELLOW;
|
||||
LCD_FillCircle(x, y, 4);
|
||||
POINT_COLOR = TP_COLORS[i];
|
||||
LCD_FillRectangle(10, 52 + i * 18, 200, 52 + i * 18 + 17);
|
||||
LCD_ShowString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)"released");
|
||||
|
||||
last_x = x;
|
||||
last_y = y;
|
||||
was_touching = true;
|
||||
last_x[i] = 0xFFFF;
|
||||
last_y[i] = 0xFFFF;
|
||||
was_active[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
was_touching = false;
|
||||
last_x = 0xFFFF;
|
||||
last_y = 0xFFFF;
|
||||
}
|
||||
|
||||
iter++;
|
||||
printf("[TOUCH] cnt=%d sta=0x%02X", active_cnt, tp_dev.sta);
|
||||
for (uint8_t i = 0; i < CT_MAX_TOUCH; i++)
|
||||
{
|
||||
if (mask & (1 << i))
|
||||
printf(" P%d(%u,%u)", i, tp_dev.x[i], tp_dev.y[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user