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_encoder_input
8 
9 #include "lvgl_common_input.h"
10 #include "lvgl_encoder_input.h"
11 
12 #include <zephyr/logging/log.h>
13 
14 LOG_MODULE_DECLARE(lvgl, CONFIG_LV_Z_LOG_LEVEL);
15 
16 struct lvgl_encoder_input_config {
17 	struct lvgl_common_input_config common_config; /* Needs to be first member */
18 	int rotation_input_code;
19 	int button_input_code;
20 };
21 
lvgl_encoder_process_event(struct input_event * evt,void * user_data)22 static void lvgl_encoder_process_event(struct input_event *evt, void *user_data)
23 {
24 	const struct device *dev = user_data;
25 	struct lvgl_common_input_data *data = dev->data;
26 	const struct lvgl_encoder_input_config *cfg = dev->config;
27 
28 	if (evt->code == cfg->rotation_input_code) {
29 		data->pending_event.enc_diff = evt->value;
30 	} else if (evt->code == cfg->button_input_code) {
31 		data->pending_event.state =
32 			evt->value ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
33 		data->pending_event.enc_diff = 0;
34 		data->pending_event.key = LV_KEY_ENTER;
35 	} else {
36 		LOG_DBG("Ignored input event: %u", evt->code);
37 		return;
38 	}
39 
40 	if (k_msgq_put(cfg->common_config.event_msgq, &data->pending_event, K_NO_WAIT) != 0) {
41 		LOG_WRN("Could not put input data into queue");
42 	}
43 }
44 
lvgl_encoder_input_init(const struct device * dev)45 int lvgl_encoder_input_init(const struct device *dev)
46 {
47 	return lvgl_input_register_driver(LV_INDEV_TYPE_ENCODER, dev);
48 }
49 
50 #define BUTTON_CODE(inst)   DT_INST_PROP_OR(inst, button_input_code, -1)
51 #define ROTATION_CODE(inst) DT_INST_PROP(inst, rotation_input_code)
52 
53 #define ASSERT_PROPERTIES(inst)                                                                    \
54 	BUILD_ASSERT(IN_RANGE(ROTATION_CODE(inst), 0, 65536),                                      \
55 		     "Property rotation-input-code needs to be between 0 and 65536.");             \
56 	BUILD_ASSERT(!DT_INST_NODE_HAS_PROP(inst, button_input_code) ||                            \
57 			     IN_RANGE(BUTTON_CODE(inst), 0, 65536),                                \
58 		     "Property button-input-code needs to be between 0 and 65536.");               \
59 	BUILD_ASSERT(ROTATION_CODE(inst) != BUTTON_CODE(inst),                                     \
60 		     "Property rotation-input-code and button-input-code should not be equal.")
61 
62 #define LVGL_ENCODER_INPUT_DEFINE(inst)                                                            \
63 	ASSERT_PROPERTIES(inst);                                                                   \
64 	LVGL_INPUT_DEFINE(inst, encoder, CONFIG_LV_Z_ENCODER_INPUT_MSGQ_COUNT,                     \
65 			  lvgl_encoder_process_event);                                             \
66 	static const struct lvgl_encoder_input_config lvgl_encoder_input_config_##inst = {         \
67 		.common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, encoder),                 \
68 		.rotation_input_code = ROTATION_CODE(inst),                                        \
69 		.button_input_code = BUTTON_CODE(inst),                                            \
70 	};                                                                                         \
71 	static struct lvgl_common_input_data lvgl_common_input_data_##inst;                        \
72 	DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &lvgl_common_input_data_##inst,                    \
73 			      &lvgl_encoder_input_config_##inst, POST_KERNEL,                      \
74 			      CONFIG_INPUT_INIT_PRIORITY, NULL);
75 
76 DT_INST_FOREACH_STATUS_OKAY(LVGL_ENCODER_INPUT_DEFINE)
77