Skip to content

Commit 05aa5f5

Browse files
committed
feat(multi-touch): Multi-touch gestures in display example
1 parent 4e89eff commit 05aa5f5

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

examples/display/main/lvgl_demo_ui.c

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: CC0-1.0
55
*/
@@ -29,6 +29,82 @@ static lv_color_t arc_color[] = {
2929
LV_COLOR_MAKE(90, 202, 228),
3030
};
3131

32+
#if (CONFIG_ESP_LCD_TOUCH_MAX_POINTS > 1 & CONFIG_LVGL_PORT_ENABLE_GESTURES)
33+
void gesture_cb(lv_event_t *e)
34+
{
35+
static uint32_t logo_scale = LV_SCALE_NONE;
36+
static int32_t logo_rotation = 0;
37+
static int32_t logo_offset_x = 0;
38+
static int32_t logo_offset_y = 0;
39+
40+
lv_indev_gesture_type_t type = lv_event_get_gesture_type(e);
41+
lv_indev_gesture_state_t state = lv_event_get_gesture_state(e, type);
42+
43+
if (state == LV_INDEV_GESTURE_STATE_RECOGNIZED || state == LV_INDEV_GESTURE_STATE_ENDED) {
44+
switch (type) {
45+
case LV_INDEV_GESTURE_PINCH:
46+
// Read current pinch scale and convert it from float to image scale
47+
uint32_t pinch_scale_rel = lv_event_get_pinch_scale(e) * LV_SCALE_NONE;
48+
// Cutoff negative results at 0
49+
uint32_t pinch_scale_abs = logo_scale + pinch_scale_rel < LV_SCALE_NONE ? 0 : logo_scale + ((int32_t)pinch_scale_rel - LV_SCALE_NONE);
50+
lv_img_set_zoom(img_logo, pinch_scale_abs);
51+
// Save the resulting image scale after the gesture ends
52+
if (state == LV_INDEV_GESTURE_STATE_ENDED) {
53+
logo_scale = pinch_scale_abs;
54+
}
55+
break;
56+
case LV_INDEV_GESTURE_ROTATE:
57+
// Get gesture rotation in degrees
58+
int32_t rotation_rel = lv_event_get_rotation(e) * (180.0f / M_PI) * 10.0f;
59+
// Calculate resulting angle and limit it to 360 degrees
60+
int32_t rotation_abs = (logo_rotation + rotation_rel) % 3600;
61+
lv_img_set_angle(img_logo, rotation_abs);
62+
// Save the resulting image rotation angle after the gesture ends
63+
if (state == LV_INDEV_GESTURE_STATE_ENDED) {
64+
logo_rotation = rotation_abs;
65+
}
66+
break;
67+
case LV_INDEV_GESTURE_TWO_FINGERS_SWIPE:
68+
// Get two finger swipe values
69+
lv_dir_t swipe_dir = lv_event_get_two_fingers_swipe_dir(e);
70+
float swipe_dist = lv_event_get_two_fingers_swipe_distance(e);
71+
int32_t swipe_dist_abs;
72+
switch (swipe_dir) {
73+
case LV_DIR_BOTTOM:
74+
case LV_DIR_TOP:
75+
// Vertical offset sign depends on the swipe direction
76+
swipe_dist_abs = swipe_dir == LV_DIR_TOP ?
77+
logo_offset_y - swipe_dist :
78+
logo_offset_y + swipe_dist;
79+
lv_obj_set_pos(img_logo, logo_offset_x, swipe_dist_abs);
80+
// Save the resulting vertical image offset after the gesture ends
81+
if (state == LV_INDEV_GESTURE_STATE_ENDED) {
82+
logo_offset_y = swipe_dist_abs;
83+
}
84+
break;
85+
case LV_DIR_RIGHT:
86+
case LV_DIR_LEFT:
87+
// Horizontal offset sign depends on the swipe direction
88+
swipe_dist_abs = swipe_dir == LV_DIR_LEFT ?
89+
logo_offset_x - swipe_dist :
90+
logo_offset_x + swipe_dist;
91+
lv_obj_set_pos(img_logo, swipe_dist_abs, logo_offset_y);
92+
// Save the resulting horizontal image offset after the gesture ends
93+
if (state == LV_INDEV_GESTURE_STATE_ENDED) {
94+
logo_offset_x = swipe_dist_abs;
95+
}
96+
break;
97+
default:
98+
break;
99+
}
100+
break;
101+
default:
102+
break;
103+
}
104+
}
105+
}
106+
#endif
107+
32108
static void anim_timer_cb(lv_timer_t *timer)
33109
{
34110
my_timer_context_t *timer_ctx = (my_timer_context_t *) lv_timer_get_user_data(timer);
@@ -81,6 +157,10 @@ void example_lvgl_demo_ui(lv_obj_t *scr)
81157
lv_img_set_src(img_logo, &esp_logo);
82158
lv_obj_center(img_logo);
83159

160+
#if (CONFIG_ESP_LCD_TOUCH_MAX_POINTS > 1 & CONFIG_LVGL_PORT_ENABLE_GESTURES)
161+
lv_indev_t *indev = bsp_display_get_input_dev();
162+
#endif
163+
84164
// Create arcs
85165
for (size_t i = 0; i < sizeof(arc) / sizeof(arc[0]); i++) {
86166
arc[i] = lv_arc_create(scr);

examples/display/main/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: CC0-1.0
55
*/

examples/display/sdkconfig.bsp.esp-box-3

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ CONFIG_LV_USE_PERF_MONITOR=y
2525
# CONFIG_LV_MEM_CUSTOM=y
2626
# CONFIG_LV_MEMCPY_MEMSET_STD=y
2727
# CONFIG_LV_SPRINTF_CUSTOM=y
28+
29+
CONFIG_LV_USE_FLOAT=y
30+
CONFIG_LV_USE_GESTURE_RECOGNITION=y
31+
CONFIG_LVGL_PORT_ENABLE_GESTURES=y

0 commit comments

Comments
 (0)