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_pointer_input
8
9 #include "lvgl_common_input.h"
10 #include "lvgl_pointer_input.h"
11
12 #include <lvgl_display.h>
13 #include <zephyr/logging/log.h>
14
15 LOG_MODULE_DECLARE(lvgl, CONFIG_LV_Z_LOG_LEVEL);
16
17 struct lvgl_pointer_input_config {
18 struct lvgl_common_input_config common_config; /* Needs to be first member */
19 bool swap_xy;
20 bool invert_x;
21 bool invert_y;
22 };
23
24 struct lvgl_pointer_input_data {
25 struct lvgl_common_input_data common_data;
26 uint32_t point_x;
27 uint32_t point_y;
28 };
29
lvgl_pointer_process_event(struct input_event * evt,void * user_data)30 static void lvgl_pointer_process_event(struct input_event *evt, void *user_data)
31 {
32 const struct device *dev = user_data;
33 const struct lvgl_pointer_input_config *cfg = dev->config;
34 struct lvgl_pointer_input_data *data = dev->data;
35 lv_display_t *disp = lv_display_get_default();
36 struct lvgl_disp_data *disp_data = (struct lvgl_disp_data *)lv_display_get_user_data(disp);
37 struct display_capabilities *cap = &disp_data->cap;
38 lv_point_t *point = &data->common_data.pending_event.point;
39
40 switch (evt->code) {
41 case INPUT_ABS_X:
42 if (cfg->swap_xy) {
43 data->point_y = evt->value;
44 } else {
45 data->point_x = evt->value;
46 }
47 break;
48 case INPUT_ABS_Y:
49 if (cfg->swap_xy) {
50 data->point_x = evt->value;
51 } else {
52 data->point_y = evt->value;
53 }
54 break;
55 case INPUT_BTN_TOUCH:
56 data->common_data.pending_event.state =
57 evt->value ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
58 break;
59 }
60
61 if (!evt->sync) {
62 return;
63 }
64
65 lv_point_t tmp_point = {
66 .x = data->point_x,
67 .y = data->point_y,
68 };
69
70 if (cfg->invert_x) {
71 if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
72 cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
73 tmp_point.x = cap->x_resolution - tmp_point.x;
74 } else {
75 tmp_point.x = cap->y_resolution - tmp_point.x;
76 }
77 }
78
79 if (cfg->invert_y) {
80 if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
81 cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
82 tmp_point.y = cap->y_resolution - tmp_point.y;
83 } else {
84 tmp_point.y = cap->x_resolution - tmp_point.y;
85 }
86 }
87
88 /* rotate touch point to match display rotation */
89 switch (cap->current_orientation) {
90 case DISPLAY_ORIENTATION_NORMAL:
91 point->x = tmp_point.x;
92 point->y = tmp_point.y;
93 break;
94 case DISPLAY_ORIENTATION_ROTATED_90:
95 point->x = tmp_point.y;
96 point->y = cap->y_resolution - tmp_point.x;
97 break;
98 case DISPLAY_ORIENTATION_ROTATED_180:
99 point->x = cap->x_resolution - tmp_point.x;
100 point->y = cap->y_resolution - tmp_point.y;
101 break;
102 case DISPLAY_ORIENTATION_ROTATED_270:
103 point->x = cap->x_resolution - tmp_point.y;
104 point->y = tmp_point.x;
105 break;
106 default:
107 LOG_ERR("Invalid display orientation");
108 break;
109 }
110
111 /* filter readings within display */
112 if (point->x <= 0) {
113 point->x = 0;
114 } else if (point->x >= cap->x_resolution) {
115 point->x = cap->x_resolution - 1;
116 }
117
118 if (point->y <= 0) {
119 point->y = 0;
120 } else if (point->y >= cap->y_resolution) {
121 point->y = cap->y_resolution - 1;
122 }
123
124 if (k_msgq_put(cfg->common_config.event_msgq, &data->common_data.pending_event,
125 K_NO_WAIT) != 0) {
126 LOG_WRN("Could not put input data into queue");
127 }
128 }
129
lvgl_pointer_input_init(const struct device * dev)130 int lvgl_pointer_input_init(const struct device *dev)
131 {
132 return lvgl_input_register_driver(LV_INDEV_TYPE_POINTER, dev);
133 }
134
135 #define LVGL_POINTER_INPUT_DEFINE(inst) \
136 LVGL_INPUT_DEFINE(inst, pointer, CONFIG_LV_Z_POINTER_INPUT_MSGQ_COUNT, \
137 lvgl_pointer_process_event); \
138 static const struct lvgl_pointer_input_config lvgl_pointer_input_config_##inst = { \
139 .common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, pointer), \
140 .swap_xy = DT_INST_PROP(inst, swap_xy), \
141 .invert_x = DT_INST_PROP(inst, invert_x), \
142 .invert_y = DT_INST_PROP(inst, invert_y), \
143 }; \
144 static struct lvgl_pointer_input_data lvgl_pointer_input_data_##inst; \
145 DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &lvgl_pointer_input_data_##inst, \
146 &lvgl_pointer_input_config_##inst, POST_KERNEL, \
147 CONFIG_INPUT_INIT_PRIORITY, NULL);
148
149 DT_INST_FOREACH_STATUS_OKAY(LVGL_POINTER_INPUT_DEFINE)
150