|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: CC0-1.0 |
5 | 5 | */ |
@@ -29,6 +29,82 @@ static lv_color_t arc_color[] = { |
29 | 29 | LV_COLOR_MAKE(90, 202, 228), |
30 | 30 | }; |
31 | 31 |
|
| 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 | + |
32 | 108 | static void anim_timer_cb(lv_timer_t *timer) |
33 | 109 | { |
34 | 110 | 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) |
81 | 157 | lv_img_set_src(img_logo, &esp_logo); |
82 | 158 | lv_obj_center(img_logo); |
83 | 159 |
|
| 160 | +#if (CONFIG_ESP_LCD_TOUCH_MAX_POINTS > 1 && CONFIG_LVGL_PORT_ENABLE_GESTURES) |
| 161 | + lv_obj_add_event_cb(scr, gesture_cb, LV_EVENT_GESTURE, NULL); |
| 162 | +#endif |
| 163 | + |
84 | 164 | // Create arcs |
85 | 165 | for (size_t i = 0; i < sizeof(arc) / sizeof(arc[0]); i++) { |
86 | 166 | arc[i] = lv_arc_create(scr); |
|
0 commit comments