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