Skip to content

Commit

Permalink
tests/drivers/touch_dev: introduce TOUCH_DEV_POLLING
Browse files Browse the repository at this point in the history
To be able to test the touch device in polling mode, variable `TOUCH_DEV_POLLING` is introduced. It is set to 0 by default and can be overriden by 1 to use the polling mode. The polling period can be controlled by the `TOUCH_DEV_POLLING_PERIOD` variable.
  • Loading branch information
gschorcht committed Aug 12, 2023
1 parent 91441db commit f6cdd15
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
12 changes: 10 additions & 2 deletions tests/drivers/touch_dev/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ include ../Makefile.drivers_common
DISABLE_MODULE += test_utils_interactive_sync

USEMODULE += touch_dev
USEMODULE += ztimer
USEMODULE += ztimer_msec

TOUCH_DEV_POLLING_MODE ?= 0
TOUCH_DEV_POLLING_PERIOD ?= 50

CFLAGS += -DTOUCH_DEV_POLLING_MODE=$(TOUCH_DEV_POLLING_MODE)
CFLAGS += -DTOUCH_DEV_POLLING_PERIOD=$(TOUCH_DEV_POLLING_PERIOD)

ifneq (0,$(DTOUCH_DEV_POLLING_MODE))
USEMODULE += ztimer_msec
endif

include $(RIOTBASE)/Makefile.include
34 changes: 34 additions & 0 deletions tests/drivers/touch_dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# About

This is a manual test application for touch device drivers using the generic
touch device API. Which touch device driver is used is determined by the board
definition.

# Usage

This test application initializes the touch device and then waits
for touch events by using interrupts by default. When touch events occur,
the application generates output like the following:
```
Event: pressed!
X: 157, Y:152
X: 158, Y:152
X: 158, Y:152
X: 158, Y:152
X: 158, Y:152
X: 158, Y:152
X: 158, Y:152
X: 158, Y:152
X: 158, Y:152
X: 158, Y:152
Event: released!
```

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. It 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 flash term
```
2 changes: 0 additions & 2 deletions tests/drivers/touch_dev/app.config.test
Original file line number Diff line number Diff line change
@@ -1,5 +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=y
CONFIG_MODULE_ZTIMER=y
CONFIG_MODULE_ZTIMER_MSEC=y
30 changes: 23 additions & 7 deletions tests/drivers/touch_dev/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* 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
Expand All @@ -14,13 +15,15 @@
* @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"
Expand All @@ -30,11 +33,16 @@
#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)
{
(void)arg;
printf("Event: ");
mutex_unlock(arg);
}
#endif

int main(void)
{
Expand All @@ -45,7 +53,11 @@ int main(void)
return -1;
}

touch_dev_set_touch_event_callback(touch_dev->dev, _touch_event_cb, NULL);
#if !IS_ACTIVE(TOUCH_DEV_POLLING_MODE)
mutex_t lock = MUTEX_INIT_LOCKED;

touch_dev_set_touch_event_callback(touch_dev->dev, _touch_event_cb, &lock);
#endif

#if IS_USED(MODULE_STMPE811)
uint16_t xmax = touch_dev_width(touch_dev->dev);
Expand All @@ -59,15 +71,21 @@ int main(void)
uint8_t last_touches = touch_dev_touches(touch_dev->dev, NULL, 1);

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 touches[1];
uint8_t current_touches = touch_dev_touches(touch_dev->dev, touches, 1);

if (current_touches != last_touches) {
if (current_touches == 0) {
puts("released!");
puts("Event: released!");
}
if (current_touches > 0) {
puts("pressed!");
puts("Event: pressed!");
}
last_touches = current_touches;
}
Expand All @@ -76,8 +94,6 @@ int main(void)
if (current_touches == 1) {
printf("X: %i, Y:%i\n", touches[0].x, touches[0].y);
}

ztimer_sleep(ZTIMER_MSEC, 10);
}

return 0;
Expand Down

0 comments on commit f6cdd15

Please sign in to comment.