Skip to content

Commit b3c71a6

Browse files
LinuxMint-UserRbb666
authored andcommitted
Fix: fix xpt2046_touch_readpoint type error
Fix & Improve: fix touch event bug with LVGL Fix touch event bug with LVGL. Improve the touch sliding experience of resistive screens. [ci][stm32f407] add lcd attach config CI check Fix: minor modification Fix format issue. Fix Ci attach file.
1 parent d76eca1 commit b3c71a6

File tree

3 files changed

+179
-19
lines changed

3 files changed

+179
-19
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
CONFIG_RT_USING_SPI=y
2+
CONFIG_RT_USING_SPI_ISR=y
3+
CONFIG_RT_USING_SPI_BITOPS=y
4+
CONFIG_RT_USING_SOFT_SPI=y
5+
CONFIG_RT_USING_SOFT_SPI0=y
6+
CONFIG_RT_SOFT_SPI0_SCK_PIN=1
7+
CONFIG_RT_SOFT_SPI0_MISO_PIN=2
8+
CONFIG_RT_SOFT_SPI0_MOSI_PIN=3
9+
CONFIG_RT_SOFT_SPI0_BUS_NAME="spi0"
10+
CONFIG_RT_SOFT_SPI0_TIMING_DELAY=1
11+
CONFIG_RT_USING_SOFT_SPI1=y
12+
CONFIG_RT_SOFT_SPI1_SCK_PIN=4
13+
CONFIG_RT_SOFT_SPI1_MISO_PIN=5
14+
CONFIG_RT_SOFT_SPI1_MOSI_PIN=6
15+
CONFIG_RT_SOFT_SPI1_BUS_NAME="spi1"
16+
CONFIG_RT_SOFT_SPI1_TIMING_DELAY=1
17+
CONFIG_RT_USING_TOUCH=y
18+
CONFIG_RT_TOUCH_PIN_IRQ=y
19+
CONFIG_BSP_USING_SRAM=y
20+
CONFIG_BSP_USING_ONBOARD_LCD=y
21+
CONFIG_BSP_USING_ONBOARD_LCD_TEST=y
22+
CONFIG_BSP_USING_TOUCH=y
23+
CONFIG_BSP_USING_TOUCH_RES=y
24+
CONFIG_BSP_XPT2046_CS_PIN="PC.13"
25+
CONFIG_BSP_XPT2046_IRQ_PIN="PB.1"
26+
CONFIG_BSP_USING_SOFT_SPI=y
27+
CONFIG_BSP_USING_SOFT_SPI1=y
28+
CONFIG_BSP_S_SPI1_SCK_PIN=16
29+
CONFIG_BSP_S_SPI1_MISO_PIN=18
30+
CONFIG_BSP_S_SPI1_MOSI_PIN=91
31+
CONFIG_BSP_USING_EXT_FMC_IO=y
32+
CONFIG_BSP_USING_FMC=y

bsp/stm32/stm32f407-atk-explorer/board/ports/touch/drv_touch_xpt.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* Copyright (c) 2006-2022, RT-Thread Development Team
2+
* Copyright (c) 2006-2026, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
7-
* Date Author Notes
8-
* 2022-6-27 solar first version
7+
* Date Author Notes
8+
* 2022-6-27 solar first version
9+
* 2026-01-12 LinuxMint-User fix xpt2046_touch_readpoint type error
910
*/
1011

1112
#include <drv_touch_xpt.h>
@@ -30,6 +31,7 @@ rt_err_t xpt2046_calibration(const char *lcd_name,const char *touch_name)
3031
LOG_E(LOG_TAG " cannot find lcd device named %s", lcd_name);
3132
return -RT_ERROR;
3233
}
34+
3335
if (rt_device_open(lcd, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
3436
{
3537
LOG_E(LOG_TAG " cannot open lcd device named %s", lcd_name);
@@ -101,6 +103,7 @@ rt_err_t xpt2046_calibration(const char *lcd_name,const char *touch_name)
101103
{
102104
break;
103105
}
106+
104107
switch (raw_idx)
105108
{
106109
case 1:
@@ -151,7 +154,7 @@ rt_err_t xpt2046_calibration(const char *lcd_name,const char *touch_name)
151154
return RT_EOK;
152155
}
153156

154-
static rt_ssize_t xpt2046_touch_readpoint(struct rt_touch_device *touch, void *buf, rt_size_t touch_num)
157+
static rt_size_t xpt2046_touch_readpoint(struct rt_touch_device *touch, void *buf, rt_size_t touch_num)
155158
{
156159
if (touch_num != 0)
157160
{
@@ -185,6 +188,7 @@ static rt_ssize_t xpt2046_touch_readpoint(struct rt_touch_device *touch, void *b
185188
y_cum += temp;
186189
}
187190
}
191+
188192
if (!x_count || !y_count)
189193
{
190194
result->event = RT_TOUCH_EVENT_NONE;
@@ -264,3 +268,4 @@ static int xpt2046_hw_init(void)
264268
INIT_DEVICE_EXPORT(xpt2046_hw_init);
265269

266270
#endif /* BSP_USING_TOUCH_RES */
271+

bsp/stm32/stm32f407-atk-explorer/board/ports/touch/drv_xpt2046_init.c

Lines changed: 138 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/*
2-
* Copyright (c) 2006-2022, RT-Thread Development Team
2+
* Copyright (c) 2006-2026, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
77
* Date Author Notes
88
* 2022-6-27 solar first version
9+
* 2026-01-12 LinuxMint-User fix touch event bug with LVGL
10+
* 2026-01-13 LinuxMint-User improve the touch sliding experience of resistive screens
911
*/
1012

1113
#include <rtdevice.h>
@@ -42,6 +44,7 @@ void xpt2046_init_hw(void)
4244
rt_kprintf("can't find touch device:%s\n", TOUCH_DEVICE_NAME);
4345
return;
4446
}
47+
4548
if (rt_device_open(touch, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
4649
{
4750
rt_kprintf("can't open touch device:%s\n", TOUCH_DEVICE_NAME);
@@ -87,38 +90,157 @@ void xpt2046_init_hw(void)
8790

8891
void xpt2046_entry(void *parameter)
8992
{
90-
/* Find the touch device */
9193
rt_device_t touch = rt_device_find(TOUCH_DEVICE_NAME);
9294
if (touch == RT_NULL)
9395
{
9496
rt_kprintf("can't find touch device:%s\n", TOUCH_DEVICE_NAME);
9597
return;
9698
}
97-
#ifndef PKG_USING_LVGL
99+
100+
#ifndef PKG_USING_LVGL
98101
rt_device_t lcd = rt_device_find(TFTLCD_DEVICE_NAME);
99102
if (lcd == RT_NULL)
100103
{
101-
rt_kprintf("can't find display device:%s\n", TFTLCD_DEVICE_NAME);
102-
return;
104+
rt_kprintf("can't find display device:%s\n", TFTLCD_DEVICE_NAME);
105+
return;
103106
}
104-
#endif /* PKG_USING_LVGL */
107+
#endif
108+
109+
static rt_bool_t is_touching = RT_FALSE;
110+
static int no_touch_count = 0;
111+
static int touch_hold_count = 0;
112+
static int last_x = 0, last_y = 0;
113+
static int stable_x = 0, stable_y = 0;
114+
115+
/* 5-point moving average filter for coordinate smoothing */
116+
#define HISTORY_SIZE 5
117+
static int history_x[HISTORY_SIZE] = {0};
118+
static int history_y[HISTORY_SIZE] = {0};
119+
static int history_index = 0;
120+
static int history_count = 0;
121+
122+
static const int DEBOUNCE_COUNT = 2;
123+
static const int RELEASE_DEBOUNCE_COUNT = 5;
124+
static const int MIN_MOVE_DISTANCE = 3;
125+
static const int SMOOTHING_FACTOR = 2;
126+
127+
rt_memset(history_x, 0, sizeof(history_x));
128+
rt_memset(history_y, 0, sizeof(history_y));
129+
105130
while (1)
106131
{
107-
/* Prepare variable to read out the touch data */
108132
struct rt_touch_data read_data;
109133
rt_memset(&read_data, 0, sizeof(struct rt_touch_data));
134+
110135
if (rt_device_read(touch, 0, &read_data, 1) == 1)
111136
{
112-
#ifdef PKG_USING_LVGL
113-
lv_port_indev_input(read_data.x_coordinate, read_data.y_coordinate,
114-
((read_data.event = RT_TOUCH_EVENT_DOWN) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL));
115-
#else /* PKG_USING_LVGL */
137+
int current_x = read_data.x_coordinate;
138+
int current_y = read_data.y_coordinate;
139+
140+
history_x[history_index] = current_x;
141+
history_y[history_index] = current_y;
142+
history_index = (history_index + 1) % HISTORY_SIZE;
143+
if (history_count < HISTORY_SIZE) history_count++;
144+
145+
int avg_x = 0, avg_y = 0;
146+
if (history_count > 0)
147+
{
148+
for (int i = 0; i < history_count; i++)
149+
{
150+
avg_x += history_x[i];
151+
avg_y += history_y[i];
152+
}
153+
avg_x /= history_count;
154+
avg_y /= history_count;
155+
}
156+
else
157+
{
158+
avg_x = current_x;
159+
avg_y = current_y;
160+
}
161+
162+
no_touch_count = 0;
163+
164+
#ifdef PKG_USING_LVGL
165+
if (!is_touching)
166+
{
167+
touch_hold_count++;
168+
169+
if (touch_hold_count >= DEBOUNCE_COUNT)
170+
{
171+
is_touching = true;
172+
stable_x = avg_x;
173+
stable_y = avg_y;
174+
touch_hold_count = 0;
175+
176+
lv_port_indev_input(stable_x, stable_y, LV_INDEV_STATE_PR);
177+
178+
last_x = stable_x;
179+
last_y = stable_y;
180+
}
181+
}
182+
else
183+
{
184+
touch_hold_count = 0;
185+
186+
int dx = avg_x - last_x;
187+
int dy = avg_y - last_y;
188+
int distance = dx * dx + dy * dy;
189+
190+
if (distance >= MIN_MOVE_DISTANCE * MIN_MOVE_DISTANCE)
191+
{
192+
/* Interpolation smoothing to reduce jitter */
193+
int smooth_x = last_x + (avg_x - last_x) / SMOOTHING_FACTOR;
194+
int smooth_y = last_y + (avg_y - last_y) / SMOOTHING_FACTOR;
195+
196+
lv_port_indev_input(smooth_x, smooth_y, LV_INDEV_STATE_PR);
197+
198+
last_x = smooth_x;
199+
last_y = smooth_y;
200+
stable_x = smooth_x;
201+
stable_y = smooth_y;
202+
}
203+
else
204+
{
205+
lv_port_indev_input(stable_x, stable_y, LV_INDEV_STATE_PR);
206+
}
207+
}
208+
209+
#else
116210
const rt_uint32_t black = 0x0;
117-
rt_graphix_ops(lcd)->set_pixel((const char *)(&black),
118-
read_data.x_coordinate,
119-
read_data.y_coordinate);
120-
#endif /* PKG_USING_LVGL */
211+
rt_graphix_ops(lcd)->set_pixel((const char *)(&black), avg_x, avg_y);
212+
#endif
121213
}
214+
else
215+
{
216+
no_touch_count++;
217+
touch_hold_count = 0;
218+
219+
if (is_touching)
220+
{
221+
if (no_touch_count >= RELEASE_DEBOUNCE_COUNT)
222+
{
223+
#ifdef PKG_USING_LVGL
224+
lv_port_indev_input(stable_x, stable_y, LV_INDEV_STATE_REL);
225+
#endif
226+
227+
is_touching = RT_FALSE;
228+
no_touch_count = 0;
229+
230+
history_count = 0;
231+
history_index = 0;
232+
rt_memset(history_x, 0, sizeof(history_x));
233+
rt_memset(history_y, 0, sizeof(history_y));
234+
}
235+
else
236+
{
237+
#ifdef PKG_USING_LVGL
238+
lv_port_indev_input(stable_x, stable_y, LV_INDEV_STATE_PR);
239+
#endif
240+
}
241+
}
242+
}
243+
122244
rt_thread_mdelay(1);
123245
}
124246
}
@@ -134,3 +256,4 @@ static int touch_xpt2046_init(void)
134256
INIT_COMPONENT_EXPORT(touch_xpt2046_init);
135257

136258
#endif /* BSP_USING_TOUCH_RES */
259+

0 commit comments

Comments
 (0)