Restore LVGL touch input with SoftSPI and temperature map.
Bind indev to the display, relax IRQ checks, and keep prebuilt firmware in sync. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
124
src/touch.cpp
124
src/touch.cpp
@@ -3,88 +3,43 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
// SoftSPI bitbang XPT2046 — 坐标变换对齐 temperature 工程(setRotation(2) + map)
|
||||
/**
|
||||
* SoftSPI XPT2046 → LVGL pointer indev
|
||||
* 读数用简单稳定协议(此前能点到);坐标变换对齐 temperature(rot2 + map 320x240)。
|
||||
*/
|
||||
|
||||
static void clk_pulse() {
|
||||
digitalWrite(CYD_TOUCH_CLK, HIGH);
|
||||
delayMicroseconds(1);
|
||||
digitalWrite(CYD_TOUCH_CLK, LOW);
|
||||
delayMicroseconds(1);
|
||||
}
|
||||
|
||||
static void soft_spi_write8(uint8_t data) {
|
||||
static void soft_write(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);
|
||||
delayMicroseconds(2);
|
||||
digitalWrite(CYD_TOUCH_CLK, LOW);
|
||||
delayMicroseconds(1);
|
||||
delayMicroseconds(2);
|
||||
}
|
||||
return v >> 3;
|
||||
}
|
||||
|
||||
static uint16_t xpt_cmd_read(uint8_t cmd) {
|
||||
soft_spi_write8(cmd);
|
||||
return soft_spi_transfer16_shr3();
|
||||
static uint16_t soft_read12() {
|
||||
uint16_t v = 0;
|
||||
digitalWrite(CYD_TOUCH_CLK, HIGH);
|
||||
delayMicroseconds(2);
|
||||
digitalWrite(CYD_TOUCH_CLK, LOW);
|
||||
delayMicroseconds(2);
|
||||
for (int i = 0; i < 12; ++i) {
|
||||
digitalWrite(CYD_TOUCH_CLK, HIGH);
|
||||
delayMicroseconds(2);
|
||||
v = (uint16_t)((v << 1) | (digitalRead(CYD_TOUCH_MISO) ? 1u : 0u));
|
||||
digitalWrite(CYD_TOUCH_CLK, LOW);
|
||||
delayMicroseconds(2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
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) {
|
||||
static uint16_t xpt_read(uint8_t cmd) {
|
||||
digitalWrite(CYD_TOUCH_CS, LOW);
|
||||
|
||||
// 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);
|
||||
|
||||
soft_write(cmd);
|
||||
uint16_t v = soft_read12();
|
||||
digitalWrite(CYD_TOUCH_CS, HIGH);
|
||||
|
||||
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;
|
||||
return v;
|
||||
}
|
||||
|
||||
void touch_init() {
|
||||
@@ -95,18 +50,30 @@ void touch_init() {
|
||||
pinMode(CYD_TOUCH_IRQ, INPUT);
|
||||
digitalWrite(CYD_TOUCH_CS, HIGH);
|
||||
digitalWrite(CYD_TOUCH_CLK, LOW);
|
||||
Serial.println("Touch: SoftSPI calib = temperature map (rot2)");
|
||||
Serial.println("Touch: LVGL indev SoftSPI");
|
||||
}
|
||||
|
||||
void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
||||
int16_t p_x = 0;
|
||||
int16_t p_y = 0;
|
||||
if (!touch_sample(&p_x, &p_y)) {
|
||||
// 不依赖 IRQ(部分板子 IRQ 不可靠):用采样有效性判断
|
||||
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);
|
||||
|
||||
const bool irq = (digitalRead(CYD_TOUCH_IRQ) == LOW);
|
||||
const bool in_range =
|
||||
(raw_x > 80 && raw_x < 4000 && raw_y > 80 && raw_y < 4000);
|
||||
|
||||
if (!in_range || (!irq && (raw_x < 200 || raw_y < 200))) {
|
||||
data->state = LV_INDEV_STATE_RELEASED;
|
||||
return;
|
||||
}
|
||||
|
||||
// 与 temperature/src/main.cpp 完全一致(逻辑横屏 320x240)
|
||||
// XPT2046_Touchscreen setRotation(2)
|
||||
const int16_t p_x = (int16_t)raw_x;
|
||||
const int16_t p_y = (int16_t)(4095 - raw_y);
|
||||
|
||||
// temperature 横屏逻辑坐标(UI 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);
|
||||
|
||||
@@ -123,6 +90,13 @@ void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
||||
y = UI_VER_RES - 1;
|
||||
}
|
||||
|
||||
static uint32_t last_log = 0;
|
||||
if (millis() - last_log > 250) {
|
||||
last_log = millis();
|
||||
Serial.printf("LVGL touch irq=%d raw(%u,%u) -> (%d,%d)\n", irq ? 1 : 0, raw_x,
|
||||
raw_y, x, y);
|
||||
}
|
||||
|
||||
data->point.x = x;
|
||||
data->point.y = y;
|
||||
data->state = LV_INDEV_STATE_PRESSED;
|
||||
|
||||
Reference in New Issue
Block a user