[STM32H7] 【STM32H745I-DISCO试用】9、触摸画板

[复制链接]
695|0
 楼主| sujingliang 发表于 2025-2-7 10:27 | 显示全部楼层 |阅读模式
STM32H745I-DISCO板载4.3寸电容触摸屏。
可以做一个触摸画板。


一、添加必要的BSP文件
添加电容触摸驱动文件,起作用的应该是FT5336
61.png
添加BSP触摸文件
62.png

二、实现一个画线的函数
利用Bresenham的直线算法,并借助BSP中的换点函数实现一个画任意线段的函数:
  1. // 绘制直线的函数
  2. void DrawLine(uint32_t Instance, int x0, int y0, int x1, int y1, uint32_t Color) {
  3.     int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
  4.     int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
  5.     int err = dx + dy, e2; // error value e_xy

  6.     while (1) {  // loop
  7.         BSP_LCD_WritePixel(Instance, x0, y0, Color); // plot x0, y0
  8.         if (x0 == x1 && y0 == y1) break;
  9.         e2 = 2 * err;
  10.         if (e2 >= dy) { err += dy; x0 += sx; } // e_xy+e_x > 0
  11.         if (e2 <= dx) { err += dx; y0 += sy; } // e_xy+e_y < 0
  12.     }
  13. }

三、实现触摸画板

  1. static void Touchscreen_demo1(void)
  2. {
  3.         uint16_t x1, y1;
  4.         int32_t probeStatus;
  5.         uint32_t ts_status = BSP_ERROR_NONE;
  6.         uint32_t x_size, y_size;
  7.         uint32_t Instance = 0;
  8.        
  9.         BSP_LCD_GetXSize(0, &x_size);
  10.   BSP_LCD_GetYSize(0, &y_size);
  11.        
  12.         hTS->Width = x_size;
  13.   hTS->Height = y_size;
  14.        
  15.         UTIL_LCD_Clear(UTIL_LCD_COLOR_BLACK);
  16.        
  17.         probeStatus = GT911_Probe(Instance);
  18.   if (probeStatus == BSP_ERROR_NONE)
  19.   {
  20.                 printf("GT911_Probe ok\r\r");
  21.     hTS->Orientation = TS_SWAP_NONE;
  22.   }
  23.   else
  24.   {
  25.     probeStatus = FT5336_Probe(Instance);
  26.     if (probeStatus == BSP_ERROR_NONE)
  27.     {
  28.                         printf("FT5336_Probe ok\r\r");
  29.       hTS->Orientation = TS_SWAP_XY;
  30.     }
  31.   }
  32.   hTS->Accuracy = 5;
  33.        
  34.         /* Touchscreen initialization */
  35.   ts_status = BSP_TS_Init(0, hTS);
  36.        
  37.         if(ts_status == BSP_ERROR_NONE)
  38.   {
  39.                 printf("BSP_TS_Init ok\r\r");
  40.                 while(1)
  41.                 {
  42.                         /* Check in polling mode in touch screen the touch status and coordinates */
  43.       /* of touches if touch occurred                                           */
  44.       ts_status = BSP_TS_GetState(0, &TS_State);
  45.       if(TS_State.TouchDetected)
  46.       {
  47.                                
  48.                                 printf("TS_State.TouchDetected\r\n");
  49.                                 x1 = TS_State.TouchX;
  50.         y1 = TS_State.TouchY;
  51.                                 printf("x1=%d,y1=%d\r\n",x1,y1);
  52.                                 if(ts_pen_up>0&&ts_pen_up<5)
  53.                                 {
  54.                                         DrawLine(0,ts_old_x,ts_old_y,x1,y1,LCD_COLOR_ARGB8888_BLUE);
  55.                                        
  56.                                 }
  57.                                 ts_old_x=x1;
  58.                                 ts_old_y=y1;
  59.                                 ts_pen_up=1;
  60.                         }
  61.                         HAL_Delay(20);
  62.                         if(ts_pen_up<128&&ts_pen_up>0) ts_pen_up++;

  63.                 }
  64.         }
  65. }
因为大量使用BSP函数,触摸屏初始化和相关函数直接调用就好。

四、运行效果
tutieshi_480x272_7s.gif

您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

147

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部