Files
Adam Shiervani 1809e271e6 fix: swap menu screen padding on display rotation (#637) (#1354)
The physical LCD panel (ST7789V, 300 rows on a 320-row controller) and
touch digitizer are misaligned by ~20px in the Y axis. LVGL's rotation
transform inverts the Y-to-X mapping between 90° and 270°, so the touch
calibration offset must be applied only at 90°.

Changes:
- Swap flex_screen_menu_style padding at 90°/270° (centering fix)
- Apply +20px touch Y calibration via lv_evdev_set_calibration() at 90°
- Recalibrate on rotation change in lvgl_set_rotation()
- Remove fragile framebuffer-based rotation centering e2e test
2026-03-28 16:59:10 +01:00

230 lines
6.7 KiB
C

#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include "log.h"
#include "screen.h"
#include <lvgl.h>
// #include "st7789/lcd.h"
#include "ui/ui.h"
#include "ui_index.h"
#include "ctrl.h"
// #define DISP_BUF_SIZE (300 * 240 * 2)
// static lv_color_t buf[DISP_BUF_SIZE];
lv_display_t *disp = NULL;
lv_indev_t *touch_indev = NULL;
indev_handler_t *indev_handler = NULL;
void lvgl_set_indev_handler(indev_handler_t *handler) {
indev_handler = handler;
}
void handle_indev_event(lv_event_t *e) {
if (indev_handler == NULL) {
return;
}
indev_handler(lv_event_get_code(e));
}
// The physical LCD panel and touch digitizer are misaligned by ~20 pixels in
// the Y axis (the 300px dimension). Because LVGL's rotation transform inverts
// the Y-to-X mapping between 90° and 270°, the calibration offset must flip
// direction with rotation. The evdev calibration remaps the raw touch Y range
// so that coordinates shift by +TOUCH_Y_OFFSET at 90° and -TOUCH_Y_OFFSET at
// 270°, keeping touch aligned with visible content at both orientations.
#define TOUCH_Y_OFFSET 20
static u_int16_t current_rotation = 270;
static void apply_touch_calibration(void) {
if (touch_indev == NULL) return;
if (current_rotation == 90) {
// At 90°, physical Y maps to logical X inverted (x = 299 - y).
// Shift Y by +TOUCH_Y_OFFSET to align touch with content.
lv_evdev_set_calibration(touch_indev,
0, -TOUCH_Y_OFFSET,
239, 299 - TOUCH_Y_OFFSET);
log_info("touch calibration: rotation=%d, Y offset=+%d", current_rotation, TOUCH_Y_OFFSET);
} else {
// At 270° (and 0°/180°), no calibration offset needed.
lv_evdev_set_calibration(touch_indev, 0, 0, 239, 299);
log_info("touch calibration: rotation=%d, no offset", current_rotation);
}
}
static void evdev_discovery_cb(lv_indev_t *indev, lv_evdev_type_t type, void *user_data) {
LV_UNUSED(user_data);
// Only handle touchscreen (absolute pointer devices)
if (type != LV_EVDEV_TYPE_ABS) {
return;
}
log_info("[C-UI-INIT] touchscreen discovered, configuring...");
lv_indev_set_group(indev, lv_group_get_default());
lv_indev_set_display(indev, disp);
lv_indev_add_event_cb(indev, handle_indev_event, LV_EVENT_ALL, NULL);
touch_indev = indev;
apply_touch_calibration();
log_info("[C-UI-INIT] touchscreen configured successfully");
}
void lvgl_init(u_int16_t rotation) {
log_trace("initalizing lvgl");
/*LittlevGL init*/
lv_init();
/*Linux frame buffer device init*/
disp = lv_linux_fbdev_create();
// lv_display_set_physical_resolution(disp, 240, 300);
lv_display_set_resolution(disp, 240, 300);
lv_linux_fbdev_set_file(disp, "/dev/fb0");
lvgl_set_rotation(disp, rotation);
log_info("[C-UI-INIT] step 4/6: initializing input device discovery");
if (lv_evdev_discovery_start(evdev_discovery_cb, NULL) != LV_RESULT_OK) {
log_warn("[C-UI-INIT] step 4/6: evdev discovery failed to start, touchscreen may not work");
} else {
log_info("[C-UI-INIT] step 4/6: evdev discovery started");
}
log_trace("initalizing ui");
ui_init();
ui_set_rpc_handler((jetkvm_rpc_handler_t *)jetkvm_call_rpc_handler);
log_info("ui initalized");
}
void lvgl_tick(void) {
lv_timer_handler();
ui_tick();
}
void lvgl_set_rotation(lv_display_t *disp_ref, u_int16_t rotation) {
if (disp_ref == NULL) {
disp_ref = disp;
}
log_info("setting rotation to %d", rotation);
if (rotation == 0) {
lv_display_set_rotation(disp_ref, LV_DISP_ROTATION_0);
} else if (rotation == 90) {
lv_display_set_rotation(disp_ref, LV_DISP_ROTATION_90);
} else if (rotation == 180) {
lv_display_set_rotation(disp_ref, LV_DISP_ROTATION_180);
} else if (rotation == 270) {
lv_display_set_rotation(disp_ref, LV_DISP_ROTATION_270);
} else {
log_error("invalid rotation %d", rotation);
}
current_rotation = rotation;
apply_touch_calibration();
lv_style_t *flex_screen_style = ui_get_style("flex_screen");
if (flex_screen_style == NULL) {
log_error("flex_screen style not found");
return;
}
lv_style_t *flex_screen_menu_style = ui_get_style("flex_screen_menu");
if (flex_screen_menu_style == NULL) {
log_error("flex_screen_menu style not found");
return;
}
if (rotation == 90) {
lv_style_set_pad_left(flex_screen_style, 24);
lv_style_set_pad_right(flex_screen_style, 44);
lv_style_set_pad_left(flex_screen_menu_style, 24);
lv_style_set_pad_right(flex_screen_menu_style, 44);
} else if (rotation == 270) {
lv_style_set_pad_left(flex_screen_style, 44);
lv_style_set_pad_right(flex_screen_style, 24);
lv_style_set_pad_left(flex_screen_menu_style, 44);
lv_style_set_pad_right(flex_screen_menu_style, 24);
}
log_info("refreshing objects");
lv_obj_report_style_change(flex_screen_style);
lv_obj_report_style_change(flex_screen_menu_style);
}
uint32_t custom_tick_get(void)
{
static uint64_t start_ms = 0;
if(start_ms == 0) {
struct timeval tv_start;
gettimeofday(&tv_start, NULL);
start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
}
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
uint64_t now_ms;
now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;
uint32_t time_ms = now_ms - start_ms;
return time_ms;
}
lv_obj_t *ui_get_obj(const char *name) {
for (size_t i = 0; i < ui_objects_size; i++) {
if (strcmp(ui_objects[i].name, name) == 0) {
return *ui_objects[i].obj;
}
}
return NULL;
}
lv_style_t *ui_get_style(const char *name) {
for (size_t i = 0; i < ui_styles_size; i++) {
if (strcmp(ui_styles[i].name, name) == 0) {
return ui_styles[i].getter();
}
}
return NULL;
}
const char *ui_get_current_screen() {
lv_obj_t *scr = lv_scr_act();
if (scr == NULL) {
return NULL;
}
for (size_t i = 0; i < ui_objects_size; i++) {
if (*(ui_objects[i].obj) == scr) {
return ui_objects[i].name;
}
}
return NULL;
}
const lv_img_dsc_t *ui_get_image(const char *name) {
for (size_t i = 0; i < ui_images_size; i++) {
if (strcmp(ui_images[i].name, name) == 0) {
return ui_images[i].img;
}
}
return NULL;
}
void ui_set_text(const char *name, const char *text) {
lv_obj_t *obj = ui_get_obj(name);
if(obj == NULL) {
log_error("ui_set_text %s %s, obj not found", name, text);
return;
}
lv_label_set_text(obj, text);
}