Skip to content

Commit

Permalink
tests/drivers/touch_dev_gestures: add gesture recognition test
Browse files Browse the repository at this point in the history
  • Loading branch information
gschorcht committed Aug 14, 2023
1 parent 6fd095d commit c88d67f
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/drivers/touch_dev_gestures/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BOARD ?= stm32f746-disco
include ../Makefile.drivers_common

DISABLE_MODULE += test_utils_interactive_sync

USEMODULE += touch_dev_gestures

include $(RIOTBASE)/Makefile.include
3 changes: 3 additions & 0 deletions tests/drivers/touch_dev_gestures/Makefile.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BOARD_INSUFFICIENT_MEMORY := \
atmega8 \
#
36 changes: 36 additions & 0 deletions tests/drivers/touch_dev_gestures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# About

This is a manual test application for gesture recognition for touch devices
that are using the generic touch device API.

# Usage

This test application initializes the touch device and then waits for touch events using interrupts by default. Once touch events are received, it calls the gesture recognition. The application generates an output like the following:
```
Single Tap X: 276, Y:185
Single Tap X: 271, Y:178
Double Tap X: 271, Y:182
Pressed X: 235, Y:168
Moving X: 246, Y:170
Moving X: 255, Y:171
Moving X: 266, Y:173
Moving X: 277, Y:175
Moving X: 283, Y:176
Moving X: 294, Y:178
Moving X: 303, Y:180
Released X: 310, Y:180
Swipe right
Swipe up
Swipe down
Swipe left
```

To use the touch device in polling mode, the environment variable
`TOUCH_DEV_POLLING_MODE` must be set to 1. The polling period in milliseconds
is defined by the environment variable `TOUCH_DEV_POLLING_PERIOD`. It is
50 ms by default and can be changed by setting the environment variable
`TOUCH_DEV_POLLING_PERIOD` in the make command, for example:
```
TOUCH_DEV_POLLING_MODE=1 TOUCH_DEV_POLLING_PERIOD=100 \
BOARD=... make -C tests/drivers/touch_dev_gestures flash term
```
3 changes: 3 additions & 0 deletions tests/drivers/touch_dev_gestures/app.config.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# this file enables modules defined in Kconfig. Do not use this file for
# application configuration. This is only needed during migration.
CONFIG_MODULE_TOUCH_DEV_GESTURES=y
128 changes: 128 additions & 0 deletions tests/drivers/touch_dev_gestures/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (C) 2020 Inria
* 2023 Gunar Schorcht
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup tests
* @{
*
* @file
* @brief Generic touch device test application
*
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
* @author Gunar Schorcht <gunar@schorcht.net>
*
* @}
*/

#include <stdio.h>
#include <stdbool.h>

#include "mutex.h"
#include "ztimer.h"

#include "touch_dev.h"
#include "touch_dev_gestures.h"

#if IS_USED(MODULE_STMPE811)
#include "stmpe811.h"
#include "test_utils/expect.h"
#endif

#ifndef TOUCH_DEV_POLLING_PERIOD
#define TOUCH_DEV_POLLING_PERIOD 50
#endif

#if !IS_ACTIVE(TOUCH_DEV_POLLING_MODE)
static void _touch_event_cb(void *arg)
{
mutex_unlock(arg);
}
#endif

int main(void)
{
/* Use the first screen */
touch_dev_reg_t *touch_reg = touch_dev_reg_find_screen(0);
if (!touch_reg) {
puts("No screen found!");
return -1;
}

touch_dev_t *dev = touch_reg->dev;
touch_dev_gesture_ctx_t ctx;

#if IS_USED(MODULE_STMPE811)
uint16_t xmax = touch_dev_width(dev);
uint16_t ymax = touch_dev_height(dev);

stmpe811_t *stmpe811 = (stmpe811_t *)dev;
expect(xmax == stmpe811->params.xmax);
expect(ymax == stmpe811->params.ymax);
#endif

touch_dev_init_gesture(dev, &ctx);

#if !IS_ACTIVE(TOUCH_DEV_POLLING_MODE)
mutex_t lock = MUTEX_INIT_LOCKED;
touch_dev_set_touch_event_callback(dev, _touch_event_cb, &lock);
#endif

while (1) {

#if IS_ACTIVE(TOUCH_DEV_POLLING_MODE)
ztimer_sleep(ZTIMER_MSEC, TOUCH_DEV_POLLING_PERIOD);
#else
/* wait for event */
mutex_lock(&lock);
#endif

touch_t pos;
touch_dev_gesture_t gesture = touch_dev_recognize_gesture(&ctx, &pos);

switch (gesture) {
case TOUCH_DEV_GEST_SINGLE_TAP:
printf("Single Tap X: %u, Y:%u\n", pos.x, pos.y);
break;
case TOUCH_DEV_GEST_DOUBLE_TAP:
printf("Double Tap X: %u, Y:%u\n", pos.x, pos.y);
break;
case TOUCH_DEV_GEST_PRESSED:
printf("Pressed X: %u, Y:%u\n", pos.x, pos.y);
break;
case TOUCH_DEV_GEST_RELEASED:
printf("Released X: %u, Y:%u\n", pos.x, pos.y);
break;
case TOUCH_DEV_GEST_MOVE:
printf("Moving X: %u, Y:%u\n", pos.x, pos.y);
break;
case TOUCH_DEV_GEST_SWIPE_LEFT:
printf("Swipe left\n");
break;
case TOUCH_DEV_GEST_SWIPE_RIGHT:
printf("Swipe right\n");
break;
case TOUCH_DEV_GEST_SWIPE_UP:
printf("Swipe up\n");
break;
case TOUCH_DEV_GEST_SWIPE_DOWN:
printf("Swipe down\n");
break;
case TOUCH_DEV_GEST_ZOOM_IN:
printf("Zoom in (spread)\n");
break;
case TOUCH_DEV_GEST_ZOOM_OUT:
printf("Zoom out (pinch)\n");
break;
default:
break;
}
}

return 0;
}

0 comments on commit c88d67f

Please sign in to comment.