|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Inria |
| 3 | + * 2023 Gunar Schorcht |
| 4 | + * |
| 5 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 6 | + * General Public License v2.1. See the file LICENSE in the top level |
| 7 | + * directory for more details. |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * @ingroup tests |
| 12 | + * @{ |
| 13 | + * |
| 14 | + * @file |
| 15 | + * @brief Generic touch device test application |
| 16 | + * |
| 17 | + * @author Alexandre Abadie <alexandre.abadie@inria.fr> |
| 18 | + * @author Gunar Schorcht <gunar@schorcht.net> |
| 19 | + * |
| 20 | + * @} |
| 21 | + */ |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdbool.h> |
| 25 | + |
| 26 | +#include "mutex.h" |
| 27 | +#include "ztimer.h" |
| 28 | + |
| 29 | +#include "touch_dev.h" |
| 30 | +#include "touch_dev_gestures.h" |
| 31 | + |
| 32 | +#ifndef TOUCH_DEV_POLLING_PERIOD |
| 33 | +#define TOUCH_DEV_POLLING_PERIOD 50 |
| 34 | +#endif |
| 35 | + |
| 36 | +#if !IS_ACTIVE(TOUCH_DEV_POLLING_MODE) |
| 37 | +static void _touch_event_cb(void *arg) |
| 38 | +{ |
| 39 | + mutex_unlock(arg); |
| 40 | +} |
| 41 | +#endif |
| 42 | + |
| 43 | +int main(void) |
| 44 | +{ |
| 45 | + /* Use the first screen */ |
| 46 | + touch_dev_reg_t *touch_reg = touch_dev_reg_find_screen(0); |
| 47 | + if (!touch_reg) { |
| 48 | + puts("No screen found!"); |
| 49 | + return -1; |
| 50 | + } |
| 51 | + |
| 52 | + touch_dev_t *dev = touch_reg->dev; |
| 53 | + touch_dev_gesture_ctx_t ctx; |
| 54 | + |
| 55 | + touch_dev_init_gesture(dev, &ctx); |
| 56 | + |
| 57 | +#if !IS_ACTIVE(TOUCH_DEV_POLLING_MODE) |
| 58 | + mutex_t lock = MUTEX_INIT_LOCKED; |
| 59 | + touch_dev_set_touch_event_callback(dev, _touch_event_cb, &lock); |
| 60 | +#endif |
| 61 | + |
| 62 | + while (1) { |
| 63 | + |
| 64 | +#if IS_ACTIVE(TOUCH_DEV_POLLING_MODE) |
| 65 | + ztimer_sleep(ZTIMER_MSEC, TOUCH_DEV_POLLING_PERIOD); |
| 66 | +#else |
| 67 | + /* wait for event */ |
| 68 | + mutex_lock(&lock); |
| 69 | +#endif |
| 70 | + |
| 71 | + touch_t pos; |
| 72 | + touch_dev_gesture_t gesture = touch_dev_recognize_gesture(&ctx, &pos); |
| 73 | + |
| 74 | + switch (gesture) { |
| 75 | + case TOUCH_DEV_GEST_SINGLE_TAP: |
| 76 | + printf("Single Tap X: %u, Y:%u\n", pos.x, pos.y); |
| 77 | + break; |
| 78 | + case TOUCH_DEV_GEST_DOUBLE_TAP: |
| 79 | + printf("Double Tap X: %u, Y:%u\n", pos.x, pos.y); |
| 80 | + break; |
| 81 | + case TOUCH_DEV_GEST_PRESSED: |
| 82 | + printf("Pressed X: %u, Y:%u\n", pos.x, pos.y); |
| 83 | + break; |
| 84 | + case TOUCH_DEV_GEST_RELEASED: |
| 85 | + printf("Released X: %u, Y:%u\n", pos.x, pos.y); |
| 86 | + break; |
| 87 | + case TOUCH_DEV_GEST_MOVE: |
| 88 | + printf("Moving X: %u, Y:%u\n", pos.x, pos.y); |
| 89 | + break; |
| 90 | + case TOUCH_DEV_GEST_SWIPE_LEFT: |
| 91 | + printf("Swipe left\n"); |
| 92 | + break; |
| 93 | + case TOUCH_DEV_GEST_SWIPE_RIGHT: |
| 94 | + printf("Swipe right\n"); |
| 95 | + break; |
| 96 | + case TOUCH_DEV_GEST_SWIPE_UP: |
| 97 | + printf("Swipe up\n"); |
| 98 | + break; |
| 99 | + case TOUCH_DEV_GEST_SWIPE_DOWN: |
| 100 | + printf("Swipe down\n"); |
| 101 | + break; |
| 102 | + case TOUCH_DEV_GEST_ZOOM_IN: |
| 103 | + printf("Zoom in (spread)\n"); |
| 104 | + break; |
| 105 | + case TOUCH_DEV_GEST_ZOOM_OUT: |
| 106 | + printf("Zoom out (pinch)\n"); |
| 107 | + break; |
| 108 | + default: |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
0 commit comments