Compare commits
10 Commits
994d21c7fb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 313a6c4747 | |||
| 8f0d1d739b | |||
| b8527e9fb8 | |||
| 348cf9d663 | |||
| 9f9bde76dd | |||
| 0e8a06d24a | |||
| e2e0f4d470 | |||
| 4b99c229a5 | |||
| 242798fef2 | |||
| 024fcd8628 |
32
README.md
32
README.md
@@ -10,7 +10,7 @@ ESP32 Cheap Yellow Display + INMP441(I2S)→ microSD **WAV 录音**,支持
|
||||
| 项目 | 2.8" · 2432S028R | 3.5" C · 3248S035C |
|
||||
|------|------------------|---------------------|
|
||||
| 屏驱 | ILI9341 | ST7796 |
|
||||
| 触控 | XPT2046 SoftSPI | GT911 Wire1(SDA33/SCL32/INT21/RST25) |
|
||||
| 触控 | XPT2046 **SoftSPI**(见下) | GT911 Wire1(SDA33/SCL32/INT21/RST25) |
|
||||
| 背光 TFT_BL | **GPIO21** | **GPIO27** |
|
||||
| INMP441 WS | **GPIO22**(CN1/P3) | **GPIO22** |
|
||||
| INMP441 SCK | **GPIO27**(CN1) | **GPIO21**(CN1;勿占 27=BL) |
|
||||
@@ -38,10 +38,31 @@ ESP32 Cheap Yellow Display + INMP441(I2S)→ microSD **WAV 录音**,支持
|
||||
- 最长约 **120 秒**(自动停)
|
||||
- 实时电平条(近似 dB,与 inmp441test 同一换算)
|
||||
- **PLAY / STOP**:用板载喇叭(GPIO26)回放最近一条录音
|
||||
- 短按顶部标题栏切换背光 4 档
|
||||
- 需插入 **FAT32 microSD**
|
||||
- 触控备用:板载 **BOOT** 键切换 REC;串口发送 `r` / `p` 切换录音 / 播放
|
||||
- 2.8" 触控用 SoftSPI(与 SD 的 VSPI 隔离;勿再用会抢总线的 XPT2046+VSPI)
|
||||
- 备用:板载 **BOOT** 键切换 REC;串口整行 `r` / `rec` / `p` / `play`(不会吃 USB 杂字节)
|
||||
- **绝无自动开录**:无触控按钮 / BOOT / 串口整行命令;上电 4s 内锁死
|
||||
|
||||
## 2.8" 触控说明(重要)
|
||||
|
||||
本板 XPT2046 **不在硬件 SPI 上**(CLK25 / MOSI32 / MISO39 / CS33 / IRQ36),与 microSD 的 **VSPI** 冲突。固件使用 vendored [`lib/CYD28_Touchscreen`](lib/CYD28_Touchscreen)(hexeguitar SoftSPI bit-bang),把 VSPI 留给 SD。
|
||||
|
||||
| 项 | 做法 |
|
||||
|----|------|
|
||||
| 总线 | SoftSPI,读触控前拉高 `SD_CS` |
|
||||
| 坐标 | raw ADC → 横屏 UI 320×240;**X/Y 均反向** 以对齐 `LVGL ROTATION_270` 画面 |
|
||||
| 点击 | 只命中画出的 REC / PLAY;按住 ≥120ms 且指头不滑出按钮才算 |
|
||||
| 防抖 | z≥70 且连续 5 采样;**2 s** 冷却;上电 **4 s** 动作锁 |
|
||||
| 备用 | BOOT(稳定按下)/ 串口整行命令 |
|
||||
|
||||
参考:[RNT CYD + SoftSPI + SD](https://randomnerdtutorials.com/esp32-cyd-display-touchscreen-microsd-card/)、[hexeguitar/CYD28_Touchscreen](https://github.com/hexeguitar/CYD28_Touchscreen)。
|
||||
|
||||
## 使用
|
||||
|
||||
1. 插入 FAT32 microSD,上电后等约 **4 秒**(动作锁结束)再操作。
|
||||
2. **录音**:用力按住屏幕下方红色 **REC** ≥0.12s 再松开;再按一次停止并保存 `/rec/Rxxxx.WAV`。
|
||||
3. **播放**:同样方式按 **PLAY**(需已有录音)。
|
||||
4. 备用:**BOOT** 键开关录音;串口 `115200` 发送整行 `rec` / `play`(或 `r` / `p`)后回车。
|
||||
5. 固件**不会**在无触控、无 BOOT、无串口命令时自行开录或开播。
|
||||
|
||||
## 构建与烧录
|
||||
|
||||
@@ -78,6 +99,7 @@ cd firmware\cyd_28r # 或 cyd_35r
|
||||
platformio.ini
|
||||
include/pins.h lv_conf.h touch.h sdcard.h recorder.h player.h
|
||||
src/main.cpp touch.cpp sdcard.cpp recorder.cpp player.cpp
|
||||
lib/CYD28_Touchscreen/ # 2.8 SoftSPI 触控(与 SD 隔离)
|
||||
scripts/exclude_lvgl_asm.py
|
||||
firmware/
|
||||
README.md
|
||||
@@ -88,3 +110,5 @@ README.md
|
||||
| 日期 | 说明 |
|
||||
|------|------|
|
||||
| 2026-10-27 | 初版:INMP441 → microSD WAV 录音 + DAC 回放;双板 `cyd_28r` / `cyd_35r` |
|
||||
| 2026-10-27 | 2.8 触控:SoftSPI + XY 反向校准、按钮命中区、1.5s 冷却;消除误触反复录音 |
|
||||
| 2026-10-27 | 显式操作门闩:上电锁 4s、触控长按确认、BOOT 防抖、串口整行;禁止无操作自启录音 |
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
录音写入 microSD `/rec/Rxxxx.WAV`;回放经 GPIO26 DAC2。
|
||||
|
||||
2.8" 触控为 SoftSPI(与 SD 的 VSPI 隔离),坐标 XY 反向对齐横屏按钮;上电 4s 锁 + 长按确认,**禁止无操作自启录音**。详见根目录 [README.md](../README.md)。
|
||||
|
||||
## 直接刷写
|
||||
|
||||
```powershell
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name: CYD inmp441recondtest (cyd_28r)
|
||||
git: 76ca618
|
||||
git: 8f0d1d7
|
||||
date: 2026-10-27
|
||||
board: ESP32-2432S028R (ILI9341)
|
||||
inmp441: WS=GPIO22 SCK=GPIO27 SD=GPIO35
|
||||
@@ -14,5 +14,5 @@ offsets:
|
||||
sha256:
|
||||
bootloader.bin 3D234A7471F67B013686DABD4DEE7C1FA915C9928463616A94BC9297ACF1ABF8
|
||||
boot_app0.bin F94C5D786A7A8FAB06AC5D10E33BF37711A6697636DC037559EA19CC410A17F0
|
||||
firmware.bin 201E74138012880FDEA3AA46187BA3353B65788D2689DD076A7456C11EF5DD04
|
||||
firmware.bin A637924B5D9A00E3E0473C50DCA582E019616C563991A0AD842AD176C2318EC1
|
||||
partitions.bin 0A8B5720E7B77FF11F1462458C3A509DEE79224E5279898F26D6A2E3AE0517B7
|
||||
|
||||
Binary file not shown.
234
lib/CYD28_Touchscreen/CYD28_TouchscreenR.cpp
Normal file
234
lib/CYD28_Touchscreen/CYD28_TouchscreenR.cpp
Normal file
@@ -0,0 +1,234 @@
|
||||
/* Touchscreen library for XPT2046 Touch Controller Chip
|
||||
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice, development funding notice, and this permission
|
||||
* notice shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* ----------------------- CYD28 (2.8" cheap yellow display) version --------------------------
|
||||
* using software bit banged SPI and fixed GPIOs + manual calibration
|
||||
* 08.2023 - Piotr Zapart www.hexefx.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#include "CYD28_TouchscreenR.h"
|
||||
|
||||
#define ISR_PREFIX IRAM_ATTR
|
||||
#define MSEC_THRESHOLD 3
|
||||
|
||||
static CYD28_TouchR *isrPinptr;
|
||||
void isrPin(void);
|
||||
// ------------------------------------------------------------
|
||||
bool CYD28_TouchR::begin()
|
||||
{
|
||||
pinMode(CYD28_TouchR_MOSI, OUTPUT);
|
||||
pinMode(CYD28_TouchR_MISO, INPUT);
|
||||
pinMode(CYD28_TouchR_CLK, OUTPUT);
|
||||
pinMode(CYD28_TouchR_CS, OUTPUT);
|
||||
digitalWrite(CYD28_TouchR_CLK, LOW);
|
||||
digitalWrite(CYD28_TouchR_CS, HIGH);
|
||||
pinMode(CYD28_TouchR_IRQ, INPUT);
|
||||
/* Polling mode: many CYD boards have flaky T_IRQ; keep isrWake true. */
|
||||
isrWake = true;
|
||||
isrPinptr = this;
|
||||
return true;
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
ISR_PREFIX
|
||||
void isrPin(void)
|
||||
{
|
||||
CYD28_TouchR *o = isrPinptr;
|
||||
o->isrWake = true;
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
uint8_t CYD28_TouchR::transfer(uint8_t val)
|
||||
{
|
||||
uint8_t out = 0;
|
||||
uint8_t del = _delay >> 1;
|
||||
uint8_t bval = 0;
|
||||
int sck = LOW;
|
||||
|
||||
int8_t bit = 8;
|
||||
while (bit)
|
||||
{
|
||||
bit--;
|
||||
digitalWrite(CYD28_TouchR_MOSI, ((val & (1 << bit)) ? HIGH : LOW)); // Write bit
|
||||
wait(del);
|
||||
sck ^= 1u;
|
||||
digitalWrite(CYD28_TouchR_CLK, sck);
|
||||
/* ... Read bit */
|
||||
bval = digitalRead(CYD28_TouchR_MISO);
|
||||
out <<= 1;
|
||||
out |= bval;
|
||||
wait(del);
|
||||
sck ^= 1u;
|
||||
digitalWrite(CYD28_TouchR_CLK, sck);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
uint16_t CYD28_TouchR::transfer16(uint16_t data)
|
||||
{
|
||||
union
|
||||
{
|
||||
uint16_t val;
|
||||
struct
|
||||
{
|
||||
uint8_t lsb;
|
||||
uint8_t msb;
|
||||
};
|
||||
} in, out;
|
||||
|
||||
in.val = data;
|
||||
out.msb = transfer(in.msb);
|
||||
out.lsb = transfer(in.lsb);
|
||||
return out.val;
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
void CYD28_TouchR::wait(uint_fast8_t del)
|
||||
{
|
||||
for (uint_fast8_t i = 0; i < del; i++)
|
||||
{
|
||||
asm volatile("nop");
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
CYD28_TS_Point CYD28_TouchR::getPointScaled()
|
||||
{
|
||||
update();
|
||||
int16_t x = xraw, y = yraw;
|
||||
convertRawXY(&x, &y);
|
||||
return CYD28_TS_Point(x, y, zraw);
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
CYD28_TS_Point CYD28_TouchR::getPointRaw()
|
||||
{
|
||||
update();
|
||||
return CYD28_TS_Point(xraw, yraw, zraw);
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
bool CYD28_TouchR::touched()
|
||||
{
|
||||
update();
|
||||
return (zraw >= threshold);
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
void CYD28_TouchR::readData(uint16_t *x, uint16_t *y, uint8_t *z)
|
||||
{
|
||||
update();
|
||||
*x = xraw;
|
||||
*y = yraw;
|
||||
*z = zraw;
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
static int16_t besttwoavg(int16_t x, int16_t y, int16_t z)
|
||||
{
|
||||
int16_t da, db, dc;
|
||||
int16_t reta = 0;
|
||||
if (x > y) da = x - y;
|
||||
else da = y - x;
|
||||
if (x > z) db = x - z;
|
||||
else db = z - x;
|
||||
if (z > y) dc = z - y;
|
||||
else dc = y - z;
|
||||
|
||||
if (da <= db && da <= dc) reta = (x + y) >> 1;
|
||||
else if (db <= da && db <= dc) reta = (x + z) >> 1;
|
||||
else reta = (y + z) >> 1;
|
||||
|
||||
return (reta);
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
void CYD28_TouchR::update()
|
||||
{
|
||||
int16_t data[6];
|
||||
int z;
|
||||
if (!isrWake)
|
||||
return;
|
||||
uint32_t now = millis();
|
||||
if (now - msraw < MSEC_THRESHOLD)
|
||||
return;
|
||||
|
||||
digitalWrite(CYD28_TouchR_CS, LOW);
|
||||
transfer(0xB1 /* Z1 */);
|
||||
int16_t z1 = transfer16(0xC1 /* Z2 */) >> 3;
|
||||
z = z1 + 4095;
|
||||
int16_t z2 = transfer16(0x91 /* X */) >> 3;
|
||||
z -= z2;
|
||||
if (z >= threshold)
|
||||
{
|
||||
transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
|
||||
data[0] = transfer16(0xD1 /* Y */) >> 3;
|
||||
data[1] = transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
|
||||
data[2] = transfer16(0xD1 /* Y */) >> 3;
|
||||
data[3] = transfer16(0x91 /* X */) >> 3;
|
||||
}
|
||||
else data[0] = data[1] = data[2] = data[3] = 0;
|
||||
data[4] = transfer16(0xD0 /* Y */) >> 3;
|
||||
data[5] = transfer16(0) >> 3;
|
||||
digitalWrite(CYD28_TouchR_CS, HIGH);
|
||||
|
||||
if (z < 0) z = 0;
|
||||
zraw = z; /* always publish pressure for diagnostics */
|
||||
if (z < threshold)
|
||||
{
|
||||
/* Keep isrWake true for continuous SoftSPI polling (no IRQ dependency). */
|
||||
return;
|
||||
}
|
||||
|
||||
int16_t x = besttwoavg(data[0], data[2], data[4]);
|
||||
int16_t y = besttwoavg(data[1], data[3], data[5]);
|
||||
|
||||
if (z >= threshold)
|
||||
{
|
||||
msraw = now; // good read completed, set wait
|
||||
xraw = x;
|
||||
yraw = y;
|
||||
//log_i("xraw= %d, yraw= %d", xraw, yraw);
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------
|
||||
void CYD28_TouchR::convertRawXY(int16_t *x, int16_t *y)
|
||||
{
|
||||
int16_t x_tmp = *x, y_tmp = *y, xx, yy;
|
||||
switch (rotation)
|
||||
{
|
||||
case 0: // PORT0
|
||||
xx = ((y_tmp - CYD28_TouchR_CAL_YMIN) * sizeY_px) / (CYD28_TouchR_CAL_YMAX - CYD28_TouchR_CAL_YMIN);
|
||||
yy = ((x_tmp - CYD28_TouchR_CAL_XMIN) * sizeX_px) / (CYD28_TouchR_CAL_XMAX - CYD28_TouchR_CAL_XMIN);
|
||||
xx = sizeY_px - xx;
|
||||
break;
|
||||
case 1: // LANDSC0
|
||||
xx = ((x_tmp - CYD28_TouchR_CAL_XMIN) * sizeX_px) / (CYD28_TouchR_CAL_XMAX - CYD28_TouchR_CAL_XMIN);
|
||||
yy = ((y_tmp - CYD28_TouchR_CAL_YMIN) * sizeY_px) / (CYD28_TouchR_CAL_YMAX - CYD28_TouchR_CAL_YMIN);
|
||||
break;
|
||||
case 2: // PORT1
|
||||
xx = ((y_tmp - CYD28_TouchR_CAL_YMIN) * sizeY_px) / (CYD28_TouchR_CAL_YMAX - CYD28_TouchR_CAL_YMIN);
|
||||
yy = ((x_tmp - CYD28_TouchR_CAL_XMIN) * sizeX_px) / (CYD28_TouchR_CAL_XMAX - CYD28_TouchR_CAL_XMIN);
|
||||
yy = sizeX_px - yy;
|
||||
break;
|
||||
default: // 3 LANDSC1
|
||||
xx = ((x_tmp - CYD28_TouchR_CAL_XMIN) * sizeX_px) / (CYD28_TouchR_CAL_XMAX - CYD28_TouchR_CAL_XMIN);
|
||||
yy = ((y_tmp - CYD28_TouchR_CAL_YMIN) * sizeY_px) / (CYD28_TouchR_CAL_YMAX - CYD28_TouchR_CAL_YMIN);
|
||||
xx = sizeX_px - xx;
|
||||
yy = sizeY_px - yy;
|
||||
break;
|
||||
}
|
||||
*x = xx;
|
||||
*y = yy;
|
||||
}
|
||||
86
lib/CYD28_Touchscreen/CYD28_TouchscreenR.h
Normal file
86
lib/CYD28_Touchscreen/CYD28_TouchscreenR.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/* Touchscreen library for XPT2046 Touch Controller Chip
|
||||
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice, development funding notice, and this permission
|
||||
* notice shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
* ----------------------- CYD28 (2.8" cheap yellow display) version --------------------------
|
||||
* using software bit banged SPI and fixed GPIOs + manual calibration
|
||||
* 08.2023 - Piotr Zapart www.hexefx.com
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _CYD28_TOUCHSCREENR_H_
|
||||
#define _CYD28_TOUCHSCREENR_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define CYD28_TouchR_Z_THRESH 300
|
||||
#define CYD28_TouchR_Z_THRES_INT 75
|
||||
|
||||
#define CYD28_TouchR_IRQ 36
|
||||
#define CYD28_TouchR_MOSI 32
|
||||
#define CYD28_TouchR_MISO 39
|
||||
#define CYD28_TouchR_CLK 25
|
||||
#define CYD28_TouchR_CS 33
|
||||
|
||||
// CALIBRAION VALUES
|
||||
#define CYD28_TouchR_CAL_XMIN 185
|
||||
#define CYD28_TouchR_CAL_XMAX 3700
|
||||
#define CYD28_TouchR_CAL_YMIN 280
|
||||
#define CYD28_TouchR_CAL_YMAX 3850
|
||||
|
||||
class CYD28_TS_Point {
|
||||
public:
|
||||
CYD28_TS_Point(void) : x(0), y(0), z(0) {}
|
||||
CYD28_TS_Point(int16_t x, int16_t y, int16_t z) : x(x), y(y), z(z) {}
|
||||
bool operator==(CYD28_TS_Point p) { return ((p.x == x) && (p.y == y) && (p.z == z)); }
|
||||
bool operator!=(CYD28_TS_Point p) { return ((p.x != x) || (p.y != y) || (p.z != z)); }
|
||||
int16_t x, y, z;
|
||||
};
|
||||
|
||||
class CYD28_TouchR {
|
||||
public:
|
||||
constexpr CYD28_TouchR(int32_t w, int32_t h)
|
||||
: _delay(20), sizeX_px(w), sizeY_px(h){ }
|
||||
bool begin();
|
||||
CYD28_TS_Point getPointScaled();
|
||||
CYD28_TS_Point getPointRaw();
|
||||
bool touched();
|
||||
void readData(uint16_t *x, uint16_t *y, uint8_t *z);
|
||||
void setRotation(uint8_t n) { rotation = n % 4; }
|
||||
void setThreshold(uint16_t th) { threshold = th;}
|
||||
|
||||
volatile bool isrWake=true;
|
||||
|
||||
private:
|
||||
void update();
|
||||
uint8_t transfer(uint8_t);
|
||||
uint16_t transfer16(uint16_t data);
|
||||
void wait(uint_fast8_t del);
|
||||
void convertRawXY(int16_t *x, int16_t *y);
|
||||
uint8_t rotation=1;
|
||||
int16_t xraw=0, yraw=0, zraw=0;
|
||||
uint16_t threshold = CYD28_TouchR_Z_THRESH;
|
||||
uint32_t msraw=0x80000000;
|
||||
uint8_t _delay;
|
||||
const int32_t sizeX_px;
|
||||
const int32_t sizeY_px;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
7
lib/CYD28_Touchscreen/library.json
Normal file
7
lib/CYD28_Touchscreen/library.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "CYD28_Touchscreen",
|
||||
"version": "1.0.0",
|
||||
"description": "Bit-banged XPT2046 for CYD 2.8 (vendored from hexeguitar)",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "espressif32"
|
||||
}
|
||||
166
src/main.cpp
166
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,32 +93,87 @@ static const char *basename_of(const char *path) {
|
||||
return p ? p + 1 : path;
|
||||
}
|
||||
|
||||
/* Absolute UI hit boxes filled after create_ui (screen coords). */
|
||||
static lv_area_t hit_rec;
|
||||
static lv_area_t hit_play;
|
||||
static lv_area_t hit_bl;
|
||||
static uint32_t last_action_ms = 0;
|
||||
static uint32_t actions_unlock_ms = 0; /* no REC/PLAY until this time */
|
||||
|
||||
static bool pt_in(const lv_area_t *a, int16_t x, int16_t y) {
|
||||
return x >= a->x1 && x <= a->x2 && y >= a->y1 && y <= a->y2;
|
||||
}
|
||||
|
||||
/* Hard gate: nothing starts/stops without an explicit, accepted input path. */
|
||||
static bool user_action_ok(const char *via) {
|
||||
const uint32_t now = millis();
|
||||
if (now < actions_unlock_ms) {
|
||||
Serial.printf("Action blocked (boot lock %lums) via %s\n",
|
||||
(unsigned long)(actions_unlock_ms - now), via);
|
||||
return false;
|
||||
}
|
||||
if (now - last_action_ms < 2000) {
|
||||
Serial.printf("Action blocked (cooldown) via %s\n", via);
|
||||
return false;
|
||||
}
|
||||
last_action_ms = now;
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
static bool press_in_rec = false;
|
||||
static bool press_in_play = false;
|
||||
|
||||
touch_read_cb(indev, data);
|
||||
|
||||
/* Touch reports landscape UI coords. Convert to native for LVGL ROTATION_270. */
|
||||
if (data->state == LV_INDEV_STATE_PRESSED) {
|
||||
const int16_t ux = data->point.x;
|
||||
const int16_t uy = data->point.y;
|
||||
data->point.x = uy;
|
||||
data->point.y = (int16_t)(UI_HOR_RES - 1 - ux);
|
||||
|
||||
if (!was_pressed) {
|
||||
was_pressed = true;
|
||||
press_ms = millis();
|
||||
consumed = false;
|
||||
press_ux = ux;
|
||||
press_uy = uy;
|
||||
press_in_rec = pt_in(&hit_rec, ux, uy);
|
||||
press_in_play = pt_in(&hit_play, ux, uy);
|
||||
Serial.printf("UI press (%d,%d) rec=%d play=%d\n", (int)ux, (int)uy,
|
||||
(int)press_in_rec, (int)press_in_play);
|
||||
} else {
|
||||
/* Finger must stay on the same button — sliding/noise cancels. */
|
||||
if (press_in_rec && !pt_in(&hit_rec, ux, uy)) press_in_rec = false;
|
||||
if (press_in_play && !pt_in(&hit_play, ux, uy)) press_in_play = false;
|
||||
}
|
||||
} 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) {
|
||||
set_backlight((bright_level + 1) & 0x03);
|
||||
char buf[24];
|
||||
lv_snprintf(buf, sizeof(buf), "brightness %u/3", (unsigned)bright_level);
|
||||
show_hint(buf, 900);
|
||||
const uint32_t held = millis() - press_ms;
|
||||
/* Real finger: latch(~25ms) + hold ≥120ms. EMI spikes are << this. */
|
||||
if (held < 120 || held > 1500) {
|
||||
Serial.printf("Touch reject hold=%lums\n", (unsigned long)held);
|
||||
press_in_rec = press_in_play = false;
|
||||
return;
|
||||
}
|
||||
if (press_in_rec && !press_in_play) {
|
||||
if (user_action_ok("touch-REC")) {
|
||||
Serial.println("Hit → REC");
|
||||
do_rec_toggle();
|
||||
}
|
||||
} else if (press_in_play && !press_in_rec) {
|
||||
if (user_action_ok("touch-PLAY")) {
|
||||
Serial.println("Hit → PLAY");
|
||||
do_play_toggle();
|
||||
}
|
||||
} else {
|
||||
Serial.printf("Touch miss/ambiguous (%d,%d)\n", (int)press_ux, (int)press_uy);
|
||||
}
|
||||
press_in_rec = press_in_play = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,14 +339,15 @@ 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 = 64;
|
||||
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, -8);
|
||||
style_btn(btn_rec, COL_REC);
|
||||
lv_obj_add_event_cb(btn_rec, on_rec_clicked, LV_EVENT_CLICKED, nullptr);
|
||||
lv_obj_set_ext_click_area(btn_rec, 16);
|
||||
lv_obj_clear_flag(btn_rec, LV_OBJ_FLAG_CLICKABLE); /* hit-test in touchscreen_read */
|
||||
lbl_rec = lv_label_create(btn_rec);
|
||||
lv_label_set_text(lbl_rec, "REC");
|
||||
lv_obj_set_style_text_font(lbl_rec, &lv_font_montserrat_16, 0);
|
||||
@@ -297,10 +356,11 @@ 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, -8);
|
||||
style_btn(btn_play, COL_ACCENT);
|
||||
lv_obj_add_event_cb(btn_play, on_play_clicked, LV_EVENT_CLICKED, nullptr);
|
||||
lv_obj_set_ext_click_area(btn_play, 16);
|
||||
lv_obj_clear_flag(btn_play, LV_OBJ_FLAG_CLICKABLE); /* hit-test in touchscreen_read */
|
||||
lbl_play = lv_label_create(btn_play);
|
||||
lv_label_set_text(lbl_play, "PLAY");
|
||||
lv_obj_set_style_text_font(lbl_play, &lv_font_montserrat_16, 0);
|
||||
@@ -309,10 +369,24 @@ 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);
|
||||
|
||||
/* Hit boxes = drawn buttons (+pad). Top strip = backlight only. */
|
||||
lv_obj_update_layout(scr);
|
||||
lv_obj_get_coords(btn_rec, &hit_rec);
|
||||
lv_obj_get_coords(btn_play, &hit_play);
|
||||
hit_rec.x1 -= 12; hit_rec.y1 -= 16; hit_rec.x2 += 12; hit_rec.y2 += 16;
|
||||
hit_play.x1 -= 12; hit_play.y1 -= 16; hit_play.x2 += 12; hit_play.y2 += 16;
|
||||
hit_bl.x1 = 0;
|
||||
hit_bl.y1 = 0;
|
||||
hit_bl.x2 = -1;
|
||||
hit_bl.y2 = -1; /* disabled */
|
||||
Serial.printf("Hit REC(%d..%d,%d..%d) PLAY(%d..%d,%d..%d)\n",
|
||||
(int)hit_rec.x1, (int)hit_rec.x2, (int)hit_rec.y1, (int)hit_rec.y2,
|
||||
(int)hit_play.x1, (int)hit_play.x2, (int)hit_play.y1, (int)hit_play.y2);
|
||||
}
|
||||
|
||||
static void refresh_ui(void) {
|
||||
@@ -423,7 +497,7 @@ void setup() {
|
||||
|
||||
Serial.println(mic_ok ? "I2S mic OK" : "I2S mic FAIL — check wiring");
|
||||
Serial.println(card_ok ? "SD OK" : "SD FAIL — insert FAT32 microSD");
|
||||
Serial.println("Hint: BOOT key or serial 'r' toggles REC; 'p' toggles PLAY");
|
||||
Serial.println("Hint: BOOT / serial line 'r'|'rec' / 'p'|'play' (explicit only)");
|
||||
#if defined(CYD_BOARD_3248S035)
|
||||
Serial.println("3.5C: SCK=GPIO21 (shared GT911 INT; touch is poll-only)");
|
||||
#endif
|
||||
@@ -447,6 +521,11 @@ void setup() {
|
||||
else if (!card_ok) show_hint("insert microSD", 3000);
|
||||
|
||||
refresh_ui();
|
||||
|
||||
/* No REC/PLAY from any path for first 4s (boot noise / USB / touch settle). */
|
||||
actions_unlock_ms = millis() + 4000;
|
||||
last_action_ms = millis();
|
||||
Serial.println("Actions armed after 4s — touch/BOOT/serial only");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -455,31 +534,58 @@ void loop() {
|
||||
recorder_tick();
|
||||
player_tick();
|
||||
|
||||
/* BOOT (GPIO0) short press = REC — works even if touch is flaky. */
|
||||
static bool boot_was = false;
|
||||
/* BOOT (GPIO0): require stable LOW, ignore glitches. */
|
||||
static uint8_t boot_low_n = 0;
|
||||
static bool boot_latched = false;
|
||||
static uint32_t boot_ms = 0;
|
||||
bool boot_now = (digitalRead(0) == LOW);
|
||||
if (boot_now && !boot_was) {
|
||||
boot_was = true;
|
||||
if (digitalRead(0) == LOW) {
|
||||
if (boot_low_n < 255) boot_low_n++;
|
||||
if (!boot_latched && boot_low_n >= 8) { /* ~40ms stable */
|
||||
boot_latched = true;
|
||||
boot_ms = millis();
|
||||
} else if (!boot_now && boot_was) {
|
||||
boot_was = false;
|
||||
if (millis() - boot_ms < 800) {
|
||||
}
|
||||
} else {
|
||||
if (boot_latched) {
|
||||
const uint32_t held = millis() - boot_ms;
|
||||
boot_latched = false;
|
||||
if (held >= 80 && held < 2000 && user_action_ok("BOOT")) {
|
||||
Serial.println("BOOT → REC");
|
||||
do_rec_toggle();
|
||||
}
|
||||
}
|
||||
boot_low_n = 0;
|
||||
}
|
||||
|
||||
/* Serial: whole line only — lone 'r' in USB garbage must NOT start REC.
|
||||
* Accept: "r" / "rec" / "p" / "play" (+ CR/LF). */
|
||||
static char sline[24];
|
||||
static uint8_t slen = 0;
|
||||
while (Serial.available() > 0) {
|
||||
char c = (char)Serial.read();
|
||||
if (c == 'r' || c == 'R') {
|
||||
if (c == '\r' || c == '\n') {
|
||||
if (slen > 0) {
|
||||
sline[slen] = 0;
|
||||
if (!strcmp(sline, "r") || !strcmp(sline, "R") ||
|
||||
!strcmp(sline, "rec") || !strcmp(sline, "REC")) {
|
||||
if (user_action_ok("serial-REC")) {
|
||||
Serial.println("Serial → REC");
|
||||
do_rec_toggle();
|
||||
} else if (c == 'p' || c == 'P') {
|
||||
}
|
||||
} else if (!strcmp(sline, "p") || !strcmp(sline, "P") ||
|
||||
!strcmp(sline, "play") || !strcmp(sline, "PLAY")) {
|
||||
if (user_action_ok("serial-PLAY")) {
|
||||
Serial.println("Serial → PLAY");
|
||||
do_play_toggle();
|
||||
}
|
||||
}
|
||||
slen = 0;
|
||||
}
|
||||
} else if (slen + 1 < sizeof(sline) && c >= 32 && c < 127) {
|
||||
sline[slen++] = c;
|
||||
} else {
|
||||
slen = 0; /* binary / overflow → drop */
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t now = millis();
|
||||
if (now - last_ui_ms >= 100) {
|
||||
|
||||
143
src/touch.cpp
143
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,111 +127,68 @@ 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);
|
||||
}
|
||||
}
|
||||
/* Idle z≈5–15. Demand a real finger — EMI/SD spikes are short & weak. */
|
||||
static constexpr int16_t Z_DOWN = 70;
|
||||
static constexpr int16_t Z_UP = 35;
|
||||
static constexpr int16_t RAW_MIN = 150;
|
||||
static constexpr uint8_t DOWN_NEED = 5; /* ≥5 polls (~25ms) before latch */
|
||||
|
||||
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);
|
||||
}
|
||||
static CYD28_TouchR ts(SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
static bool s_down = false;
|
||||
static uint8_t s_down_streak = 0;
|
||||
static int16_t s_ux = 0, s_uy = 0;
|
||||
|
||||
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(55);
|
||||
Serial.println("Touch: SoftSPI strict (5-sample latch, z>=70)");
|
||||
}
|
||||
|
||||
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();
|
||||
const bool raw_ok = (p.x >= RAW_MIN && p.y >= RAW_MIN);
|
||||
|
||||
static uint32_t last_log = 0;
|
||||
if (down && millis() - last_log > 200) {
|
||||
last_log = millis();
|
||||
Serial.printf("Touch soft x=%u y=%u\n", (unsigned)adc_x, (unsigned)adc_y);
|
||||
if (s_down) {
|
||||
if (p.z < Z_UP) {
|
||||
s_down = false;
|
||||
s_down_streak = 0;
|
||||
}
|
||||
} else if (p.z >= Z_DOWN && raw_ok) {
|
||||
if (s_down_streak < 255) s_down_streak++;
|
||||
if (s_down_streak >= DOWN_NEED) s_down = true;
|
||||
} else {
|
||||
s_down_streak = 0;
|
||||
}
|
||||
|
||||
if (!down) {
|
||||
static uint32_t last_log = 0;
|
||||
if (millis() - last_log > (s_down ? 250u : 1000u)) {
|
||||
last_log = millis();
|
||||
Serial.printf("Touch raw(%d,%d) z=%d down=%d streak=%u\n", (int)p.x,
|
||||
(int)p.y, (int)p.z, (int)s_down, (unsigned)s_down_streak);
|
||||
}
|
||||
|
||||
if (!s_down || !raw_ok) {
|
||||
data->state = LV_INDEV_STATE_RELEASED;
|
||||
data->point.x = s_ux;
|
||||
data->point.y = s_uy;
|
||||
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);
|
||||
/* Both axes inverted on this CYD + ROTATION_270 panel. */
|
||||
int16_t ux = (int16_t)map(p.x, 3700, 200, 0, UI_HOR_RES - 1);
|
||||
int16_t uy = (int16_t)map(p.y, 3800, 240, 0, UI_VER_RES - 1);
|
||||
s_ux = clampi(ux, 0, UI_HOR_RES - 1);
|
||||
s_uy = clampi(uy, 0, 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);
|
||||
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 = s_ux;
|
||||
data->point.y = s_uy;
|
||||
data->state = LV_INDEV_STATE_PRESSED;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user