diff --git a/drivers/include/stmpe811.h b/drivers/include/stmpe811.h index 5464f28c40..8cc4e6fbfa 100644 --- a/drivers/include/stmpe811.h +++ b/drivers/include/stmpe811.h @@ -25,6 +25,10 @@ #include "periph/gpio.h" #include "periph/i2c.h" +#ifdef MODULE_TOUCH_DEV +#include "touch_dev.h" +#endif + #ifdef __cplusplus extern "C" { #endif @@ -77,9 +81,12 @@ typedef struct { * @brief Device descriptor for the STMPE811 sensor */ typedef struct { - stmpe811_params_t params; /**< Device parameters */ - uint16_t prev_x; /**< Previous X coordinate */ - uint16_t prev_y; /**< Previous Y coordinate */ +#ifdef MODULE_TOUCH_DEV + touch_dev_t *dev; /**< Pointer to the generic touch device */ +#endif + stmpe811_params_t params; /**< Device parameters */ + uint16_t prev_x; /**< Previous X coordinate */ + uint16_t prev_y; /**< Previous Y coordinate */ } stmpe811_t; /** diff --git a/drivers/stmpe811/include/stmpe811_touch_dev.h b/drivers/stmpe811/include/stmpe811_touch_dev.h new file mode 100644 index 0000000000..6668b5a8e7 --- /dev/null +++ b/drivers/stmpe811/include/stmpe811_touch_dev.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 drivers_stmpe811 + * @{ + * + * @file + * @brief Definition of the driver for the touch_dev generic interface + * + * @author Alexandre Abadie + */ + +#ifndef STMPE811_TOUCH_DEV_H +#define STMPE811_TOUCH_DEV_H + +#include "touch_dev.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Reference to the touch device driver struct + */ +extern const touch_dev_driver_t stmpe811_touch_dev_driver; + +#ifdef __cplusplus +} +#endif + +#endif /* STMPE811_TOUCH_DEV_H */ diff --git a/drivers/stmpe811/stmpe811_touch_dev.c b/drivers/stmpe811/stmpe811_touch_dev.c new file mode 100644 index 0000000000..f4da762213 --- /dev/null +++ b/drivers/stmpe811/stmpe811_touch_dev.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 drivers_stmpe811 + * @{ + * + * @file + * @brief Driver adaption to touch_dev generic interface + * + * @author Alexandre Abadie + * @} + */ + +#include +#include +#include +#include + +#include "kernel_defines.h" + +#include "stmpe811.h" +#include "stmpe811_touch_dev.h" + +uint16_t _stmpe811_height(const touch_dev_t *touch_dev) +{ + const stmpe811_t *dev = (const stmpe811_t *)touch_dev; + assert(dev); + + return dev->params.ymax; +} + +uint16_t _stmpe811_width(const touch_dev_t *touch_dev) +{ + const stmpe811_t *dev = (const stmpe811_t *)touch_dev; + assert(dev); + + return dev->params.xmax; +} + +uint8_t _stmpe811_touches(const touch_dev_t *touch_dev, touch_t *touches, size_t len) +{ + (void)len; + + stmpe811_t *dev = (stmpe811_t *)touch_dev; + assert(dev); + + stmpe811_touch_state_t state; + stmpe811_read_touch_state(dev, &state); + uint8_t ret = (state == STMPE811_TOUCH_STATE_PRESSED); + + if (ret && touches != NULL) { + stmpe811_touch_position_t pos; + stmpe811_read_touch_position(dev, &pos); + touches[0].x = pos.x; + touches[0].y = pos.y; + } + + return ret; +} + +const touch_dev_driver_t stmpe811_touch_dev_driver = { + .height = _stmpe811_height, + .width = _stmpe811_width, + .touches = _stmpe811_touches, +};