modules: lvgl: add zephyr,lvgl-pointer-input pseudo device

Add the scaffolding to create input lvgl pseudo devices which route zephyr
input_event to their lvgl `indev` equivalent. As a first cut also add a
`zephyr,lvgl-pointer-input compatible which can be a drop-in replacement
for the existing kscan solution.

Signed-off-by: Fabian Blatz <fabianblatz@gmail.com>
This commit is contained in:
Fabian Blatz 2023-07-26 14:04:42 +02:00 committed by Carles Cufí
parent 5e01466d97
commit c536bd3845
7 changed files with 292 additions and 0 deletions

View file

@ -0,0 +1,10 @@
# Copyright 2023 Fabian Blatz <fabianblatz@gmail.com>
# SPDX-License-Identifier: Apache-2.0
# Common fields for input lvgl pseudo devices
properties:
input:
type: phandle
description: |
Input device phandle.

View file

@ -0,0 +1,36 @@
# Copyright 2023 Fabian Blatz <fabianblatz@gmail.com>
# SPDX-License-Identifier: Apache-2.0
description: |
LVGL pointer indev pseudo-device
Listens for touch input events and routes the
lv_indev_data_t to the underlying pointer lv_indev_t managed by LVGL.
Needs to be associated to a specific device to listen for events
from that device. Example configuration:
pointer {
compatible = "zephyr,lvgl-pointer-input";
input = <&input_sdl_touch>;
};
compatible: "zephyr,lvgl-pointer-input"
include: zephyr,lvgl-common-input.yaml
properties:
swap-xy:
type: boolean
description: |
Swap x-y axes to align input with the display.
invert-x:
type: boolean
description: |
Invert x axes to align input with the display.
invert-y:
type: boolean
description: |
Invert y axes to align input with the display.

View file

@ -223,6 +223,9 @@ zephyr_library_sources_ifdef(CONFIG_LV_Z_USE_FILESYSTEM lvgl_fs.c)
zephyr_library_sources_ifdef(CONFIG_LV_Z_MEM_POOL_SYS_HEAP lvgl_mem.c)
zephyr_library_sources_ifdef(CONFIG_LV_Z_SHELL lvgl_shell.c)
zephyr_library_sources(input/lvgl_common_input.c)
zephyr_library_sources_ifdef(CONFIG_LV_Z_POINTER_INPUT input/lvgl_pointer_input.c)
zephyr_library_link_libraries(LVGL)
target_link_libraries(LVGL INTERFACE zephyr_interface)

View file

@ -4,6 +4,10 @@
menu "Input device settings"
config LV_Z_INPUT_INIT_PRIORITY
int
default 91
config LV_Z_POINTER_KSCAN
bool "Keyboard scan pointer input"
depends on KSCAN
@ -38,4 +42,17 @@ config LV_Z_POINTER_KSCAN_INVERT_Y
endif # LV_Z_POINTER_KSCAN
config LV_Z_POINTER_INPUT
bool "Input lvgl pointer"
default y
depends on INPUT
depends on DT_HAS_ZEPHYR_LVGL_POINTER_INPUT_ENABLED
config LV_Z_POINTER_INPUT_MSGQ_COUNT
int "Input pointer queue message count"
default 10
depends on LV_Z_POINTER_INPUT
help
Size of the pointer message queue buffering input events.
endmenu

View file

@ -0,0 +1,46 @@
/*
* Copyright 2023 Fabian Blatz <fabianblatz@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "lvgl_common_input.h"
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(lvgl);
static void lvgl_input_read_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
{
const struct device *dev = drv->user_data;
const struct lvgl_common_input_config *cfg = dev->config;
k_msgq_get(cfg->event_msgq, data, K_NO_WAIT);
data->continue_reading = k_msgq_num_used_get(cfg->event_msgq) > 0;
}
int lvgl_input_register_driver(lv_indev_type_t indev_type, const struct device *dev)
{
/* Currently no indev binding has its dedicated data
* if that ever changes ensure that `lvgl_common_input_data`
* remains the first member
*/
struct lvgl_common_input_data *common_data = dev->data;
if (common_data == NULL) {
return -EINVAL;
}
lv_indev_drv_init(&common_data->indev_drv);
common_data->indev_drv.type = indev_type;
common_data->indev_drv.read_cb = lvgl_input_read_cb;
common_data->indev_drv.user_data = (void *)dev;
common_data->indev = lv_indev_drv_register(&common_data->indev_drv);
if (common_data->indev == NULL) {
return -EINVAL;
}
return 0;
}

View file

@ -0,0 +1,50 @@
/*
* Copyright 2023 Fabian Blatz <fabianblatz@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_MODULES_LVGL_LVGL_COMMON_INPUT_H_
#define ZEPHYR_MODULES_LVGL_LVGL_COMMON_INPUT_H_
#include <lvgl.h>
#include <zephyr/device.h>
#include <zephyr/input/input.h>
#ifdef __cplusplus
extern "C" {
#endif
struct lvgl_common_input_config {
struct k_msgq *event_msgq;
};
struct lvgl_common_input_data {
lv_indev_drv_t indev_drv;
lv_indev_t *indev;
lv_indev_data_t pending_event;
};
int lvgl_input_register_driver(lv_indev_type_t indev_type, const struct device *dev);
#define LVGL_INPUT_EVENT_MSGQ(inst, type) lvgl_input_msgq_##type##_##inst
#define LVGL_INPUT_DEVICE(inst) DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, input))
#define LVGL_COORD_VALID(coord) IN_RANGE(coord, LV_COORD_MIN, LV_COORD_MAX)
#define LVGL_KEY_VALID(key) IN_RANGE(key, 0, UINT8_MAX)
#define LVGL_INPUT_DEFINE(inst, type, msgq_size, process_evt_cb) \
static void lvgl_input_cb_##_##inst(struct input_event *evt) \
{ \
process_evt_cb(DEVICE_DT_INST_GET(inst), evt); \
} \
INPUT_CALLBACK_DEFINE(LVGL_INPUT_DEVICE(inst), lvgl_input_cb_##_##inst); \
K_MSGQ_DEFINE(lvgl_input_msgq_##type##_##inst, sizeof(lv_indev_data_t), msgq_size, 4)
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* ZEPHYR_MODULES_LVGL_LVGL_COMMON_INPUT_H_ */

View file

@ -0,0 +1,130 @@
/*
* Copyright 2023 Fabian Blatz <fabianblatz@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT zephyr_lvgl_pointer_input
#include "lvgl_common_input.h"
#include <lvgl_display.h>
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(lvgl);
struct lvgl_pointer_input_config {
struct lvgl_common_input_config common_config; /* Needs to be first member */
bool swap_xy;
bool invert_x;
bool invert_y;
};
static void lvgl_pointer_process_event(const struct device *dev, struct input_event *evt)
{
const struct lvgl_pointer_input_config *cfg = dev->config;
struct lvgl_common_input_data *data = dev->data;
lv_disp_t *disp = lv_disp_get_default();
struct lvgl_disp_data *disp_data = disp->driver->user_data;
struct display_capabilities *cap = &disp_data->cap;
lv_point_t *point = &data->pending_event.point;
switch (evt->code) {
case INPUT_ABS_X:
point->x = evt->value;
break;
case INPUT_ABS_Y:
point->y = evt->value;
break;
case INPUT_BTN_TOUCH:
data->pending_event.state = evt->value ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
break;
}
if (!evt->sync) {
return;
}
/* adjust coordinates */
if (cfg->swap_xy) {
lv_coord_t tmp;
tmp = point->x;
point->x = point->y;
point->y = tmp;
}
if (cfg->invert_x) {
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
point->x = cap->x_resolution - point->x;
} else {
point->x = cap->y_resolution - point->x;
}
}
if (cfg->invert_y) {
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
point->y = cap->y_resolution - point->y;
} else {
point->y = cap->x_resolution - point->y;
}
}
/* rotate touch point to match display rotation */
if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_90) {
lv_coord_t tmp;
tmp = point->x;
point->x = point->y;
point->y = cap->y_resolution - tmp;
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
point->x = cap->x_resolution - point->x;
point->y = cap->y_resolution - point->y;
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_270) {
lv_coord_t tmp;
tmp = point->x;
point->x = cap->x_resolution - point->y;
point->y = tmp;
}
/* filter readings within display */
if (point->x <= 0) {
point->x = 0;
} else if (point->x >= cap->x_resolution) {
point->x = cap->x_resolution - 1;
}
if (point->y <= 0) {
point->y = 0;
} else if (point->y >= cap->y_resolution) {
point->y = cap->y_resolution - 1;
}
if (k_msgq_put(cfg->common_config.event_msgq, &data->pending_event, K_NO_WAIT) != 0) {
LOG_WRN("Could not put input data into queue");
}
}
static int lvgl_pointer_input_init(const struct device *dev)
{
return lvgl_input_register_driver(LV_INDEV_TYPE_POINTER, dev);
}
#define LVGL_POINTER_INPUT_DEFINE(inst) \
LVGL_INPUT_DEFINE(inst, pointer, CONFIG_LV_Z_POINTER_INPUT_MSGQ_COUNT, \
lvgl_pointer_process_event); \
static const struct lvgl_pointer_input_config lvgl_pointer_input_config_##inst = { \
.common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, pointer), \
.swap_xy = DT_INST_PROP(inst, swap_xy), \
.invert_x = DT_INST_PROP(inst, invert_x), \
.invert_y = DT_INST_PROP(inst, invert_y), \
}; \
static struct lvgl_common_input_data lvgl_common_input_data_##inst; \
DEVICE_DT_INST_DEFINE(inst, lvgl_pointer_input_init, NULL, &lvgl_common_input_data_##inst, \
&lvgl_pointer_input_config_##inst, APPLICATION, \
CONFIG_LV_Z_INPUT_INIT_PRIORITY, NULL);
DT_INST_FOREACH_STATUS_OKAY(LVGL_POINTER_INPUT_DEFINE)