diff --git a/drivers/cst816s/Makefile b/drivers/cst816s/Makefile index 48422e909a..834f121424 100644 --- a/drivers/cst816s/Makefile +++ b/drivers/cst816s/Makefile @@ -1 +1 @@ -include $(RIOTBASE)/Makefile.base +include $(RIOTMAKE)/driver_with_touch_dev.mk diff --git a/drivers/cst816s/cst816s_touch_dev.c b/drivers/cst816s/cst816s_touch_dev.c new file mode 100644 index 0000000000..1782195277 --- /dev/null +++ b/drivers/cst816s/cst816s_touch_dev.c @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2022 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_cst816s + * @{ + * + * @file + * @brief Driver adaption to touch_dev generic interface + * + * @author Alexandre Abadie + * @} + */ + +#include +#include +#include +#include + +#include "kernel_defines.h" + +#include "board.h" +#include "cst816s.h" +#include "cst816s_touch_dev.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +#ifndef CST816S_XMAX +#define CST816S_XMAX 240 +#endif + +#ifndef CST816S_YMAX +#define CST816S_YMAX 240 +#endif + +uint16_t _cst816s_height(const touch_dev_t *touch_dev) +{ + const cst816s_t *dev = (const cst816s_t *)touch_dev; + assert(dev); + + return CST816S_YMAX; +} + +uint16_t _cst816s_width(const touch_dev_t *touch_dev) +{ + const cst816s_t *dev = (const cst816s_t *)touch_dev; + assert(dev); + + return CST816S_XMAX; +} + +uint8_t _cst816s_touches(const touch_dev_t *touch_dev, touch_t *touches, size_t len) +{ + (void)len; + + cst816s_t *dev = (cst816s_t *)touch_dev; + assert(dev); + + cst816s_touch_data_t data; + cst816s_read(dev, &data); + uint8_t ret = (data.action == CST816S_TOUCH_DOWN); + + if (ret && touches != NULL) { + touches[0].x = data.x; + touches[0].y = data.y; + + DEBUG("X: %i, Y: %i\n", touches[0].x, touches[0].y); + } + + return ret; +} + +void _cst816s_set_event_callback(const touch_dev_t *touch_dev, touch_event_cb_t cb, void *arg) +{ + cst816s_t *dev = (cst816s_t *)touch_dev; + assert(dev); + + dev->cb = (cst816s_irq_cb_t)cb; + dev->cb_arg = arg; +} + +const touch_dev_driver_t cst816s_touch_dev_driver = { + .height = _cst816s_height, + .width = _cst816s_width, + .touches = _cst816s_touches, + .set_event_callback = _cst816s_set_event_callback, +}; diff --git a/drivers/cst816s/include/cst816s_params.h b/drivers/cst816s/include/cst816s_params.h index 83791eb9a6..af87ef50e6 100644 --- a/drivers/cst816s/include/cst816s_params.h +++ b/drivers/cst816s/include/cst816s_params.h @@ -75,6 +75,21 @@ static const cst816s_params_t cst816s_params[] = */ #define CST816S_NUMOF ARRAY_SIZE(cst816s_params) +/** + * @brief Default screen identifiers + */ +#ifndef CST816S_PARAM_SCREEN_IDS +#define CST816S_PARAM_SCREEN_IDS 0 +#endif + +/** + * @brief Configure screen identifiers + */ +static const uint8_t cst816s_screen_ids[] = +{ + CST816S_PARAM_SCREEN_IDS, +}; + #ifdef __cplusplus } #endif diff --git a/drivers/cst816s/include/cst816s_touch_dev.h b/drivers/cst816s/include/cst816s_touch_dev.h new file mode 100644 index 0000000000..6bbfa10278 --- /dev/null +++ b/drivers/cst816s/include/cst816s_touch_dev.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2022 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_cst816s + * @{ + * + * @file + * @brief Definition of the driver for the touch_dev generic interface + * + * @author Alexandre Abadie + */ + +#ifndef CST816S_TOUCH_DEV_H +#define CST816S_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 cst816s_touch_dev_driver; + +#ifdef __cplusplus +} +#endif + +#endif /* CST816S_TOUCH_DEV_H */ +/** @} */ diff --git a/drivers/include/cst816s.h b/drivers/include/cst816s.h index 9a21ca2318..6c8f47b573 100644 --- a/drivers/include/cst816s.h +++ b/drivers/include/cst816s.h @@ -46,6 +46,10 @@ #include "periph/i2c.h" #include "periph/gpio.h" +#ifdef MODULE_TOUCH_DEV +#include "touch_dev.h" +#endif + #ifdef __cplusplus extern "C" { #endif @@ -108,6 +112,9 @@ typedef struct { * @brief cst816s device descriptor */ typedef struct { +#ifdef MODULE_TOUCH_DEV + touch_dev_t *dev; /**< Pointer to the generic touch device */ +#endif const cst816s_params_t *params; /**< Device parameters */ cst816s_irq_cb_t cb; /**< Configured IRQ event callback */ void *cb_arg; /**< Extra argument for the callback */ diff --git a/sys/auto_init/screen/auto_init_cst816s.c b/sys/auto_init/screen/auto_init_cst816s.c new file mode 100644 index 0000000000..457e2f37e5 --- /dev/null +++ b/sys/auto_init/screen/auto_init_cst816s.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2022 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 sys_auto_init + * @{ + * @file + * @brief initializes cst816s display device + * + * @author Alexandre Abadie + * @} + */ + +#include + +#include "log.h" + +#include "touch_dev.h" + +#include "cst816s.h" +#include "cst816s_params.h" +#include "cst816s_touch_dev.h" + +cst816s_t cst816s_devs[CST816S_NUMOF]; +static touch_dev_reg_t touch_dev_entries[CST816S_NUMOF]; + +void auto_init_cst816s(void) +{ + assert(CST816S_NUMOF == ARRAY_SIZE(cst816s_screen_ids)); + + for (size_t i = 0; i < CST816S_NUMOF; i++) { + LOG_DEBUG("[auto_init_screen] initializing cst816s #%u\n", i); + if (cst816s_init(&cst816s_devs[i], &cst816s_params[i], NULL, NULL) < 0) { + LOG_ERROR("[auto_init_screen] error initializing cst816s #%u\n", i); + continue; + } + + touch_dev_entries[i].dev = (touch_dev_t *)&cst816s_devs[i]; + touch_dev_entries[i].screen_id = cst816s_screen_ids[i]; + touch_dev_entries[i].dev->driver = &cst816s_touch_dev_driver; + + /* add to touch_dev registry */ + touch_dev_reg_add(&(touch_dev_entries[i])); + } +} diff --git a/sys/auto_init/screen/init.c b/sys/auto_init/screen/init.c index 38d38c3a4e..c6ba9938b0 100644 --- a/sys/auto_init/screen/init.c +++ b/sys/auto_init/screen/init.c @@ -41,6 +41,10 @@ void auto_init_screen(void) if (IS_USED(MODULE_TOUCH_DEV)) { DEBUG("auto_init_screen: init touch drivers\n"); + if (IS_USED(MODULE_CST816S)) { + extern void auto_init_cst816s(void); + auto_init_cst816s(); + } if (IS_USED(MODULE_STMPE811)) { extern void auto_init_stmpe811(void); auto_init_stmpe811();