Skip to content

Commit 5e5d9f4

Browse files
faxe1008carlescufi
authored andcommitted
modules: lvgl: Move lvgl kscan pointer handling to separate file
Moves lvgl pointer driver handling based on kscan to its own file, to ease deletion when the usage of kscan for display touch input has been deprecated. Signed-off-by: Fabian Blatz <[email protected]>
1 parent c536bd3 commit 5e5d9f4

File tree

3 files changed

+155
-144
lines changed

3 files changed

+155
-144
lines changed

modules/lvgl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ zephyr_library_sources(
222222
zephyr_library_sources_ifdef(CONFIG_LV_Z_USE_FILESYSTEM lvgl_fs.c)
223223
zephyr_library_sources_ifdef(CONFIG_LV_Z_MEM_POOL_SYS_HEAP lvgl_mem.c)
224224
zephyr_library_sources_ifdef(CONFIG_LV_Z_SHELL lvgl_shell.c)
225+
zephyr_library_sources_ifdef(CONFIG_LV_Z_POINTER_KSCAN input/lvgl_kscan_pointer.c)
225226

226227
zephyr_library_sources(input/lvgl_common_input.c)
227228
zephyr_library_sources_ifdef(CONFIG_LV_Z_POINTER_INPUT input/lvgl_pointer_input.c)
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 2023 Fabian Blatz <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <lvgl.h>
8+
#include <zephyr/drivers/kscan.h>
9+
#include <zephyr/kernel.h>
10+
#include "lvgl_display.h"
11+
12+
#include <zephyr/logging/log.h>
13+
LOG_MODULE_DECLARE(lvgl);
14+
15+
static lv_indev_drv_t indev_drv;
16+
#define KSCAN_NODE DT_CHOSEN(zephyr_keyboard_scan)
17+
18+
K_MSGQ_DEFINE(kscan_msgq, sizeof(lv_indev_data_t), CONFIG_LV_Z_POINTER_KSCAN_MSGQ_COUNT, 4);
19+
20+
static void lvgl_pointer_kscan_callback(const struct device *dev, uint32_t row, uint32_t col,
21+
bool pressed)
22+
{
23+
lv_indev_data_t data = {
24+
.point.x = col,
25+
.point.y = row,
26+
.state = pressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL,
27+
};
28+
29+
if (k_msgq_put(&kscan_msgq, &data, K_NO_WAIT) != 0) {
30+
LOG_DBG("Could not put input data into queue");
31+
}
32+
}
33+
34+
static void lvgl_pointer_kscan_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
35+
{
36+
lv_disp_t *disp;
37+
struct lvgl_disp_data *disp_data;
38+
struct display_capabilities *cap;
39+
lv_indev_data_t curr;
40+
41+
static lv_indev_data_t prev = {
42+
.point.x = 0,
43+
.point.y = 0,
44+
.state = LV_INDEV_STATE_REL,
45+
};
46+
47+
if (k_msgq_get(&kscan_msgq, &curr, K_NO_WAIT) != 0) {
48+
goto set_and_release;
49+
}
50+
51+
prev = curr;
52+
53+
disp = lv_disp_get_default();
54+
disp_data = disp->driver->user_data;
55+
cap = &disp_data->cap;
56+
57+
/* adjust kscan coordinates */
58+
if (IS_ENABLED(CONFIG_LV_Z_POINTER_KSCAN_SWAP_XY)) {
59+
lv_coord_t x;
60+
61+
x = prev.point.x;
62+
prev.point.x = prev.point.y;
63+
prev.point.y = x;
64+
}
65+
66+
if (IS_ENABLED(CONFIG_LV_Z_POINTER_KSCAN_INVERT_X)) {
67+
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
68+
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
69+
prev.point.x = cap->x_resolution - prev.point.x;
70+
} else {
71+
prev.point.x = cap->y_resolution - prev.point.x;
72+
}
73+
}
74+
75+
if (IS_ENABLED(CONFIG_LV_Z_POINTER_KSCAN_INVERT_Y)) {
76+
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
77+
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
78+
prev.point.y = cap->y_resolution - prev.point.y;
79+
} else {
80+
prev.point.y = cap->x_resolution - prev.point.y;
81+
}
82+
}
83+
84+
/* rotate touch point to match display rotation */
85+
if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_90) {
86+
lv_coord_t x;
87+
88+
x = prev.point.x;
89+
prev.point.x = prev.point.y;
90+
prev.point.y = cap->y_resolution - x;
91+
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
92+
prev.point.x = cap->x_resolution - prev.point.x;
93+
prev.point.y = cap->y_resolution - prev.point.y;
94+
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_270) {
95+
lv_coord_t x;
96+
97+
x = prev.point.x;
98+
prev.point.x = cap->x_resolution - prev.point.y;
99+
prev.point.y = x;
100+
}
101+
102+
/* filter readings within display */
103+
if (prev.point.x <= 0) {
104+
prev.point.x = 0;
105+
} else if (prev.point.x >= cap->x_resolution) {
106+
prev.point.x = cap->x_resolution - 1;
107+
}
108+
109+
if (prev.point.y <= 0) {
110+
prev.point.y = 0;
111+
} else if (prev.point.y >= cap->y_resolution) {
112+
prev.point.y = cap->y_resolution - 1;
113+
}
114+
115+
set_and_release:
116+
*data = prev;
117+
118+
data->continue_reading = k_msgq_num_used_get(&kscan_msgq) > 0;
119+
}
120+
121+
static int lvgl_kscan_pointer_init(void)
122+
{
123+
const struct device *kscan_dev = DEVICE_DT_GET(KSCAN_NODE);
124+
125+
if (!device_is_ready(kscan_dev)) {
126+
LOG_ERR("Keyboard scan device not ready.");
127+
return -ENODEV;
128+
}
129+
130+
if (kscan_config(kscan_dev, lvgl_pointer_kscan_callback) < 0) {
131+
LOG_ERR("Could not configure keyboard scan device.");
132+
return -ENODEV;
133+
}
134+
135+
lv_indev_drv_init(&indev_drv);
136+
indev_drv.type = LV_INDEV_TYPE_POINTER;
137+
indev_drv.read_cb = lvgl_pointer_kscan_read;
138+
139+
if (lv_indev_drv_register(&indev_drv) == NULL) {
140+
LOG_ERR("Failed to register input device.");
141+
return -EPERM;
142+
}
143+
144+
kscan_enable_callback(kscan_dev);
145+
146+
return 0;
147+
}
148+
149+
SYS_INIT(lvgl_kscan_pointer_init, APPLICATION, CONFIG_LV_Z_INPUT_INIT_PRIORITY);

modules/lvgl/lvgl.c

Lines changed: 5 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#ifdef CONFIG_LV_Z_USE_FILESYSTEM
1212
#include "lvgl_fs.h"
1313
#endif
14-
#ifdef CONFIG_LV_Z_POINTER_KSCAN
15-
#include <zephyr/drivers/kscan.h>
16-
#endif
1714
#include LV_MEM_CUSTOM_INCLUDE
1815

1916
#define LOG_LEVEL CONFIG_LV_LOG_LEVEL
@@ -24,12 +21,8 @@ static lv_disp_drv_t disp_drv;
2421
struct lvgl_disp_data disp_data = {
2522
.blanking_on = false,
2623
};
27-
#ifdef CONFIG_LV_Z_POINTER_KSCAN
28-
static lv_indev_drv_t indev_drv;
29-
#endif /* CONFIG_LV_Z_POINTER_KSCAN */
3024

3125
#define DISPLAY_NODE DT_CHOSEN(zephyr_display)
32-
#define KSCAN_NODE DT_CHOSEN(zephyr_keyboard_scan)
3326

3427
#ifdef CONFIG_LV_Z_BUFFER_ALLOC_STATIC
3528

@@ -195,139 +188,6 @@ static int lvgl_allocate_rendering_buffers(lv_disp_drv_t *disp_driver)
195188
}
196189
#endif /* CONFIG_LV_Z_BUFFER_ALLOC_STATIC */
197190

198-
#ifdef CONFIG_LV_Z_POINTER_KSCAN
199-
K_MSGQ_DEFINE(kscan_msgq, sizeof(lv_indev_data_t), CONFIG_LV_Z_POINTER_KSCAN_MSGQ_COUNT, 4);
200-
201-
static void lvgl_pointer_kscan_callback(const struct device *dev, uint32_t row, uint32_t col,
202-
bool pressed)
203-
{
204-
lv_indev_data_t data = {
205-
.point.x = col,
206-
.point.y = row,
207-
.state = pressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL,
208-
};
209-
210-
if (k_msgq_put(&kscan_msgq, &data, K_NO_WAIT) != 0) {
211-
LOG_DBG("Could not put input data into queue");
212-
}
213-
}
214-
215-
static void lvgl_pointer_kscan_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
216-
{
217-
lv_disp_t *disp;
218-
struct lvgl_disp_data *disp_data;
219-
struct display_capabilities *cap;
220-
lv_indev_data_t curr;
221-
222-
static lv_indev_data_t prev = {
223-
.point.x = 0,
224-
.point.y = 0,
225-
.state = LV_INDEV_STATE_REL,
226-
};
227-
228-
if (k_msgq_get(&kscan_msgq, &curr, K_NO_WAIT) != 0) {
229-
goto set_and_release;
230-
}
231-
232-
prev = curr;
233-
234-
disp = lv_disp_get_default();
235-
disp_data = disp->driver->user_data;
236-
cap = &disp_data->cap;
237-
238-
/* adjust kscan coordinates */
239-
if (IS_ENABLED(CONFIG_LV_Z_POINTER_KSCAN_SWAP_XY)) {
240-
lv_coord_t x;
241-
242-
x = prev.point.x;
243-
prev.point.x = prev.point.y;
244-
prev.point.y = x;
245-
}
246-
247-
if (IS_ENABLED(CONFIG_LV_Z_POINTER_KSCAN_INVERT_X)) {
248-
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
249-
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
250-
prev.point.x = cap->x_resolution - prev.point.x;
251-
} else {
252-
prev.point.x = cap->y_resolution - prev.point.x;
253-
}
254-
}
255-
256-
if (IS_ENABLED(CONFIG_LV_Z_POINTER_KSCAN_INVERT_Y)) {
257-
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
258-
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
259-
prev.point.y = cap->y_resolution - prev.point.y;
260-
} else {
261-
prev.point.y = cap->x_resolution - prev.point.y;
262-
}
263-
}
264-
265-
/* rotate touch point to match display rotation */
266-
if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_90) {
267-
lv_coord_t x;
268-
269-
x = prev.point.x;
270-
prev.point.x = prev.point.y;
271-
prev.point.y = cap->y_resolution - x;
272-
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
273-
prev.point.x = cap->x_resolution - prev.point.x;
274-
prev.point.y = cap->y_resolution - prev.point.y;
275-
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_270) {
276-
lv_coord_t x;
277-
278-
x = prev.point.x;
279-
prev.point.x = cap->x_resolution - prev.point.y;
280-
prev.point.y = x;
281-
}
282-
283-
/* filter readings within display */
284-
if (prev.point.x <= 0) {
285-
prev.point.x = 0;
286-
} else if (prev.point.x >= cap->x_resolution) {
287-
prev.point.x = cap->x_resolution - 1;
288-
}
289-
290-
if (prev.point.y <= 0) {
291-
prev.point.y = 0;
292-
} else if (prev.point.y >= cap->y_resolution) {
293-
prev.point.y = cap->y_resolution - 1;
294-
}
295-
296-
set_and_release:
297-
*data = prev;
298-
299-
data->continue_reading = k_msgq_num_used_get(&kscan_msgq) > 0;
300-
}
301-
302-
static int lvgl_pointer_kscan_init(void)
303-
{
304-
const struct device *kscan_dev = DEVICE_DT_GET(KSCAN_NODE);
305-
306-
if (!device_is_ready(kscan_dev)) {
307-
LOG_ERR("Keyboard scan device not ready.");
308-
return -ENODEV;
309-
}
310-
311-
if (kscan_config(kscan_dev, lvgl_pointer_kscan_callback) < 0) {
312-
LOG_ERR("Could not configure keyboard scan device.");
313-
return -ENODEV;
314-
}
315-
316-
lv_indev_drv_init(&indev_drv);
317-
indev_drv.type = LV_INDEV_TYPE_POINTER;
318-
indev_drv.read_cb = lvgl_pointer_kscan_read;
319-
320-
if (lv_indev_drv_register(&indev_drv) == NULL) {
321-
LOG_ERR("Failed to register input device.");
322-
return -EPERM;
323-
}
324-
325-
kscan_enable_callback(kscan_dev);
326-
327-
return 0;
328-
}
329-
#endif /* CONFIG_LV_Z_POINTER_KSCAN */
330-
331191
static int lvgl_init(void)
332192
{
333193

@@ -375,11 +235,12 @@ static int lvgl_init(void)
375235
return -EPERM;
376236
}
377237

378-
#ifdef CONFIG_LV_Z_POINTER_KSCAN
379-
lvgl_pointer_kscan_init();
380-
#endif /* CONFIG_LV_Z_POINTER_KSCAN */
381-
382238
return 0;
383239
}
384240

241+
BUILD_ASSERT(CONFIG_APPLICATION_INIT_PRIORITY < CONFIG_LV_Z_INPUT_INIT_PRIORITY);
242+
#ifdef CONFIG_INPUT
243+
BUILD_ASSERT(CONFIG_INPUT_INIT_PRIORITY < CONFIG_LV_Z_INPUT_INIT_PRIORITY);
244+
#endif /* CONFIG_INPUT */
245+
385246
SYS_INIT(lvgl_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);

0 commit comments

Comments
 (0)