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);
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 lv_coord_t *coordinates;
21 };
22 
lvgl_button_process_event(const struct device * dev,struct input_event * evt)23 static void lvgl_button_process_event(const struct device *dev, struct input_event *evt)
24 {
25 	struct lvgl_common_input_data *data = dev->data;
26 	const struct lvgl_button_input_config *cfg = dev->config;
27 	uint8_t i;
28 
29 	for (i = 0; i < cfg->num_codes; i++) {
30 		if (evt->code == cfg->input_codes[i]) {
31 			break;
32 		}
33 	}
34 
35 	if (i == cfg->num_codes) {
36 		LOG_DBG("Ignored input event: %u", evt->code);
37 		return;
38 	}
39 
40 	data->pending_event.btn_id = i;
41 	data->pending_event.state = evt->value ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
42 
43 	if (k_msgq_put(cfg->common_config.event_msgq, &data->pending_event, K_NO_WAIT) != 0) {
44 		LOG_WRN("Could not put input data into queue");
45 	}
46 }
47 
lvgl_button_input_init(const struct device * dev)48 int lvgl_button_input_init(const struct device *dev)
49 {
50 	struct lvgl_common_input_data *data = dev->data;
51 	const struct lvgl_button_input_config *cfg = dev->config;
52 	int ret;
53 
54 	ret = lvgl_input_register_driver(LV_INDEV_TYPE_BUTTON, dev);
55 	if (ret < 0) {
56 		return ret;
57 	}
58 
59 	lv_indev_set_button_points(data->indev, (const lv_point_t *)cfg->coordinates);
60 
61 	return ret;
62 }
63 
64 #define ASSERT_PROPERTIES(inst)                                                                    \
65 	BUILD_ASSERT(DT_INST_PROP_LEN(inst, input_codes) * 2 ==                                    \
66 			     DT_INST_PROP_LEN(inst, coordinates),                                  \
67 		     "Property coordinates must contain one coordinate per input-code.")
68 
69 #define LVGL_BUTTON_INPUT_DEFINE(inst)                                                             \
70 	ASSERT_PROPERTIES(inst);                                                                   \
71 	LVGL_INPUT_DEFINE(inst, button, CONFIG_LV_Z_BUTTON_INPUT_MSGQ_COUNT,                       \
72 			  lvgl_button_process_event);                                              \
73 	static const uint16_t lvgl_button_input_codes_##inst[] = DT_INST_PROP(inst, input_codes);  \
74 	static const lv_coord_t lvgl_button_coordinates_##inst[] =                                 \
75 		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