Skip to content

Commit b296d11

Browse files
faxe1008carlescufi
authored andcommitted
input: add zephyr,lvgl-button-input device binding
Add a pseudo device which can be used to hook into gpio-keys input_events and relay the events to a lv_indev. Signed-off-by: Fabian Blatz <[email protected]>
1 parent 116ec2c commit b296d11

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed
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 button indev pseudo-device
6+
7+
Listens for button input events and routes the
8+
lv_indev_data_t to the underlying button lv_indev_t managed by LVGL.
9+
10+
Example configuration:
11+
12+
pointer {
13+
compatible = "zephyr,lvgl-button-input";
14+
input = <&buttons>;
15+
input-codes = <INPUT_KEY_0 INPUT_KEY_1>;
16+
coordinates = <120 220>, <150 250>;
17+
};
18+
19+
When the device receives an input_event with code INPUT_KEY_0
20+
a click event will be performed at (120,220).
21+
22+
compatible: "zephyr,lvgl-button-input"
23+
24+
include: zephyr,lvgl-common-input.yaml
25+
26+
properties:
27+
input-codes:
28+
type: array
29+
required: true
30+
description: |
31+
Array of input event key codes (INPUT_KEY_* or INPUT_BTN_*).
32+
33+
coordinates:
34+
type: array
35+
description: |
36+
Array of points (x,y) the associated input-code is mapped to.

modules/lvgl/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,11 @@ zephyr_library_sources(
222222
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)
225-
zephyr_library_sources_ifdef(CONFIG_LV_Z_POINTER_KSCAN input/lvgl_kscan_pointer.c)
226225

227226
zephyr_library_sources(input/lvgl_common_input.c)
227+
zephyr_library_sources_ifdef(CONFIG_LV_Z_POINTER_KSCAN input/lvgl_pointer_kscan.c)
228228
zephyr_library_sources_ifdef(CONFIG_LV_Z_POINTER_INPUT input/lvgl_pointer_input.c)
229+
zephyr_library_sources_ifdef(CONFIG_LV_Z_BUTTON_INPUT input/lvgl_button_input.c)
229230

230231
zephyr_library_link_libraries(LVGL)
231232
target_link_libraries(LVGL INTERFACE zephyr_interface)

modules/lvgl/Kconfig.input

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,17 @@ config LV_Z_POINTER_INPUT_MSGQ_COUNT
5555
help
5656
Size of the pointer message queue buffering input events.
5757

58+
config LV_Z_BUTTON_INPUT
59+
bool "Input lvgl button"
60+
default y
61+
depends on INPUT
62+
depends on DT_HAS_ZEPHYR_LVGL_BUTTON_INPUT_ENABLED
63+
64+
config LV_Z_BUTTON_INPUT_MSGQ_COUNT
65+
int "Input button queue message count"
66+
default 4
67+
depends on LV_Z_BUTTON_INPUT
68+
help
69+
Size of the button message queue buffering input events.
70+
5871
endmenu
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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_button_input
8+
9+
#include "lvgl_common_input.h"
10+
11+
#include <zephyr/logging/log.h>
12+
13+
LOG_MODULE_DECLARE(lvgl);
14+
15+
struct lvgl_button_input_config {
16+
struct lvgl_common_input_config common_config; /* Needs to be first member */
17+
const uint16_t *input_codes;
18+
uint8_t num_codes;
19+
const lv_coord_t *coordinates;
20+
};
21+
22+
static void lvgl_button_process_event(const struct device *dev, struct input_event *evt)
23+
{
24+
struct lvgl_common_input_data *data = dev->data;
25+
const struct lvgl_button_input_config *cfg = dev->config;
26+
uint8_t i;
27+
28+
for (i = 0; i < cfg->num_codes; i++) {
29+
if (evt->code == cfg->input_codes[i]) {
30+
break;
31+
}
32+
}
33+
34+
if (i == cfg->num_codes) {
35+
LOG_DBG("Ignored input event: %u", evt->code);
36+
return;
37+
}
38+
39+
data->pending_event.btn_id = i;
40+
data->pending_event.state = evt->value ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
41+
42+
if (k_msgq_put(cfg->common_config.event_msgq, &data->pending_event, K_NO_WAIT) != 0) {
43+
LOG_WRN("Could not put input data into queue");
44+
}
45+
}
46+
47+
static int lvgl_button_input_init(const struct device *dev)
48+
{
49+
struct lvgl_common_input_data *data = dev->data;
50+
const struct lvgl_button_input_config *cfg = dev->config;
51+
int ret;
52+
53+
ret = lvgl_input_register_driver(LV_INDEV_TYPE_BUTTON, dev);
54+
if (ret < 0) {
55+
return ret;
56+
}
57+
58+
lv_indev_set_button_points(data->indev, (const lv_point_t *)cfg->coordinates);
59+
60+
return ret;
61+
}
62+
63+
#define ASSERT_PROPERTIES(inst) \
64+
BUILD_ASSERT(DT_INST_PROP_LEN(inst, input_codes) * 2 == \
65+
DT_INST_PROP_LEN(inst, coordinates), \
66+
"Property coordinates must contain one coordinate per input-code.")
67+
68+
#define LVGL_BUTTON_INPUT_DEFINE(inst) \
69+
ASSERT_PROPERTIES(inst); \
70+
LVGL_INPUT_DEFINE(inst, button, CONFIG_LV_Z_BUTTON_INPUT_MSGQ_COUNT, \
71+
lvgl_button_process_event); \
72+
static const uint16_t lvgl_button_input_codes_##inst[] = DT_INST_PROP(inst, input_codes); \
73+
static const lv_coord_t lvgl_button_coordinates_##inst[] = \
74+
DT_INST_PROP(inst, coordinates); \
75+
static const struct lvgl_button_input_config lvgl_button_input_config_##inst = { \
76+
.common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, button), \
77+
.input_codes = lvgl_button_input_codes_##inst, \
78+
.num_codes = DT_INST_PROP_LEN(inst, input_codes), \
79+
.coordinates = lvgl_button_coordinates_##inst, \
80+
}; \
81+
static struct lvgl_common_input_data lvgl_common_input_data_##inst; \
82+
DEVICE_DT_INST_DEFINE(inst, lvgl_button_input_init, NULL, &lvgl_common_input_data_##inst, \
83+
&lvgl_button_input_config_##inst, APPLICATION, \
84+
CONFIG_LV_Z_INPUT_INIT_PRIORITY, NULL);
85+
86+
DT_INST_FOREACH_STATUS_OKAY(LVGL_BUTTON_INPUT_DEFINE)

0 commit comments

Comments
 (0)