1 /*
2 * Copyright 2023 Fabian Blatz <fabianblatz@gmail.com>
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 #include "lvgl_button_input.h"
11
12 #include <zephyr/logging/log.h>
13
14 LOG_MODULE_DECLARE(lvgl, CONFIG_LV_Z_LOG_LEVEL);
15
16 struct lvgl_button_input_config {
17 struct lvgl_common_input_config common_config; /* Needs to be first member */
18 const uint16_t *input_codes;
19 uint8_t num_codes;
20 const int32_t *coordinates;
21 };
22
lvgl_button_process_event(struct input_event * evt,void * user_data)23 static void lvgl_button_process_event(struct input_event *evt, void *user_data)
24 {
25 const struct device *dev = user_data;
26 struct lvgl_common_input_data *data = dev->data;
27 const struct lvgl_button_input_config *cfg = dev->config;
28 uint8_t i;
29
30 for (i = 0; i < cfg->num_codes; i++) {
31 if (evt->code == cfg->input_codes[i]) {
32 break;
33 }
34 }
35
36 if (i == cfg->num_codes) {
37 LOG_DBG("Ignored input event: %u", evt->code);
38 return;
39 }
40
41 data->pending_event.btn_id = i;
42 data->pending_event.state = evt->value ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
43
44 if (k_msgq_put(cfg->common_config.event_msgq, &data->pending_event, K_NO_WAIT) != 0) {
45 LOG_WRN("Could not put input data into queue");
46 }
47 }
48
lvgl_button_input_init(const struct device * dev)49 int lvgl_button_input_init(const struct device *dev)
50 {
51 struct lvgl_common_input_data *data = dev->data;
52 const struct lvgl_button_input_config *cfg = dev->config;
53 int ret;
54
55 ret = lvgl_input_register_driver(LV_INDEV_TYPE_BUTTON, dev);
56 if (ret < 0) {
57 return ret;
58 }
59
60 lv_indev_set_button_points(data->indev, (const lv_point_t *)cfg->coordinates);
61
62 return ret;
63 }
64
65 #define ASSERT_PROPERTIES(inst) \
66 BUILD_ASSERT(DT_INST_PROP_LEN(inst, input_codes) * 2 == \
67 DT_INST_PROP_LEN(inst, coordinates), \
68 "Property coordinates must contain one coordinate per input-code.")
69
70 #define LVGL_BUTTON_INPUT_DEFINE(inst) \
71 ASSERT_PROPERTIES(inst); \
72 LVGL_INPUT_DEFINE(inst, button, CONFIG_LV_Z_BUTTON_INPUT_MSGQ_COUNT, \
73 lvgl_button_process_event); \
74 static const uint16_t lvgl_button_input_codes_##inst[] = DT_INST_PROP(inst, input_codes); \
75 static const int32_t lvgl_button_coordinates_##inst[] = DT_INST_PROP(inst, coordinates); \
76 static const struct lvgl_button_input_config lvgl_button_input_config_##inst = { \
77 .common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, button), \
78 .input_codes = lvgl_button_input_codes_##inst, \
79 .num_codes = DT_INST_PROP_LEN(inst, input_codes), \
80 .coordinates = lvgl_button_coordinates_##inst, \
81 }; \
82 static struct lvgl_common_input_data lvgl_common_input_data_##inst; \
83 DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &lvgl_common_input_data_##inst, \
84 &lvgl_button_input_config_##inst, POST_KERNEL, \
85 CONFIG_INPUT_INIT_PRIORITY, NULL);
86
87 DT_INST_FOREACH_STATUS_OKAY(LVGL_BUTTON_INPUT_DEFINE)
88