Fix CYD touch with SoftSPI bitbang + RNT coords.
Use hexeguitar CYD28 SoftSPI (frees VSPI for SD), Random Nerd Tutorials 240x320 feed for LVGL ROTATION_270, and direct UI hit-test for REC/PLAY. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
57
src/main.cpp
57
src/main.cpp
@@ -77,6 +77,9 @@ static void set_backlight(uint8_t level) {
|
||||
ledcWrite(0, BRIGHT_PWM[bright_level]);
|
||||
}
|
||||
|
||||
static void do_rec_toggle(void);
|
||||
static void do_play_toggle(void);
|
||||
|
||||
static void show_hint(const char *text, uint32_t ms) {
|
||||
lv_label_set_text(lbl_hint, text);
|
||||
lv_obj_remove_flag(lbl_hint, LV_OBJ_FLAG_HIDDEN);
|
||||
@@ -90,30 +93,58 @@ static const char *basename_of(const char *path) {
|
||||
return p ? p + 1 : path;
|
||||
}
|
||||
|
||||
/* Mirror LVGL 9 ROTATION_270 indev transform (create 240×320). */
|
||||
static void native_to_ui(int16_t nx, int16_t ny, int16_t *ux, int16_t *uy) {
|
||||
int32_t x = nx;
|
||||
int32_t y = ny;
|
||||
x = SCREEN_WIDTH - x - 1;
|
||||
y = SCREEN_HEIGHT - y - 1;
|
||||
int32_t tmp = y;
|
||||
y = x;
|
||||
x = SCREEN_HEIGHT - tmp - 1;
|
||||
*ux = (int16_t)x;
|
||||
*uy = (int16_t)y;
|
||||
}
|
||||
|
||||
static void touchscreen_read(lv_indev_t *indev, lv_indev_data_t *data) {
|
||||
static bool was_pressed = false;
|
||||
static uint32_t press_ms = 0;
|
||||
static bool consumed = false;
|
||||
static int16_t press_ux = 0;
|
||||
static int16_t press_uy = 0;
|
||||
|
||||
touch_read_cb(indev, data);
|
||||
|
||||
if (data->state == LV_INDEV_STATE_PRESSED) {
|
||||
int16_t ux = 0, uy = 0;
|
||||
native_to_ui(data->point.x, data->point.y, &ux, &uy);
|
||||
if (!was_pressed) {
|
||||
was_pressed = true;
|
||||
press_ms = millis();
|
||||
consumed = false;
|
||||
press_ux = ux;
|
||||
press_uy = uy;
|
||||
Serial.printf("UI press (%d,%d)\n", (int)ux, (int)uy);
|
||||
} else {
|
||||
press_ux = ux;
|
||||
press_uy = uy;
|
||||
}
|
||||
} else if (was_pressed) {
|
||||
was_pressed = false;
|
||||
uint32_t held = millis() - press_ms;
|
||||
if (!consumed && held < 350) {
|
||||
/* Brightness only if press was not on a button — LVGL already handled
|
||||
* buttons; use short idle-area taps via header region. */
|
||||
if (data->point.y < 28) {
|
||||
if (held < 700) {
|
||||
/* Direct hit-test in landscape UI — bypass flaky LVGL button hitboxes. */
|
||||
if (press_uy < 36) {
|
||||
set_backlight((bright_level + 1) & 0x03);
|
||||
char buf[24];
|
||||
lv_snprintf(buf, sizeof(buf), "brightness %u/3", (unsigned)bright_level);
|
||||
show_hint(buf, 900);
|
||||
} else if (press_uy > 130) {
|
||||
if (press_ux < (UI_HOR_RES / 2)) {
|
||||
Serial.println("Hit → REC");
|
||||
do_rec_toggle();
|
||||
} else {
|
||||
Serial.println("Hit → PLAY");
|
||||
do_play_toggle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -281,13 +312,14 @@ static void create_ui(void) {
|
||||
lv_obj_set_style_text_color(lbl_sd, COL_MUTED, 0);
|
||||
lv_obj_align(lbl_sd, LV_ALIGN_TOP_LEFT, 0, 40 + UI_BAR_H + 28);
|
||||
|
||||
const int btn_y = 40 + UI_BAR_H + 52;
|
||||
const int btn_h = 56;
|
||||
const int btn_w = (content_w - 16 - UI_G) / 2;
|
||||
|
||||
btn_rec = lv_button_create(p_main);
|
||||
lv_obj_set_size(btn_rec, btn_w, UI_BTN_H);
|
||||
lv_obj_align(btn_rec, LV_ALIGN_TOP_LEFT, 0, btn_y);
|
||||
lv_obj_set_size(btn_rec, btn_w, btn_h);
|
||||
lv_obj_align(btn_rec, LV_ALIGN_BOTTOM_LEFT, 0, -18);
|
||||
style_btn(btn_rec, COL_REC);
|
||||
lv_obj_set_ext_click_area(btn_rec, 16);
|
||||
lv_obj_add_event_cb(btn_rec, on_rec_clicked, LV_EVENT_CLICKED, nullptr);
|
||||
lbl_rec = lv_label_create(btn_rec);
|
||||
lv_label_set_text(lbl_rec, "REC");
|
||||
@@ -297,9 +329,10 @@ static void create_ui(void) {
|
||||
lv_obj_center(lbl_rec);
|
||||
|
||||
btn_play = lv_button_create(p_main);
|
||||
lv_obj_set_size(btn_play, btn_w, UI_BTN_H);
|
||||
lv_obj_align(btn_play, LV_ALIGN_TOP_RIGHT, 0, btn_y);
|
||||
lv_obj_set_size(btn_play, btn_w, btn_h);
|
||||
lv_obj_align(btn_play, LV_ALIGN_BOTTOM_RIGHT, 0, -18);
|
||||
style_btn(btn_play, COL_ACCENT);
|
||||
lv_obj_set_ext_click_area(btn_play, 16);
|
||||
lv_obj_add_event_cb(btn_play, on_play_clicked, LV_EVENT_CLICKED, nullptr);
|
||||
lbl_play = lv_label_create(btn_play);
|
||||
lv_label_set_text(lbl_play, "PLAY");
|
||||
@@ -309,7 +342,7 @@ static void create_ui(void) {
|
||||
lv_obj_center(lbl_play);
|
||||
|
||||
lv_obj_t *hint2 = lv_label_create(p_main);
|
||||
lv_label_set_text(hint2, "tap header: brightness | max 120s");
|
||||
lv_label_set_text(hint2, "bottom: REC|PLAY header: BL BOOT/r: REC");
|
||||
lv_obj_set_style_text_font(hint2, &lv_font_montserrat_12, 0);
|
||||
lv_obj_set_style_text_color(hint2, COL_MUTED, 0);
|
||||
lv_obj_align(hint2, LV_ALIGN_BOTTOM_MID, 0, 0);
|
||||
|
||||
115
src/touch.cpp
115
src/touch.cpp
@@ -3,6 +3,18 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
/**
|
||||
* 2.8": CYD28 SoftSPI bit-bang (hexeguitar) — frees VSPI for microSD.
|
||||
* Coord feed matches Random Nerd Tutorials LVGL9 + ROTATION_270:
|
||||
* map raw → 240×320 native, let LVGL rotate to landscape UI.
|
||||
* 3.5C: GT911 Wire1; release INT before I2S SCK=21.
|
||||
*
|
||||
* Refs:
|
||||
* - https://randomnerdtutorials.com/esp32-cyd-display-touchscreen-microsd-card/
|
||||
* - https://github.com/hexeguitar/CYD28_Touchscreen
|
||||
* - https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/discussions/88
|
||||
*/
|
||||
|
||||
static int16_t clampi(int32_t v, int32_t lo, int32_t hi) {
|
||||
if (v < lo) return (int16_t)lo;
|
||||
if (v > hi) return (int16_t)hi;
|
||||
@@ -115,86 +127,32 @@ void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
||||
|
||||
#else
|
||||
|
||||
/* Bit-bang SoftSPI — does not share VSPI with microSD (tftest path). */
|
||||
#include <CYD28_TouchscreenR.h>
|
||||
|
||||
static void soft_write(uint8_t data) {
|
||||
for (int i = 7; i >= 0; --i) {
|
||||
digitalWrite(CYD_TOUCH_MOSI, (data >> i) & 1);
|
||||
digitalWrite(CYD_TOUCH_CLK, HIGH);
|
||||
delayMicroseconds(10);
|
||||
digitalWrite(CYD_TOUCH_CLK, LOW);
|
||||
delayMicroseconds(10);
|
||||
}
|
||||
}
|
||||
|
||||
static uint16_t soft_read16(void) {
|
||||
uint16_t v = 0;
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
digitalWrite(CYD_TOUCH_CLK, HIGH);
|
||||
delayMicroseconds(10);
|
||||
v = (uint16_t)((v << 1) | (digitalRead(CYD_TOUCH_MISO) ? 1u : 0u));
|
||||
digitalWrite(CYD_TOUCH_CLK, LOW);
|
||||
delayMicroseconds(10);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/* One CS transaction: Z, X, Y (closer to XPT2046 lib sequence). */
|
||||
static void xpt_sample(uint16_t *x, uint16_t *y, uint16_t *z) {
|
||||
digitalWrite(SD_CS, HIGH);
|
||||
digitalWrite(CYD_TOUCH_CS, LOW);
|
||||
delayMicroseconds(10);
|
||||
|
||||
soft_write(0xB1); /* Z1 */
|
||||
uint16_t z1 = soft_read16() >> 3;
|
||||
soft_write(0xC1); /* Z2 */
|
||||
uint16_t z2 = soft_read16() >> 3;
|
||||
*z = (uint16_t)((z1 + 4095) - z2);
|
||||
|
||||
soft_write(0x91); /* dummy X */
|
||||
(void)soft_read16();
|
||||
soft_write(0xD1); /* Y */
|
||||
uint16_t y0 = soft_read16() >> 3;
|
||||
soft_write(0x91); /* X */
|
||||
uint16_t x0 = soft_read16() >> 3;
|
||||
soft_write(0xD0); /* Y power-down */
|
||||
uint16_t y1 = soft_read16() >> 3;
|
||||
soft_write(0x00);
|
||||
uint16_t x1 = soft_read16() >> 3;
|
||||
|
||||
digitalWrite(CYD_TOUCH_CS, HIGH);
|
||||
|
||||
*x = (uint16_t)((x0 + x1) / 2);
|
||||
*y = (uint16_t)((y0 + y1) / 2);
|
||||
}
|
||||
/* Native panel size — LVGL create(240,320) + ROTATION_270. */
|
||||
static CYD28_TouchR ts(SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
|
||||
void touch_init() {
|
||||
pinMode(SD_CS, OUTPUT);
|
||||
digitalWrite(SD_CS, HIGH);
|
||||
pinMode(CYD_TOUCH_CS, OUTPUT);
|
||||
pinMode(CYD_TOUCH_CLK, OUTPUT);
|
||||
pinMode(CYD_TOUCH_MOSI, OUTPUT);
|
||||
pinMode(CYD_TOUCH_MISO, INPUT);
|
||||
pinMode(CYD_TOUCH_IRQ, INPUT);
|
||||
digitalWrite(CYD_TOUCH_CS, HIGH);
|
||||
digitalWrite(CYD_TOUCH_CLK, LOW);
|
||||
Serial.println("Touch: SoftSPI Z/X/Y (SD-safe, no VSPI)");
|
||||
ts.begin();
|
||||
ts.setThreshold(40); /* idle z~10; press usually >>100 */
|
||||
Serial.println("Touch: CYD28 SoftSPI bitbang (SD-safe, poll) RNT map");
|
||||
}
|
||||
|
||||
void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
||||
uint16_t adc_x = 0, adc_y = 0, adc_z = 0;
|
||||
xpt_sample(&adc_x, &adc_y, &adc_z);
|
||||
digitalWrite(SD_CS, HIGH);
|
||||
ts.isrWake = true;
|
||||
|
||||
/* SoftSPI Z on this board is noisy (~4095 idle); use X/Y window only.
|
||||
* Idle typically x≈50; a real press lands both axes in mid-range. */
|
||||
(void)adc_z;
|
||||
const bool down =
|
||||
(adc_x > 200 && adc_x < 3900 && adc_y > 200 && adc_y < 3900);
|
||||
CYD28_TS_Point p = ts.getPointRaw();
|
||||
/* z threshold only — x/y stay 0 until press updates them in library */
|
||||
const bool down = (p.z >= 40);
|
||||
|
||||
static uint32_t last_log = 0;
|
||||
if (down && millis() - last_log > 200) {
|
||||
if (millis() - last_log > (down ? 120u : 500u)) {
|
||||
last_log = millis();
|
||||
Serial.printf("Touch soft x=%u y=%u\n", (unsigned)adc_x, (unsigned)adc_y);
|
||||
Serial.printf("Touch raw(%d,%d) z=%d down=%d\n", (int)p.x, (int)p.y,
|
||||
(int)p.z, (int)down);
|
||||
}
|
||||
|
||||
if (!down) {
|
||||
@@ -202,24 +160,15 @@ void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* tftest SoftSPI → native feed for LVGL ROTATION_270 */
|
||||
const int16_t p_x = (int16_t)adc_x;
|
||||
const int16_t p_y = (int16_t)(4095 - adc_y);
|
||||
|
||||
int16_t x = map(p_x, 200, 3700, 1, SCREEN_WIDTH);
|
||||
int16_t y = map(p_y, 240, 3800, 1, SCREEN_HEIGHT);
|
||||
/* Random Nerd Tutorials LVGL9 mapping → native 240×320 feed
|
||||
* (lv_display_set_rotation 270 transforms to 320×240 UI). */
|
||||
int16_t x = (int16_t)map(p.x, 200, 3700, 1, SCREEN_WIDTH);
|
||||
int16_t y = (int16_t)map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
|
||||
x = clampi(x, 0, SCREEN_WIDTH - 1);
|
||||
y = clampi(y, 0, SCREEN_HEIGHT - 1);
|
||||
|
||||
const int32_t old_ux = y;
|
||||
const int32_t old_uy = (SCREEN_WIDTH - 1) - x;
|
||||
int16_t ui_x = clampi((old_uy * (UI_HOR_RES - 1)) / (UI_VER_RES - 1), 0,
|
||||
UI_HOR_RES - 1);
|
||||
int16_t ui_y = clampi((old_ux * (UI_VER_RES - 1)) / (UI_HOR_RES - 1), 0,
|
||||
UI_VER_RES - 1);
|
||||
|
||||
data->point.x = (int16_t)((SCREEN_WIDTH - 1) - ui_y);
|
||||
data->point.y = ui_x;
|
||||
data->point.x = x;
|
||||
data->point.y = y;
|
||||
data->state = LV_INDEV_STATE_PRESSED;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user