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 = evt->value ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
32 data->pending_event.enc_diff = 0;
33 data->pending_event.key = LV_KEY_ENTER;
34 } else {
35 LOG_DBG("Ignored input event: %u", evt->code);
36 return;
37 }
38
39 if (k_msgq_put(cfg->common_config.event_msgq, &data->pending_event, K_NO_WAIT) != 0) {
40 LOG_WRN("Could not put input data into queue");
41 }
42 }
43
lvgl_encoder_input_init(const struct device * dev)44 int lvgl_encoder_input_init(const struct device *dev)
45 {
46 return lvgl_input_register_driver(LV_INDEV_TYPE_ENCODER, dev);
47 }
48
49 #define BUTTON_CODE(inst) DT_INST_PROP_OR(inst, button_input_code, -1)
50 #define ROTATION_CODE(inst) DT_INST_PROP(inst, rotation_input_code)
51
52 #define ASSERT_PROPERTIES(inst) \
53 BUILD_ASSERT(IN_RANGE(ROTATION_CODE(inst), 0, 65536), \
54 "Property rotation-input-code needs to be between 0 and 65536."); \
55 BUILD_ASSERT(!DT_INST_NODE_HAS_PROP(inst, button_input_code) || \
56 IN_RANGE(BUTTON_CODE(inst), 0, 65536), \
57 "Property button-input-code needs to be between 0 and 65536."); \
58 BUILD_ASSERT(ROTATION_CODE(inst) != BUTTON_CODE(inst), \
59 "Property rotation-input-code and button-input-code should not be equal.")
60
61 #define LVGL_ENCODER_INPUT_DEFINE(inst) \
62 ASSERT_PROPERTIES(inst); \
63 LVGL_INPUT_DEFINE(inst, encoder, CONFIG_LV_Z_ENCODER_INPUT_MSGQ_COUNT, \
64 lvgl_encoder_process_event); \
65 static const struct lvgl_encoder_input_config lvgl_encoder_input_config_##inst = { \
66 .common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, encoder), \
67 .rotation_input_code = ROTATION_CODE(inst), \
68 .button_input_code = BUTTON_CODE(inst), \
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_encoder_input_config_##inst, POST_KERNEL, \
73 CONFIG_INPUT_INIT_PRIORITY, NULL);
74
75 DT_INST_FOREACH_STATUS_OKAY(LVGL_ENCODER_INPUT_DEFINE)
76