1 /*
2 * Copyright 2023 Fabian Blatz <fabianblatz@gmail.com>
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 #include <lvgl_input_device.h>
12 #include "lvgl_pointer_input.h"
13 #include "lvgl_button_input.h"
14 #include "lvgl_encoder_input.h"
15
16 LOG_MODULE_DECLARE(lvgl);
17
lvgl_input_get_indev(const struct device * dev)18 lv_indev_t *lvgl_input_get_indev(const struct device *dev)
19 {
20 struct lvgl_common_input_data *common_data = dev->data;
21
22 return common_data->indev;
23 }
24
lvgl_input_read_cb(lv_indev_drv_t * drv,lv_indev_data_t * data)25 static void lvgl_input_read_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
26 {
27 const struct device *dev = drv->user_data;
28 const struct lvgl_common_input_config *cfg = dev->config;
29 struct lvgl_common_input_data *common_data = dev->data;
30
31 if (k_msgq_get(cfg->event_msgq, data, K_NO_WAIT) != 0) {
32 memcpy(data, &common_data->previous_event, sizeof(lv_indev_data_t));
33 if (drv->type == LV_INDEV_TYPE_ENCODER) {
34 data->enc_diff = 0; /* For encoders, clear last movement */
35 }
36 data->continue_reading = false;
37 return;
38 }
39
40 memcpy(&common_data->previous_event, data, sizeof(lv_indev_data_t));
41 data->continue_reading = k_msgq_num_used_get(cfg->event_msgq) > 0;
42 }
43
lvgl_input_register_driver(lv_indev_type_t indev_type,const struct device * dev)44 int lvgl_input_register_driver(lv_indev_type_t indev_type, const struct device *dev)
45 {
46 /* Currently no indev binding has its dedicated data
47 * if that ever changes ensure that `lvgl_common_input_data`
48 * remains the first member
49 */
50 struct lvgl_common_input_data *common_data = dev->data;
51
52 if (common_data == NULL) {
53 return -EINVAL;
54 }
55
56 lv_indev_drv_init(&common_data->indev_drv);
57 common_data->indev_drv.type = indev_type;
58 common_data->indev_drv.read_cb = lvgl_input_read_cb;
59 common_data->indev_drv.user_data = (void *)dev;
60 common_data->indev = lv_indev_drv_register(&common_data->indev_drv);
61
62 if (common_data->indev == NULL) {
63 return -EINVAL;
64 }
65
66 return 0;
67 }
68
69 #define LV_DEV_INIT(node_id, init_fn) \
70 do { \
71 int ret = init_fn(DEVICE_DT_GET(node_id)); \
72 if (ret) { \
73 return ret; \
74 } \
75 } while (0)
76
lvgl_init_input_devices(void)77 int lvgl_init_input_devices(void)
78 {
79 #ifdef CONFIG_LV_Z_POINTER_INPUT
80 DT_FOREACH_STATUS_OKAY_VARGS(zephyr_lvgl_pointer_input, LV_DEV_INIT,
81 lvgl_pointer_input_init);
82 #endif /* CONFIG_LV_Z_POINTER_INPUT */
83
84 #ifdef CONFIG_LV_Z_BUTTON_INPUT
85 DT_FOREACH_STATUS_OKAY_VARGS(zephyr_lvgl_button_input, LV_DEV_INIT, lvgl_button_input_init);
86 #endif /* CONFIG_LV_Z_BUTTON_INPUT */
87
88 #ifdef CONFIG_LV_Z_ENCODER_INPUT
89 DT_FOREACH_STATUS_OKAY_VARGS(zephyr_lvgl_encoder_input, LV_DEV_INIT,
90 lvgl_encoder_input_init);
91 #endif /* CONFIG_LV_Z_ENCODER_INPUT */
92
93 return 0;
94 }
95