Align touch and draw in one 320x240 space like CYD LVGL9.

Drop LVGL ROTATION_270 double-transform; SoftSPI rot3 maps directly to screen pixels.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-17 18:14:26 +08:00
parent 3768e545c0
commit 1381c221d9
7 changed files with 72 additions and 45 deletions

View File

@@ -4,9 +4,12 @@
#include <Arduino.h>
/**
* SoftSPI XPT2046(与 SD 的 VSPI 分脚,互不抢总线)
* 标定对齐 temperature(rot2 + map 到 UI 320x240),再反变换喂给 LVGL
* ROTATION_270(见 lv_indev.c):out=(y_in, 239-x_in)
* SoftSPI XPT2046 — 与 SD 分脚
*
* 对齐官方 CYD LVGL9 例程(witnessmenow):
* - 显示直接建为 320x240,LVGL rotation=0(触控不再被二次变换)
* - touch setRotation(3) + map 到 SCREEN_WIDTH/HEIGHT
* 参考:https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/Examples/LVGL9/
*/
int16_t g_dbg_phys_x = -1;
@@ -57,64 +60,60 @@ void touch_init() {
pinMode(CYD_TOUCH_IRQ, INPUT);
digitalWrite(CYD_TOUCH_CS, HIGH);
digitalWrite(CYD_TOUCH_CLK, LOW);
Serial.println("Touch: SoftSPI + temp map + LVGL inverse");
Serial.println("Touch: SoftSPI rot3 map 320x240 (no LVGL rot)");
}
void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
uint16_t raw_x = xpt_read(0x90);
uint16_t raw_y = xpt_read(0xD0);
raw_x = (uint16_t)((raw_x + xpt_read(0x90)) / 2);
raw_y = (uint16_t)((raw_y + xpt_read(0xD0)) / 2);
uint16_t adc_x = xpt_read(0x90);
uint16_t adc_y = xpt_read(0xD0);
adc_x = (uint16_t)((adc_x + xpt_read(0x90)) / 2);
adc_y = (uint16_t)((adc_y + xpt_read(0xD0)) / 2);
const bool irq = (digitalRead(CYD_TOUCH_IRQ) == LOW);
const bool in_range =
(raw_x > 80 && raw_x < 4000 && raw_y > 80 && raw_y < 4000);
(adc_x > 80 && adc_x < 4000 && adc_y > 80 && adc_y < 4000);
if (!in_range || (!irq && (raw_x < 200 || raw_y < 200))) {
if (!in_range || (!irq && (adc_x < 200 || adc_y < 200))) {
data->state = LV_INDEV_STATE_RELEASED;
g_dbg_touch_down = false;
return;
}
// XPT2046_Touchscreen setRotation(2)
const int16_t p_x = (int16_t)raw_x;
const int16_t p_y = (int16_t)(4095 - raw_y);
// XPT2046_Touchscreen setRotation(3):
// xraw = 4095 - Y_sample, yraw = 4095 - X_sample
const int16_t p_x = (int16_t)(4095 - adc_y);
const int16_t p_y = (int16_t)(4095 - adc_x);
// temperature 横屏 UI 坐标
int16_t lx = map(p_y, 240, 3800, 1, UI_HOR_RES);
int16_t ly = map(p_x, 200, 3700, UI_VER_RES, 1);
int16_t x = map(p_x, 200, 3700, 1, SCREEN_WIDTH);
int16_t y = map(p_y, 240, 3800, 1, SCREEN_HEIGHT);
if (lx < 0) {
lx = 0;
if (x < 0) {
x = 0;
}
if (ly < 0) {
ly = 0;
if (y < 0) {
y = 0;
}
if (lx >= UI_HOR_RES) {
lx = UI_HOR_RES - 1;
if (x >= SCREEN_WIDTH) {
x = SCREEN_WIDTH - 1;
}
if (ly >= UI_VER_RES) {
ly = UI_VER_RES - 1;
if (y >= SCREEN_HEIGHT) {
y = SCREEN_HEIGHT - 1;
}
// 反变换:让 LVGL ROTATION_270 之后落到 (lx, ly)
const int16_t x_in = (int16_t)((SCREEN_WIDTH - 1) - ly);
const int16_t y_in = lx;
g_dbg_phys_x = p_x;
g_dbg_phys_y = p_y;
g_dbg_logic_x = lx;
g_dbg_logic_y = ly;
g_dbg_logic_x = x;
g_dbg_logic_y = y;
g_dbg_touch_down = true;
static uint32_t last_log = 0;
if (millis() - last_log > 120) {
last_log = millis();
Serial.printf("Touch raw(%u,%u) UI(%d,%d) feed(%d,%d)\n", raw_x, raw_y, lx,
ly, x_in, y_in);
Serial.printf("Touch adc(%u,%u) p(%d,%d) -> (%d,%d)\n", adc_x, adc_y, p_x,
p_y, x, y);
}
data->point.x = x_in;
data->point.y = y_in;
data->point.x = x;
data->point.y = y;
data->state = LV_INDEV_STATE_PRESSED;
}