Skip to content

Commit c536bd3

Browse files
faxe1008carlescufi
authored andcommitted
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 <[email protected]>
1 parent 5e01466 commit c536bd3

File tree

7 files changed

+292
-0
lines changed

7 files changed

+292
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright 2023 Fabian Blatz <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Common fields for input lvgl pseudo devices
5+
6+
properties:
7+
input:
8+
type: phandle
9+
description: |
10+
Input device phandle.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2023 Fabian Blatz <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
LVGL pointer indev pseudo-device
6+
7+
Listens for touch input events and routes the
8+
lv_indev_data_t to the underlying pointer lv_indev_t managed by LVGL.
9+
10+
Needs to be associated to a specific device to listen for events
11+
from that device. Example configuration:
12+
13+
pointer {
14+
compatible = "zephyr,lvgl-pointer-input";
15+
input = <&input_sdl_touch>;
16+
};
17+
18+
compatible: "zephyr,lvgl-pointer-input"
19+
20+
include: zephyr,lvgl-common-input.yaml
21+
22+
properties:
23+
swap-xy:
24+
type: boolean
25+
description: |
26+
Swap x-y axes to align input with the display.
27+
28+
invert-x:
29+
type: boolean
30+
description: |
31+
Invert x axes to align input with the display.
32+
33+
invert-y:
34+
type: boolean
35+
description: |
36+
Invert y axes to align input with the display.

modules/lvgl/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ zephyr_library_sources_ifdef(CONFIG_LV_Z_USE_FILESYSTEM lvgl_fs.c)
223223
zephyr_library_sources_ifdef(CONFIG_LV_Z_MEM_POOL_SYS_HEAP lvgl_mem.c)
224224
zephyr_library_sources_ifdef(CONFIG_LV_Z_SHELL lvgl_shell.c)
225225

226+
zephyr_library_sources(input/lvgl_common_input.c)
227+
zephyr_library_sources_ifdef(CONFIG_LV_Z_POINTER_INPUT input/lvgl_pointer_input.c)
228+
226229
zephyr_library_link_libraries(LVGL)
227230
target_link_libraries(LVGL INTERFACE zephyr_interface)
228231

modules/lvgl/Kconfig.input

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
menu "Input device settings"
66

7+
config LV_Z_INPUT_INIT_PRIORITY
8+
int
9+
default 91
10+
711
config LV_Z_POINTER_KSCAN
812
bool "Keyboard scan pointer input"
913
depends on KSCAN
@@ -38,4 +42,17 @@ config LV_Z_POINTER_KSCAN_INVERT_Y
3842

3943
endif # LV_Z_POINTER_KSCAN
4044

45+
config LV_Z_POINTER_INPUT
46+
bool "Input lvgl pointer"
47+
default y
48+
depends on INPUT
49+
depends on DT_HAS_ZEPHYR_LVGL_POINTER_INPUT_ENABLED
50+
51+
config LV_Z_POINTER_INPUT_MSGQ_COUNT
52+
int "Input pointer queue message count"
53+
default 10
54+
depends on LV_Z_POINTER_INPUT
55+
help
56+
Size of the pointer message queue buffering input events.
57+
4158
endmenu
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2023 Fabian Blatz <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "lvgl_common_input.h"
7+
8+
#include <zephyr/device.h>
9+
#include <zephyr/kernel.h>
10+
#include <zephyr/logging/log.h>
11+
12+
LOG_MODULE_DECLARE(lvgl);
13+
14+
static void lvgl_input_read_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
15+
{
16+
const struct device *dev = drv->user_data;
17+
const struct lvgl_common_input_config *cfg = dev->config;
18+
19+
k_msgq_get(cfg->event_msgq, data, K_NO_WAIT);
20+
data->continue_reading = k_msgq_num_used_get(cfg->event_msgq) > 0;
21+
}
22+
23+
int lvgl_input_register_driver(lv_indev_type_t indev_type, const struct device *dev)
24+
{
25+
/* Currently no indev binding has its dedicated data
26+
* if that ever changes ensure that `lvgl_common_input_data`
27+
* remains the first member
28+
*/
29+
struct lvgl_common_input_data *common_data = dev->data;
30+
31+
if (common_data == NULL) {
32+
return -EINVAL;
33+
}
34+
35+
lv_indev_drv_init(&common_data->indev_drv);
36+
common_data->indev_drv.type = indev_type;
37+
common_data->indev_drv.read_cb = lvgl_input_read_cb;
38+
common_data->indev_drv.user_data = (void *)dev;
39+
common_data->indev = lv_indev_drv_register(&common_data->indev_drv);
40+
41+
if (common_data->indev == NULL) {
42+
return -EINVAL;
43+
}
44+
45+
return 0;
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2023 Fabian Blatz <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_MODULES_LVGL_LVGL_COMMON_INPUT_H_
8+
#define ZEPHYR_MODULES_LVGL_LVGL_COMMON_INPUT_H_
9+
10+
#include <lvgl.h>
11+
#include <zephyr/device.h>
12+
#include <zephyr/input/input.h>
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
struct lvgl_common_input_config {
19+
struct k_msgq *event_msgq;
20+
};
21+
22+
struct lvgl_common_input_data {
23+
lv_indev_drv_t indev_drv;
24+
lv_indev_t *indev;
25+
lv_indev_data_t pending_event;
26+
};
27+
28+
int lvgl_input_register_driver(lv_indev_type_t indev_type, const struct device *dev);
29+
30+
#define LVGL_INPUT_EVENT_MSGQ(inst, type) lvgl_input_msgq_##type##_##inst
31+
#define LVGL_INPUT_DEVICE(inst) DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, input))
32+
33+
#define LVGL_COORD_VALID(coord) IN_RANGE(coord, LV_COORD_MIN, LV_COORD_MAX)
34+
#define LVGL_KEY_VALID(key) IN_RANGE(key, 0, UINT8_MAX)
35+
36+
#define LVGL_INPUT_DEFINE(inst, type, msgq_size, process_evt_cb) \
37+
static void lvgl_input_cb_##_##inst(struct input_event *evt) \
38+
{ \
39+
process_evt_cb(DEVICE_DT_INST_GET(inst), evt); \
40+
} \
41+
INPUT_CALLBACK_DEFINE(LVGL_INPUT_DEVICE(inst), lvgl_input_cb_##_##inst); \
42+
K_MSGQ_DEFINE(lvgl_input_msgq_##type##_##inst, sizeof(lv_indev_data_t), msgq_size, 4)
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
/** @} */
49+
50+
#endif /* ZEPHYR_MODULES_LVGL_LVGL_COMMON_INPUT_H_ */
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2023 Fabian Blatz <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT zephyr_lvgl_pointer_input
8+
9+
#include "lvgl_common_input.h"
10+
11+
#include <lvgl_display.h>
12+
#include <zephyr/logging/log.h>
13+
14+
LOG_MODULE_DECLARE(lvgl);
15+
16+
struct lvgl_pointer_input_config {
17+
struct lvgl_common_input_config common_config; /* Needs to be first member */
18+
bool swap_xy;
19+
bool invert_x;
20+
bool invert_y;
21+
};
22+
23+
static void lvgl_pointer_process_event(const struct device *dev, struct input_event *evt)
24+
{
25+
const struct lvgl_pointer_input_config *cfg = dev->config;
26+
struct lvgl_common_input_data *data = dev->data;
27+
lv_disp_t *disp = lv_disp_get_default();
28+
struct lvgl_disp_data *disp_data = disp->driver->user_data;
29+
struct display_capabilities *cap = &disp_data->cap;
30+
lv_point_t *point = &data->pending_event.point;
31+
32+
switch (evt->code) {
33+
case INPUT_ABS_X:
34+
point->x = evt->value;
35+
break;
36+
case INPUT_ABS_Y:
37+
point->y = evt->value;
38+
break;
39+
case INPUT_BTN_TOUCH:
40+
data->pending_event.state = evt->value ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
41+
break;
42+
}
43+
44+
if (!evt->sync) {
45+
return;
46+
}
47+
48+
/* adjust coordinates */
49+
if (cfg->swap_xy) {
50+
lv_coord_t tmp;
51+
52+
tmp = point->x;
53+
point->x = point->y;
54+
point->y = tmp;
55+
}
56+
57+
if (cfg->invert_x) {
58+
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
59+
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
60+
point->x = cap->x_resolution - point->x;
61+
} else {
62+
point->x = cap->y_resolution - point->x;
63+
}
64+
}
65+
66+
if (cfg->invert_y) {
67+
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
68+
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
69+
point->y = cap->y_resolution - point->y;
70+
} else {
71+
point->y = cap->x_resolution - point->y;
72+
}
73+
}
74+
75+
/* rotate touch point to match display rotation */
76+
if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_90) {
77+
lv_coord_t tmp;
78+
79+
tmp = point->x;
80+
point->x = point->y;
81+
point->y = cap->y_resolution - tmp;
82+
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
83+
point->x = cap->x_resolution - point->x;
84+
point->y = cap->y_resolution - point->y;
85+
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_270) {
86+
lv_coord_t tmp;
87+
88+
tmp = point->x;
89+
point->x = cap->x_resolution - point->y;
90+
point->y = tmp;
91+
}
92+
93+
/* filter readings within display */
94+
if (point->x <= 0) {
95+
point->x = 0;
96+
} else if (point->x >= cap->x_resolution) {
97+
point->x = cap->x_resolution - 1;
98+
}
99+
100+
if (point->y <= 0) {
101+
point->y = 0;
102+
} else if (point->y >= cap->y_resolution) {
103+
point->y = cap->y_resolution - 1;
104+
}
105+
106+
if (k_msgq_put(cfg->common_config.event_msgq, &data->pending_event, K_NO_WAIT) != 0) {
107+
LOG_WRN("Could not put input data into queue");
108+
}
109+
}
110+
111+
static int lvgl_pointer_input_init(const struct device *dev)
112+
{
113+
return lvgl_input_register_driver(LV_INDEV_TYPE_POINTER, dev);
114+
}
115+
116+
#define LVGL_POINTER_INPUT_DEFINE(inst) \
117+
LVGL_INPUT_DEFINE(inst, pointer, CONFIG_LV_Z_POINTER_INPUT_MSGQ_COUNT, \
118+
lvgl_pointer_process_event); \
119+
static const struct lvgl_pointer_input_config lvgl_pointer_input_config_##inst = { \
120+
.common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, pointer), \
121+
.swap_xy = DT_INST_PROP(inst, swap_xy), \
122+
.invert_x = DT_INST_PROP(inst, invert_x), \
123+
.invert_y = DT_INST_PROP(inst, invert_y), \
124+
}; \
125+
static struct lvgl_common_input_data lvgl_common_input_data_##inst; \
126+
DEVICE_DT_INST_DEFINE(inst, lvgl_pointer_input_init, NULL, &lvgl_common_input_data_##inst, \
127+
&lvgl_pointer_input_config_##inst, APPLICATION, \
128+
CONFIG_LV_Z_INPUT_INIT_PRIORITY, NULL);
129+
130+
DT_INST_FOREACH_STATUS_OKAY(LVGL_POINTER_INPUT_DEFINE)

0 commit comments

Comments
 (0)