diff --git a/README.md b/README.md index 11564c0..d721891 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ cd firmware\cyd_28r | `cyd_28r` | 标准 ILI9341 CYD(默认) | | `cyd_28r_inv` | 颜色反相 / 发白 | | `cyd_28_st7789` | USB-C 双口 / ST7789 变体 | +| `cyd_35r` | 3.5" ESP32-3248S035(ST7796 + GT911) | ## 3D 外壳 @@ -137,3 +138,4 @@ git reset --hard v1.1-case-snaps | 2026-10-17 | UI 重排:修文字压图表;表盘/底栏分区;动画与短按/长按交互 | | 2026-10-17 | 顶栏改单行,避免 up 文案被表盘面板盖住 | | 2026-10-17 | 归档预编译固件到 `firmware/cyd_28r/`,附一键刷写脚本 | +| 2026-10-25 | 3.5" 表盘读数:数值+单位整体居中,数值变宽后不再盖住单位 | diff --git a/firmware/README.md b/firmware/README.md index 030d949..828cf69 100644 --- a/firmware/README.md +++ b/firmware/README.md @@ -1,17 +1,19 @@ # 预编译固件(可随时刷写) -当前归档目录:[`cyd_28r/`](cyd_28r/) +| 目录 | 板子 | +|------|------| +| [`cyd_28r/`](cyd_28r/) | CYD ESP32-2432S028R(2.8" ILI9341),DHT11 → GPIO22 | +| [`cyd_35r/`](cyd_35r/) | CYD ESP32-3248S035(3.5" ST7796 + GT911),DHT11 → GPIO22 | -对应板子:**CYD ESP32-2432S028R**(ILI9341),DHT11 → GPIO22。 -版本信息见 [`cyd_28r/VERSION.txt`](cyd_28r/VERSION.txt)。 +版本信息见各目录 `VERSION.txt`。 ## 直接刷写(无需重新编译) 1. USB 连接板子 -2. 在本目录执行: +2. 进入对应目录执行: ```powershell -cd firmware\cyd_28r +cd firmware\cyd_28r # 或 cyd_35r .\flash.ps1 # 或指定端口 .\flash.ps1 -Port COM3 @@ -41,6 +43,8 @@ esptool.py --chip esp32 --port COM3 --baud 921600 write_flash -z ^ ```powershell .\firmware\export_from_build.ps1 +.\firmware\export_from_build.ps1 -Env cyd_35r # 仅复制、不编译: .\firmware\export_from_build.ps1 -SkipBuild +.\firmware\export_from_build.ps1 -Env cyd_35r -SkipBuild ``` diff --git a/firmware/cyd_35r/VERSION.txt b/firmware/cyd_35r/VERSION.txt new file mode 100644 index 0000000..9790289 --- /dev/null +++ b/firmware/cyd_35r/VERSION.txt @@ -0,0 +1,15 @@ +name: CYD temperature (cyd_35r) +git: de177b2 +date: 2026-10-25 +board: ESP32-3248S035 (ST7796) +flash_size: 4MB +offsets: + 0x1000 bootloader.bin + 0x8000 partitions.bin + 0xe000 boot_app0.bin + 0x10000 firmware.bin +sha256: + bootloader.bin 3D234A7471F67B013686DABD4DEE7C1FA915C9928463616A94BC9297ACF1ABF8 + boot_app0.bin F94C5D786A7A8FAB06AC5D10E33BF37711A6697636DC037559EA19CC410A17F0 + firmware.bin A8033223DDE79874B4CA41AFE3DBB247938184123FB66BCAA6EAF7F5C1D32516 + partitions.bin 0A8B5720E7B77FF11F1462458C3A509DEE79224E5279898F26D6A2E3AE0517B7 diff --git a/firmware/cyd_35r/boot_app0.bin b/firmware/cyd_35r/boot_app0.bin new file mode 100644 index 0000000..13562ca Binary files /dev/null and b/firmware/cyd_35r/boot_app0.bin differ diff --git a/firmware/cyd_35r/bootloader.bin b/firmware/cyd_35r/bootloader.bin new file mode 100644 index 0000000..d592359 Binary files /dev/null and b/firmware/cyd_35r/bootloader.bin differ diff --git a/firmware/cyd_35r/firmware.bin b/firmware/cyd_35r/firmware.bin new file mode 100644 index 0000000..9f36b36 Binary files /dev/null and b/firmware/cyd_35r/firmware.bin differ diff --git a/firmware/cyd_35r/flash.bat b/firmware/cyd_35r/flash.bat new file mode 100644 index 0000000..bf983b3 --- /dev/null +++ b/firmware/cyd_35r/flash.bat @@ -0,0 +1,5 @@ +@echo off +setlocal +cd /d "%~dp0" +powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0flash.ps1" %* +exit /b %ERRORLEVEL% diff --git a/firmware/cyd_35r/flash.ps1 b/firmware/cyd_35r/flash.ps1 new file mode 100644 index 0000000..6a4251b --- /dev/null +++ b/firmware/cyd_35r/flash.ps1 @@ -0,0 +1,84 @@ +# Flash archived CYD climate firmware (no rebuild needed) +# Usage: +# .\flash.ps1 +# .\flash.ps1 -Port COM5 +# .\flash.ps1 -Port COM5 -Baud 460800 + +param( + [string]$Port = "", + [int]$Baud = 921600 +) + +$ErrorActionPreference = "Stop" +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +Set-Location $here + +function Find-Esptool { + $pio = Get-Command pio -ErrorAction SilentlyContinue + if ($pio) { + $pkg = Join-Path $env:USERPROFILE ".platformio\packages\tool-esptoolpy\esptool.py" + if (Test-Path $pkg) { return @{ Kind = "py"; Path = $pkg } } + } + $py = Get-Command python -ErrorAction SilentlyContinue + if ($py) { + try { + & python -c "import esptool" 2>$null + if ($LASTEXITCODE -eq 0) { return @{ Kind = "mod"; Path = "esptool" } } + } catch {} + } + $esp = Get-Command esptool.py -ErrorAction SilentlyContinue + if ($esp) { return @{ Kind = "py"; Path = $esp.Source } } + return $null +} + +if (-not $Port) { + $ports = @() + try { + Add-Type -AssemblyName System.IO.Ports -ErrorAction SilentlyContinue | Out-Null + $ports = [System.IO.Ports.SerialPort]::GetPortNames() | Sort-Object + } catch {} + if (-not $ports -or $ports.Count -eq 0) { + $ports = Get-CimInstance Win32_SerialPort -ErrorAction SilentlyContinue | + Select-Object -ExpandProperty DeviceID | Sort-Object + } + if (-not $ports -or $ports.Count -eq 0) { + Write-Error "No serial ports found. Plug in the CYD USB cable, then retry with -Port COMx" + } + if ($ports.Count -eq 1) { + $Port = $ports[0] + } else { + Write-Host "Available ports: $($ports -join ', ')" + $Port = $ports[-1] + Write-Host "Using last port: $Port (override with -Port)" + } +} + +$tool = Find-Esptool +if (-not $tool) { + Write-Error "esptool not found. Install PlatformIO (`pio`) or: pip install esptool" +} + +$argsCommon = @( + "--chip", "esp32", + "--port", $Port, + "--baud", "$Baud", + "--before", "default_reset", + "--after", "hard_reset", + "write_flash", "-z", + "--flash_mode", "dio", + "--flash_freq", "40m", + "--flash_size", "4MB", + "0x1000", "bootloader.bin", + "0x8000", "partitions.bin", + "0xe000", "boot_app0.bin", + "0x10000", "firmware.bin" +) + +Write-Host "Flashing $here -> $Port @ $Baud" +if ($tool.Kind -eq "mod") { + & python -m esptool @argsCommon +} else { + & python $tool.Path @argsCommon +} +if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } +Write-Host "Done. Open serial monitor at 115200 if needed." diff --git a/firmware/cyd_35r/media/preview.html b/firmware/cyd_35r/media/preview.html new file mode 100644 index 0000000..5c7c6e6 --- /dev/null +++ b/firmware/cyd_35r/media/preview.html @@ -0,0 +1,82 @@ + + + + + +CLIMATE + + + +
+ +
+
CLIMATEup 01:24 + OK
+
+
TEMP
+
24.6C
22.1 / 26.0
+
HUM
+
48%
41 / 55
+
+
+
COMFORT
+
DHT11 · GPIO22
+
+ + +
+
+
+ + +
+ + diff --git a/firmware/cyd_35r/meta.json b/firmware/cyd_35r/meta.json new file mode 100644 index 0000000..6a13e5b --- /dev/null +++ b/firmware/cyd_35r/meta.json @@ -0,0 +1,14 @@ +{ + "boardId": "esp32-3248s035", + "name": "CYD climate dashboard (3.5\")", + "summary": "Temperature & humidity gauges with comfort bar and chart", + "description": "DHT11 on GPIO22. Dual arcs for TEMP/HUM, comfort status, live sparkline.", + "tags": [ + "climate", + "dht11", + "gauge" + ], + "media": { + "preview": "media/preview.html" + } +} diff --git a/firmware/cyd_35r/partitions.bin b/firmware/cyd_35r/partitions.bin new file mode 100644 index 0000000..21800fa Binary files /dev/null and b/firmware/cyd_35r/partitions.bin differ diff --git a/include/touch.h b/include/touch.h new file mode 100644 index 0000000..788ec0f --- /dev/null +++ b/include/touch.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +void touch_init(); +void touch_read_cb(lv_indev_t *indev, lv_indev_data_t *data); diff --git a/platformio.ini b/platformio.ini index ee58296..035cc7f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,4 +1,4 @@ -; CYD ESP32-2432S028R â€?LVGL 温湿度仪表盘 +; CYD ESP32-2432S028R �?LVGL 温湿度仪表盘 ; DHT11 DATA -> GPIO22 [platformio] @@ -46,12 +46,12 @@ build_flags = -D LV_CONF_INCLUDE_SIMPLE=1 -I include -; 标准 CYD 2.8" 电阻å±?(ILI9341) +; 标准 CYD 2.8" 电阻�?(ILI9341) [env:cyd_28r] build_flags = ${env.build_flags} -; 颜色反相版本(若画面色偏/发白可改用此环境ï¼? +; 颜色反相版本(若画面色偏/发白可改用此环境�? [env:cyd_28r_inv] build_flags = ${env.build_flags} @@ -66,18 +66,27 @@ build_flags = -D TFT_RGB_ORDER=TFT_BGR -D TFT_INVERSION_ON=1 -; Optional 3.5" CYD ESP32-3248S035 (ST7796 resistive). Default remains cyd_28r. +; Optional 3.5" CYD ESP32-3248S035C (ST7796 + GT911 capacitive). Default remains cyd_28r. ; Build: pio run -e cyd_35r +; Keep TOUCH_CS=-1 — GPIO33 is GT911 SDA, not resistive touch CS. [env:cyd_35r] +lib_deps = + lvgl/lvgl@9.2.2 + bodmer/TFT_eSPI@2.5.43 + beegee-tokyo/DHT sensor library for ESPx@^1.19 build_flags = ${env.build_flags} - -U ILI9341_2_DRIVER - -D ST7796_DRIVER=1 - -U TFT_WIDTH - -U TFT_HEIGHT - -D TFT_WIDTH=320 - -D TFT_HEIGHT=480 - -U TFT_BL - -D TFT_BL=27 - -D CYD_BOARD_3248S035=1 + -UILI9341_2_DRIVER + -DST7796_DRIVER=1 + -UTFT_WIDTH + -UTFT_HEIGHT + -DTFT_WIDTH=320 + -DTFT_HEIGHT=480 + -UTFT_BL + -DTFT_BL=21 + -DCYD_BOARD_3248S035=1 + -DSUPPORT_TRANSACTIONS + -DTFT_RGB_ORDER=TFT_BGR + -ULV_MEM_SIZE + -DLV_MEM_SIZE=32768 diff --git a/src/main.cpp b/src/main.cpp index ecee504..59c39c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,28 +1,22 @@ /** * CYD ESP32-2432S028R — LVGL 温湿度仪表盘 - * DHT11 DATA -> GPIO22 + * DHT11 DATA -> GPIO22 (3.5 TFT_BL on GPIO21) */ #include #include #include -#include #include #include +#include "touch.h" #define DHT_PIN 22 #if defined(CYD_BOARD_3248S035) -#define TFT_BL_PIN 27 +#define TFT_BL_PIN 21 #else #define TFT_BL_PIN 21 #endif -#define XPT2046_IRQ 36 -#define XPT2046_MOSI 32 -#define XPT2046_MISO 39 -#define XPT2046_CLK 25 -#define XPT2046_CS 33 - #if defined(CYD_BOARD_3248S035) #define SCREEN_WIDTH 480 #define SCREEN_HEIGHT 320 @@ -34,7 +28,11 @@ #define DHT_FAIL_TOLERANCE 3 #define EMA_ALPHA 0.35f #define HISTORY_LEN 48 +#if defined(CYD_BOARD_3248S035) +#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 20 * (LV_COLOR_DEPTH / 8)) +#else #define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8)) +#endif /* Layout grid — every region has exclusive space, no overlaps */ #if defined(CYD_BOARD_3248S035) @@ -78,8 +76,6 @@ static const lv_color_t COL_BAD = lv_color_hex(0xE05A5A); static const lv_color_t COL_ARC_BG = lv_color_hex(0x243247); static const lv_color_t COL_CHART_BG = lv_color_hex(0x0E1622); -SPIClass touchscreenSPI = SPIClass(VSPI); -XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ); DHTesp dht; static uint32_t draw_buf[DRAW_BUF_SIZE / 4]; @@ -88,6 +84,8 @@ static lv_obj_t *arc_temp; static lv_obj_t *arc_hum; static lv_obj_t *lbl_temp_val; static lv_obj_t *lbl_hum_val; +static lv_obj_t *lbl_temp_unit; +static lv_obj_t *lbl_hum_unit; static lv_obj_t *lbl_temp_range; static lv_obj_t *lbl_hum_range; static lv_obj_t *lbl_comfort; @@ -197,32 +195,26 @@ static void reset_minmax(void) { } static void touchscreen_read(lv_indev_t *indev, lv_indev_data_t *data) { - LV_UNUSED(indev); static bool was_pressed = false; static uint32_t press_ms = 0; - if (touchscreen.tirqTouched() && touchscreen.touched()) { - TS_Point p = touchscreen.getPoint(); - data->point.x = map(p.y, 240, 3800, 1, SCREEN_WIDTH); - data->point.y = map(p.x, 200, 3700, SCREEN_HEIGHT, 1); - data->state = LV_INDEV_STATE_PRESSED; + touch_read_cb(indev, data); + + if (data->state == LV_INDEV_STATE_PRESSED) { if (!was_pressed) { was_pressed = true; press_ms = millis(); } - } else { - data->state = LV_INDEV_STATE_RELEASED; - if (was_pressed) { - was_pressed = false; - uint32_t held = millis() - press_ms; - if (held >= 700) { - reset_minmax(); - } else if (held < 400) { - set_backlight((bright_level + 1) & 0x03); - char buf[20]; - lv_snprintf(buf, sizeof(buf), "brightness %u/3", (unsigned)bright_level); - show_hint(buf, 900); - } + } else if (was_pressed) { + was_pressed = false; + uint32_t held = millis() - press_ms; + if (held >= 700) { + reset_minmax(); + } else if (held < 400) { + set_backlight((bright_level + 1) & 0x03); + char buf[20]; + lv_snprintf(buf, sizeof(buf), "brightness %u/3", (unsigned)bright_level); + show_hint(buf, 900); } } } @@ -298,6 +290,36 @@ static lv_obj_t *make_gauge(lv_obj_t *parent, lv_color_t accent, int max_val) { return arc; } +/* Center value+unit as a group on the arc (re-run after text changes). */ +static void layout_gauge_readout(lv_obj_t *arc, lv_obj_t *val, lv_obj_t *unit) { + const int gap = 3; + const int lift = 4; /* open-bottom arc: nudge readout slightly up */ + + lv_obj_update_layout(lv_obj_get_parent(arc)); + lv_obj_update_layout(arc); + lv_obj_update_layout(val); + lv_obj_update_layout(unit); + + const int32_t vw = lv_obj_get_width(val); + const int32_t vh = lv_obj_get_height(val); + const int32_t uw = lv_obj_get_width(unit); + const int32_t uh = lv_obj_get_height(unit); + const int32_t pair_w = vw + gap + uw; + + const int32_t ax = lv_obj_get_x(arc); + const int32_t ay = lv_obj_get_y(arc); + const int32_t aw = lv_obj_get_width(arc); + const int32_t ah = lv_obj_get_height(arc); + const int32_t cx = ax + aw / 2; + const int32_t cy = ay + ah / 2 - lift; + + const int32_t vx = cx - pair_w / 2; + const int32_t vy = cy - vh / 2; + lv_obj_set_pos(val, vx, vy); + /* Unit sits on the baseline of the value, to the right */ + lv_obj_set_pos(unit, vx + vw + gap, vy + vh - uh - 1); +} + static void create_ui(void) { lv_obj_t *scr = lv_screen_active(); lv_obj_set_style_bg_color(scr, COL_BG, 0); @@ -354,14 +376,12 @@ static void create_ui(void) { lv_label_set_text(lbl_temp_val, "--.-"); lv_obj_set_style_text_font(lbl_temp_val, &lv_font_montserrat_24, 0); lv_obj_set_style_text_color(lbl_temp_val, COL_TEMP_OK, 0); - /* Center of arc: arc top+2, size 90 → center ~ y 47 */ - lv_obj_align(lbl_temp_val, LV_ALIGN_TOP_MID, -4, 32); - lv_obj_t *t_unit = lv_label_create(p_temp); - lv_label_set_text(t_unit, "C"); - lv_obj_set_style_text_font(t_unit, &lv_font_montserrat_12, 0); - lv_obj_set_style_text_color(t_unit, COL_MUTED, 0); - lv_obj_align_to(t_unit, lbl_temp_val, LV_ALIGN_OUT_RIGHT_MID, 2, 2); + lbl_temp_unit = lv_label_create(p_temp); + lv_label_set_text(lbl_temp_unit, "C"); + lv_obj_set_style_text_font(lbl_temp_unit, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(lbl_temp_unit, COL_MUTED, 0); + layout_gauge_readout(arc_temp, lbl_temp_val, lbl_temp_unit); lbl_temp_range = lv_label_create(p_temp); lv_label_set_text(lbl_temp_range, "-- / --"); @@ -381,13 +401,12 @@ static void create_ui(void) { lv_label_set_text(lbl_hum_val, "--"); lv_obj_set_style_text_font(lbl_hum_val, &lv_font_montserrat_24, 0); lv_obj_set_style_text_color(lbl_hum_val, COL_HUM, 0); - lv_obj_align(lbl_hum_val, LV_ALIGN_TOP_MID, -2, 32); - lv_obj_t *h_unit = lv_label_create(p_hum); - lv_label_set_text(h_unit, "%"); - lv_obj_set_style_text_font(h_unit, &lv_font_montserrat_12, 0); - lv_obj_set_style_text_color(h_unit, COL_MUTED, 0); - lv_obj_align_to(h_unit, lbl_hum_val, LV_ALIGN_OUT_RIGHT_MID, 2, 2); + lbl_hum_unit = lv_label_create(p_hum); + lv_label_set_text(lbl_hum_unit, "%"); + lv_obj_set_style_text_font(lbl_hum_unit, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(lbl_hum_unit, COL_MUTED, 0); + layout_gauge_readout(arc_hum, lbl_hum_val, lbl_hum_unit); lbl_hum_range = lv_label_create(p_hum); lv_label_set_text(lbl_hum_range, "-- / --"); @@ -499,11 +518,13 @@ static void update_ui(bool ok) { lv_snprintf(buf, sizeof(buf), "%.1f", disp_temp); lv_label_set_text(lbl_temp_val, buf); lv_obj_set_style_text_color(lbl_temp_val, tc, 0); + layout_gauge_readout(arc_temp, lbl_temp_val, lbl_temp_unit); lv_obj_set_style_arc_color(arc_temp, tc, LV_PART_INDICATOR); anim_arc(arc_temp, (int)lroundf(constrain(disp_temp, 0.0f, 50.0f))); lv_snprintf(buf, sizeof(buf), "%.0f", disp_hum); lv_label_set_text(lbl_hum_val, buf); + layout_gauge_readout(arc_hum, lbl_hum_val, lbl_hum_unit); anim_arc(arc_hum, (int)lroundf(constrain(disp_hum, 0.0f, 100.0f))); lv_snprintf(buf, sizeof(buf), "%.0f / %.0f", min_temp, max_temp); @@ -584,11 +605,9 @@ void setup() { lv_init(); - touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); - touchscreen.begin(touchscreenSPI); - touchscreen.setRotation(2); + touch_init(); - #if defined(CYD_BOARD_3248S035) +#if defined(CYD_BOARD_3248S035) lv_display_t *disp = lv_tft_espi_create(320, 480, draw_buf, sizeof(draw_buf)); #else lv_display_t *disp = lv_tft_espi_create(240, 320, draw_buf, sizeof(draw_buf)); @@ -598,6 +617,7 @@ void setup() { lv_indev_t *indev = lv_indev_create(); lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER); lv_indev_set_read_cb(indev, touchscreen_read); + lv_indev_set_display(indev, disp); create_ui(); delay(1500); diff --git a/src/touch.cpp b/src/touch.cpp new file mode 100644 index 0000000..4e3c91f --- /dev/null +++ b/src/touch.cpp @@ -0,0 +1,194 @@ +#include "touch.h" + +#include + +/** + * 2.8": XPT2046 resistive SoftSPI bus (CS33). + * 3.5C (3248S035C): GT911 capacitive on I2C Wire SDA33/SCL32/INT21/RST25. + * Feed coords are always native panel 320x480 for LVGL ROTATION_270. + * (DHT11 is 1-wire on GPIO22 — Wire is free for GT911.) + */ + +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; + return (int16_t)v; +} + +#if defined(CYD_BOARD_3248S035) + +#include + +/* Native panel size — not UI landscape SCREEN_* (480x320). */ +static constexpr int16_t PANEL_W = 320; +static constexpr int16_t PANEL_H = 480; + +#define CYD_GT911_SDA 33 +#define CYD_GT911_SCL 32 +#define CYD_GT911_INT 21 +#define CYD_GT911_RST 25 + +/* Minimal GT911 — no config rewrite (TAMC setResolution can hang Wire). */ +static uint8_t s_gt_addr = 0x5D; +static constexpr uint16_t GT_POINT_INFO = 0x814E; +static constexpr uint16_t GT_POINT_1 = 0x814F; + +static bool gt_write8(uint16_t reg, uint8_t val) { + Wire.beginTransmission(s_gt_addr); + Wire.write((uint8_t)(reg >> 8)); + Wire.write((uint8_t)(reg & 0xFF)); + Wire.write(val); + return Wire.endTransmission() == 0; +} + +static bool gt_read(uint16_t reg, uint8_t *buf, uint8_t len) { + Wire.beginTransmission(s_gt_addr); + Wire.write((uint8_t)(reg >> 8)); + Wire.write((uint8_t)(reg & 0xFF)); + if (Wire.endTransmission(false) != 0) { + return false; + } + const uint8_t n = Wire.requestFrom(s_gt_addr, len); + if (n != len) { + return false; + } + for (uint8_t i = 0; i < len; i++) { + buf[i] = Wire.read(); + } + return true; +} + +static bool gt_probe(uint8_t addr) { + Wire.beginTransmission(addr); + return Wire.endTransmission() == 0; +} + +/* TAMC-compatible address select: INT low => 0x5D, INT high => 0x14 */ +static void gt_hw_reset(bool addr_0x14) { + pinMode(CYD_GT911_INT, OUTPUT); + pinMode(CYD_GT911_RST, OUTPUT); + digitalWrite(CYD_GT911_INT, LOW); + digitalWrite(CYD_GT911_RST, LOW); + delay(10); + digitalWrite(CYD_GT911_INT, addr_0x14 ? HIGH : LOW); + delay(1); + digitalWrite(CYD_GT911_RST, HIGH); + delay(5); + digitalWrite(CYD_GT911_INT, LOW); + delay(50); + /* INT shares TFT_BL on 3.5 — keep backlight HIGH after address select. */ + pinMode(CYD_GT911_INT, OUTPUT); + digitalWrite(CYD_GT911_INT, HIGH); + delay(50); +} + +static bool gt_find_addr() { + for (uint8_t a : {uint8_t(0x5D), uint8_t(0x14)}) { + if (gt_probe(a)) { + s_gt_addr = a; + return true; + } + } + return false; +} + +void touch_init() { + Serial.println("Touch: GT911 I2C capacitive (3.5C) minimal"); + Wire.begin(CYD_GT911_SDA, CYD_GT911_SCL); + Wire.setClock(100000); +#if defined(WIRE_HAS_TIMEOUT) || defined(ESP32) + Wire.setTimeOut(50); +#endif + + if (!gt_find_addr()) { + Serial.println("GT911 not found — hw reset 0x5D"); + gt_hw_reset(false); + if (!gt_find_addr()) { + Serial.println("GT911 not found — hw reset 0x14"); + gt_hw_reset(true); + gt_find_addr(); + } + } + + Serial.printf("GT911 probe 0x5D=%s 0x14=%s using=0x%02X\n", + gt_probe(0x5D) ? "OK" : "no", gt_probe(0x14) ? "OK" : "no", + s_gt_addr); + Serial.printf("GT911 SDA=%d SCL=%d INT=%d RST=%d\n", CYD_GT911_SDA, + CYD_GT911_SCL, CYD_GT911_INT, CYD_GT911_RST); +} + +void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) { + uint8_t info = 0; + + if (!gt_read(GT_POINT_INFO, &info, 1)) { + data->state = LV_INDEV_STATE_RELEASED; + return; + } + + const uint8_t touches = info & 0x0F; + const bool buffered = (info & 0x80) != 0; + + if (!buffered || touches == 0) { + if (buffered) { + gt_write8(GT_POINT_INFO, 0); + } + data->state = LV_INDEV_STATE_RELEASED; + return; + } + + uint8_t pt[7]; + if (!gt_read(GT_POINT_1, pt, 7)) { + gt_write8(GT_POINT_INFO, 0); + data->state = LV_INDEV_STATE_RELEASED; + return; + } + gt_write8(GT_POINT_INFO, 0); + + const int16_t raw_x = (int16_t)(pt[1] | (pt[2] << 8)); + const int16_t raw_y = (int16_t)(pt[3] | (pt[4] << 8)); + + /* Native 320x480 → LVGL 270. UI ≈ (H-1-raw_y, raw_x). */ + const int16_t nx = clampi(raw_x, 0, PANEL_W - 1); + const int16_t ny = clampi(raw_y, 0, PANEL_H - 1); + data->point.x = (int16_t)((PANEL_W - 1) - nx); + data->point.y = (int16_t)((PANEL_H - 1) - ny); + data->state = LV_INDEV_STATE_PRESSED; +} + +#else + +#include +#include + +#define XPT2046_IRQ 36 +#define XPT2046_MOSI 32 +#define XPT2046_MISO 39 +#define XPT2046_CLK 25 +#define XPT2046_CS 33 + +/* 2.8 UI landscape feed (unchanged from prior main.cpp). */ +static constexpr int16_t UI_W = 320; +static constexpr int16_t UI_H = 240; + +static SPIClass touchscreenSPI = SPIClass(VSPI); +static XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ); + +void touch_init() { + touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); + touchscreen.begin(touchscreenSPI); + touchscreen.setRotation(2); + Serial.println("Touch: XPT2046 resistive (2.8)"); +} + +void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) { + if (touchscreen.tirqTouched() && touchscreen.touched()) { + TS_Point p = touchscreen.getPoint(); + data->point.x = map(p.y, 240, 3800, 1, UI_W); + data->point.y = map(p.x, 200, 3700, UI_H, 1); + data->state = LV_INDEV_STATE_PRESSED; + } else { + data->state = LV_INDEV_STATE_RELEASED; + } +} + +#endif