STM32H745I-DISCO板载4.3寸电容触摸屏。
可以做一个触摸画板。
一、添加必要的BSP文件
添加电容触摸驱动文件,起作用的应该是FT5336
添加BSP触摸文件
二、实现一个画线的函数
利用Bresenham的直线算法,并借助BSP中的换点函数实现一个画任意线段的函数:
// 绘制直线的函数
void DrawLine(uint32_t Instance, int x0, int y0, int x1, int y1, uint32_t Color) {
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = dx + dy, e2; // error value e_xy
while (1) { // loop
BSP_LCD_WritePixel(Instance, x0, y0, Color); // plot x0, y0
if (x0 == x1 && y0 == y1) break;
e2 = 2 * err;
if (e2 >= dy) { err += dy; x0 += sx; } // e_xy+e_x > 0
if (e2 <= dx) { err += dx; y0 += sy; } // e_xy+e_y < 0
}
}
三、实现触摸画板
static void Touchscreen_demo1(void)
{
uint16_t x1, y1;
int32_t probeStatus;
uint32_t ts_status = BSP_ERROR_NONE;
uint32_t x_size, y_size;
uint32_t Instance = 0;
BSP_LCD_GetXSize(0, &x_size);
BSP_LCD_GetYSize(0, &y_size);
hTS->Width = x_size;
hTS->Height = y_size;
UTIL_LCD_Clear(UTIL_LCD_COLOR_BLACK);
probeStatus = GT911_Probe(Instance);
if (probeStatus == BSP_ERROR_NONE)
{
printf("GT911_Probe ok\r\r");
hTS->Orientation = TS_SWAP_NONE;
}
else
{
probeStatus = FT5336_Probe(Instance);
if (probeStatus == BSP_ERROR_NONE)
{
printf("FT5336_Probe ok\r\r");
hTS->Orientation = TS_SWAP_XY;
}
}
hTS->Accuracy = 5;
/* Touchscreen initialization */
ts_status = BSP_TS_Init(0, hTS);
if(ts_status == BSP_ERROR_NONE)
{
printf("BSP_TS_Init ok\r\r");
while(1)
{
/* Check in polling mode in touch screen the touch status and coordinates */
/* of touches if touch occurred */
ts_status = BSP_TS_GetState(0, &TS_State);
if(TS_State.TouchDetected)
{
printf("TS_State.TouchDetected\r\n");
x1 = TS_State.TouchX;
y1 = TS_State.TouchY;
printf("x1=%d,y1=%d\r\n",x1,y1);
if(ts_pen_up>0&&ts_pen_up<5)
{
DrawLine(0,ts_old_x,ts_old_y,x1,y1,LCD_COLOR_ARGB8888_BLUE);
}
ts_old_x=x1;
ts_old_y=y1;
ts_pen_up=1;
}
HAL_Delay(20);
if(ts_pen_up<128&&ts_pen_up>0) ts_pen_up++;
}
}
}
因为大量使用BSP函数,触摸屏初始化和相关函数直接调用就好。
四、运行效果
|