From 74e84a9dfd41efe97bc512c82ab2868ac1893479 Mon Sep 17 00:00:00 2001 From: Dominik Lau Date: Wed, 7 Aug 2024 12:25:50 +0200 Subject: [PATCH] drivers: input: common properties parsing for touchscreen drivers Adds a way of reporting touchscreen events taking common properties into account. Signed-off-by: Dominik Lau Signed-off-by: Filip Kokosinski --- doc/services/input/index.rst | 5 ++ drivers/input/CMakeLists.txt | 1 + drivers/input/Kconfig | 1 + drivers/input/Kconfig.touch | 7 +++ drivers/input/input_touch.c | 20 +++++++ include/zephyr/input/input_touch.h | 94 ++++++++++++++++++++++++++++++ 6 files changed, 128 insertions(+) create mode 100644 drivers/input/Kconfig.touch create mode 100644 drivers/input/input_touch.c create mode 100644 include/zephyr/input/input_touch.h diff --git a/doc/services/input/index.rst b/doc/services/input/index.rst index e42af58ecbc..e2e28430185 100644 --- a/doc/services/input/index.rst +++ b/doc/services/input/index.rst @@ -131,3 +131,8 @@ Analog Axis API Reference ************************* .. doxygengroup:: input_analog_axis + +Touchscreen API Reference +************************* + +.. doxygengroup:: touch_events diff --git a/drivers/input/CMakeLists.txt b/drivers/input/CMakeLists.txt index a996cb93ead..aa1f8434762 100644 --- a/drivers/input/CMakeLists.txt +++ b/drivers/input/CMakeLists.txt @@ -26,6 +26,7 @@ zephyr_library_sources_ifdef(CONFIG_INPUT_PINNACLE input_pinnacle.c) zephyr_library_sources_ifdef(CONFIG_INPUT_PMW3610 input_pmw3610.c) zephyr_library_sources_ifdef(CONFIG_INPUT_SBUS input_sbus.c) zephyr_library_sources_ifdef(CONFIG_INPUT_STMPE811 input_stmpe811.c) +zephyr_library_sources_ifdef(CONFIG_INPUT_TOUCH input_touch.c) zephyr_library_sources_ifdef(CONFIG_INPUT_XEC_KBD input_xec_kbd.c) zephyr_library_sources_ifdef(CONFIG_INPUT_XPT2046 input_xpt2046.c) # zephyr-keep-sorted-stop diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig index 34363e3eca9..e76d3fc6fc7 100644 --- a/drivers/input/Kconfig +++ b/drivers/input/Kconfig @@ -29,6 +29,7 @@ source "drivers/input/Kconfig.pmw3610" source "drivers/input/Kconfig.sbus" source "drivers/input/Kconfig.sdl" source "drivers/input/Kconfig.stmpe811" +source "drivers/input/Kconfig.touch" source "drivers/input/Kconfig.xec" source "drivers/input/Kconfig.xpt2046" # zephyr-keep-sorted-stop diff --git a/drivers/input/Kconfig.touch b/drivers/input/Kconfig.touch new file mode 100644 index 00000000000..0d767029bc3 --- /dev/null +++ b/drivers/input/Kconfig.touch @@ -0,0 +1,7 @@ +# Copyright (c) 2024 Antmicro Ltd +# SPDX-License-Identifier: Apache-2.0 + +config INPUT_TOUCH + bool + help + Enable library used for touchscreen drivers. diff --git a/drivers/input/input_touch.c b/drivers/input/input_touch.c new file mode 100644 index 00000000000..82f9b39b2e1 --- /dev/null +++ b/drivers/input/input_touch.c @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Antmicro + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include + +void input_touchscreen_report_pos(const struct device *dev, + uint32_t x, uint32_t y, + k_timeout_t timeout) +{ + const struct input_touchscreen_common_config *cfg = dev->config; + const uint32_t reported_x_code = cfg->swapped_x_y ? INPUT_ABS_Y : INPUT_ABS_X; + const uint32_t reported_y_code = cfg->swapped_x_y ? INPUT_ABS_X : INPUT_ABS_Y; + const uint32_t reported_x = cfg->inverted_x ? cfg->screen_width - x : x; + const uint32_t reported_y = cfg->inverted_y ? cfg->screen_height - y : y; + + input_report_abs(dev, reported_x_code, reported_x, false, timeout); + input_report_abs(dev, reported_y_code, reported_y, false, timeout); +} diff --git a/include/zephyr/input/input_touch.h b/include/zephyr/input/input_touch.h new file mode 100644 index 00000000000..450107417f1 --- /dev/null +++ b/include/zephyr/input/input_touch.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2024 Antmicro + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef ZEPHYR_INCLUDE_INPUT_TOUCH_H_ +#define ZEPHYR_INCLUDE_INPUT_TOUCH_H_ + +/** + * @brief Touch Events API + * @defgroup touch_events Touchscreen Event Report API + * @since 3.7 + * @version 0.1.0 + * @ingroup io_interfaces + * @{ + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Common touchscreen config. + * + * This structure **must** be placed first in the driver's config structure. + * + * @param screen_width Horizontal resolution of touchscreen + * @param screen_height Vertical resolution of touchscreen + * @param inverted_x X axis is inverted + * @param inverted_y Y axis is inverted + * @param swapped_x_y X and Y axes are swapped + * + * see touchscreem-common.yaml for more details + */ +struct input_touchscreen_common_config { + uint32_t screen_width; + uint32_t screen_height; + bool inverted_x; + bool inverted_y; + bool swapped_x_y; +}; + +/** + * @brief Initialize common touchscreen config from devicetree + * + * @param node_id The devicetree node identifier. + */ +#define INPUT_TOUCH_DT_COMMON_CONFIG_INIT(node_id) \ + { \ + .screen_width = DT_PROP(node_id, screen_width), \ + .screen_height = DT_PROP(node_id, screen_height), \ + .inverted_x = DT_PROP(node_id, inverted_x), \ + .inverted_y = DT_PROP(node_id, inverted_y), \ + .swapped_x_y = DT_PROP(node_id, swapped_x_y) \ + } + +/** + * @brief Initialize common touchscreen config from devicetree instance. + * + * @param inst Instance. + */ +#define INPUT_TOUCH_DT_INST_COMMON_CONFIG_INIT(inst) \ + INPUT_TOUCH_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst)) + +/** + * @brief Validate the offset of the common config structure. + * + * @param config Name of the config structure. + */ +#define INPUT_TOUCH_STRUCT_CHECK(config) \ + BUILD_ASSERT(offsetof(config, common) == 0, \ + "struct input_touchscreen_common_config must be placed first"); + +/** + * @brief Common utility for reporting touchscreen position events. + * + * @param dev Touchscreen controller + * @param x X coordinate as reported by the controller + * @param y Y coordinate as reported by the controller + * @param timeout Timeout for reporting the event + */ +void input_touchscreen_report_pos(const struct device *dev, + uint32_t x, uint32_t y, + k_timeout_t timeout); + +#ifdef __cplusplus +} +#endif + +/** @} */ + +#endif /* ZEPHYR_INCLUDE_INPUT_TOUCH_H_ */