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_keypad_input
8 
9 #include "lvgl_common_input.h"
10 #include "lvgl_keypad_input.h"
11 #include <zephyr/sys/util.h>
12 
13 #include <zephyr/logging/log.h>
14 
15 LOG_MODULE_DECLARE(lvgl, CONFIG_LV_Z_LOG_LEVEL);
16 
17 struct lvgl_keypad_input_config {
18 	struct lvgl_common_input_config common_config; /* Needs to be first member */
19 	const uint16_t *input_codes;
20 	const uint16_t *lvgl_codes;
21 	uint8_t num_codes;
22 };
23 
lvgl_keypad_process_event(struct input_event * evt,void * user_data)24 static void lvgl_keypad_process_event(struct input_event *evt, void *user_data)
25 {
26 	const struct device *dev = user_data;
27 	struct lvgl_common_input_data *data = dev->data;
28 	const struct lvgl_keypad_input_config *cfg = dev->config;
29 	uint8_t i;
30 
31 	for (i = 0; i < cfg->num_codes; i++) {
32 		if (evt->code == cfg->input_codes[i]) {
33 			data->pending_event.key = cfg->lvgl_codes[i];
34 			break;
35 		}
36 	}
37 
38 	if (i == cfg->num_codes) {
39 		LOG_WRN("Ignored input event: %u", evt->code);
40 		return;
41 	}
42 
43 	data->pending_event.state = evt->value ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
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 keypad queue");
46 	}
47 }
48 
lvgl_keypad_input_init(const struct device * dev)49 int lvgl_keypad_input_init(const struct device *dev)
50 {
51 	return lvgl_input_register_driver(LV_INDEV_TYPE_KEYPAD, dev);
52 }
53 
54 #define ASSERT_PROPERTIES(inst)                                                                    \
55 	BUILD_ASSERT(DT_INST_PROP_LEN(inst, input_codes) == DT_INST_PROP_LEN(inst, lvgl_codes),    \
56 		     "Property input-codes must have the same length as lvgl-codes.");
57 
58 #define LVGL_KEYPAD_INPUT_DEFINE(inst)                                                             \
59 	ASSERT_PROPERTIES(inst);                                                                   \
60 	LVGL_INPUT_DEFINE(inst, keypad, CONFIG_LV_Z_KEYPAD_INPUT_MSGQ_COUNT,                       \
61 			  lvgl_keypad_process_event);                                              \
62 	static const uint16_t lvgl_keypad_input_codes_##inst[] = DT_INST_PROP(inst, input_codes);  \
63 	static const uint16_t lvgl_keypad_lvgl_codes_##inst[] = DT_INST_PROP(inst, lvgl_codes);    \
64 	static const struct lvgl_keypad_input_config lvgl_keypad_input_config_##inst = {           \
65 		.common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, keypad),                  \
66 		.input_codes = lvgl_keypad_input_codes_##inst,                                     \
67 		.lvgl_codes = lvgl_keypad_lvgl_codes_##inst,                                       \
68 		.num_codes = DT_INST_PROP_LEN(inst, input_codes),                                  \
69 	};                                                                                         \
70 	static struct lvgl_common_input_data lvgl_common_input_data_##inst;                        \
71 	DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &lvgl_common_input_data_##inst,                    \
72 			      &lvgl_keypad_input_config_##inst, POST_KERNEL,                       \
73 			      CONFIG_INPUT_INIT_PRIORITY, NULL);
74 
75 DT_INST_FOREACH_STATUS_OKAY(LVGL_KEYPAD_INPUT_DEFINE)
76