Fix touchscreen offset to match temperature calibration.

Apply XPT2046 rotation-2 transform and the same landscape map used by the climate project.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-17 17:48:36 +08:00
parent b54e36b487
commit fe4b085641
5 changed files with 89 additions and 39 deletions

View File

@@ -3,39 +3,88 @@
#include <Arduino.h>
static void soft_spi_write(uint8_t data) {
for (int i = 7; i >= 0; --i) {
digitalWrite(CYD_TOUCH_MOSI, (data >> i) & 1);
digitalWrite(CYD_TOUCH_CLK, HIGH);
delayMicroseconds(1);
digitalWrite(CYD_TOUCH_CLK, LOW);
delayMicroseconds(1);
}
}
// SoftSPI bitbang XPT2046 — 坐标变换对齐 temperature 工程(setRotation(2) + map)
static uint16_t soft_spi_read12() {
uint16_t v = 0;
for (int i = 0; i < 12; ++i) {
digitalWrite(CYD_TOUCH_CLK, HIGH);
delayMicroseconds(1);
v = (v << 1) | (digitalRead(CYD_TOUCH_MISO) ? 1 : 0);
digitalWrite(CYD_TOUCH_CLK, LOW);
delayMicroseconds(1);
}
// 丢掉多余时钟位
static void clk_pulse() {
digitalWrite(CYD_TOUCH_CLK, HIGH);
delayMicroseconds(1);
digitalWrite(CYD_TOUCH_CLK, LOW);
return v;
delayMicroseconds(1);
}
static uint16_t xpt_read(uint8_t cmd) {
static void soft_spi_write8(uint8_t data) {
for (int i = 7; i >= 0; --i) {
digitalWrite(CYD_TOUCH_MOSI, (data >> i) & 1);
clk_pulse();
}
}
// 模拟 library 的 transfer16 >> 3(12-bit ADC)
static uint16_t soft_spi_transfer16_shr3() {
uint16_t v = 0;
for (int i = 0; i < 16; ++i) {
digitalWrite(CYD_TOUCH_CLK, HIGH);
delayMicroseconds(1);
v = (v << 1) | (digitalRead(CYD_TOUCH_MISO) ? 1u : 0u);
digitalWrite(CYD_TOUCH_CLK, LOW);
delayMicroseconds(1);
}
return v >> 3;
}
static uint16_t xpt_cmd_read(uint8_t cmd) {
soft_spi_write8(cmd);
return soft_spi_transfer16_shr3();
}
static int16_t best_two_avg(int16_t a, int16_t b, int16_t c) {
const int16_t ab = abs(a - b);
const int16_t ac = abs(a - c);
const int16_t bc = abs(b - c);
if (ab <= ac && ab <= bc) {
return (a + b) >> 1;
}
if (ac <= ab && ac <= bc) {
return (a + c) >> 1;
}
return (b + c) >> 1;
}
// 返回 true 并写出 raw(已做 rotation=2,与 XPT2046_Touchscreen 一致)
static bool touch_sample(int16_t *out_x, int16_t *out_y) {
digitalWrite(CYD_TOUCH_CS, LOW);
soft_spi_write(cmd);
delayMicroseconds(5);
uint16_t v = soft_spi_read12();
// Z 压力(参考 Stoffregen 库)
int16_t z1 = (int16_t)xpt_cmd_read(0xB1);
int16_t z2 = (int16_t)xpt_cmd_read(0xC1);
int z = z1 + 4095;
z -= (int16_t)xpt_cmd_read(0x91);
int16_t data[6] = {0, 0, 0, 0, 0, 0};
if (z >= 300) {
(void)xpt_cmd_read(0x91); // dummy X
data[0] = (int16_t)xpt_cmd_read(0xD1); // Y
data[1] = (int16_t)xpt_cmd_read(0x91); // X
data[2] = (int16_t)xpt_cmd_read(0xD1);
data[3] = (int16_t)xpt_cmd_read(0x91);
}
data[4] = (int16_t)xpt_cmd_read(0xD0); // Y power down
data[5] = (int16_t)xpt_cmd_read(0x00);
digitalWrite(CYD_TOUCH_CS, HIGH);
return v;
if (z < 300) {
return false;
}
// 库内:x=avg(Y采样), y=avg(X采样)
const int16_t avg_y_cmd = best_two_avg(data[0], data[2], data[4]);
const int16_t avg_x_cmd = best_two_avg(data[1], data[3], data[5]);
// setRotation(2)
*out_x = avg_x_cmd;
*out_y = 4095 - avg_y_cmd;
return true;
}
void touch_init() {
@@ -46,23 +95,20 @@ void touch_init() {
pinMode(CYD_TOUCH_IRQ, INPUT);
digitalWrite(CYD_TOUCH_CS, HIGH);
digitalWrite(CYD_TOUCH_CLK, LOW);
Serial.println("Touch: XPT2046 SoftSPI ready");
Serial.println("Touch: SoftSPI calib = temperature map (rot2)");
}
void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
const bool irq = (digitalRead(CYD_TOUCH_IRQ) == LOW);
uint16_t raw_x = xpt_read(0x90);
uint16_t raw_y = xpt_read(0xD0);
if (!irq || raw_x < 80 || raw_y < 80 || raw_x > 4000 || raw_y > 4000) {
int16_t p_x = 0;
int16_t p_y = 0;
if (!touch_sample(&p_x, &p_y)) {
data->state = LV_INDEV_STATE_RELEASED;
return;
}
// LV_DISPLAY_ROTATION_270 → 逻辑 320x240
int16_t x = map((int)raw_y, 200, 3800, 0, UI_HOR_RES - 1);
int16_t y = map((int)raw_x, 240, 3700, 0, UI_VER_RES - 1);
// 与 temperature/src/main.cpp 完全一致(逻辑横屏 320x240)
int16_t x = map(p_y, 240, 3800, 1, UI_HOR_RES);
int16_t y = map(p_x, 200, 3700, UI_VER_RES, 1);
if (x < 0) {
x = 0;