Compare commits
10 Commits
7c6eb9a09b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 3711e37f28 | |||
| 71fde27f05 | |||
| 0a58ed8a79 | |||
| 51938cd76a | |||
| 69d37005b7 | |||
| 652f4a0499 | |||
| 5afe3971fe | |||
| 54a70d09e2 | |||
| b44bd7d3f0 | |||
| fbb62d3d53 |
62
CHANGELOG.md
62
CHANGELOG.md
@@ -1,5 +1,67 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2026-10-25 — 3.5" backlight pin correction
|
||||||
|
|
||||||
|
- `cyd_35r`: `TFT_BL` / `TFT_BL_PIN` **GPIO27** (was wrongly 21; 21 is GT911 INT).
|
||||||
|
- `../CYD_PROJECT_GUIDE.md` §A:2.8 vs 3.5 对照(触控 2046/911、背光、外设脚 22/27 vs 22)。
|
||||||
|
|
||||||
|
## 2026-10-18 — Pomodoro presets + custom minutes
|
||||||
|
|
||||||
|
- Duration presets: 1 / 3 / 5 / 10 minutes; `Set` opens numeric keypad (1–99).
|
||||||
|
- Keep `+/-`, Start/Pause/Reset, sound picker, speaker + volume.
|
||||||
|
|
||||||
|
## 2026-10-18 — Pomodoro timer UI
|
||||||
|
|
||||||
|
- Tag baseline `v1.0-beep-wav` (Beep + WAV list UI).
|
||||||
|
- New Pomodoro screen with Start/Pause/Reset and selectable alarm sound.
|
||||||
|
- Keep speaker toggle + volume slider; TF status in header.
|
||||||
|
|
||||||
|
## 2026-10-18 — WAV via DAC2 only (touch-safe)
|
||||||
|
|
||||||
|
- Drop I2S INTERNAL_DAC (it enables GPIO25 and kills touch).
|
||||||
|
- PCM WAV → RAM → dac_output on GPIO26 only; Beep tab unchanged.
|
||||||
|
|
||||||
|
## 2026-10-18 — Beep tab + WAV tab
|
||||||
|
|
||||||
|
- Keep 16 LEDC beeps on Beep tab.
|
||||||
|
- WAV tab scans TF (/ and /wav); short files load to RAM then play via Internal DAC.
|
||||||
|
- Core0 decode task; gain capped for onboard amp.
|
||||||
|
|
||||||
|
## 2026-10-18 — Built-in UI beep sounds
|
||||||
|
|
||||||
|
- Drop MP3; LEDC tone bank: Click/Key/Pop/Confirm/Cancel/Success/Error/...
|
||||||
|
- Scrollable SOUNDS list; tap to play.
|
||||||
|
|
||||||
|
## 2026-10-18 — Smooth MP3 + keep UI alive
|
||||||
|
|
||||||
|
- MP3 decode on FreeRTOS core0; UI/touch on core1.
|
||||||
|
- DAC1(GPIO25) disabled while playing so touch CLK works.
|
||||||
|
- No SD rescan during playback (SPI contention).
|
||||||
|
|
||||||
|
## 2026-10-18 — Music list from TF
|
||||||
|
|
||||||
|
- Scan root + /music for .mp3; scrollable list; tap row to play.
|
||||||
|
- PLAY uses selected track; auto-enables speaker on list tap.
|
||||||
|
|
||||||
|
## 2026-10-18 — Play /paomo.mp3 from TF
|
||||||
|
|
||||||
|
- PLAY streams `/paomo.mp3` via ESP8266Audio + internal DAC (GPIO26).
|
||||||
|
- Serial `#PUT` + `tools/sd_put.py` to copy MP3 onto the TF card.
|
||||||
|
|
||||||
|
## 2026-10-18 — Canon in D melody
|
||||||
|
|
||||||
|
- Replace Twinkle Twinkle with Pachelbel Canon in D (monophonic).
|
||||||
|
|
||||||
|
## 2026-10-17 — ASCII glyphs + volume slider
|
||||||
|
|
||||||
|
- Replace ·/— (tofu boxes) with ASCII `-` / `|`.
|
||||||
|
- Add VOL slider (0-100); live duty update while playing.
|
||||||
|
|
||||||
|
## 2026-10-17 — UI layout refresh
|
||||||
|
|
||||||
|
- Compact 320x240 layout: header status, capacity hero, usage bar, dual actions.
|
||||||
|
- Drop on-screen touch debug cursor; keep calibrated touch.cpp unchanged.
|
||||||
|
|
||||||
## 2026-10-17 — Restore TF + speaker (touch map frozen)
|
## 2026-10-17 — Restore TF + speaker (touch map frozen)
|
||||||
|
|
||||||
- Re-add SD info UI and speaker ON/OFF + Play Music.
|
- Re-add SD info UI and speaker ON/OFF + Play Music.
|
||||||
|
|||||||
38
README.md
38
README.md
@@ -1,17 +1,43 @@
|
|||||||
# CYD TF + Speaker
|
# CYD Pomodoro
|
||||||
|
|
||||||
ESP32-2432S028R:LVGL 横屏显示 TF 卡信息,触控开关喇叭并播放音乐。
|
默认板:**ESP32-2432S028R**(2.8"):LVGL 横屏番茄钟,可选内置提示音,触控喇叭播放。
|
||||||
|
|
||||||
|
可选 **3.5" C**:`pio run -e cyd_35r`。
|
||||||
|
|
||||||
|
| | 2.8" `cyd` | 3.5C `cyd_35r` |
|
||||||
|
|--|------------|----------------|
|
||||||
|
| 屏 | ILI9341 · UI 320×240 | ST7796 · UI 480×320 |
|
||||||
|
| 触控 | **XPT2046** SoftSPI | **GT911** I2C |
|
||||||
|
| 背光 | GPIO**21** | GPIO**27** |
|
||||||
|
| 外设首选 | CN1 **IO22 + IO27** | 优先 **IO22**(21=INT,27=BL) |
|
||||||
|
|
||||||
|
完整对照与避坑:[`../CYD_PROJECT_GUIDE.md`](../CYD_PROJECT_GUIDE.md) **§A**。
|
||||||
|
|
||||||
## 功能
|
## 功能
|
||||||
|
|
||||||
- SoftSPI 触摸(已标定,勿改 `src/touch.cpp` 映射)
|
- SoftSPI 触摸(2.8,已标定,勿改 `src/touch.cpp` 映射);3.5C 为 GT911 I2C
|
||||||
- TF 卡信息(VSPI)
|
- 番茄钟:预设 1 / 3 / 5 / 10 分钟;`Set` 自定义输入;`+/-` 微调;Start / Pause / Reset
|
||||||
- 喇叭 GPIO26,默认音量 **20%**;按钮:Speaker ON/OFF、Play Music
|
- 结束音效:内置 Beep 列表自选(默认 Alarm),点名称可试听
|
||||||
|
- 喇叭 GPIO26 + 音量滑条;TF 卡状态显示在右上角
|
||||||
|
- 基线标签:`v1.0-beep-wav`(改番茄钟前的 Beep + WAV 界面)
|
||||||
|
|
||||||
## 构建 / 烧录
|
## 构建 / 烧录
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
# 2.8"
|
||||||
pio run -e cyd -t upload --upload-port COM3
|
pio run -e cyd -t upload --upload-port COM3
|
||||||
|
# 3.5" 电容板
|
||||||
|
pio run -e cyd_35r -t upload --upload-port COM3
|
||||||
```
|
```
|
||||||
|
|
||||||
或 `firmware/cyd/flash.bat`。
|
或 `firmware/cyd/flash.bat`(2.8 预编译包)。
|
||||||
|
|
||||||
|
浏览器刷写(推荐):在 [`../cydweb`](../cydweb) 运行 `npm run dev`,打开 http://localhost:3000 ,选择 **CYD Pomodoro**。
|
||||||
|
|
||||||
|
更新预编译包:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
.\firmware\export_from_build.ps1
|
||||||
|
# 仅复制已有构建:
|
||||||
|
.\firmware\export_from_build.ps1 -SkipBuild
|
||||||
|
```
|
||||||
|
|||||||
@@ -3,9 +3,11 @@
|
|||||||
当前归档目录:[`cyd/`](cyd/)
|
当前归档目录:[`cyd/`](cyd/)
|
||||||
|
|
||||||
对应板子:**CYD ESP32-2432S028R**(ILI9341)
|
对应板子:**CYD ESP32-2432S028R**(ILI9341)
|
||||||
功能:TF 卡信息 + 触屏扬声器控制
|
功能:**番茄钟(Pomodoro)** + 触控喇叭 + TF 状态
|
||||||
版本信息见 [`cyd/VERSION.txt`](cyd/VERSION.txt)。
|
版本信息见 [`cyd/VERSION.txt`](cyd/VERSION.txt)。
|
||||||
|
|
||||||
|
也可在 [`../../cydweb`](../../cydweb)(CYD Flasher)里通过浏览器串口刷写本目录固件。
|
||||||
|
|
||||||
## 直接刷写(无需重新编译)
|
## 直接刷写(无需重新编译)
|
||||||
|
|
||||||
1. USB 连接板子
|
1. USB 连接板子
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
name: CYD TF card + touch speaker (cyd)
|
name: CYD Pomodoro (cyd)
|
||||||
git: 7b5f984
|
git: 54a70d0
|
||||||
date: 2026-10-17
|
date: 2026-10-19
|
||||||
board: ESP32-2432S028R (ILI9341)
|
board: ESP32-2432S028R (ILI9341)
|
||||||
features: TF/SD info, SoftSPI touch, GPIO26 speaker
|
features: Pomodoro timer, SoftSPI touch, GPIO26 speaker, TF status
|
||||||
flash_size: 4MB
|
flash_size: 4MB
|
||||||
offsets:
|
offsets:
|
||||||
0x1000 bootloader.bin
|
0x1000 bootloader.bin
|
||||||
@@ -12,5 +12,5 @@ offsets:
|
|||||||
sha256:
|
sha256:
|
||||||
bootloader.bin 3D234A7471F67B013686DABD4DEE7C1FA915C9928463616A94BC9297ACF1ABF8
|
bootloader.bin 3D234A7471F67B013686DABD4DEE7C1FA915C9928463616A94BC9297ACF1ABF8
|
||||||
boot_app0.bin F94C5D786A7A8FAB06AC5D10E33BF37711A6697636DC037559EA19CC410A17F0
|
boot_app0.bin F94C5D786A7A8FAB06AC5D10E33BF37711A6697636DC037559EA19CC410A17F0
|
||||||
firmware.bin D2A9F0E360CCA1ED115E042A3127D6D9C990E84B12CD972D713A46AF8A4D4A46
|
firmware.bin 4C064140E8F8E0D2DD55D0085F7974FA57CAF5A24DFFACC9B3ECF03B576A033B
|
||||||
partitions.bin 148B959CBFF1C38AA8E1D5C0BA9D612C54997B945E56A63F41223EEF650653A1
|
partitions.bin 148B959CBFF1C38AA8E1D5C0BA9D612C54997B945E56A63F41223EEF650653A1
|
||||||
|
|||||||
Binary file not shown.
55
firmware/cyd/media/preview.html
Normal file
55
firmware/cyd/media/preview.html
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>POMO</title>
|
||||||
|
<style>
|
||||||
|
*{box-sizing:border-box;margin:0;padding:0}
|
||||||
|
html,body{width:320px;height:240px;overflow:hidden;background:#000;margin:0}
|
||||||
|
.bezel{width:320px;height:240px;background:#1a1408}
|
||||||
|
.screen{width:320px;height:240px;background:#0A1016;color:#EAF0F6;font:12px/1.2 system-ui,sans-serif;
|
||||||
|
position:relative;overflow:hidden;}
|
||||||
|
.title{position:absolute;left:12px;top:4px;color:#E85D4C;font-size:20px;font-weight:700}
|
||||||
|
.chip{position:absolute;right:12px;top:8px;color:#7A8B9C}
|
||||||
|
.pm{position:absolute;left:12px;top:32px;width:40px;height:44px;border:0;border-radius:8px;background:#243140;color:#EAF0F6;font-size:18px}
|
||||||
|
.pp{left:auto;right:12px}
|
||||||
|
.timer{position:absolute;left:0;right:0;top:34px;text-align:center;font-size:28px;font-weight:700;color:#4FC3F7}
|
||||||
|
.status{position:absolute;left:0;right:0;top:74px;text-align:center;color:#7A8B9C}
|
||||||
|
.presets{position:absolute;left:12px;right:12px;top:94px;display:flex;gap:6px}
|
||||||
|
.presets button{flex:1;height:30px;border:0;border-radius:6px;background:#15202C;color:#EAF0F6;font-weight:600}
|
||||||
|
.presets button.on{background:#1E3A4A}
|
||||||
|
.snd{position:absolute;left:12px;top:134px;color:#7A8B9C}
|
||||||
|
.sndrow{position:absolute;left:68px;right:12px;top:128px;display:flex;gap:4px;align-items:center}
|
||||||
|
.sndrow button{height:28px;border:0;border-radius:6px;background:#15202C;color:#EAF0F6;min-width:32px}
|
||||||
|
.sndrow .name{flex:1;height:28px;border-radius:6px;background:#15202C;display:grid;place-items:center}
|
||||||
|
.vol{position:absolute;left:12px;top:164px;color:#7A8B9C}
|
||||||
|
.slider{position:absolute;left:52px;right:56px;top:168px;height:14px;border-radius:4px;background:#1C2A38;overflow:hidden}
|
||||||
|
.slider>i{display:block;height:100%;width:40%;background:#3DDC97}
|
||||||
|
.volpct{position:absolute;right:12px;top:164px}
|
||||||
|
.audio{position:absolute;left:12px;top:188px;color:#7A8B9C;font-size:11px}
|
||||||
|
.acts{position:absolute;left:12px;right:12px;bottom:10px;display:flex;gap:8px}
|
||||||
|
.acts button{flex:1;height:32px;border:0;border-radius:8px;font-weight:700;color:#EAF0F6}
|
||||||
|
.start{background:#1A4A6E}.reset{background:#243140}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="bezel"><div class="screen" id="s">
|
||||||
|
<div class="title">POMO</div><div class="chip">TF OK</div>
|
||||||
|
<button class="pm" type="button">-</button>
|
||||||
|
<div class="timer">05:00</div>
|
||||||
|
<button class="pm pp" type="button">+</button>
|
||||||
|
<div class="status">5m - READY</div>
|
||||||
|
<div class="presets">
|
||||||
|
<button type="button">1</button><button type="button">3</button>
|
||||||
|
<button class="on" type="button">5</button><button type="button">10</button>
|
||||||
|
<button type="button">Set</button>
|
||||||
|
</div>
|
||||||
|
<div class="snd">SOUND</div>
|
||||||
|
<div class="sndrow"><button type="button"><</button><div class="name">Alarm</div><button type="button">></button></div>
|
||||||
|
<div class="vol">VOL</div><div class="slider"><i></i></div><div class="volpct">40%</div>
|
||||||
|
<div class="audio">Speaker OFF</div>
|
||||||
|
<div class="acts"><button class="start" type="button">START</button><button class="reset" type="button">RESET</button></div>
|
||||||
|
</div></div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
10
firmware/cyd/meta.json
Normal file
10
firmware/cyd/meta.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"boardId": "esp32-2432s028",
|
||||||
|
"name": "CYD Pomodoro",
|
||||||
|
"summary": "Landscape Pomodoro timer with touch, speaker, and TF status",
|
||||||
|
"description": "Presets 1/3/5/10 minutes, custom duration, alarm sound picker, volume slider on GPIO26.",
|
||||||
|
"tags": ["pomodoro", "touch", "speaker", "timer"],
|
||||||
|
"media": {
|
||||||
|
"preview": "media/preview.html"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,13 @@
|
|||||||
# Rebuild (optional) and refresh firmware/cyd from PlatformIO build output.
|
# Export firmware bins from PlatformIO build.
|
||||||
# Usage:
|
# Default: 2.8" (cyd -> firmware/cyd)
|
||||||
# .\firmware\export_from_build.ps1
|
# 3.5": .\firmware\export_from_build.ps1 -Env cyd_35r
|
||||||
# .\firmware\export_from_build.ps1 -SkipBuild
|
# (only after: pio run -e cyd_35r)
|
||||||
|
|
||||||
param([switch]$SkipBuild)
|
param(
|
||||||
|
[switch]$SkipBuild,
|
||||||
|
[ValidateSet("cyd", "cyd_35r")]
|
||||||
|
[string]$Env = "cyd"
|
||||||
|
)
|
||||||
|
|
||||||
$ErrorActionPreference = "Stop"
|
$ErrorActionPreference = "Stop"
|
||||||
$root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
|
$root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
|
||||||
@@ -12,17 +16,26 @@ Set-Location $root
|
|||||||
$env:Path = "C:\Users\dsx2016\AppData\Local\Programs\Python\Python312\Scripts;" + $env:Path
|
$env:Path = "C:\Users\dsx2016\AppData\Local\Programs\Python\Python312\Scripts;" + $env:Path
|
||||||
|
|
||||||
if (-not $SkipBuild) {
|
if (-not $SkipBuild) {
|
||||||
pio run -e cyd
|
pio run -e $Env
|
||||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||||
}
|
}
|
||||||
|
|
||||||
$src = Join-Path $root ".pio\build\cyd"
|
$src = Join-Path $root (".pio\build\" + $Env)
|
||||||
$dst = Join-Path $root "firmware\cyd"
|
if ($Env -eq "cyd_35r") {
|
||||||
|
$dstName = "cyd_35r"
|
||||||
|
$boardLine = "board: ESP32-3248S035 (ST7796)"
|
||||||
|
$boardIdHint = "esp32-3248s035"
|
||||||
|
} else {
|
||||||
|
$dstName = "cyd"
|
||||||
|
$boardLine = "board: ESP32-2432S028R (ILI9341)"
|
||||||
|
$boardIdHint = "esp32-2432s028"
|
||||||
|
}
|
||||||
|
$dst = Join-Path $root ("firmware\" + $dstName)
|
||||||
New-Item -ItemType Directory -Force -Path $dst | Out-Null
|
New-Item -ItemType Directory -Force -Path $dst | Out-Null
|
||||||
|
|
||||||
foreach ($f in @("bootloader.bin", "partitions.bin", "firmware.bin")) {
|
foreach ($f in @("bootloader.bin", "partitions.bin", "firmware.bin")) {
|
||||||
$p = Join-Path $src $f
|
$p = Join-Path $src $f
|
||||||
if (-not (Test-Path $p)) { Write-Error "Missing build artifact: $p" }
|
if (-not (Test-Path $p)) { Write-Error "Missing build artifact: $p — build with: pio run -e $Env" }
|
||||||
Copy-Item $p $dst -Force
|
Copy-Item $p $dst -Force
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,17 +45,15 @@ if (-not (Test-Path $bootApp)) {
|
|||||||
}
|
}
|
||||||
Copy-Item $bootApp (Join-Path $dst "boot_app0.bin") -Force
|
Copy-Item $bootApp (Join-Path $dst "boot_app0.bin") -Force
|
||||||
|
|
||||||
# Keep flash scripts if already present
|
|
||||||
$git = ""
|
$git = ""
|
||||||
try { $git = (git -C $root rev-parse --short HEAD).Trim() } catch { $git = "unknown" }
|
try { $git = (git -C $root rev-parse --short HEAD).Trim() } catch { $git = "unknown" }
|
||||||
$date = Get-Date -Format "yyyy-MM-dd"
|
$date = Get-Date -Format "yyyy-MM-dd"
|
||||||
$hashes = Get-ChildItem (Join-Path $dst "*.bin") | Get-FileHash -Algorithm SHA256
|
$hashes = Get-ChildItem (Join-Path $dst "*.bin") | Get-FileHash -Algorithm SHA256
|
||||||
$lines = @(
|
$lines = @(
|
||||||
"name: CYD TF card + touch speaker (cyd)",
|
"name: CYD tftest ($dstName)",
|
||||||
"git: $git",
|
"git: $git",
|
||||||
"date: $date",
|
"date: $date",
|
||||||
"board: ESP32-2432S028R (ILI9341)",
|
$boardLine,
|
||||||
"features: TF/SD info, SoftSPI touch, GPIO26 speaker",
|
|
||||||
"flash_size: 4MB",
|
"flash_size: 4MB",
|
||||||
"offsets:",
|
"offsets:",
|
||||||
" 0x1000 bootloader.bin",
|
" 0x1000 bootloader.bin",
|
||||||
@@ -55,4 +66,22 @@ foreach ($h in $hashes) {
|
|||||||
$lines += (" {0,-15} {1}" -f (Split-Path $h.Path -Leaf), $h.Hash)
|
$lines += (" {0,-15} {1}" -f (Split-Path $h.Path -Leaf), $h.Hash)
|
||||||
}
|
}
|
||||||
$lines | Set-Content -Encoding utf8 (Join-Path $dst "VERSION.txt")
|
$lines | Set-Content -Encoding utf8 (Join-Path $dst "VERSION.txt")
|
||||||
Write-Host "Exported -> $dst (git $git)"
|
|
||||||
|
# Keep / refresh meta.json boardId for catalog filtering
|
||||||
|
$metaPath = Join-Path $dst "meta.json"
|
||||||
|
if (Test-Path $metaPath) {
|
||||||
|
try {
|
||||||
|
$meta = Get-Content $metaPath -Raw | ConvertFrom-Json
|
||||||
|
$meta.boardId = $boardIdHint
|
||||||
|
if ($Env -eq "cyd_35r" -and $meta.name -notmatch "3\.5") {
|
||||||
|
$meta.name = ($meta.name -replace "\s*$","") + " (3.5\")"
|
||||||
|
}
|
||||||
|
$meta | ConvertTo-Json -Depth 8 | Set-Content -Encoding utf8 $metaPath
|
||||||
|
} catch {
|
||||||
|
Write-Host "meta.json update skipped: $_"
|
||||||
|
}
|
||||||
|
} elseif ($Env -eq "cyd_35r") {
|
||||||
|
Write-Host "Note: add firmware/cyd_35r/meta.json with boardId=$boardIdHint for the web catalog"
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Exported -> $dst (env=$Env git=$git)"
|
||||||
|
|||||||
@@ -1,20 +1,29 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
void audio_init();
|
void audio_init();
|
||||||
|
|
||||||
void audio_set_enabled(bool on);
|
void audio_set_enabled(bool on);
|
||||||
bool audio_is_enabled();
|
bool audio_is_enabled();
|
||||||
|
|
||||||
// 音量 0–100,默认 20
|
|
||||||
void audio_set_volume(uint8_t percent);
|
void audio_set_volume(uint8_t percent);
|
||||||
uint8_t audio_get_volume();
|
uint8_t audio_get_volume();
|
||||||
|
|
||||||
void audio_play_music();
|
// 内置提示音
|
||||||
|
uint8_t sound_count();
|
||||||
|
const char *sound_name(uint8_t idx);
|
||||||
|
int8_t sound_selected();
|
||||||
|
void sound_set_selected(int8_t idx);
|
||||||
|
void audio_play_sound(uint8_t idx);
|
||||||
|
|
||||||
|
// WAV(TF)
|
||||||
|
void audio_play_wav_index(uint8_t idx);
|
||||||
|
void audio_play_selected(); // 按当前 tab 语义由 UI 决定;保留播 beep 选中
|
||||||
|
|
||||||
void audio_stop();
|
void audio_stop();
|
||||||
bool audio_is_playing();
|
bool audio_is_playing();
|
||||||
|
|
||||||
void audio_tick();
|
void audio_tick();
|
||||||
|
|
||||||
const char *audio_last_status();
|
const char *audio_last_status();
|
||||||
|
|||||||
@@ -1,24 +1,49 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// CYD ESP32-2432S028R
|
// Board pins — 2.8" default; 3.5" when CYD_BOARD_3248S035 is set (env:cyd_35r)
|
||||||
|
|
||||||
|
#if defined(CYD_BOARD_3248S035)
|
||||||
|
#define TFT_BL_PIN 27
|
||||||
|
#define SCREEN_WIDTH 320
|
||||||
|
#define SCREEN_HEIGHT 480
|
||||||
|
#define UI_HOR_RES 480
|
||||||
|
#define UI_VER_RES 320
|
||||||
|
/* 3.5C capacitive GT911 (ESP32-3248S035C). GPIO33 is I2C SDA, not TOUCH_CS.
|
||||||
|
* Backlight is GPIO27; INT is GPIO21 — they are NOT the same pin. */
|
||||||
|
#define CYD_GT911_SDA 33
|
||||||
|
#define CYD_GT911_SCL 32
|
||||||
|
#define CYD_GT911_INT 21
|
||||||
|
#define CYD_GT911_RST 25
|
||||||
|
#else
|
||||||
#define TFT_BL_PIN 21
|
#define TFT_BL_PIN 21
|
||||||
|
#define SCREEN_WIDTH 240
|
||||||
// TF / microSD (VSPI;触摸用 SoftSPI,不抢总线)
|
#define SCREEN_HEIGHT 320
|
||||||
#define SD_MISO 19
|
#define UI_HOR_RES 320
|
||||||
#define SD_MOSI 23
|
#define UI_VER_RES 240
|
||||||
#define SD_SCK 18
|
/* 2.8" CYD: dedicated SoftSPI touch bus. */
|
||||||
#define SD_CS 5
|
|
||||||
|
|
||||||
#define SPEAKER_PIN 26
|
|
||||||
|
|
||||||
#define CYD_TOUCH_IRQ 36
|
#define CYD_TOUCH_IRQ 36
|
||||||
#define CYD_TOUCH_MOSI 32
|
#define CYD_TOUCH_MOSI 32
|
||||||
#define CYD_TOUCH_MISO 39
|
#define CYD_TOUCH_MISO 39
|
||||||
#define CYD_TOUCH_CLK 25
|
#define CYD_TOUCH_CLK 25
|
||||||
#define CYD_TOUCH_CS 33
|
#define CYD_TOUCH_CS 33
|
||||||
|
#endif
|
||||||
|
|
||||||
#define SCREEN_WIDTH 240
|
/* Map 320x240 design coordinates onto the active UI resolution (identity on 2.8"). */
|
||||||
#define SCREEN_HEIGHT 320
|
#define UX(x) ((int)(((long)(x) * (UI_HOR_RES)) / 320))
|
||||||
#define UI_HOR_RES 320
|
#define UY(y) ((int)(((long)(y) * (UI_VER_RES)) / 240))
|
||||||
#define UI_VER_RES 240
|
#define UW(w) UX(w)
|
||||||
|
#define UH(h) UY(h)
|
||||||
|
|
||||||
|
/* Partial frame buffer: 3.5 needs smaller ratio to fit DRAM. */
|
||||||
|
#if defined(CYD_BOARD_3248S035)
|
||||||
|
#define DRAW_BUF_DIV 20
|
||||||
|
#else
|
||||||
|
#define DRAW_BUF_DIV 10
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#define SD_MISO 19
|
||||||
|
#define SD_MOSI 23
|
||||||
|
#define SD_SCK 18
|
||||||
|
#define SD_CS 5
|
||||||
|
#define SPEAKER_PIN 26
|
||||||
|
|||||||
25
include/pomodoro.h
Normal file
25
include/pomodoro.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
enum PomoState : uint8_t {
|
||||||
|
POMO_IDLE = 0,
|
||||||
|
POMO_RUNNING = 1,
|
||||||
|
POMO_PAUSED = 2,
|
||||||
|
POMO_DONE = 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
void pomo_init();
|
||||||
|
void pomo_tick();
|
||||||
|
|
||||||
|
PomoState pomo_state();
|
||||||
|
uint32_t pomo_remaining_sec();
|
||||||
|
uint16_t pomo_duration_minutes(); // last configured length
|
||||||
|
|
||||||
|
void pomo_set_minutes(uint16_t minutes); // 1..99; idle/paused/done only
|
||||||
|
void pomo_start_pause();
|
||||||
|
void pomo_reset();
|
||||||
|
void pomo_adjust_minutes(int8_t delta);
|
||||||
|
|
||||||
|
const char *pomo_state_name();
|
||||||
|
bool pomo_consume_alarm();
|
||||||
7
include/sd_put.h
Normal file
7
include/sd_put.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
// 串口协议: "#PUT <path> <bytes>\n" -> READY -> raw -> DONE/FAIL
|
||||||
|
// 在 loop 里调用;返回 true 表示刚处理完一次传输
|
||||||
|
bool sd_put_poll();
|
||||||
@@ -5,5 +5,6 @@
|
|||||||
|
|
||||||
void ui_create();
|
void ui_create();
|
||||||
void ui_update(const SdCardInfo &info);
|
void ui_update(const SdCardInfo &info);
|
||||||
|
void ui_tick(); // refresh timer + fire alarm sound
|
||||||
void ui_refresh_audio_buttons();
|
void ui_refresh_audio_buttons();
|
||||||
void ui_set_touch_debug(int16_t lx, int16_t ly, bool down);
|
void ui_set_touch_debug(int16_t lx, int16_t ly, bool down);
|
||||||
|
|||||||
23
include/wav_list.h
Normal file
23
include/wav_list.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
static constexpr size_t WAV_MAX = 24;
|
||||||
|
static constexpr size_t WAV_PATH_MAX = 64;
|
||||||
|
static constexpr size_t WAV_NAME_MAX = 28;
|
||||||
|
|
||||||
|
struct WavTrack {
|
||||||
|
char path[WAV_PATH_MAX];
|
||||||
|
char name[WAV_NAME_MAX];
|
||||||
|
uint32_t bytes;
|
||||||
|
};
|
||||||
|
|
||||||
|
uint8_t wav_list_count();
|
||||||
|
int8_t wav_list_selected();
|
||||||
|
void wav_list_set_selected(int8_t idx);
|
||||||
|
const WavTrack *wav_list_get(uint8_t idx);
|
||||||
|
const char *wav_list_current_path();
|
||||||
|
|
||||||
|
bool wav_list_scan();
|
||||||
12
include/wav_player.h
Normal file
12
include/wav_player.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void wav_player_init();
|
||||||
|
void wav_player_stop();
|
||||||
|
bool wav_player_is_playing();
|
||||||
|
bool wav_player_play_path(const char *path);
|
||||||
|
void wav_player_set_volume(uint8_t percent); // 0-100, maps to SetGain
|
||||||
|
void wav_player_tick(); // no-op if task handles; kept for API
|
||||||
|
const char *wav_player_status();
|
||||||
@@ -3,7 +3,8 @@
|
|||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = esp32dev
|
board = esp32dev
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 460800
|
||||||
|
upload_speed = 460800
|
||||||
board_build.partitions = default.csv
|
board_build.partitions = default.csv
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
@@ -42,11 +43,38 @@ build_flags =
|
|||||||
-DLV_FONT_MONTSERRAT_16=1
|
-DLV_FONT_MONTSERRAT_16=1
|
||||||
-DLV_FONT_MONTSERRAT_18=1
|
-DLV_FONT_MONTSERRAT_18=1
|
||||||
-DLV_FONT_MONTSERRAT_20=1
|
-DLV_FONT_MONTSERRAT_20=1
|
||||||
|
-DLV_FONT_MONTSERRAT_28=1
|
||||||
-DLV_USE_TFT_ESPI=1
|
-DLV_USE_TFT_ESPI=1
|
||||||
-DLV_USE_FLEX=1
|
-DLV_USE_FLEX=1
|
||||||
-DLV_USE_BUTTON=1
|
-DLV_USE_BUTTON=1
|
||||||
-DLV_USE_LABEL=1
|
-DLV_USE_LABEL=1
|
||||||
-DLV_USE_BAR=1
|
-DLV_USE_BAR=1
|
||||||
|
-DLV_USE_SLIDER=1
|
||||||
-I${PROJECT_DIR}/include
|
-I${PROJECT_DIR}/include
|
||||||
|
|
||||||
extra_scripts = pre:copy_lv_conf.py
|
extra_scripts = pre:copy_lv_conf.py
|
||||||
|
|
||||||
|
; Optional 3.5" CYD ESP32-3248S035C (ST7796 + GT911 capacitive). Default [env:cyd].
|
||||||
|
; Build: pio run -e cyd_35r
|
||||||
|
; Keep TOUCH_CS=-1 — GPIO33 is GT911 SDA, not resistive touch CS.
|
||||||
|
; TFT_BL=GPIO27 (not 21 — 21 is GT911 INT). See ../CYD_PROJECT_GUIDE.md §1.1
|
||||||
|
[env:cyd_35r]
|
||||||
|
extends = env:cyd
|
||||||
|
build_unflags =
|
||||||
|
-DILI9341_2_DRIVER=1
|
||||||
|
-DTFT_WIDTH=240
|
||||||
|
-DTFT_HEIGHT=320
|
||||||
|
-DTFT_BL=21
|
||||||
|
-DLV_MEM_SIZE=49152
|
||||||
|
build_flags =
|
||||||
|
${env:cyd.build_flags}
|
||||||
|
-DST7796_DRIVER=1
|
||||||
|
-DTFT_WIDTH=320
|
||||||
|
-DTFT_HEIGHT=480
|
||||||
|
-DTFT_BL=27
|
||||||
|
-DCYD_BOARD_3248S035=1
|
||||||
|
-DUSE_HSPI_PORT
|
||||||
|
-DSUPPORT_TRANSACTIONS
|
||||||
|
-DTFT_RGB_ORDER=TFT_BGR
|
||||||
|
-DLV_MEM_SIZE=32768
|
||||||
|
|
||||||
|
|||||||
@@ -1,34 +1,92 @@
|
|||||||
#include "audio_test.h"
|
#include "audio_test.h"
|
||||||
#include "pins.h"
|
#include "pins.h"
|
||||||
|
#include "wav_list.h"
|
||||||
|
#include "wav_player.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static const char *g_status = "Speaker OFF";
|
static const char *g_status = "Speaker OFF";
|
||||||
static bool g_enabled = false;
|
static bool g_enabled = false;
|
||||||
static bool g_playing = false;
|
static bool g_beep_playing = false;
|
||||||
static uint8_t g_volume = 20; // 默认 20%
|
static uint8_t g_volume = 40;
|
||||||
|
static int8_t g_selected = 0;
|
||||||
|
|
||||||
static size_t g_note_i = 0;
|
static size_t g_note_i = 0;
|
||||||
static uint32_t g_until_ms = 0;
|
static uint32_t g_until_ms = 0;
|
||||||
|
enum Phase : uint8_t { kStart, kWaitTone, kWaitGap };
|
||||||
enum Phase : uint8_t { kStartNote, kWaitNote, kWaitGap };
|
static Phase g_phase = kStart;
|
||||||
static Phase g_phase = kStartNote;
|
|
||||||
|
|
||||||
static constexpr int k_ledc_ch = 0;
|
static constexpr int k_ledc_ch = 0;
|
||||||
static constexpr int k_ledc_res = 8;
|
static constexpr int k_ledc_res = 8;
|
||||||
|
|
||||||
struct Note {
|
struct ToneNote {
|
||||||
uint16_t freq;
|
uint16_t freq;
|
||||||
uint16_t ms;
|
uint16_t ms;
|
||||||
};
|
};
|
||||||
|
|
||||||
static const Note k_music[] = {
|
struct SoundDef {
|
||||||
{262, 280}, {262, 280}, {392, 280}, {392, 280}, {440, 280}, {440, 280}, {392, 520},
|
const char *name;
|
||||||
{349, 280}, {349, 280}, {330, 280}, {330, 280}, {294, 280}, {294, 280}, {262, 520},
|
const ToneNote *notes;
|
||||||
{392, 280}, {392, 280}, {349, 280}, {349, 280}, {330, 280}, {330, 280}, {294, 520},
|
uint8_t count;
|
||||||
{392, 280}, {392, 280}, {349, 280}, {349, 280}, {330, 280}, {330, 280}, {294, 520},
|
|
||||||
{262, 280}, {262, 280}, {392, 280}, {392, 280}, {440, 280}, {440, 280}, {392, 520},
|
|
||||||
{349, 280}, {349, 280}, {330, 280}, {330, 280}, {294, 280}, {294, 280}, {262, 700},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr size_t k_music_len = sizeof(k_music) / sizeof(k_music[0]);
|
static const ToneNote k_click[] = {{1200, 40}};
|
||||||
|
static const ToneNote k_confirm[] = {{880, 70}, {0, 30}, {1175, 90}};
|
||||||
|
static const ToneNote k_cancel[] = {{600, 80}, {0, 40}, {440, 100}};
|
||||||
|
static const ToneNote k_success[] = {
|
||||||
|
{523, 80}, {0, 20}, {659, 80}, {0, 20}, {784, 120}};
|
||||||
|
static const ToneNote k_error[] = {
|
||||||
|
{400, 120}, {0, 40}, {400, 120}, {0, 40}, {300, 180}};
|
||||||
|
static const ToneNote k_warn[] = {{900, 90}, {0, 70}, {900, 90}};
|
||||||
|
static const ToneNote k_notify[] = {
|
||||||
|
{1000, 50}, {0, 40}, {1000, 50}, {0, 40}, {1000, 50}};
|
||||||
|
static const ToneNote k_boot[] = {
|
||||||
|
{440, 70}, {0, 20}, {554, 70}, {0, 20}, {659, 100}};
|
||||||
|
static const ToneNote k_shutdown[] = {
|
||||||
|
{659, 80}, {0, 20}, {554, 80}, {0, 20}, {440, 120}};
|
||||||
|
static const ToneNote k_alarm[] = {
|
||||||
|
{880, 120}, {0, 80}, {880, 120}, {0, 80}, {880, 120},
|
||||||
|
{0, 80}, {880, 200}};
|
||||||
|
static const ToneNote k_connect[] = {{700, 60}, {0, 30}, {1050, 100}};
|
||||||
|
static const ToneNote k_disconnect[] = {{1050, 60}, {0, 30}, {700, 100}};
|
||||||
|
static const ToneNote k_lowbat[] = {{500, 150}, {0, 100}, {400, 200}};
|
||||||
|
static const ToneNote k_chime[] = {
|
||||||
|
{784, 100}, {0, 30}, {988, 100}, {0, 30}, {1175, 140}};
|
||||||
|
static const ToneNote k_key[] = {{1400, 25}};
|
||||||
|
static const ToneNote k_pop[] = {{1800, 30}, {0, 15}, {1200, 40}};
|
||||||
|
|
||||||
|
static const SoundDef k_sounds[] = {
|
||||||
|
{"Click", k_click, (uint8_t)(sizeof(k_click) / sizeof(k_click[0]))},
|
||||||
|
{"Key", k_key, (uint8_t)(sizeof(k_key) / sizeof(k_key[0]))},
|
||||||
|
{"Pop", k_pop, (uint8_t)(sizeof(k_pop) / sizeof(k_pop[0]))},
|
||||||
|
{"Confirm", k_confirm, (uint8_t)(sizeof(k_confirm) / sizeof(k_confirm[0]))},
|
||||||
|
{"Cancel", k_cancel, (uint8_t)(sizeof(k_cancel) / sizeof(k_cancel[0]))},
|
||||||
|
{"Success", k_success, (uint8_t)(sizeof(k_success) / sizeof(k_success[0]))},
|
||||||
|
{"Error", k_error, (uint8_t)(sizeof(k_error) / sizeof(k_error[0]))},
|
||||||
|
{"Warn", k_warn, (uint8_t)(sizeof(k_warn) / sizeof(k_warn[0]))},
|
||||||
|
{"Notify", k_notify, (uint8_t)(sizeof(k_notify) / sizeof(k_notify[0]))},
|
||||||
|
{"Boot", k_boot, (uint8_t)(sizeof(k_boot) / sizeof(k_boot[0]))},
|
||||||
|
{"Shutdown", k_shutdown, (uint8_t)(sizeof(k_shutdown) / sizeof(k_shutdown[0]))},
|
||||||
|
{"Alarm", k_alarm, (uint8_t)(sizeof(k_alarm) / sizeof(k_alarm[0]))},
|
||||||
|
{"Connect", k_connect, (uint8_t)(sizeof(k_connect) / sizeof(k_connect[0]))},
|
||||||
|
{"Disconnect", k_disconnect,
|
||||||
|
(uint8_t)(sizeof(k_disconnect) / sizeof(k_disconnect[0]))},
|
||||||
|
{"LowBat", k_lowbat, (uint8_t)(sizeof(k_lowbat) / sizeof(k_lowbat[0]))},
|
||||||
|
{"Chime", k_chime, (uint8_t)(sizeof(k_chime) / sizeof(k_chime[0]))},
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr uint8_t k_sound_count =
|
||||||
|
(uint8_t)(sizeof(k_sounds) / sizeof(k_sounds[0]));
|
||||||
|
|
||||||
|
static const SoundDef *g_active = nullptr;
|
||||||
|
|
||||||
|
static uint32_t volume_duty() {
|
||||||
|
uint32_t duty = (127u * g_volume) / 100u;
|
||||||
|
if (duty < 1 && g_volume > 0) {
|
||||||
|
duty = 1;
|
||||||
|
}
|
||||||
|
return duty;
|
||||||
|
}
|
||||||
|
|
||||||
static void speaker_off_pin() {
|
static void speaker_off_pin() {
|
||||||
ledcWrite(k_ledc_ch, 0);
|
ledcWrite(k_ledc_ch, 0);
|
||||||
@@ -43,22 +101,51 @@ static void speaker_tone(uint16_t freq) {
|
|||||||
}
|
}
|
||||||
ledcSetup(k_ledc_ch, freq, k_ledc_res);
|
ledcSetup(k_ledc_ch, freq, k_ledc_res);
|
||||||
ledcAttachPin(SPEAKER_PIN, k_ledc_ch);
|
ledcAttachPin(SPEAKER_PIN, k_ledc_ch);
|
||||||
// 方波占空比随音量;20% → duty≈25/255
|
ledcWrite(k_ledc_ch, volume_duty());
|
||||||
uint32_t duty = (127u * g_volume) / 100u;
|
}
|
||||||
if (duty < 1) {
|
|
||||||
duty = 1;
|
static void stop_beep_only() {
|
||||||
|
g_beep_playing = false;
|
||||||
|
g_active = nullptr;
|
||||||
|
g_note_i = 0;
|
||||||
|
g_phase = kStart;
|
||||||
|
speaker_off_pin();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t sound_count() {
|
||||||
|
return k_sound_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *sound_name(uint8_t idx) {
|
||||||
|
if (idx >= k_sound_count) {
|
||||||
|
return "-";
|
||||||
}
|
}
|
||||||
ledcWrite(k_ledc_ch, duty);
|
return k_sounds[idx].name;
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t sound_selected() {
|
||||||
|
return g_selected;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sound_set_selected(int8_t idx) {
|
||||||
|
if (idx < 0 || idx >= (int8_t)k_sound_count) {
|
||||||
|
g_selected = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
g_selected = idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
void audio_init() {
|
void audio_init() {
|
||||||
pinMode(SPEAKER_PIN, OUTPUT);
|
pinMode(SPEAKER_PIN, OUTPUT);
|
||||||
digitalWrite(SPEAKER_PIN, LOW);
|
digitalWrite(SPEAKER_PIN, LOW);
|
||||||
g_enabled = false;
|
g_enabled = false;
|
||||||
g_playing = false;
|
g_beep_playing = false;
|
||||||
g_volume = 20;
|
g_volume = 40;
|
||||||
|
g_selected = 0;
|
||||||
g_status = "Speaker OFF";
|
g_status = "Speaker OFF";
|
||||||
Serial.printf("Speaker GPIO%d vol=%u%%\n", SPEAKER_PIN, (unsigned)g_volume);
|
wav_player_init();
|
||||||
|
Serial.printf("Speaker GPIO%d vol=%u%% beeps=%u\n", SPEAKER_PIN,
|
||||||
|
(unsigned)g_volume, (unsigned)k_sound_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
void audio_set_volume(uint8_t percent) {
|
void audio_set_volume(uint8_t percent) {
|
||||||
@@ -66,6 +153,7 @@ void audio_set_volume(uint8_t percent) {
|
|||||||
percent = 100;
|
percent = 100;
|
||||||
}
|
}
|
||||||
g_volume = percent;
|
g_volume = percent;
|
||||||
|
wav_player_set_volume(percent);
|
||||||
Serial.printf("Volume %u%%\n", (unsigned)g_volume);
|
Serial.printf("Volume %u%%\n", (unsigned)g_volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,8 +168,9 @@ void audio_set_enabled(bool on) {
|
|||||||
g_status = "Speaker OFF";
|
g_status = "Speaker OFF";
|
||||||
} else {
|
} else {
|
||||||
g_status = "Speaker ON";
|
g_status = "Speaker ON";
|
||||||
|
wav_player_stop();
|
||||||
speaker_tone(1000);
|
speaker_tone(1000);
|
||||||
delay(80);
|
delay(60);
|
||||||
speaker_off_pin();
|
speaker_off_pin();
|
||||||
}
|
}
|
||||||
Serial.printf("Speaker %s\n", on ? "ON" : "OFF");
|
Serial.printf("Speaker %s\n", on ? "ON" : "OFF");
|
||||||
@@ -92,35 +181,69 @@ bool audio_is_enabled() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void audio_stop() {
|
void audio_stop() {
|
||||||
g_playing = false;
|
stop_beep_only();
|
||||||
g_note_i = 0;
|
wav_player_stop();
|
||||||
g_phase = kStartNote;
|
|
||||||
speaker_off_pin();
|
|
||||||
if (g_enabled) {
|
if (g_enabled) {
|
||||||
g_status = "Speaker ON";
|
g_status = "Speaker ON";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool audio_is_playing() {
|
bool audio_is_playing() {
|
||||||
return g_playing;
|
return g_beep_playing || wav_player_is_playing();
|
||||||
}
|
}
|
||||||
|
|
||||||
void audio_play_music() {
|
void audio_play_sound(uint8_t idx) {
|
||||||
if (!g_enabled) {
|
if (idx >= k_sound_count) {
|
||||||
g_status = "Turn ON first";
|
g_status = "Bad sound";
|
||||||
Serial.println("Audio: blocked (speaker off)");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
g_playing = true;
|
if (!g_enabled) {
|
||||||
|
g_enabled = true;
|
||||||
|
}
|
||||||
|
wav_player_stop();
|
||||||
|
sound_set_selected((int8_t)idx);
|
||||||
|
g_active = &k_sounds[idx];
|
||||||
g_note_i = 0;
|
g_note_i = 0;
|
||||||
g_phase = kStartNote;
|
g_phase = kStart;
|
||||||
g_until_ms = 0;
|
g_until_ms = 0;
|
||||||
g_status = "Playing...";
|
g_beep_playing = true;
|
||||||
Serial.println("Audio: play music");
|
g_status = g_active->name;
|
||||||
|
Serial.printf("Beep: %s\n", g_active->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void audio_play_wav_index(uint8_t idx) {
|
||||||
|
const WavTrack *t = wav_list_get(idx);
|
||||||
|
if (!t) {
|
||||||
|
g_status = "No wav";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!g_enabled) {
|
||||||
|
g_enabled = true;
|
||||||
|
}
|
||||||
|
stop_beep_only();
|
||||||
|
wav_list_set_selected((int8_t)idx);
|
||||||
|
if (wav_player_play_path(t->path)) {
|
||||||
|
g_status = wav_player_status();
|
||||||
|
} else {
|
||||||
|
g_status = wav_player_status();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void audio_play_selected() {
|
||||||
|
if (g_selected < 0) {
|
||||||
|
g_status = "No sound";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
audio_play_sound((uint8_t)g_selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
void audio_tick() {
|
void audio_tick() {
|
||||||
if (!g_playing || !g_enabled) {
|
if (wav_player_is_playing()) {
|
||||||
|
g_status = wav_player_status();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!g_beep_playing || !g_active || !g_enabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,31 +252,33 @@ void audio_tick() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_phase == kWaitNote) {
|
if (g_phase == kWaitTone) {
|
||||||
speaker_off_pin();
|
speaker_off_pin();
|
||||||
g_phase = kWaitGap;
|
g_phase = kWaitGap;
|
||||||
g_until_ms = now + 35;
|
g_until_ms = now + 12;
|
||||||
g_note_i++;
|
g_note_i++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_phase == kWaitGap) {
|
if (g_phase == kWaitGap) {
|
||||||
g_phase = kStartNote;
|
g_phase = kStart;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_note_i >= k_music_len) {
|
if (g_note_i >= g_active->count) {
|
||||||
audio_stop();
|
stop_beep_only();
|
||||||
g_status = "Music done";
|
g_status = g_enabled ? "Done" : "Speaker OFF";
|
||||||
Serial.println("Audio: music done");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Note &n = k_music[g_note_i];
|
const ToneNote &n = g_active->notes[g_note_i];
|
||||||
speaker_tone(n.freq);
|
speaker_tone(n.freq);
|
||||||
g_until_ms = now + n.ms;
|
g_until_ms = now + n.ms;
|
||||||
g_phase = kWaitNote;
|
g_phase = kWaitTone;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *audio_last_status() {
|
const char *audio_last_status() {
|
||||||
|
if (wav_player_is_playing()) {
|
||||||
|
return wav_player_status();
|
||||||
|
}
|
||||||
return g_status;
|
return g_status;
|
||||||
}
|
}
|
||||||
|
|||||||
40
src/main.cpp
40
src/main.cpp
@@ -4,16 +4,19 @@
|
|||||||
#include "audio_test.h"
|
#include "audio_test.h"
|
||||||
#include "pins.h"
|
#include "pins.h"
|
||||||
#include "sd_info.h"
|
#include "sd_info.h"
|
||||||
|
#include "sd_put.h"
|
||||||
#include "touch.h"
|
#include "touch.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
|
#include "wav_list.h"
|
||||||
|
|
||||||
static constexpr uint32_t DRAW_BUF_SIZE =
|
static constexpr uint32_t DRAW_BUF_SIZE =
|
||||||
(SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8));
|
(SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8));
|
||||||
static uint32_t draw_buf[DRAW_BUF_SIZE / 4];
|
static uint32_t draw_buf[DRAW_BUF_SIZE / 4];
|
||||||
|
|
||||||
static uint32_t last_refresh_ms = 0;
|
static uint32_t last_refresh_ms = 0;
|
||||||
static uint32_t last_btn_ui_ms = 0;
|
static uint32_t last_ui_ms = 0;
|
||||||
static constexpr uint32_t REFRESH_MS = 3000;
|
static constexpr uint32_t REFRESH_MS = 8000;
|
||||||
|
static constexpr uint32_t UI_TICK_MS = 200;
|
||||||
|
|
||||||
static void refresh_sd_ui() {
|
static void refresh_sd_ui() {
|
||||||
SdCardInfo info = sd_read_info();
|
SdCardInfo info = sd_read_info();
|
||||||
@@ -24,22 +27,26 @@ static void refresh_sd_ui() {
|
|||||||
} else {
|
} else {
|
||||||
Serial.printf("Type: %s FS: %s Cap: %s\n", info.type, info.filesystem,
|
Serial.printf("Type: %s FS: %s Cap: %s\n", info.type, info.filesystem,
|
||||||
format_bytes(info.card_bytes).c_str());
|
format_bytes(info.card_bytes).c_str());
|
||||||
|
if (!audio_is_playing()) {
|
||||||
|
wav_list_scan();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_update(info);
|
ui_update(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.setRxBufferSize(16384);
|
||||||
|
Serial.begin(460800);
|
||||||
delay(200);
|
delay(200);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.printf("LVGL %d.%d.%d | CYD TF + Speaker\n", lv_version_major(),
|
Serial.printf("LVGL %d.%d.%d | CYD Pomodoro\n", lv_version_major(),
|
||||||
lv_version_minor(), lv_version_patch());
|
lv_version_minor(), lv_version_patch());
|
||||||
|
Serial.println("BOOT_OK");
|
||||||
|
|
||||||
pinMode(TFT_BL_PIN, OUTPUT);
|
pinMode(TFT_BL_PIN, OUTPUT);
|
||||||
digitalWrite(TFT_BL_PIN, HIGH);
|
digitalWrite(TFT_BL_PIN, HIGH);
|
||||||
|
|
||||||
// 先触摸再 SD:触控 SoftSPI 与 SD VSPI 分脚,映射勿改 touch.cpp
|
|
||||||
touch_init();
|
touch_init();
|
||||||
audio_init();
|
audio_init();
|
||||||
|
|
||||||
@@ -62,25 +69,30 @@ void setup() {
|
|||||||
|
|
||||||
refresh_sd_ui();
|
refresh_sd_ui();
|
||||||
last_refresh_ms = millis();
|
last_refresh_ms = millis();
|
||||||
last_btn_ui_ms = millis();
|
last_ui_ms = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
const bool playing = audio_is_playing();
|
||||||
|
|
||||||
|
if (!playing && sd_put_poll()) {
|
||||||
|
refresh_sd_ui();
|
||||||
|
}
|
||||||
|
|
||||||
lv_timer_handler();
|
lv_timer_handler();
|
||||||
lv_tick_inc(5);
|
lv_tick_inc(5);
|
||||||
audio_tick();
|
audio_tick();
|
||||||
|
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
if (now - last_refresh_ms >= REFRESH_MS) {
|
if (now - last_ui_ms >= UI_TICK_MS) {
|
||||||
|
last_ui_ms = now;
|
||||||
|
ui_tick();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!playing && now - last_refresh_ms >= REFRESH_MS) {
|
||||||
last_refresh_ms = now;
|
last_refresh_ms = now;
|
||||||
refresh_sd_ui();
|
refresh_sd_ui();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (now - last_btn_ui_ms >= 200) {
|
delay(playing ? 2 : 5);
|
||||||
last_btn_ui_ms = now;
|
|
||||||
ui_refresh_audio_buttons();
|
|
||||||
ui_set_touch_debug(g_dbg_logic_x, g_dbg_logic_y, g_dbg_touch_down);
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(5);
|
|
||||||
}
|
}
|
||||||
|
|||||||
143
src/pomodoro.cpp
Normal file
143
src/pomodoro.cpp
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
#include "pomodoro.h"
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
static constexpr uint16_t MIN_MINUTES = 1;
|
||||||
|
static constexpr uint16_t MAX_MINUTES = 99;
|
||||||
|
static constexpr uint16_t DEFAULT_MINUTES = 5;
|
||||||
|
|
||||||
|
static PomoState g_state = POMO_IDLE;
|
||||||
|
static uint32_t g_duration_ms = DEFAULT_MINUTES * 60UL * 1000UL;
|
||||||
|
static uint32_t g_remain_ms = DEFAULT_MINUTES * 60UL * 1000UL;
|
||||||
|
static uint32_t g_last_ms = 0;
|
||||||
|
static bool g_alarm_pending = false;
|
||||||
|
|
||||||
|
static uint32_t clamp_minutes_ms(uint16_t minutes) {
|
||||||
|
if (minutes < MIN_MINUTES) {
|
||||||
|
minutes = MIN_MINUTES;
|
||||||
|
}
|
||||||
|
if (minutes > MAX_MINUTES) {
|
||||||
|
minutes = MAX_MINUTES;
|
||||||
|
}
|
||||||
|
return (uint32_t)minutes * 60UL * 1000UL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pomo_init() {
|
||||||
|
g_state = POMO_IDLE;
|
||||||
|
g_duration_ms = clamp_minutes_ms(DEFAULT_MINUTES);
|
||||||
|
g_remain_ms = g_duration_ms;
|
||||||
|
g_last_ms = 0;
|
||||||
|
g_alarm_pending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
PomoState pomo_state() {
|
||||||
|
return g_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t pomo_remaining_sec() {
|
||||||
|
return (g_remain_ms + 999UL) / 1000UL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t pomo_duration_minutes() {
|
||||||
|
return (uint16_t)((g_duration_ms + 59999UL) / 60000UL);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *pomo_state_name() {
|
||||||
|
switch (g_state) {
|
||||||
|
case POMO_RUNNING:
|
||||||
|
return "RUN";
|
||||||
|
case POMO_PAUSED:
|
||||||
|
return "PAUSE";
|
||||||
|
case POMO_DONE:
|
||||||
|
return "DONE";
|
||||||
|
case POMO_IDLE:
|
||||||
|
default:
|
||||||
|
return "READY";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pomo_set_minutes(uint16_t minutes) {
|
||||||
|
if (g_state == POMO_RUNNING) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
g_duration_ms = clamp_minutes_ms(minutes);
|
||||||
|
g_remain_ms = g_duration_ms;
|
||||||
|
g_state = POMO_IDLE;
|
||||||
|
g_alarm_pending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pomo_start_pause() {
|
||||||
|
if (g_state == POMO_DONE) {
|
||||||
|
g_remain_ms = g_duration_ms;
|
||||||
|
g_state = POMO_IDLE;
|
||||||
|
}
|
||||||
|
if (g_state == POMO_RUNNING) {
|
||||||
|
g_state = POMO_PAUSED;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (g_remain_ms == 0) {
|
||||||
|
g_remain_ms = g_duration_ms;
|
||||||
|
}
|
||||||
|
g_state = POMO_RUNNING;
|
||||||
|
g_last_ms = millis();
|
||||||
|
g_alarm_pending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pomo_reset() {
|
||||||
|
g_remain_ms = g_duration_ms;
|
||||||
|
g_state = POMO_IDLE;
|
||||||
|
g_alarm_pending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pomo_adjust_minutes(int8_t delta) {
|
||||||
|
if (g_state == POMO_RUNNING) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int32_t mins = (int32_t)pomo_duration_minutes() + (int32_t)delta;
|
||||||
|
if (mins < (int32_t)MIN_MINUTES) {
|
||||||
|
mins = MIN_MINUTES;
|
||||||
|
}
|
||||||
|
if (mins > (int32_t)MAX_MINUTES) {
|
||||||
|
mins = MAX_MINUTES;
|
||||||
|
}
|
||||||
|
// When paused mid-timer, adjust remaining by same delta minutes from current remain
|
||||||
|
if (g_state == POMO_PAUSED) {
|
||||||
|
int32_t sec = (int32_t)(g_remain_ms / 1000UL) + (int32_t)delta * 60;
|
||||||
|
if (sec < 60) {
|
||||||
|
sec = 60;
|
||||||
|
}
|
||||||
|
if (sec > (int32_t)MAX_MINUTES * 60) {
|
||||||
|
sec = (int32_t)MAX_MINUTES * 60;
|
||||||
|
}
|
||||||
|
g_remain_ms = (uint32_t)sec * 1000UL;
|
||||||
|
g_duration_ms = g_remain_ms;
|
||||||
|
g_alarm_pending = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pomo_set_minutes((uint16_t)mins);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pomo_tick() {
|
||||||
|
if (g_state != POMO_RUNNING) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const uint32_t now = millis();
|
||||||
|
const uint32_t elapsed = now - g_last_ms;
|
||||||
|
g_last_ms = now;
|
||||||
|
|
||||||
|
if (elapsed >= g_remain_ms) {
|
||||||
|
g_remain_ms = 0;
|
||||||
|
g_state = POMO_DONE;
|
||||||
|
g_alarm_pending = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
g_remain_ms -= elapsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool pomo_consume_alarm() {
|
||||||
|
if (!g_alarm_pending) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
g_alarm_pending = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -21,7 +21,7 @@ static const char *card_type_name(uint8_t t) {
|
|||||||
static const char *detect_filesystem() {
|
static const char *detect_filesystem() {
|
||||||
File root = SD.open("/");
|
File root = SD.open("/");
|
||||||
if (!root) {
|
if (!root) {
|
||||||
return "—";
|
return "-";
|
||||||
}
|
}
|
||||||
root.close();
|
root.close();
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ SdCardInfo sd_read_info() {
|
|||||||
info.mounted = false;
|
info.mounted = false;
|
||||||
info.present = false;
|
info.present = false;
|
||||||
info.type = "NONE";
|
info.type = "NONE";
|
||||||
info.filesystem = "—";
|
info.filesystem = "-";
|
||||||
info.card_bytes = 0;
|
info.card_bytes = 0;
|
||||||
info.total_bytes = 0;
|
info.total_bytes = 0;
|
||||||
info.used_bytes = 0;
|
info.used_bytes = 0;
|
||||||
|
|||||||
121
src/sd_put.cpp
Normal file
121
src/sd_put.cpp
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
#include "sd_put.h"
|
||||||
|
#include "sd_info.h"
|
||||||
|
|
||||||
|
#include <SD.h>
|
||||||
|
|
||||||
|
static String read_line_timeout(uint32_t timeout_ms) {
|
||||||
|
String line;
|
||||||
|
const uint32_t start = millis();
|
||||||
|
while (millis() - start < timeout_ms) {
|
||||||
|
while (Serial.available()) {
|
||||||
|
const char c = (char)Serial.read();
|
||||||
|
if (c == '\n' || c == '\r') {
|
||||||
|
if (line.length()) {
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
line += c;
|
||||||
|
if (line.length() > 200) {
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delay(1);
|
||||||
|
}
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool sd_put_poll() {
|
||||||
|
if (!Serial.available()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只在行首是 #PUT 时进入,避免吃掉普通日志
|
||||||
|
if (Serial.peek() != '#') {
|
||||||
|
// 丢弃非协议噪声到换行,防止堵死
|
||||||
|
while (Serial.available() && Serial.peek() != '#') {
|
||||||
|
const int c = Serial.read();
|
||||||
|
if (c == '\n' || c == '\r') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const String line = read_line_timeout(500);
|
||||||
|
if (!line.startsWith("#PUT ")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// #PUT /paomo.mp3 4143009
|
||||||
|
int sp1 = line.indexOf(' ', 5);
|
||||||
|
if (sp1 < 0) {
|
||||||
|
Serial.println("PUT_ERR bad args");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const String path = line.substring(5, sp1);
|
||||||
|
const uint32_t size = (uint32_t)line.substring(sp1 + 1).toInt();
|
||||||
|
if (path.length() < 2 || path[0] != '/' || size == 0 || size > 64UL * 1024UL * 1024UL) {
|
||||||
|
Serial.println("PUT_ERR bad path/size");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sd_init()) {
|
||||||
|
Serial.println("PUT_ERR sd");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SD.exists(path.c_str())) {
|
||||||
|
SD.remove(path.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
File f = SD.open(path.c_str(), FILE_WRITE);
|
||||||
|
if (!f) {
|
||||||
|
Serial.println("PUT_ERR open");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("READY");
|
||||||
|
Serial.flush();
|
||||||
|
|
||||||
|
// 窗口协议:每收满一块回 ACK,避免 RX 溢出
|
||||||
|
constexpr size_t k_win = 4096;
|
||||||
|
uint8_t buf[k_win];
|
||||||
|
uint32_t got = 0;
|
||||||
|
while (got < size) {
|
||||||
|
const uint32_t need = size - got;
|
||||||
|
const size_t chunk = need > k_win ? k_win : (size_t)need;
|
||||||
|
size_t n = 0;
|
||||||
|
const uint32_t wait_start = millis();
|
||||||
|
while (n < chunk && millis() - wait_start < 30000) {
|
||||||
|
if (!Serial.available()) {
|
||||||
|
delay(1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Serial.setTimeout(50);
|
||||||
|
const int r = Serial.readBytes(buf + n, chunk - n);
|
||||||
|
if (r > 0) {
|
||||||
|
n += (size_t)r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (n == 0) {
|
||||||
|
f.close();
|
||||||
|
SD.remove(path.c_str());
|
||||||
|
Serial.println("PUT_ERR timeout");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (f.write(buf, n) != n) {
|
||||||
|
f.close();
|
||||||
|
SD.remove(path.c_str());
|
||||||
|
Serial.println("PUT_ERR write");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
got += n;
|
||||||
|
Serial.printf("ACK %u\n", (unsigned)got);
|
||||||
|
Serial.flush();
|
||||||
|
}
|
||||||
|
f.flush();
|
||||||
|
f.close();
|
||||||
|
Serial.println("DONE");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
191
src/touch.cpp
191
src/touch.cpp
@@ -3,10 +3,13 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#if defined(CYD_BOARD_3248S035)
|
||||||
|
#include <Wire.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SoftSPI XPT2046
|
* 2.8": dedicated SoftSPI + BL/TR diagonal fix.
|
||||||
* BL↔TR 是矩形主对角线镜像(XY 对调),不是 180°(180° 会搞成 TL↔BR)。
|
* 3.5C (3248S035C): capacitive GT911 on I2C SDA33/SCL32/INT21/RST25.
|
||||||
* 先按 rot2 得到旧 UI,再做对角修正,反变换喂给 LVGL 270。
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int16_t g_dbg_phys_x = -1;
|
int16_t g_dbg_phys_x = -1;
|
||||||
@@ -15,6 +18,173 @@ int16_t g_dbg_logic_x = -1;
|
|||||||
int16_t g_dbg_logic_y = -1;
|
int16_t g_dbg_logic_y = -1;
|
||||||
bool g_dbg_touch_down = false;
|
bool g_dbg_touch_down = false;
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
/* Backlight is GPIO27 — leave INT as input for polling / optional IRQ. */
|
||||||
|
pinMode(CYD_GT911_INT, INPUT);
|
||||||
|
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;
|
||||||
|
static uint32_t last_log = 0;
|
||||||
|
const bool do_log = (millis() - last_log > 200);
|
||||||
|
|
||||||
|
if (!gt_read(GT_POINT_INFO, &info, 1)) {
|
||||||
|
data->state = LV_INDEV_STATE_RELEASED;
|
||||||
|
g_dbg_touch_down = false;
|
||||||
|
if (do_log) {
|
||||||
|
last_log = millis();
|
||||||
|
Serial.println("TouchProbe GT911 I2C err");
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
g_dbg_touch_down = false;
|
||||||
|
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;
|
||||||
|
g_dbg_touch_down = false;
|
||||||
|
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. Mirror long axis so L/R matches UI
|
||||||
|
* (else preset 3 hits 10). Resulting UI ≈ (H-1-raw_y, raw_x). */
|
||||||
|
const int16_t nx = clampi(raw_x, 0, SCREEN_WIDTH - 1);
|
||||||
|
const int16_t ny = clampi(raw_y, 0, SCREEN_HEIGHT - 1);
|
||||||
|
const int16_t feed_x = (int16_t)((SCREEN_WIDTH - 1) - nx);
|
||||||
|
const int16_t feed_y = (int16_t)((SCREEN_HEIGHT - 1) - ny);
|
||||||
|
const int16_t ui_x = (int16_t)((SCREEN_HEIGHT - 1) - ny);
|
||||||
|
const int16_t ui_y = nx;
|
||||||
|
|
||||||
|
g_dbg_phys_x = raw_x;
|
||||||
|
g_dbg_phys_y = raw_y;
|
||||||
|
g_dbg_logic_x = ui_x;
|
||||||
|
g_dbg_logic_y = ui_y;
|
||||||
|
g_dbg_touch_down = true;
|
||||||
|
|
||||||
|
if (do_log) {
|
||||||
|
last_log = millis();
|
||||||
|
Serial.printf("Touch HIT GT911 raw(%d,%d) ~ui(%d,%d)\n", raw_x, raw_y,
|
||||||
|
ui_x, ui_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
data->point.x = feed_x;
|
||||||
|
data->point.y = feed_y;
|
||||||
|
data->state = LV_INDEV_STATE_PRESSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
static void soft_write(uint8_t data) {
|
static void soft_write(uint8_t data) {
|
||||||
for (int i = 7; i >= 0; --i) {
|
for (int i = 7; i >= 0; --i) {
|
||||||
digitalWrite(CYD_TOUCH_MOSI, (data >> i) & 1);
|
digitalWrite(CYD_TOUCH_MOSI, (data >> i) & 1);
|
||||||
@@ -49,16 +219,6 @@ static uint16_t xpt_read(uint8_t cmd) {
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void touch_init() {
|
void touch_init() {
|
||||||
pinMode(CYD_TOUCH_CS, OUTPUT);
|
pinMode(CYD_TOUCH_CS, OUTPUT);
|
||||||
pinMode(CYD_TOUCH_CLK, OUTPUT);
|
pinMode(CYD_TOUCH_CLK, OUTPUT);
|
||||||
@@ -94,8 +254,6 @@ void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
|||||||
x = clampi(x, 0, SCREEN_WIDTH - 1);
|
x = clampi(x, 0, SCREEN_WIDTH - 1);
|
||||||
y = clampi(y, 0, SCREEN_HEIGHT - 1);
|
y = clampi(y, 0, SCREEN_HEIGHT - 1);
|
||||||
|
|
||||||
// 旧 UI(LVGL270):(y, 239-x) — 该空间里 BL↔TR,做主对角线镜像
|
|
||||||
// (ux,uy) -> (uy*(319/239), ux*(239/319))
|
|
||||||
const int32_t old_ux = y;
|
const int32_t old_ux = y;
|
||||||
const int32_t old_uy = (SCREEN_WIDTH - 1) - x;
|
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,
|
int16_t ui_x = clampi((old_uy * (UI_HOR_RES - 1)) / (UI_VER_RES - 1), 0,
|
||||||
@@ -103,7 +261,6 @@ void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
|||||||
int16_t ui_y = clampi((old_ux * (UI_VER_RES - 1)) / (UI_HOR_RES - 1), 0,
|
int16_t ui_y = clampi((old_ux * (UI_VER_RES - 1)) / (UI_HOR_RES - 1), 0,
|
||||||
UI_VER_RES - 1);
|
UI_VER_RES - 1);
|
||||||
|
|
||||||
// 反变换喂 LVGL,使最终落在 (ui_x, ui_y)
|
|
||||||
const int16_t feed_x = (int16_t)((SCREEN_WIDTH - 1) - ui_y);
|
const int16_t feed_x = (int16_t)((SCREEN_WIDTH - 1) - ui_y);
|
||||||
const int16_t feed_y = ui_x;
|
const int16_t feed_y = ui_x;
|
||||||
|
|
||||||
@@ -123,3 +280,5 @@ void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
|||||||
data->point.y = feed_y;
|
data->point.y = feed_y;
|
||||||
data->state = LV_INDEV_STATE_PRESSED;
|
data->state = LV_INDEV_STATE_PRESSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
684
src/ui.cpp
684
src/ui.cpp
@@ -1,46 +1,161 @@
|
|||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "audio_test.h"
|
#include "audio_test.h"
|
||||||
#include "pins.h"
|
#include "pins.h"
|
||||||
|
#include "pomodoro.h"
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <lvgl.h>
|
#include <lvgl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static constexpr uint32_t COL_BG = 0x0A1016;
|
||||||
|
static constexpr uint32_t COL_INK = 0xEAF0F6;
|
||||||
|
static constexpr uint32_t COL_MUTED = 0x7A8B9C;
|
||||||
|
static constexpr uint32_t COL_LINE = 0x1C2A38;
|
||||||
|
static constexpr uint32_t COL_OK = 0x3DDC97;
|
||||||
|
static constexpr uint32_t COL_BAD = 0xE85D4C;
|
||||||
|
static constexpr uint32_t COL_ACCENT = 0x4FC3F7;
|
||||||
|
static constexpr uint32_t COL_BTN_OFF = 0x243140;
|
||||||
|
static constexpr uint32_t COL_BTN_ON = 0x1B5E4A;
|
||||||
|
static constexpr uint32_t COL_BTN_PLAY = 0x1A4A6E;
|
||||||
|
static constexpr uint32_t COL_BTN_STOP = 0x8B2E2E;
|
||||||
|
static constexpr uint32_t COL_MODE = 0x15202C;
|
||||||
|
static constexpr uint32_t COL_MODE_ON = 0x1E3A4A;
|
||||||
|
static constexpr uint32_t COL_TOMATO = 0xE85D4C;
|
||||||
|
|
||||||
|
static constexpr uint16_t PRESETS[] = {1, 3, 5, 10};
|
||||||
|
static constexpr int PRESET_N = 4;
|
||||||
|
|
||||||
|
static lv_obj_t *lbl_chip;
|
||||||
|
static lv_obj_t *lbl_timer;
|
||||||
static lv_obj_t *lbl_status;
|
static lv_obj_t *lbl_status;
|
||||||
static lv_obj_t *lbl_type;
|
static lv_obj_t *lbl_sound;
|
||||||
static lv_obj_t *lbl_fs;
|
static lv_obj_t *lbl_vol;
|
||||||
static lv_obj_t *lbl_capacity;
|
|
||||||
static lv_obj_t *lbl_audio;
|
static lv_obj_t *lbl_audio;
|
||||||
static lv_obj_t *bar_usage;
|
static lv_obj_t *slider_vol;
|
||||||
static lv_obj_t *btn_speaker;
|
static lv_obj_t *btn_speaker;
|
||||||
static lv_obj_t *btn_play;
|
static lv_obj_t *btn_start;
|
||||||
|
static lv_obj_t *btn_reset;
|
||||||
static lv_obj_t *lbl_speaker;
|
static lv_obj_t *lbl_speaker;
|
||||||
static lv_obj_t *lbl_play;
|
static lv_obj_t *lbl_start;
|
||||||
static lv_obj_t *lbl_touch_dbg;
|
static lv_obj_t *btn_preset[PRESET_N];
|
||||||
static lv_obj_t *touch_cursor;
|
static lv_obj_t *btn_custom;
|
||||||
|
static lv_obj_t *panel_custom;
|
||||||
|
static lv_obj_t *lbl_custom_val;
|
||||||
|
|
||||||
static lv_obj_t *make_row(lv_obj_t *parent, const char *title, lv_obj_t **value_out) {
|
static char g_timer_text[8] = "05:00";
|
||||||
lv_obj_t *row = lv_obj_create(parent);
|
static char g_status_text[40] = "5m - READY";
|
||||||
lv_obj_set_width(row, LV_PCT(100));
|
static char g_sound_text[28] = "Alarm";
|
||||||
lv_obj_set_height(row, LV_SIZE_CONTENT);
|
static char g_custom_buf[4] = "";
|
||||||
lv_obj_set_style_bg_opa(row, LV_OPA_TRANSP, 0);
|
static char g_custom_disp[16] = "min: _";
|
||||||
lv_obj_set_style_border_width(row, 0, 0);
|
|
||||||
lv_obj_set_style_pad_all(row, 1, 0);
|
|
||||||
lv_obj_set_flex_flow(row, LV_FLEX_FLOW_ROW);
|
|
||||||
lv_obj_set_flex_align(row, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER,
|
|
||||||
LV_FLEX_ALIGN_CENTER);
|
|
||||||
lv_obj_clear_flag(row, LV_OBJ_FLAG_SCROLLABLE);
|
|
||||||
|
|
||||||
lv_obj_t *t = lv_label_create(row);
|
static void style_btn(lv_obj_t *btn, uint32_t bg) {
|
||||||
lv_label_set_text(t, title);
|
lv_obj_set_style_radius(btn, 10, 0);
|
||||||
lv_obj_set_style_text_color(t, lv_color_hex(0x90A4AE), 0);
|
lv_obj_set_style_bg_color(btn, lv_color_hex(bg), 0);
|
||||||
lv_obj_set_style_text_font(t, &lv_font_montserrat_14, 0);
|
lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);
|
||||||
|
lv_obj_set_style_shadow_width(btn, 0, 0);
|
||||||
|
lv_obj_set_style_border_width(btn, 0, 0);
|
||||||
|
lv_obj_set_style_pad_all(btn, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
lv_obj_t *v = lv_label_create(row);
|
static void sync_vol_label() {
|
||||||
lv_label_set_text(v, "—");
|
if (!lbl_vol) {
|
||||||
lv_obj_set_style_text_color(v, lv_color_hex(0xECEFF1), 0);
|
return;
|
||||||
lv_obj_set_style_text_font(v, &lv_font_montserrat_14, 0);
|
}
|
||||||
*value_out = v;
|
char buf[16];
|
||||||
return row;
|
snprintf(buf, sizeof(buf), "%u%%", (unsigned)audio_get_volume());
|
||||||
|
lv_label_set_text(lbl_vol, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void refresh_preset_highlight() {
|
||||||
|
const uint16_t mins = pomo_duration_minutes();
|
||||||
|
bool matched = false;
|
||||||
|
for (int i = 0; i < PRESET_N; i++) {
|
||||||
|
if (!btn_preset[i]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const bool match = mins == PRESETS[i];
|
||||||
|
if (match) {
|
||||||
|
matched = true;
|
||||||
|
}
|
||||||
|
style_btn(btn_preset[i], match ? COL_MODE_ON : COL_MODE);
|
||||||
|
}
|
||||||
|
if (btn_custom) {
|
||||||
|
style_btn(btn_custom, matched ? COL_MODE : COL_MODE_ON);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void refresh_timer_labels() {
|
||||||
|
const uint32_t sec = pomo_remaining_sec();
|
||||||
|
const uint32_t mm = sec / 60U;
|
||||||
|
const uint32_t ss = sec % 60U;
|
||||||
|
snprintf(g_timer_text, sizeof(g_timer_text), "%02u:%02u", (unsigned)mm,
|
||||||
|
(unsigned)ss);
|
||||||
|
if (lbl_timer) {
|
||||||
|
lv_label_set_text(lbl_timer, g_timer_text);
|
||||||
|
const uint32_t color = (pomo_state() == POMO_DONE) ? COL_TOMATO : COL_ACCENT;
|
||||||
|
lv_obj_set_style_text_color(lbl_timer, lv_color_hex(color), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(g_status_text, sizeof(g_status_text), "%um - %s",
|
||||||
|
(unsigned)pomo_duration_minutes(), pomo_state_name());
|
||||||
|
if (lbl_status) {
|
||||||
|
lv_label_set_text(lbl_status, g_status_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lbl_start) {
|
||||||
|
if (pomo_state() == POMO_RUNNING) {
|
||||||
|
lv_label_set_text(lbl_start, "PAUSE");
|
||||||
|
} else if (pomo_state() == POMO_DONE) {
|
||||||
|
lv_label_set_text(lbl_start, "AGAIN");
|
||||||
|
} else {
|
||||||
|
lv_label_set_text(lbl_start, "START");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (btn_start) {
|
||||||
|
style_btn(btn_start,
|
||||||
|
pomo_state() == POMO_RUNNING ? COL_BTN_STOP : COL_BTN_PLAY);
|
||||||
|
}
|
||||||
|
refresh_preset_highlight();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void refresh_sound_label() {
|
||||||
|
const int8_t sel = sound_selected();
|
||||||
|
if (sel < 0) {
|
||||||
|
snprintf(g_sound_text, sizeof(g_sound_text), "-");
|
||||||
|
} else {
|
||||||
|
snprintf(g_sound_text, sizeof(g_sound_text), "%s", sound_name((uint8_t)sel));
|
||||||
|
}
|
||||||
|
if (lbl_sound) {
|
||||||
|
lv_label_set_text(lbl_sound, g_sound_text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sync_custom_label() {
|
||||||
|
if (g_custom_buf[0] == '\0') {
|
||||||
|
snprintf(g_custom_disp, sizeof(g_custom_disp), "min: _");
|
||||||
|
} else {
|
||||||
|
snprintf(g_custom_disp, sizeof(g_custom_disp), "min: %s", g_custom_buf);
|
||||||
|
}
|
||||||
|
if (lbl_custom_val) {
|
||||||
|
lv_label_set_text(lbl_custom_val, g_custom_disp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void custom_hide() {
|
||||||
|
if (panel_custom) {
|
||||||
|
lv_obj_add_flag(panel_custom, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void custom_show() {
|
||||||
|
g_custom_buf[0] = '\0';
|
||||||
|
sync_custom_label();
|
||||||
|
if (panel_custom) {
|
||||||
|
lv_obj_clear_flag(panel_custom, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
lv_obj_move_foreground(panel_custom);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_speaker_clicked(lv_event_t *e) {
|
static void on_speaker_clicked(lv_event_t *e) {
|
||||||
@@ -49,178 +164,425 @@ static void on_speaker_clicked(lv_event_t *e) {
|
|||||||
ui_refresh_audio_buttons();
|
ui_refresh_audio_buttons();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_play_clicked(lv_event_t *e) {
|
static void on_start_clicked(lv_event_t *e) {
|
||||||
LV_UNUSED(e);
|
LV_UNUSED(e);
|
||||||
if (audio_is_playing()) {
|
pomo_start_pause();
|
||||||
audio_stop();
|
refresh_timer_labels();
|
||||||
} else {
|
}
|
||||||
audio_play_music();
|
|
||||||
}
|
static void on_reset_clicked(lv_event_t *e) {
|
||||||
|
LV_UNUSED(e);
|
||||||
|
pomo_reset();
|
||||||
|
audio_stop();
|
||||||
|
refresh_timer_labels();
|
||||||
ui_refresh_audio_buttons();
|
ui_refresh_audio_buttons();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void on_preset_clicked(lv_event_t *e) {
|
||||||
|
const int idx = (int)(intptr_t)lv_event_get_user_data(e);
|
||||||
|
if (idx < 0 || idx >= PRESET_N) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pomo_set_minutes(PRESETS[idx]);
|
||||||
|
refresh_timer_labels();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_custom_open(lv_event_t *e) {
|
||||||
|
LV_UNUSED(e);
|
||||||
|
if (pomo_state() == POMO_RUNNING) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
custom_show();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_custom_cancel(lv_event_t *e) {
|
||||||
|
LV_UNUSED(e);
|
||||||
|
custom_hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_custom_ok(lv_event_t *e) {
|
||||||
|
LV_UNUSED(e);
|
||||||
|
if (g_custom_buf[0] == '\0') {
|
||||||
|
custom_hide();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int mins = atoi(g_custom_buf);
|
||||||
|
if (mins < 1) {
|
||||||
|
mins = 1;
|
||||||
|
}
|
||||||
|
if (mins > 99) {
|
||||||
|
mins = 99;
|
||||||
|
}
|
||||||
|
pomo_set_minutes((uint16_t)mins);
|
||||||
|
custom_hide();
|
||||||
|
refresh_timer_labels();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_custom_digit(lv_event_t *e) {
|
||||||
|
const int d = (int)(intptr_t)lv_event_get_user_data(e);
|
||||||
|
const size_t len = strlen(g_custom_buf);
|
||||||
|
if (d == -1) { // clear
|
||||||
|
g_custom_buf[0] = '\0';
|
||||||
|
sync_custom_label();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (len >= 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (len == 0 && d == 0) {
|
||||||
|
return; // no leading zero
|
||||||
|
}
|
||||||
|
g_custom_buf[len] = (char)('0' + d);
|
||||||
|
g_custom_buf[len + 1] = '\0';
|
||||||
|
sync_custom_label();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_minus_clicked(lv_event_t *e) {
|
||||||
|
LV_UNUSED(e);
|
||||||
|
pomo_adjust_minutes(-1);
|
||||||
|
refresh_timer_labels();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_plus_clicked(lv_event_t *e) {
|
||||||
|
LV_UNUSED(e);
|
||||||
|
pomo_adjust_minutes(1);
|
||||||
|
refresh_timer_labels();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_sound_prev(lv_event_t *e) {
|
||||||
|
LV_UNUSED(e);
|
||||||
|
const uint8_t n = sound_count();
|
||||||
|
if (n == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int8_t sel = sound_selected();
|
||||||
|
if (sel <= 0) {
|
||||||
|
sel = (int8_t)(n - 1);
|
||||||
|
} else {
|
||||||
|
sel--;
|
||||||
|
}
|
||||||
|
sound_set_selected(sel);
|
||||||
|
refresh_sound_label();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_sound_next(lv_event_t *e) {
|
||||||
|
LV_UNUSED(e);
|
||||||
|
const uint8_t n = sound_count();
|
||||||
|
if (n == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int8_t sel = sound_selected();
|
||||||
|
sel++;
|
||||||
|
if (sel >= (int8_t)n) {
|
||||||
|
sel = 0;
|
||||||
|
}
|
||||||
|
sound_set_selected(sel);
|
||||||
|
refresh_sound_label();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_sound_preview(lv_event_t *e) {
|
||||||
|
LV_UNUSED(e);
|
||||||
|
const int8_t sel = sound_selected();
|
||||||
|
if (sel < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
audio_play_sound((uint8_t)sel);
|
||||||
|
ui_refresh_audio_buttons();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_vol_changed(lv_event_t *e) {
|
||||||
|
lv_obj_t *slider = (lv_obj_t *)lv_event_get_target(e);
|
||||||
|
audio_set_volume((uint8_t)lv_slider_get_value(slider));
|
||||||
|
sync_vol_label();
|
||||||
|
}
|
||||||
|
|
||||||
|
static lv_obj_t *make_small_btn(lv_obj_t *parent, const char *txt, int x, int y,
|
||||||
|
int w, int h, lv_event_cb_t cb, void *ud) {
|
||||||
|
lv_obj_t *btn = lv_button_create(parent);
|
||||||
|
lv_obj_set_pos(btn, x, y);
|
||||||
|
lv_obj_set_size(btn, w, h);
|
||||||
|
lv_obj_add_flag(btn, LV_OBJ_FLAG_CLICKABLE);
|
||||||
|
if (cb) {
|
||||||
|
lv_obj_add_event_cb(btn, cb, LV_EVENT_CLICKED, ud);
|
||||||
|
}
|
||||||
|
style_btn(btn, COL_BTN_OFF);
|
||||||
|
|
||||||
|
lv_obj_t *lbl = lv_label_create(btn);
|
||||||
|
lv_label_set_text(lbl, txt);
|
||||||
|
lv_obj_set_style_text_font(lbl, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(lbl, lv_color_hex(COL_INK), 0);
|
||||||
|
lv_obj_center(lbl);
|
||||||
|
return btn;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void build_custom_panel(lv_obj_t *scr) {
|
||||||
|
panel_custom = lv_obj_create(scr);
|
||||||
|
lv_obj_set_size(panel_custom, UI_HOR_RES, UI_VER_RES);
|
||||||
|
lv_obj_set_pos(panel_custom, UX(0), UY(0));
|
||||||
|
lv_obj_set_style_bg_color(panel_custom, lv_color_hex(COL_BG), 0);
|
||||||
|
lv_obj_set_style_bg_opa(panel_custom, LV_OPA_COVER, 0);
|
||||||
|
lv_obj_set_style_border_width(panel_custom, 0, 0);
|
||||||
|
lv_obj_set_style_pad_all(panel_custom, 0, 0);
|
||||||
|
lv_obj_set_style_radius(panel_custom, 0, 0);
|
||||||
|
lv_obj_clear_flag(panel_custom, LV_OBJ_FLAG_SCROLLABLE);
|
||||||
|
lv_obj_add_flag(panel_custom, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
|
||||||
|
lv_obj_t *title = lv_label_create(panel_custom);
|
||||||
|
lv_label_set_text(title, "Custom minutes");
|
||||||
|
lv_obj_set_style_text_font(title, &lv_font_montserrat_16, 0);
|
||||||
|
lv_obj_set_style_text_color(title, lv_color_hex(COL_ACCENT), 0);
|
||||||
|
lv_obj_align(title, LV_ALIGN_TOP_MID, UX(0), UY(8));
|
||||||
|
|
||||||
|
lbl_custom_val = lv_label_create(panel_custom);
|
||||||
|
lv_label_set_text(lbl_custom_val, "min: _");
|
||||||
|
lv_obj_set_style_text_font(lbl_custom_val, &lv_font_montserrat_28, 0);
|
||||||
|
lv_obj_set_style_text_color(lbl_custom_val, lv_color_hex(COL_INK), 0);
|
||||||
|
lv_obj_align(lbl_custom_val, LV_ALIGN_TOP_MID, UX(0), UY(36));
|
||||||
|
|
||||||
|
// Numpad 3x4
|
||||||
|
static const char *keys[12] = {"1", "2", "3", "4", "5", "6",
|
||||||
|
"7", "8", "9", "C", "0", "OK"};
|
||||||
|
const int kw = 90;
|
||||||
|
const int kh = 32;
|
||||||
|
const int ox = 16;
|
||||||
|
const int oy = 72;
|
||||||
|
const int gap = 6;
|
||||||
|
for (int i = 0; i < 12; i++) {
|
||||||
|
const int r = i / 3;
|
||||||
|
const int c = i % 3;
|
||||||
|
const int x = ox + c * (kw + gap);
|
||||||
|
const int y = oy + r * (kh + gap);
|
||||||
|
lv_event_cb_t cb = on_custom_digit;
|
||||||
|
void *ud = nullptr;
|
||||||
|
if (i <= 8) {
|
||||||
|
ud = (void *)(intptr_t)(i + 1); // 1..9
|
||||||
|
} else if (i == 9) {
|
||||||
|
ud = (void *)(intptr_t)-1; // clear
|
||||||
|
} else if (i == 10) {
|
||||||
|
ud = (void *)(intptr_t)0; // 0
|
||||||
|
} else {
|
||||||
|
cb = on_custom_ok;
|
||||||
|
}
|
||||||
|
lv_obj_t *b = make_small_btn(panel_custom, keys[i], x, y, kw, kh, cb, ud);
|
||||||
|
if (i == 11) {
|
||||||
|
style_btn(b, COL_BTN_PLAY);
|
||||||
|
} else if (i == 9) {
|
||||||
|
style_btn(b, COL_BTN_STOP);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
make_small_btn(panel_custom, "Cancel", 16, UI_VER_RES - 34, UI_HOR_RES - 32,
|
||||||
|
28, on_custom_cancel, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
void ui_refresh_audio_buttons() {
|
void ui_refresh_audio_buttons() {
|
||||||
if (lbl_speaker) {
|
if (lbl_speaker) {
|
||||||
char buf[28];
|
lv_label_set_text(lbl_speaker, audio_is_enabled() ? "ON" : "OFF");
|
||||||
snprintf(buf, sizeof(buf), "%s %u%%",
|
|
||||||
audio_is_enabled() ? "Speaker ON" : "Speaker OFF",
|
|
||||||
(unsigned)audio_get_volume());
|
|
||||||
lv_label_set_text(lbl_speaker, buf);
|
|
||||||
}
|
}
|
||||||
if (btn_speaker) {
|
if (btn_speaker) {
|
||||||
lv_obj_set_style_bg_color(
|
style_btn(btn_speaker, audio_is_enabled() ? COL_BTN_ON : COL_BTN_OFF);
|
||||||
btn_speaker,
|
|
||||||
audio_is_enabled() ? lv_color_hex(0x2E7D32) : lv_color_hex(0x455A64), 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lbl_play) {
|
|
||||||
lv_label_set_text(lbl_play, audio_is_playing() ? "Stop" : "Play Music");
|
|
||||||
}
|
|
||||||
if (btn_play) {
|
|
||||||
lv_obj_set_style_bg_color(
|
|
||||||
btn_play,
|
|
||||||
audio_is_playing() ? lv_color_hex(0xC62828) : lv_color_hex(0x1565C0), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lbl_audio) {
|
if (lbl_audio) {
|
||||||
lv_label_set_text(lbl_audio, audio_last_status());
|
lv_label_set_text(lbl_audio, audio_last_status());
|
||||||
}
|
}
|
||||||
|
if (slider_vol &&
|
||||||
|
(int32_t)audio_get_volume() != lv_slider_get_value(slider_vol)) {
|
||||||
|
lv_slider_set_value(slider_vol, audio_get_volume(), LV_ANIM_OFF);
|
||||||
|
}
|
||||||
|
sync_vol_label();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_create() {
|
void ui_create() {
|
||||||
|
pomo_init();
|
||||||
|
sound_set_selected(11); // Alarm
|
||||||
|
|
||||||
lv_obj_t *scr = lv_screen_active();
|
lv_obj_t *scr = lv_screen_active();
|
||||||
lv_obj_set_style_bg_color(scr, lv_color_hex(0x0D1B2A), 0);
|
lv_obj_set_style_bg_color(scr, lv_color_hex(COL_BG), 0);
|
||||||
lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0);
|
lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0);
|
||||||
|
lv_obj_clear_flag(scr, LV_OBJ_FLAG_SCROLLABLE);
|
||||||
|
|
||||||
lv_obj_t *col = lv_obj_create(scr);
|
lv_obj_t *lbl_title = lv_label_create(scr);
|
||||||
lv_obj_set_size(col, LV_PCT(100), LV_PCT(100));
|
lv_label_set_text(lbl_title, "POMO");
|
||||||
lv_obj_set_style_bg_opa(col, LV_OPA_TRANSP, 0);
|
lv_obj_set_style_text_font(lbl_title, &lv_font_montserrat_20, 0);
|
||||||
lv_obj_set_style_border_width(col, 0, 0);
|
lv_obj_set_style_text_color(lbl_title, lv_color_hex(COL_TOMATO), 0);
|
||||||
lv_obj_set_style_pad_left(col, 10, 0);
|
lv_obj_set_pos(lbl_title, UX(12), UY(4));
|
||||||
lv_obj_set_style_pad_right(col, 10, 0);
|
|
||||||
lv_obj_set_style_pad_top(col, 6, 0);
|
|
||||||
lv_obj_set_style_pad_bottom(col, 6, 0);
|
|
||||||
lv_obj_set_flex_flow(col, LV_FLEX_FLOW_COLUMN);
|
|
||||||
lv_obj_set_flex_align(col, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER,
|
|
||||||
LV_FLEX_ALIGN_CENTER);
|
|
||||||
lv_obj_set_style_pad_row(col, 2, 0);
|
|
||||||
lv_obj_clear_flag(col, LV_OBJ_FLAG_SCROLLABLE);
|
|
||||||
|
|
||||||
lv_obj_t *title = lv_label_create(col);
|
lbl_chip = lv_label_create(scr);
|
||||||
lv_label_set_text(title, "TF + Speaker");
|
lv_label_set_text(lbl_chip, "-");
|
||||||
lv_obj_set_style_text_font(title, &lv_font_montserrat_18, 0);
|
lv_obj_set_style_text_font(lbl_chip, &lv_font_montserrat_14, 0);
|
||||||
lv_obj_set_style_text_color(title, lv_color_hex(0x4FC3F7), 0);
|
lv_obj_set_style_text_color(lbl_chip, lv_color_hex(COL_MUTED), 0);
|
||||||
|
lv_obj_align(lbl_chip, LV_ALIGN_TOP_RIGHT, UX(-12), UY(8));
|
||||||
|
|
||||||
make_row(col, "Status", &lbl_status);
|
make_small_btn(scr, "-", 12, 32, 40, 44, on_minus_clicked, nullptr);
|
||||||
make_row(col, "Type", &lbl_type);
|
|
||||||
make_row(col, "Format", &lbl_fs);
|
|
||||||
make_row(col, "Capacity", &lbl_capacity);
|
|
||||||
make_row(col, "Audio", &lbl_audio);
|
|
||||||
|
|
||||||
bar_usage = lv_bar_create(col);
|
lbl_timer = lv_label_create(scr);
|
||||||
lv_obj_set_width(bar_usage, LV_PCT(100));
|
lv_label_set_text(lbl_timer, "05:00");
|
||||||
lv_obj_set_height(bar_usage, 10);
|
lv_obj_set_style_text_font(lbl_timer, &lv_font_montserrat_28, 0);
|
||||||
lv_bar_set_range(bar_usage, 0, 100);
|
lv_obj_set_style_text_color(lbl_timer, lv_color_hex(COL_ACCENT), 0);
|
||||||
lv_bar_set_value(bar_usage, 0, LV_ANIM_OFF);
|
lv_obj_align(lbl_timer, LV_ALIGN_TOP_MID, UX(0), UY(34));
|
||||||
lv_obj_set_style_bg_color(bar_usage, lv_color_hex(0x263238), 0);
|
|
||||||
lv_obj_set_style_bg_color(bar_usage, lv_color_hex(0x29B6F6), LV_PART_INDICATOR);
|
|
||||||
|
|
||||||
lv_obj_t *row_btns = lv_obj_create(col);
|
make_small_btn(scr, "+", UI_HOR_RES - 52, 32, 40, 44, on_plus_clicked,
|
||||||
lv_obj_set_width(row_btns, LV_PCT(100));
|
nullptr);
|
||||||
lv_obj_set_height(row_btns, 64);
|
|
||||||
lv_obj_set_style_bg_opa(row_btns, LV_OPA_TRANSP, 0);
|
|
||||||
lv_obj_set_style_border_width(row_btns, 0, 0);
|
|
||||||
lv_obj_set_style_pad_all(row_btns, 0, 0);
|
|
||||||
lv_obj_set_flex_flow(row_btns, LV_FLEX_FLOW_ROW);
|
|
||||||
lv_obj_set_flex_align(row_btns, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER,
|
|
||||||
LV_FLEX_ALIGN_CENTER);
|
|
||||||
lv_obj_clear_flag(row_btns, LV_OBJ_FLAG_SCROLLABLE);
|
|
||||||
|
|
||||||
btn_speaker = lv_button_create(row_btns);
|
lbl_status = lv_label_create(scr);
|
||||||
lv_obj_set_size(btn_speaker, 150, 60);
|
lv_label_set_text(lbl_status, "5m - READY");
|
||||||
lv_obj_set_style_radius(btn_speaker, 8, 0);
|
lv_obj_set_style_text_font(lbl_status, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(lbl_status, lv_color_hex(COL_MUTED), 0);
|
||||||
|
lv_obj_align(lbl_status, LV_ALIGN_TOP_MID, UX(0), UY(74));
|
||||||
|
|
||||||
|
// Presets: 1 3 5 10 + Set
|
||||||
|
const int gap = 6;
|
||||||
|
const int pw = (UI_HOR_RES - 24 - gap * 4) / 5;
|
||||||
|
static const char *ptxt[PRESET_N] = {"1", "3", "5", "10"};
|
||||||
|
for (int i = 0; i < PRESET_N; i++) {
|
||||||
|
btn_preset[i] =
|
||||||
|
make_small_btn(scr, ptxt[i], 12 + i * (pw + gap), 94, pw, 30,
|
||||||
|
on_preset_clicked, (void *)(intptr_t)i);
|
||||||
|
}
|
||||||
|
btn_custom = make_small_btn(scr, "Set", 12 + PRESET_N * (pw + gap), 94, pw,
|
||||||
|
30, on_custom_open, nullptr);
|
||||||
|
|
||||||
|
// Sound
|
||||||
|
lv_obj_t *snd_cap = lv_label_create(scr);
|
||||||
|
lv_label_set_text(snd_cap, "SOUND");
|
||||||
|
lv_obj_set_style_text_font(snd_cap, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(snd_cap, lv_color_hex(COL_MUTED), 0);
|
||||||
|
lv_obj_set_pos(snd_cap, UX(12), UY(134));
|
||||||
|
|
||||||
|
make_small_btn(scr, "<", 68, 128, 32, 28, on_sound_prev, nullptr);
|
||||||
|
|
||||||
|
lv_obj_t *btn_snd = lv_button_create(scr);
|
||||||
|
lv_obj_set_pos(btn_snd, UX(104), UY(128));
|
||||||
|
lv_obj_set_size(btn_snd, UI_HOR_RES - 104 - 48, 28);
|
||||||
|
lv_obj_add_flag(btn_snd, LV_OBJ_FLAG_CLICKABLE);
|
||||||
|
lv_obj_add_event_cb(btn_snd, on_sound_preview, LV_EVENT_CLICKED, nullptr);
|
||||||
|
style_btn(btn_snd, COL_MODE);
|
||||||
|
|
||||||
|
lbl_sound = lv_label_create(btn_snd);
|
||||||
|
lv_label_set_text(lbl_sound, "Alarm");
|
||||||
|
lv_obj_set_style_text_font(lbl_sound, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(lbl_sound, lv_color_hex(COL_INK), 0);
|
||||||
|
lv_obj_center(lbl_sound);
|
||||||
|
|
||||||
|
make_small_btn(scr, ">", UI_HOR_RES - 44, 128, 32, 28, on_sound_next,
|
||||||
|
nullptr);
|
||||||
|
|
||||||
|
// Volume
|
||||||
|
lv_obj_t *vol_cap = lv_label_create(scr);
|
||||||
|
lv_label_set_text(vol_cap, "VOL");
|
||||||
|
lv_obj_set_style_text_font(vol_cap, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(vol_cap, lv_color_hex(COL_MUTED), 0);
|
||||||
|
lv_obj_set_pos(vol_cap, UX(12), UY(164));
|
||||||
|
|
||||||
|
slider_vol = lv_slider_create(scr);
|
||||||
|
lv_obj_set_size(slider_vol, UI_HOR_RES - 12 - 56 - 44, 14);
|
||||||
|
lv_obj_set_pos(slider_vol, UX(52), UY(164));
|
||||||
|
lv_slider_set_range(slider_vol, 0, 100);
|
||||||
|
lv_slider_set_value(slider_vol, audio_get_volume(), LV_ANIM_OFF);
|
||||||
|
lv_obj_set_style_bg_color(slider_vol, lv_color_hex(COL_LINE), LV_PART_MAIN);
|
||||||
|
lv_obj_set_style_bg_opa(slider_vol, LV_OPA_COVER, LV_PART_MAIN);
|
||||||
|
lv_obj_set_style_radius(slider_vol, 4, LV_PART_MAIN);
|
||||||
|
lv_obj_set_style_bg_color(slider_vol, lv_color_hex(COL_OK), LV_PART_INDICATOR);
|
||||||
|
lv_obj_set_style_bg_opa(slider_vol, LV_OPA_COVER, LV_PART_INDICATOR);
|
||||||
|
lv_obj_set_style_bg_color(slider_vol, lv_color_hex(COL_INK), LV_PART_KNOB);
|
||||||
|
lv_obj_set_style_pad_all(slider_vol, 4, LV_PART_KNOB);
|
||||||
|
lv_obj_add_event_cb(slider_vol, on_vol_changed, LV_EVENT_VALUE_CHANGED,
|
||||||
|
nullptr);
|
||||||
|
|
||||||
|
lbl_vol = lv_label_create(scr);
|
||||||
|
lv_label_set_text(lbl_vol, "40%");
|
||||||
|
lv_obj_set_style_text_font(lbl_vol, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(lbl_vol, lv_color_hex(COL_INK), 0);
|
||||||
|
lv_obj_align(lbl_vol, LV_ALIGN_TOP_RIGHT, UX(-12), UY(164));
|
||||||
|
|
||||||
|
lbl_audio = lv_label_create(scr);
|
||||||
|
lv_label_set_text(lbl_audio, "Speaker OFF");
|
||||||
|
lv_obj_set_style_text_font(lbl_audio, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(lbl_audio, lv_color_hex(COL_MUTED), 0);
|
||||||
|
lv_obj_set_pos(lbl_audio, UX(12), UY(182));
|
||||||
|
|
||||||
|
const int btn_y = 200;
|
||||||
|
const int btn_h = 36;
|
||||||
|
const int bgap = 8;
|
||||||
|
const int btn_w = (UI_HOR_RES - 24 - bgap * 2) / 3;
|
||||||
|
|
||||||
|
btn_speaker = lv_button_create(scr);
|
||||||
|
lv_obj_set_pos(btn_speaker, 12, btn_y);
|
||||||
|
lv_obj_set_size(btn_speaker, btn_w, btn_h);
|
||||||
lv_obj_add_flag(btn_speaker, LV_OBJ_FLAG_CLICKABLE);
|
lv_obj_add_flag(btn_speaker, LV_OBJ_FLAG_CLICKABLE);
|
||||||
lv_obj_add_event_cb(btn_speaker, on_speaker_clicked, LV_EVENT_CLICKED, nullptr);
|
lv_obj_add_event_cb(btn_speaker, on_speaker_clicked, LV_EVENT_CLICKED,
|
||||||
|
nullptr);
|
||||||
|
style_btn(btn_speaker, COL_BTN_OFF);
|
||||||
|
|
||||||
|
lv_obj_t *spk_cap = lv_label_create(btn_speaker);
|
||||||
|
lv_label_set_text(spk_cap, "SPK");
|
||||||
|
lv_obj_set_style_text_font(spk_cap, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(spk_cap, lv_color_hex(COL_MUTED), 0);
|
||||||
|
lv_obj_align(spk_cap, LV_ALIGN_LEFT_MID, UX(8), UY(0));
|
||||||
|
|
||||||
lbl_speaker = lv_label_create(btn_speaker);
|
lbl_speaker = lv_label_create(btn_speaker);
|
||||||
lv_label_set_text(lbl_speaker, "Speaker OFF");
|
lv_label_set_text(lbl_speaker, "OFF");
|
||||||
lv_obj_set_style_text_font(lbl_speaker, &lv_font_montserrat_14, 0);
|
lv_obj_set_style_text_font(lbl_speaker, &lv_font_montserrat_14, 0);
|
||||||
lv_obj_center(lbl_speaker);
|
lv_obj_set_style_text_color(lbl_speaker, lv_color_hex(COL_INK), 0);
|
||||||
|
lv_obj_align(lbl_speaker, LV_ALIGN_RIGHT_MID, UX(-8), UY(0));
|
||||||
|
|
||||||
btn_play = lv_button_create(row_btns);
|
btn_start = lv_button_create(scr);
|
||||||
lv_obj_set_size(btn_play, 150, 60);
|
lv_obj_set_pos(btn_start, 12 + btn_w + bgap, btn_y);
|
||||||
lv_obj_set_style_radius(btn_play, 8, 0);
|
lv_obj_set_size(btn_start, btn_w, btn_h);
|
||||||
lv_obj_add_flag(btn_play, LV_OBJ_FLAG_CLICKABLE);
|
lv_obj_add_flag(btn_start, LV_OBJ_FLAG_CLICKABLE);
|
||||||
lv_obj_add_event_cb(btn_play, on_play_clicked, LV_EVENT_CLICKED, nullptr);
|
lv_obj_add_event_cb(btn_start, on_start_clicked, LV_EVENT_CLICKED, nullptr);
|
||||||
lbl_play = lv_label_create(btn_play);
|
style_btn(btn_start, COL_BTN_PLAY);
|
||||||
lv_label_set_text(lbl_play, "Play Music");
|
|
||||||
lv_obj_set_style_text_font(lbl_play, &lv_font_montserrat_14, 0);
|
|
||||||
lv_obj_center(lbl_play);
|
|
||||||
|
|
||||||
lbl_touch_dbg = lv_label_create(col);
|
lbl_start = lv_label_create(btn_start);
|
||||||
lv_label_set_text(lbl_touch_dbg, "Touch: --");
|
lv_label_set_text(lbl_start, "START");
|
||||||
lv_obj_set_style_text_font(lbl_touch_dbg, &lv_font_montserrat_14, 0);
|
lv_obj_set_style_text_font(lbl_start, &lv_font_montserrat_16, 0);
|
||||||
lv_obj_set_style_text_color(lbl_touch_dbg, lv_color_hex(0xFFB74D), 0);
|
lv_obj_set_style_text_color(lbl_start, lv_color_hex(COL_INK), 0);
|
||||||
|
lv_obj_center(lbl_start);
|
||||||
|
|
||||||
touch_cursor = lv_obj_create(scr);
|
btn_reset = lv_button_create(scr);
|
||||||
lv_obj_set_size(touch_cursor, 14, 14);
|
lv_obj_set_pos(btn_reset, 12 + (btn_w + bgap) * 2, btn_y);
|
||||||
lv_obj_set_style_radius(touch_cursor, LV_RADIUS_CIRCLE, 0);
|
lv_obj_set_size(btn_reset, btn_w, btn_h);
|
||||||
lv_obj_set_style_bg_color(touch_cursor, lv_color_hex(0xFF5252), 0);
|
lv_obj_add_flag(btn_reset, LV_OBJ_FLAG_CLICKABLE);
|
||||||
lv_obj_set_style_bg_opa(touch_cursor, LV_OPA_COVER, 0);
|
lv_obj_add_event_cb(btn_reset, on_reset_clicked, LV_EVENT_CLICKED, nullptr);
|
||||||
lv_obj_set_style_border_width(touch_cursor, 1, 0);
|
style_btn(btn_reset, COL_BTN_OFF);
|
||||||
lv_obj_set_style_border_color(touch_cursor, lv_color_hex(0xFFFFFF), 0);
|
|
||||||
lv_obj_add_flag(touch_cursor, LV_OBJ_FLAG_HIDDEN);
|
|
||||||
lv_obj_clear_flag(touch_cursor, LV_OBJ_FLAG_CLICKABLE);
|
|
||||||
lv_obj_clear_flag(touch_cursor, LV_OBJ_FLAG_SCROLLABLE);
|
|
||||||
|
|
||||||
|
lv_obj_t *lbl_rst = lv_label_create(btn_reset);
|
||||||
|
lv_label_set_text(lbl_rst, "RESET");
|
||||||
|
lv_obj_set_style_text_font(lbl_rst, &lv_font_montserrat_16, 0);
|
||||||
|
lv_obj_set_style_text_color(lbl_rst, lv_color_hex(COL_INK), 0);
|
||||||
|
lv_obj_center(lbl_rst);
|
||||||
|
|
||||||
|
build_custom_panel(scr);
|
||||||
|
|
||||||
|
refresh_sound_label();
|
||||||
|
refresh_timer_labels();
|
||||||
ui_refresh_audio_buttons();
|
ui_refresh_audio_buttons();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_set_touch_debug(int16_t lx, int16_t ly, bool down) {
|
void ui_set_touch_debug(int16_t /*lx*/, int16_t /*ly*/, bool /*down*/) {}
|
||||||
if (!lbl_touch_dbg) {
|
|
||||||
return;
|
void ui_tick() {
|
||||||
}
|
pomo_tick();
|
||||||
if (!down) {
|
if (pomo_consume_alarm()) {
|
||||||
lv_label_set_text(lbl_touch_dbg, "Touch: up");
|
const int8_t sel = sound_selected();
|
||||||
if (touch_cursor) {
|
if (sel >= 0) {
|
||||||
lv_obj_add_flag(touch_cursor, LV_OBJ_FLAG_HIDDEN);
|
audio_play_sound((uint8_t)sel);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
char buf[40];
|
|
||||||
snprintf(buf, sizeof(buf), "Touch %d,%d", (int)lx, (int)ly);
|
|
||||||
lv_label_set_text(lbl_touch_dbg, buf);
|
|
||||||
if (touch_cursor) {
|
|
||||||
lv_obj_clear_flag(touch_cursor, LV_OBJ_FLAG_HIDDEN);
|
|
||||||
lv_obj_set_pos(touch_cursor, lx - 7, ly - 7);
|
|
||||||
lv_obj_move_foreground(touch_cursor);
|
|
||||||
}
|
}
|
||||||
|
refresh_timer_labels();
|
||||||
|
ui_refresh_audio_buttons();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_update(const SdCardInfo &info) {
|
void ui_update(const SdCardInfo &info) {
|
||||||
if (!info.mounted || !info.present) {
|
if (!info.mounted || !info.present) {
|
||||||
lv_label_set_text(lbl_status, info.error.length() ? info.error.c_str() : "Fail");
|
lv_label_set_text(lbl_chip,
|
||||||
lv_obj_set_style_text_color(lbl_status, lv_color_hex(0xEF5350), 0);
|
info.error.length() ? info.error.c_str() : "TF?");
|
||||||
lv_label_set_text(lbl_type, info.type);
|
lv_obj_set_style_text_color(lbl_chip, lv_color_hex(COL_BAD), 0);
|
||||||
lv_label_set_text(lbl_fs, "—");
|
|
||||||
lv_label_set_text(lbl_capacity, "—");
|
|
||||||
lv_bar_set_value(bar_usage, 0, LV_ANIM_OFF);
|
|
||||||
} else {
|
} else {
|
||||||
lv_label_set_text(lbl_status, "Mounted OK");
|
char chip[40];
|
||||||
lv_obj_set_style_text_color(lbl_status, lv_color_hex(0x66BB6A), 0);
|
snprintf(chip, sizeof(chip), "OK %s", info.type);
|
||||||
lv_label_set_text(lbl_type, info.type);
|
lv_label_set_text(lbl_chip, chip);
|
||||||
lv_label_set_text(lbl_fs, info.filesystem);
|
lv_obj_set_style_text_color(lbl_chip, lv_color_hex(COL_OK), 0);
|
||||||
String cap = format_bytes(info.card_bytes);
|
|
||||||
lv_label_set_text(lbl_capacity, cap.c_str());
|
|
||||||
|
|
||||||
int pct = 0;
|
|
||||||
if (info.total_bytes > 0) {
|
|
||||||
pct = static_cast<int>((info.used_bytes * 100ULL) / info.total_bytes);
|
|
||||||
if (pct > 100) {
|
|
||||||
pct = 100;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lv_bar_set_value(bar_usage, pct, LV_ANIM_ON);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_refresh_audio_buttons();
|
ui_refresh_audio_buttons();
|
||||||
}
|
}
|
||||||
|
|||||||
129
src/wav_list.cpp
Normal file
129
src/wav_list.cpp
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
#include "wav_list.h"
|
||||||
|
#include "sd_info.h"
|
||||||
|
|
||||||
|
#include <SD.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static WavTrack g_tracks[WAV_MAX];
|
||||||
|
static uint8_t g_count = 0;
|
||||||
|
static int8_t g_selected = -1;
|
||||||
|
|
||||||
|
static bool ends_with_wav(const char *name) {
|
||||||
|
const size_t n = strlen(name);
|
||||||
|
if (n < 4) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const char *e = name + n - 4;
|
||||||
|
return (e[0] == '.' &&
|
||||||
|
(e[1] == 'w' || e[1] == 'W') &&
|
||||||
|
(e[2] == 'a' || e[2] == 'A') &&
|
||||||
|
(e[3] == 'v' || e[3] == 'V'));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void make_display_name(const char *filename, char *out, size_t out_len) {
|
||||||
|
size_t len = strlen(filename);
|
||||||
|
if (len >= 4 && ends_with_wav(filename)) {
|
||||||
|
len -= 4;
|
||||||
|
}
|
||||||
|
char tmp[WAV_NAME_MAX];
|
||||||
|
size_t j = 0;
|
||||||
|
for (size_t i = 0; i < len && j + 1 < sizeof(tmp); i++) {
|
||||||
|
const unsigned char c = (unsigned char)filename[i];
|
||||||
|
tmp[j++] = (c >= 0x20 && c < 0x7F) ? (char)c : '_';
|
||||||
|
}
|
||||||
|
tmp[j] = '\0';
|
||||||
|
if (tmp[0] == '\0') {
|
||||||
|
snprintf(out, out_len, "wav");
|
||||||
|
} else {
|
||||||
|
snprintf(out, out_len, "%s", tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool add_track(const char *dir, const char *filename, uint32_t bytes) {
|
||||||
|
if (g_count >= WAV_MAX || !ends_with_wav(filename)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
WavTrack &t = g_tracks[g_count];
|
||||||
|
if (dir && dir[0] && !(dir[0] == '/' && dir[1] == '\0')) {
|
||||||
|
snprintf(t.path, sizeof(t.path), "%s/%s", dir, filename);
|
||||||
|
} else {
|
||||||
|
snprintf(t.path, sizeof(t.path), "/%s", filename);
|
||||||
|
}
|
||||||
|
make_display_name(filename, t.name, sizeof(t.name));
|
||||||
|
t.bytes = bytes;
|
||||||
|
g_count++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void scan_dir(const char *dir) {
|
||||||
|
File root = SD.open(dir);
|
||||||
|
if (!root || !root.isDirectory()) {
|
||||||
|
if (root) {
|
||||||
|
root.close();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
File f = root.openNextFile();
|
||||||
|
while (f && g_count < WAV_MAX) {
|
||||||
|
if (!f.isDirectory()) {
|
||||||
|
const char *name = f.name();
|
||||||
|
const char *base = strrchr(name, '/');
|
||||||
|
base = base ? base + 1 : name;
|
||||||
|
add_track(dir, base, (uint32_t)f.size());
|
||||||
|
}
|
||||||
|
f = root.openNextFile();
|
||||||
|
}
|
||||||
|
root.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wav_list_scan() {
|
||||||
|
g_count = 0;
|
||||||
|
g_selected = -1;
|
||||||
|
if (!sd_init()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
scan_dir("/");
|
||||||
|
scan_dir("/wav");
|
||||||
|
scan_dir("/WAV");
|
||||||
|
scan_dir("/sounds");
|
||||||
|
scan_dir("/Sounds");
|
||||||
|
if (g_count > 0) {
|
||||||
|
g_selected = 0;
|
||||||
|
}
|
||||||
|
Serial.printf("WAV list: %u\n", (unsigned)g_count);
|
||||||
|
for (uint8_t i = 0; i < g_count; i++) {
|
||||||
|
Serial.printf(" [%u] %s %uB\n", (unsigned)i, g_tracks[i].path,
|
||||||
|
(unsigned)g_tracks[i].bytes);
|
||||||
|
}
|
||||||
|
return g_count > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t wav_list_count() {
|
||||||
|
return g_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t wav_list_selected() {
|
||||||
|
return g_selected;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wav_list_set_selected(int8_t idx) {
|
||||||
|
if (idx < 0 || idx >= (int8_t)g_count) {
|
||||||
|
g_selected = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
g_selected = idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
const WavTrack *wav_list_get(uint8_t idx) {
|
||||||
|
if (idx >= g_count) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return &g_tracks[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *wav_list_current_path() {
|
||||||
|
if (g_selected < 0 || g_selected >= (int8_t)g_count) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return g_tracks[g_selected].path;
|
||||||
|
}
|
||||||
365
src/wav_player.cpp
Normal file
365
src/wav_player.cpp
Normal file
@@ -0,0 +1,365 @@
|
|||||||
|
#include "wav_player.h"
|
||||||
|
#include "pins.h"
|
||||||
|
#include "sd_info.h"
|
||||||
|
|
||||||
|
#include <SD.h>
|
||||||
|
#include <driver/dac.h>
|
||||||
|
#include <esp32-hal.h>
|
||||||
|
#include <esp_timer.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
|
|
||||||
|
// DAC2-only;大文件边读边降采样成 8bit mono,避免 "TooBig"
|
||||||
|
|
||||||
|
static constexpr uint32_t k_max_pcm = 120 * 1024; // ~7.5s @16k
|
||||||
|
static constexpr uint32_t k_max_file = 3 * 1024 * 1024;
|
||||||
|
static constexpr uint32_t k_target_hz = 22050;
|
||||||
|
|
||||||
|
static const char *g_status = "idle";
|
||||||
|
static volatile bool g_playing = false;
|
||||||
|
static volatile bool g_stop_req = false;
|
||||||
|
static uint8_t g_volume = 40;
|
||||||
|
|
||||||
|
static uint8_t *g_pcm = nullptr;
|
||||||
|
static uint32_t g_pcm_len = 0;
|
||||||
|
static uint32_t g_rate = 16000;
|
||||||
|
|
||||||
|
static TaskHandle_t g_task = nullptr;
|
||||||
|
|
||||||
|
static void release_dac_pins() {
|
||||||
|
dac_output_disable(DAC_CHANNEL_1);
|
||||||
|
dac_output_disable(DAC_CHANNEL_2);
|
||||||
|
pinMode(SPEAKER_PIN, OUTPUT);
|
||||||
|
digitalWrite(SPEAKER_PIN, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void teardown_pcm() {
|
||||||
|
if (g_pcm) {
|
||||||
|
free(g_pcm);
|
||||||
|
g_pcm = nullptr;
|
||||||
|
}
|
||||||
|
g_pcm_len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t scale_sample(uint8_t u8) {
|
||||||
|
int c = (int)u8 - 128;
|
||||||
|
c = (c * (int)g_volume) / 100;
|
||||||
|
c = (c * 45) / 100;
|
||||||
|
int o = 128 + c;
|
||||||
|
if (o < 0) {
|
||||||
|
o = 0;
|
||||||
|
}
|
||||||
|
if (o > 255) {
|
||||||
|
o = 255;
|
||||||
|
}
|
||||||
|
return (uint8_t)o;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool read_u16(File &f, uint16_t &v) {
|
||||||
|
uint8_t b[2];
|
||||||
|
if (f.read(b, 2) != 2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
v = (uint16_t)b[0] | ((uint16_t)b[1] << 8);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool read_u32(File &f, uint32_t &v) {
|
||||||
|
uint8_t b[4];
|
||||||
|
if (f.read(b, 4) != 4) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
v = (uint32_t)b[0] | ((uint32_t)b[1] << 8) | ((uint32_t)b[2] << 16) |
|
||||||
|
((uint32_t)b[3] << 24);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool read_frame_s16(File &f, uint16_t channels, uint16_t bits, int32_t &mono) {
|
||||||
|
int32_t mix = 0;
|
||||||
|
for (uint16_t c = 0; c < channels; c++) {
|
||||||
|
if (bits == 8) {
|
||||||
|
const int b = f.read();
|
||||||
|
if (b < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
mix += (b - 128) * 256;
|
||||||
|
} else {
|
||||||
|
uint16_t s16;
|
||||||
|
if (!read_u16(f, s16)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
mix += (int16_t)s16;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (channels == 2) {
|
||||||
|
mix /= 2;
|
||||||
|
}
|
||||||
|
mono = mix;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool load_wav_pcm(const char *path) {
|
||||||
|
teardown_pcm();
|
||||||
|
|
||||||
|
File f = SD.open(path, FILE_READ);
|
||||||
|
if (!f) {
|
||||||
|
g_status = "OpenFail";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const uint32_t fsz = f.size();
|
||||||
|
if (fsz > k_max_file) {
|
||||||
|
f.close();
|
||||||
|
g_status = "TooBig";
|
||||||
|
Serial.printf("WAV reject size=%u\n", (unsigned)fsz);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t tag = 0;
|
||||||
|
if (!read_u32(f, tag) || tag != 0x46464952) {
|
||||||
|
f.close();
|
||||||
|
g_status = "NotRIFF";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uint32_t riff_size = 0;
|
||||||
|
read_u32(f, riff_size);
|
||||||
|
(void)riff_size;
|
||||||
|
if (!read_u32(f, tag) || tag != 0x45564157) {
|
||||||
|
f.close();
|
||||||
|
g_status = "NotWAVE";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t audio_fmt = 0, channels = 0, bits = 0;
|
||||||
|
uint32_t sample_rate = 0;
|
||||||
|
bool got_fmt = false;
|
||||||
|
uint32_t data_size = 0;
|
||||||
|
uint32_t data_pos = 0;
|
||||||
|
bool got_data = false;
|
||||||
|
|
||||||
|
while (f.available()) {
|
||||||
|
if (!read_u32(f, tag)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
uint32_t chunk_size = 0;
|
||||||
|
if (!read_u32(f, chunk_size)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (tag == 0x20746d66) { // fmt
|
||||||
|
uint16_t block_align = 0;
|
||||||
|
uint32_t byte_rate = 0;
|
||||||
|
if (!read_u16(f, audio_fmt) || !read_u16(f, channels) ||
|
||||||
|
!read_u32(f, sample_rate) || !read_u32(f, byte_rate) ||
|
||||||
|
!read_u16(f, block_align) || !read_u16(f, bits)) {
|
||||||
|
f.close();
|
||||||
|
g_status = "BadFmt";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
(void)byte_rate;
|
||||||
|
(void)block_align;
|
||||||
|
if (chunk_size > 16) {
|
||||||
|
f.seek(f.position() + (chunk_size - 16));
|
||||||
|
}
|
||||||
|
// WAVE_FORMAT_EXTENSIBLE(0xFFFE) 也常见,当 PCM 用若 bits 合法
|
||||||
|
got_fmt = true;
|
||||||
|
} else if (tag == 0x61746164) { // data
|
||||||
|
data_pos = f.position();
|
||||||
|
data_size = chunk_size;
|
||||||
|
got_data = true;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
f.seek(f.position() + chunk_size + (chunk_size & 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1=PCM, 0xFFFE=extensible(很多导出工具)
|
||||||
|
if (!got_fmt || !got_data || (audio_fmt != 1 && audio_fmt != 0xFFFE)) {
|
||||||
|
f.close();
|
||||||
|
g_status = "NeedPCM";
|
||||||
|
Serial.printf("WAV fmt=%u\n", (unsigned)audio_fmt);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (channels < 1 || channels > 2 || (bits != 8 && bits != 16)) {
|
||||||
|
f.close();
|
||||||
|
g_status = "BadBits";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (sample_rate < 4000 || sample_rate > 96000) {
|
||||||
|
f.close();
|
||||||
|
g_status = "BadRate";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 降采样到 <= 22.05k,省 RAM、也更好定时
|
||||||
|
uint32_t out_rate = sample_rate;
|
||||||
|
uint32_t decim = 1;
|
||||||
|
while (out_rate > k_target_hz) {
|
||||||
|
out_rate /= 2;
|
||||||
|
decim *= 2;
|
||||||
|
}
|
||||||
|
if (decim < 1) {
|
||||||
|
decim = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t bytes_per_frame = (bits / 8u) * channels;
|
||||||
|
const uint32_t src_frames = data_size / bytes_per_frame;
|
||||||
|
uint32_t out_frames = src_frames / decim;
|
||||||
|
if (out_frames > k_max_pcm) {
|
||||||
|
out_frames = k_max_pcm;
|
||||||
|
}
|
||||||
|
if (out_frames == 0) {
|
||||||
|
f.close();
|
||||||
|
g_status = "Empty";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_pcm = (uint8_t *)malloc(out_frames);
|
||||||
|
if (!g_pcm) {
|
||||||
|
f.close();
|
||||||
|
g_status = "OOM";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
f.seek(data_pos);
|
||||||
|
uint32_t got = 0;
|
||||||
|
for (uint32_t oi = 0; oi < out_frames; oi++) {
|
||||||
|
int64_t acc = 0;
|
||||||
|
uint32_t n = 0;
|
||||||
|
for (uint32_t d = 0; d < decim; d++) {
|
||||||
|
int32_t mono = 0;
|
||||||
|
if (!read_frame_s16(f, channels, bits, mono)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
acc += mono;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
if (n == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const int32_t mix = (int32_t)(acc / (int64_t)n);
|
||||||
|
int v = (mix / 256) + 128;
|
||||||
|
if (v < 0) {
|
||||||
|
v = 0;
|
||||||
|
}
|
||||||
|
if (v > 255) {
|
||||||
|
v = 255;
|
||||||
|
}
|
||||||
|
g_pcm[got++] = (uint8_t)v;
|
||||||
|
}
|
||||||
|
f.close();
|
||||||
|
|
||||||
|
if (got == 0) {
|
||||||
|
teardown_pcm();
|
||||||
|
g_status = "Empty";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_pcm_len = got;
|
||||||
|
g_rate = out_rate;
|
||||||
|
Serial.printf("WAV ok %uB -> %u samp @%u (decim %u)\n", (unsigned)fsz,
|
||||||
|
(unsigned)g_pcm_len, (unsigned)g_rate, (unsigned)decim);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void wav_task(void * /*arg*/) {
|
||||||
|
for (;;) {
|
||||||
|
if (!g_playing || !g_pcm || g_pcm_len == 0) {
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(15));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_stop_req = false;
|
||||||
|
dac_output_disable(DAC_CHANNEL_1);
|
||||||
|
dac_output_enable(DAC_CHANNEL_2);
|
||||||
|
|
||||||
|
const uint32_t period_us = 1000000UL / (g_rate ? g_rate : 16000);
|
||||||
|
int64_t next = esp_timer_get_time();
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < g_pcm_len; i++) {
|
||||||
|
if (g_stop_req) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
dac_output_voltage(DAC_CHANNEL_2, scale_sample(g_pcm[i]));
|
||||||
|
next += (int64_t)period_us;
|
||||||
|
const int64_t now = esp_timer_get_time();
|
||||||
|
const int64_t wait = next - now;
|
||||||
|
if (wait > 50) {
|
||||||
|
ets_delay_us((uint32_t)wait);
|
||||||
|
} else if (wait < -3000) {
|
||||||
|
next = now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
release_dac_pins();
|
||||||
|
g_playing = false;
|
||||||
|
g_status = g_stop_req ? "Stop" : "Done";
|
||||||
|
Serial.println(g_status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wav_player_init() {
|
||||||
|
release_dac_pins();
|
||||||
|
xTaskCreatePinnedToCore(wav_task, "wavdac", 4096, nullptr, 4, &g_task, 0);
|
||||||
|
g_status = "idle";
|
||||||
|
Serial.println("WAV: DAC2-only + downsample");
|
||||||
|
}
|
||||||
|
|
||||||
|
void wav_player_set_volume(uint8_t percent) {
|
||||||
|
if (percent > 100) {
|
||||||
|
percent = 100;
|
||||||
|
}
|
||||||
|
g_volume = percent;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wav_player_stop() {
|
||||||
|
if (g_playing) {
|
||||||
|
g_stop_req = true;
|
||||||
|
for (int i = 0; i < 40 && g_playing; i++) {
|
||||||
|
delay(5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g_playing = false;
|
||||||
|
g_stop_req = false;
|
||||||
|
release_dac_pins();
|
||||||
|
g_status = "idle";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wav_player_is_playing() {
|
||||||
|
return g_playing;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *wav_player_status() {
|
||||||
|
return g_status;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wav_player_play_path(const char *path) {
|
||||||
|
if (!path || !path[0]) {
|
||||||
|
g_status = "NoFile";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!sd_init()) {
|
||||||
|
g_status = "SDFail";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!SD.exists(path)) {
|
||||||
|
g_status = "Missing";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
wav_player_stop();
|
||||||
|
ledcDetachPin(SPEAKER_PIN);
|
||||||
|
release_dac_pins();
|
||||||
|
|
||||||
|
if (!load_wav_pcm(path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_stop_req = false;
|
||||||
|
g_playing = true;
|
||||||
|
g_status = "Play";
|
||||||
|
Serial.printf("WAV play %s\n", path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wav_player_tick() {}
|
||||||
150
tools/sd_put.py
Normal file
150
tools/sd_put.py
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Upload a file to the CYD TF card over serial (#PUT + ACK window)."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
try:
|
||||||
|
import serial
|
||||||
|
from serial.tools import list_ports
|
||||||
|
except ImportError:
|
||||||
|
print("Need pyserial: pip install pyserial", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
WIN = 4096
|
||||||
|
|
||||||
|
|
||||||
|
def pick_port(preferred: str | None) -> str:
|
||||||
|
if preferred:
|
||||||
|
return preferred
|
||||||
|
ports = list(list_ports.comports())
|
||||||
|
for p in ports:
|
||||||
|
desc = (p.description or "") + (p.hwid or "")
|
||||||
|
if "USB" in desc.upper() or "UART" in desc.upper() or "CP210" in desc.upper() or "CH340" in desc.upper():
|
||||||
|
return p.device
|
||||||
|
if ports:
|
||||||
|
return ports[0].device
|
||||||
|
raise SystemExit("No serial port found")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
ap = argparse.ArgumentParser()
|
||||||
|
ap.add_argument("file", type=Path, help="Local file to upload")
|
||||||
|
ap.add_argument("--port", default=None)
|
||||||
|
ap.add_argument("--baud", type=int, default=460800)
|
||||||
|
ap.add_argument("--remote", default="/paomo.mp3")
|
||||||
|
args = ap.parse_args()
|
||||||
|
|
||||||
|
path: Path = args.file
|
||||||
|
if not path.is_file():
|
||||||
|
print(f"Missing file: {path}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
size = path.stat().st_size
|
||||||
|
port = pick_port(args.port)
|
||||||
|
print(f"Port {port} {path.name} -> {args.remote} ({size} bytes)", flush=True)
|
||||||
|
|
||||||
|
ser = serial.Serial()
|
||||||
|
ser.port = port
|
||||||
|
ser.baudrate = args.baud
|
||||||
|
ser.timeout = 1
|
||||||
|
ser.dtr = False
|
||||||
|
ser.rts = False
|
||||||
|
ser.open()
|
||||||
|
# 硬复位 ESP32
|
||||||
|
ser.rts = True
|
||||||
|
time.sleep(0.1)
|
||||||
|
ser.rts = False
|
||||||
|
print("waiting BOOT_OK...", flush=True)
|
||||||
|
deadline = time.time() + 12
|
||||||
|
booted = False
|
||||||
|
while time.time() < deadline:
|
||||||
|
line = ser.readline().decode("utf-8", errors="ignore").strip()
|
||||||
|
if line:
|
||||||
|
print("<", line, flush=True)
|
||||||
|
if "BOOT_OK" in line:
|
||||||
|
booted = True
|
||||||
|
break
|
||||||
|
if not booted:
|
||||||
|
print("warn: BOOT_OK not seen, continuing", flush=True)
|
||||||
|
time.sleep(0.5)
|
||||||
|
ser.reset_input_buffer()
|
||||||
|
|
||||||
|
cmd = f"#PUT {args.remote} {size}\n".encode("ascii")
|
||||||
|
print(f"send {cmd!r}", flush=True)
|
||||||
|
ser.write(cmd)
|
||||||
|
ser.flush()
|
||||||
|
|
||||||
|
deadline = time.time() + 20
|
||||||
|
while time.time() < deadline:
|
||||||
|
line = ser.readline().decode("utf-8", errors="ignore").strip()
|
||||||
|
if line:
|
||||||
|
print("<", line, flush=True)
|
||||||
|
if line == "READY":
|
||||||
|
break
|
||||||
|
if line.startswith("PUT_ERR"):
|
||||||
|
return 2
|
||||||
|
else:
|
||||||
|
print("Timeout waiting READY", file=sys.stderr, flush=True)
|
||||||
|
return 3
|
||||||
|
|
||||||
|
sent = 0
|
||||||
|
t0 = time.time()
|
||||||
|
with path.open("rb") as f:
|
||||||
|
while sent < size:
|
||||||
|
chunk = f.read(min(WIN, size - sent))
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
ser.write(chunk)
|
||||||
|
ser.flush()
|
||||||
|
sent += len(chunk)
|
||||||
|
|
||||||
|
# 等 ACK <sent>
|
||||||
|
ack_deadline = time.time() + 30
|
||||||
|
acked = False
|
||||||
|
while time.time() < ack_deadline:
|
||||||
|
line = ser.readline().decode("utf-8", errors="ignore").strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
print("<", line, flush=True)
|
||||||
|
if line.startswith("PUT_ERR"):
|
||||||
|
return 4
|
||||||
|
if line.startswith("ACK "):
|
||||||
|
try:
|
||||||
|
got = int(line.split()[1])
|
||||||
|
except (IndexError, ValueError):
|
||||||
|
continue
|
||||||
|
if got >= sent:
|
||||||
|
acked = True
|
||||||
|
break
|
||||||
|
if line == "DONE" and sent >= size:
|
||||||
|
print("OK", flush=True)
|
||||||
|
return 0
|
||||||
|
if not acked:
|
||||||
|
print(f"Timeout waiting ACK after {sent}", file=sys.stderr, flush=True)
|
||||||
|
return 4
|
||||||
|
|
||||||
|
if sent % (256 * 1024) < WIN or sent == size:
|
||||||
|
rate = sent / max(time.time() - t0, 0.001)
|
||||||
|
print(f"sent {sent}/{size} ({rate/1024:.1f} KB/s)", flush=True)
|
||||||
|
|
||||||
|
deadline = time.time() + 30
|
||||||
|
while time.time() < deadline:
|
||||||
|
line = ser.readline().decode("utf-8", errors="ignore").strip()
|
||||||
|
if line:
|
||||||
|
print("<", line, flush=True)
|
||||||
|
if line == "DONE":
|
||||||
|
print("OK", flush=True)
|
||||||
|
return 0
|
||||||
|
if line.startswith("PUT_ERR"):
|
||||||
|
return 5
|
||||||
|
|
||||||
|
print("Timeout waiting DONE", file=sys.stderr, flush=True)
|
||||||
|
return 6
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
Reference in New Issue
Block a user