2023-07-26 14:04:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2023 Fabian Blatz <fabianblatz@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT zephyr_lvgl_pointer_input
|
|
|
|
|
|
|
|
#include "lvgl_common_input.h"
|
2023-09-18 07:05:22 +02:00
|
|
|
#include "lvgl_pointer_input.h"
|
2023-07-26 14:04:42 +02:00
|
|
|
|
|
|
|
#include <lvgl_display.h>
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
|
2023-10-25 19:35:48 +02:00
|
|
|
LOG_MODULE_DECLARE(lvgl, CONFIG_LV_Z_LOG_LEVEL);
|
2023-07-26 14:04:42 +02:00
|
|
|
|
|
|
|
struct lvgl_pointer_input_config {
|
|
|
|
struct lvgl_common_input_config common_config; /* Needs to be first member */
|
|
|
|
bool swap_xy;
|
|
|
|
bool invert_x;
|
|
|
|
bool invert_y;
|
|
|
|
};
|
|
|
|
|
2024-03-21 09:06:17 +01:00
|
|
|
struct lvgl_pointer_input_data {
|
|
|
|
struct lvgl_common_input_data common_data;
|
|
|
|
uint32_t point_x;
|
|
|
|
uint32_t point_y;
|
|
|
|
};
|
|
|
|
|
2023-07-26 14:04:42 +02:00
|
|
|
static void lvgl_pointer_process_event(const struct device *dev, struct input_event *evt)
|
|
|
|
{
|
|
|
|
const struct lvgl_pointer_input_config *cfg = dev->config;
|
2024-03-21 09:06:17 +01:00
|
|
|
struct lvgl_pointer_input_data *data = dev->data;
|
2023-07-26 14:04:42 +02:00
|
|
|
lv_disp_t *disp = lv_disp_get_default();
|
|
|
|
struct lvgl_disp_data *disp_data = disp->driver->user_data;
|
|
|
|
struct display_capabilities *cap = &disp_data->cap;
|
2024-03-21 09:06:17 +01:00
|
|
|
lv_point_t *point = &data->common_data.pending_event.point;
|
2023-07-26 14:04:42 +02:00
|
|
|
|
|
|
|
switch (evt->code) {
|
|
|
|
case INPUT_ABS_X:
|
2024-03-21 09:06:17 +01:00
|
|
|
if (cfg->swap_xy) {
|
|
|
|
data->point_y = evt->value;
|
|
|
|
} else {
|
|
|
|
data->point_x = evt->value;
|
|
|
|
}
|
2023-07-26 14:04:42 +02:00
|
|
|
break;
|
|
|
|
case INPUT_ABS_Y:
|
2024-03-21 09:06:17 +01:00
|
|
|
if (cfg->swap_xy) {
|
|
|
|
data->point_x = evt->value;
|
|
|
|
} else {
|
|
|
|
data->point_y = evt->value;
|
|
|
|
}
|
2023-07-26 14:04:42 +02:00
|
|
|
break;
|
|
|
|
case INPUT_BTN_TOUCH:
|
2024-03-21 09:06:17 +01:00
|
|
|
data->common_data.pending_event.state =
|
|
|
|
evt->value ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
|
2023-07-26 14:04:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!evt->sync) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-21 09:06:17 +01:00
|
|
|
point->x = data->point_x;
|
|
|
|
point->y = data->point_y;
|
2023-07-26 14:04:42 +02:00
|
|
|
|
|
|
|
if (cfg->invert_x) {
|
|
|
|
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
|
|
|
|
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
|
2024-03-21 09:06:17 +01:00
|
|
|
point->x = cap->x_resolution - data->point_x;
|
2023-07-26 14:04:42 +02:00
|
|
|
} else {
|
2024-03-21 09:06:17 +01:00
|
|
|
point->x = cap->y_resolution - data->point_x;
|
2023-07-26 14:04:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg->invert_y) {
|
|
|
|
if (cap->current_orientation == DISPLAY_ORIENTATION_NORMAL ||
|
|
|
|
cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
|
2024-03-21 09:06:17 +01:00
|
|
|
point->y = cap->y_resolution - data->point_y;
|
2023-07-26 14:04:42 +02:00
|
|
|
} else {
|
2024-03-21 09:06:17 +01:00
|
|
|
point->y = cap->x_resolution - data->point_y;
|
2023-07-26 14:04:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rotate touch point to match display rotation */
|
|
|
|
if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_90) {
|
2024-03-21 09:06:17 +01:00
|
|
|
point->x = data->point_y;
|
|
|
|
point->y = cap->y_resolution - data->point_x;
|
2023-07-26 14:04:42 +02:00
|
|
|
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_180) {
|
2024-03-21 09:06:17 +01:00
|
|
|
point->x = cap->x_resolution - data->point_x;
|
|
|
|
point->y = cap->y_resolution - data->point_y;
|
2023-07-26 14:04:42 +02:00
|
|
|
} else if (cap->current_orientation == DISPLAY_ORIENTATION_ROTATED_270) {
|
2024-03-21 09:06:17 +01:00
|
|
|
point->x = cap->x_resolution - data->point_y;
|
|
|
|
point->y = data->point_x;
|
2023-07-26 14:04:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* filter readings within display */
|
|
|
|
if (point->x <= 0) {
|
|
|
|
point->x = 0;
|
|
|
|
} else if (point->x >= cap->x_resolution) {
|
|
|
|
point->x = cap->x_resolution - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (point->y <= 0) {
|
|
|
|
point->y = 0;
|
|
|
|
} else if (point->y >= cap->y_resolution) {
|
|
|
|
point->y = cap->y_resolution - 1;
|
|
|
|
}
|
|
|
|
|
2024-03-21 09:06:17 +01:00
|
|
|
if (k_msgq_put(cfg->common_config.event_msgq, &data->common_data.pending_event,
|
|
|
|
K_NO_WAIT) != 0) {
|
2023-07-26 14:04:42 +02:00
|
|
|
LOG_WRN("Could not put input data into queue");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 07:05:22 +02:00
|
|
|
int lvgl_pointer_input_init(const struct device *dev)
|
2023-07-26 14:04:42 +02:00
|
|
|
{
|
|
|
|
return lvgl_input_register_driver(LV_INDEV_TYPE_POINTER, dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LVGL_POINTER_INPUT_DEFINE(inst) \
|
|
|
|
LVGL_INPUT_DEFINE(inst, pointer, CONFIG_LV_Z_POINTER_INPUT_MSGQ_COUNT, \
|
|
|
|
lvgl_pointer_process_event); \
|
|
|
|
static const struct lvgl_pointer_input_config lvgl_pointer_input_config_##inst = { \
|
|
|
|
.common_config.event_msgq = &LVGL_INPUT_EVENT_MSGQ(inst, pointer), \
|
|
|
|
.swap_xy = DT_INST_PROP(inst, swap_xy), \
|
|
|
|
.invert_x = DT_INST_PROP(inst, invert_x), \
|
|
|
|
.invert_y = DT_INST_PROP(inst, invert_y), \
|
|
|
|
}; \
|
2024-03-21 09:06:17 +01:00
|
|
|
static struct lvgl_pointer_input_data lvgl_pointer_input_data_##inst; \
|
|
|
|
DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &lvgl_pointer_input_data_##inst, \
|
2023-09-13 15:15:12 +02:00
|
|
|
&lvgl_pointer_input_config_##inst, POST_KERNEL, \
|
2023-09-18 07:05:22 +02:00
|
|
|
CONFIG_INPUT_INIT_PRIORITY, NULL);
|
2023-07-26 14:04:42 +02:00
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(LVGL_POINTER_INPUT_DEFINE)
|