Teensy Stereo Audio Recorder

Teensy Stereo Audio Recorder

Teensy 双声道音频记录器

https://ift.tt/4W2Sf1Y

// 2026年2月12日 凌晨12:15

// 添加了文件溢出保护

#include
#include
#include
#include
#include
#include
#include

// —————- 音频 —————-
AudioPlaySdWav playWav;
AudioRecordQueue recordQueueL;
AudioRecordQueue recordQueueR;
AudioOutputI2S i2s_out;
AudioInputI2S i2s_in;
AudioControlSGTL5000 sgtl5000;
AudioAnalyzePeak peakL;
AudioAnalyzePeak peakR;
AudioAnalyzeFFT1024 fft1;
AudioMixer4 mixerL;
AudioMixer4 mixerR;

// 音频连接
////AudioConnection patch1(playWav, 0, i2s_out, 0); // 用于播放
////AudioConnection patch2(playWav, 1, i2s_out, 1);
AudioConnection patch5(i2s_in, 0, recordQueueL, 0); // 用于录音
AudioConnection patch6(i2s_in, 1, recordQueueR, 0);

AudioConnection patchPeakL(i2s_in, 0, peakL, 0);
AudioConnection patchPeakR(i2s_in, 1, peakR, 0);

// 播放到混音器
AudioConnection patchPlayL(playWav, 0, mixerL, 0);
AudioConnection patchPlayR(playWav, 1, mixerR, 0);

// 混音器输出
AudioConnection patchOutL(mixerL, 0, i2s_out, 0);
AudioConnection patchOutR(mixerR, 0, i2s_out, 1);

// 混音器FFT
AudioConnection patchFFT(mixerL, 0, fft1, 0);

// —————- 状态 —————-
enum PlayerState { STOPPED,
PLAY,
SETTINGS,
MONITOR,
RECORD
};
PlayerState state = STOPPED;
PlayerState lastState = STOPPED; //// 需要吗?

// —————- 按钮 —————-
#define BTN_STOP 37
#define BTN_PLAY 38
#define BTN_SETTINGS 39
#define BTN_MONITOR 40
#define BTN_RECORD 41
#define ENCODER_A 33
#define ENCODER_B 35

unsigned long lastPress[5] = { 0, 0, 0, 0, 0 };
const unsigned long debounce = 100;

//—————— TFT ——————
#define TFT_CS 28
#define TFT_DC 25
#define TFT_RST 24
#define SCREEN_WIDTH 160
#define SCREEN_HEIGHT 128
#define STATUS_HEIGHT 20
#define ST7735_PURPLE 0x780F
// 0x780F 深紫色(状态栏用)
// 0x981F 更亮的紫色
// 0x7016 更暗的紫色
// 0xA0BF 品红紫色

// —————- SD & 文件 —————-
////#define SD_CS_PIN 10
#define MAX_FILES 200

char fileList[MAX_FILES][13];
int numFiles = 0;
int currentFile = -1;

bool isRecording = false;
File recordFile;

// 使用 SPI1(引脚 26 = MOSI,27 = SCK)
Adafruit_ST7735 tft = Adafruit_ST7735(&SPI1, TFT_CS, TFT_DC, TFT_RST);

// —————- SD 写入时序调试 —————-
unsigned long sdWriteStart = 0;
unsigned long sdWriteMaxUs = 0;
unsigned long sdWriteTotalUs = 0;
unsigned long sdWriteCount = 0;

// 块追踪
elapsedMicros timer; // 录音的经过时间,用于跟踪应接收到多少数据块
unsigned long blocksReceived = 0;
unsigned long blocksWritten = 0;

#define SWEEP_SECONDS 10
#define SWEEP_SAMPLES (SWEEP_SECONDS * 34) // 在 44.1kHz 下约 34 次峰值更新每秒

float peakSweep[SWEEP_SAMPLES]; // 存储合并后的左/右峰值历史
int sweepIndex = 0;
bool sweepFull = false;
unsigned long lastSweepUpdate = 0;

// ——————FFT ———————-
const int FFT_BARS = 128;
const int FFT_HEIGHT = 77;
const int FFT_Y = 52;

float fftFall[FFT_BARS];
float fftPeak[FFT_BARS];

// —————- 原型函数 —————-
void stateStopped();
void statePlay();
void stateSettings();
void stateMonitor();
void stateRecord();

void scanFiles();
void listFiles(void);
void showStoppedUI(void);
void saveRecording();
void displayStatusLine(const char* prefix, const char* name);
void printNoExt(const char* n);
void createWavPlaceholder(File& f);
void finalizeWav(File& f);
int findNextRecNumber();
void drawPeakSweepMeter();
void updatePeakSweep();
int readEncoder();
void displayHeader();
void applySetting(int);
void drawInputBadge();
void drawInputParameter();

// ——- 其他全局变量 ———
volatile uint32_t recordedSamples = 0;
bool useMic = false; // 开始为线路输入
bool bassBoostOn = false;
bool eqSmileyOn = false;
bool hpfOn = false;
bool autoVolOn = false;
int micGainLevel = 25; // AOM-6738P-R 的初始增益
bool micBiasOn = true; // 电容麦克风需要此开启

// —————- 设置 —————-
void setup() {
Serial.begin(115200);

// 音频
AudioMemory(1000);
sgtl5000.enable();
delay(100); // 关键:给编解码器初始化时间
sgtl5000.adcHighPassFilterDisable();
sgtl5000.volume(0.6);
//sgtl5000.lineInLevel(5); // 范围 0-15 (0 = 最大增益 ~+30 dB, 15 = 最小增益 0 dB,默认是 5)
sgtl5000.lineInLevel(13);
sgtl5000.unmuteHeadphone();
////sgtl5000.volume(0.8);
////sgtl5000.dacVolume(1.0); // 全部 DAC 音量
////sgtl5000.lineOutLevel(13); // 最大输出
sgtl5000.inputSelect(AUDIO_INPUT_LINEIN);
mixerL.gain(0, 1.0); // 左声道全音量
fft1.windowFunction(AudioWindowHanning1024);
// 或使用 AudioWindowBlackmanNuttall1024 来获得更尖锐的峰值

// I2C
Wire.setSDA(18);
Wire.setSCL(19);

// TFT
SPI1.begin(); // 显式启动 SPI1
tft.initR(INITR_BLACKTAB);
tft.setRotation(3);

// 按钮的公共地
pinMode(36, OUTPUT);
digitalWrite(36, LOW); // 将其设为按钮的地线
pinMode(34, OUTPUT);
digitalWrite(34, LOW); // 引脚 35 变为编码器的 GND

// 按钮引脚,内置上拉电阻
pinMode(BTN_STOP, INPUT_PULLUP);
pinMode(BTN_PLAY, INPUT_PULLUP);
pinMode(BTN_SETTINGS, INPUT_PULLUP);
pinMode(BTN_MONITOR, INPUT_PULLUP);
pinMode(BTN_RECORD, INPUT_PULLUP);
pinMode(ENCODER_A, INPUT_PULLUP);
pinMode(ENCODER_B, INPUT_PULLUP);

// 启动时 LED 闪烁两次
pinMode(LED_BUILTIN, OUTPUT); // 引脚 13 上的内置 LED
for (int i = 0; i < 2; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(200); // 点亮 200ms digitalWrite(LED_BUILTIN, LOW); delay(200); // 熄灭 200ms } // SD 卡 if (!SD.begin(BUILTIN_SDCARD)) { Serial.println("SD 卡失败"); tft.fillScreen(ST7735_BLACK); tft.setTextColor(ST7735_RED); tft.setTextSize(2); tft.setCursor(0, 40); tft.println(" 无卡"); while (1) ; } Serial.println("SD 已就绪"); //// } // ---------------- LOOP ---------------- void loop() { switch (state) { case STOPPED: stateStopped(); break; case PLAY: statePlay(); break; case SETTINGS: stateSettings(); break; case MONITOR: stateMonitor(); break; case RECORD: stateRecord(); break; } } // ---------------- 状态 ---------------- void stateStopped() { Serial.println("已停止"); displayHeader(); // 进入时扫描文件 scanFiles(); // 确定文件数量(numFiles) // 确保有有效的选中项 if (currentFile < 0 && numFiles > 0) currentFile = 0;
tft.setTextColor(ST7735_YELLOW);
tft.setTextSize(1);
showStoppedUI(); // 进入时在 TFT 上显示列表

// 若文件空间不足则发出警告
if (numFiles >= MAX_FILES-20 && MAX_FILES > 20) {
tft.setTextColor(ST7735_YELLOW);
tft.setCursor(80, 26);
tft.print(MAX_FILES – numFiles);
tft.setCursor(80, 36);
tft.print(“录音”);
tft.setCursor(80, 46);
tft.print(“剩余”);
}

// 停止循环
while (1) {
unsigned long now = millis();

// 检查按键
if (digitalRead(BTN_PLAY) == LOW && now – lastPress[1] > debounce) {
lastPress[1] = now;
if (numFiles > 0) {
state = PLAY;
return;
}
}

if (digitalRead(BTN_SETTINGS) == LOW && now – lastPress[2] > debounce) {
lastPress[2] = now;
state = SETTINGS;
return;
}
if (digitalRead(BTN_MONITOR) == LOW && now – lastPress[3] > debounce) {
lastPress[3] = now;
state = MONITOR;
return;
}

if (digitalRead(BTN_RECORD) == LOW && now – lastPress[4] > debounce) {
lastPress[4] = now;
state = RECORD;
return;
}

int step = readEncoder();

if (step != 0) {
if (step > 0) {
// 顺时针向下滚动
if (currentFile < numFiles - 1) { currentFile++; } } else { // 逆时针向上滚动 if (currentFile > 0) {
currentFile–;
}
}
showStoppedUI(); // 重新绘制列表
}

// 串口
if (Serial.available()) {
String s = Serial.readStringUntil(‘\n’);
s.trim();
if (s == “l”) {
listFiles();
} else if (s == “v”) {
verifyWav();
} else
{
int n = s.toInt();
if (n > 0 && n <= numFiles) { currentFile = n - 1; state = PLAY; return; } // 文件数量检查 } // 播放命令 } // 串口命令 } // 循环 } // 函数 // ----------------------------------------------------------- void statePlay() { // 条目进入(仅执行一次) displayHeader(); if (playWav.isPlaying()) playWav.stop(); playWav.play(fileList[currentFile]); delay(5); // 让库读取 WAV 头信息的小延迟 // 显示文件名 tft.setTextColor(ST7735_GREEN); tft.setTextSize(1); tft.setCursor(0, 26); //tft.print(prefix); printNoExt(fileList[currentFile]); Serial.print("正在播放 "); Serial.println(fileList[currentFile]); // 获取总时长(WAV 头中有) unsigned long totalMs = playWav.lengthMillis(); unsigned long totalSec = totalMs / 1000; unsigned long totalMin = totalSec / 60; unsigned long totalS = totalSec % 60; unsigned long startTime = millis(); // 记录起始时间 unsigned long lastElapsedUpdate = 0; // 记录上次更新时间 // 播放循环 while (1) { unsigned long now = millis(); // 播放结束时自动退出 if (!playWav.isPlaying()) { playWav.stop(); Serial.println("歌曲播放结束"); state = STOPPED; return; } // 检查按键和命令 // 停止 if (digitalRead(BTN_STOP) == LOW && now - lastPress[1] > debounce) {
lastPress[1] = now;
playWav.stop();
state = STOPPED;
return;
}

// 串口
if (Serial.available()) {
String s = Serial.readStringUntil(‘\n’);
s.trim();
if (s == “s”) {
playWav.stop();
state = STOPPED;
return;
}
}

// ———- 每秒更新已用时间 ———-
if (now – lastElapsedUpdate >= 100) {
lastElapsedUpdate = now;
uint32_t elapsedSec = (now – startTime) / 1000;
;
uint32_t min = elapsedSec / 60;
uint32_t sec = elapsedSec % 60;

tft.fillRect(0, 36, SCREEN_WIDTH, 20, ST7735_BLACK); // 清除上一次时间行
tft.setCursor(0, 38);
tft.print(min);
tft.print(“:”);
if (sec < 10) tft.print("0"); // 2 位数字 tft.print(sec); tft.print(" / "); tft.print(totalMin); tft.print(":"); if (totalS < 10) tft.print("0"); // 2 位数字 tft.println(totalSec % 60); delay(5); // 给后台进程让出时间 } static unsigned long lastAvailable = 0; static int countAvailable = 0; if (fft1.available()) { countAvailable++; unsigned long now = millis(); if (now - lastAvailable > 0) {
Serial.print(“FFT 在 “);
Serial.print(now – lastAvailable);
Serial.print(” ms 可用(计数:”);
Serial.print(countAvailable);
Serial.println(“)”);
lastAvailable = now;
}
drawFFT();
}
}
}

//———————————————————————–

void stateMonitor() {

displayHeader();
Serial.println(“Monitor 模式”);
drawInputBadge();
tft.setTextSize(1);
tft.setCursor(0, 25);

// 为新绘制重置扫描
sweepIndex = 0;
sweepFull = false;
memset(peakSweep, 0, sizeof(peakSweep));

unsigned long lastDraw = 0;

while (true) {
updatePeakSweep();

if (millis() – lastDraw > 100) {
lastDraw = millis();
drawPeakSweepMeter();
}

if (digitalRead(BTN_STOP) == LOW) {
delay(200);
state = STOPPED;
return;
}
if (digitalRead(BTN_PLAY) == LOW) {
delay(200);
state = PLAY;
return;
}

if (digitalRead(BTN_RECORD) == LOW) {
delay(200);
state = RECORD;
return;
}

delay(10);
}
}

//————————————————————————-

void stateRecord() {

// ———- 进入 ———-

displayHeader();

// 确保系统不崩溃
if (numFiles >= MAX_FILES) {
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.setCursor(0, 26);
tft.print(“文件数量已达到上限”);
Serial.println(“文件数量达到上限”);
delay(5000);
state = STOPPED;
return;
}

drawInputBadge();
int n = findNextRecNumber();
if (n == 0) {
Serial.println(“没有空闲的文件名!”);
state = STOPPED;
return;
}

char name[13];
sprintf(name, “REC%04d.WAV”, n);

tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.setCursor(2, 22);
tft.print(name);

Serial.print(“正在录音 “);
Serial.println(name);

recordFile = SD.open(name, FILE_WRITE);
createWavPlaceholder(recordFile);

unsigned long startTime = millis(); // 录音起始时间
unsigned long lastElapsedUpdate = 0; // 记录最后一次更新时间

recordQueueL.begin();
recordQueueR.begin();
timer = 0;
isRecording = true;

sdWriteMaxUs = 0;
sdWriteTotalUs = 0;
sdWriteCount = 0;

// ———- 运行 ———-
while (1) {
unsigned long now = millis();

saveRecording();

// ———- 每秒更新已用时间 ———-
if (now – lastElapsedUpdate >= 200) {
lastElapsedUpdate = now;
uint32_t elapsedSec = (millis() – startTime) / 1000;
;
uint32_t min = elapsedSec / 60;
uint32_t sec = elapsedSec % 60;

tft.fillRect(0, 35, 50, 10, ST7735_BLACK); // 清除上一次时间线
tft.setCursor(0, 36);
tft.print(min);
tft.print(“:”);
if (sec < 10) tft.print("0"); tft.println(sec); updatePeakSweep(); drawPeakSweepMeter(); } // 检查按钮和串口以结束录音并决定下一个状态 // 停止按钮 if (digitalRead(BTN_STOP) == LOW && now - lastPress[1] > debounce) {
lastPress[1] = now;
state = STOPPED;
break;

} // 播放按钮
if (digitalRead(BTN_PLAY) == LOW && now – lastPress[2] > debounce) {
lastPress[1] = now;
state = PLAY;
break;
}

// 监视按钮
if (digitalRead(BTN_MONITOR) == LOW && now – lastPress[3] > debounce) {
lastPress[3] = now;
state = MONITOR;
break;
}

// 串口停止
if (Serial.available()) {
String s = Serial.readStringUntil(‘\n’);
s.trim();
if (s == “s”) {
state = STOPPED;
break;
}
}
}
// ———- 退出 ———-

recordQueueL.end();
recordQueueR.end();
saveRecording();

finalizeWav(recordFile);
uint32_t size = recordFile.size();
recordFile.close();
isRecording = false;

Serial.print(“录音已停止。字节数:”);
Serial.println(size);

if (sdWriteCount > 0) {
float avg = (float)sdWriteTotalUs / sdWriteCount;
Serial.print(“SD 写入最大值: “);
Serial.print(sdWriteMaxUs);
Serial.print(” us,平均值: “);
Serial.println(avg, 1);
}

//// 测试
scanFiles(); // 刷新列表以包含新文件
//currentFile = 0; // 不找到时默认为第一项(安全性)
for (int i = 0; i < numFiles; i++) { if (strcmp(fileList[i], name) == 0) { currentFile = i; break; } } // state = STOPPED; return; } //--------------------------------------------------------- void listFiles(void) { scanFiles(); Serial.println("\n文件列表:"); for (int i = 0; i < numFiles; i++) { File f = SD.open(fileList[i]); Serial.print(i + 1); Serial.print(": "); Serial.print(fileList[i]); Serial.print(" "); Serial.println(f.size()); f.close(); } } //------------------------------------------------------------------- void saveRecording() { static uint8_t sdBuffer[512]; static uint16_t sdIndex = 0; unsigned long startTime = millis(); while (recordQueueL.available() && recordQueueR.available()) { int16_t* l = recordQueueL.readBuffer(); int16_t* r = recordQueueR.readBuffer(); recordQueueL.freeBuffer(); recordQueueR.freeBuffer(); blocksWritten++; for (int i = 0; i < 128; i++) { // 交错写入 L/R 样本(每个为 16 位) sdBuffer[sdIndex++] = l[i] & 0xFF; sdBuffer[sdIndex++] = (l[i] >> 8) & 0xFF;
sdBuffer[sdIndex++] = r[i] & 0xFF;
sdBuffer[sdIndex++] = (r[i] >> 8) & 0xFF;

// 写满 SD 块
if (sdIndex >= 512) {
recordFile.write(sdBuffer, 512);
sdIndex = 0;
}
}

//if(digitalReadFast(BTN_STOP) == LOW) return; // 这似乎可行
if (millis() > startTime + 200) return; // 解决两个问题
}
}

// —————- 文件与 UI —————-
void scanFiles() {
numFiles = 0;
File root = SD.open(“/”);
while (true) {
File f = root.openNextFile();
if (!f) break;

if (!f.isDirectory() && f.size() >= 44) strncpy(fileList[numFiles++], f.name(), 12);
f.close();
}
root.close();
}

//————————————————

void printNoExt(const char* n) {
char b[13];
strncpy(b, n, 12);
b[12] = 0;
char* d = strrchr(b, ‘.’);
if (d) *d = 0;
tft.print(b);
}

// —————- WAV —————-
void createWavPlaceholder(File & f) {
uint8_t h[44] = { ‘R’, ‘I’, ‘F’, ‘F’, 0, 0, 0, 0, ‘W’, ‘A’, ‘V’, ‘E’, ‘f’, ‘m’, ‘t’, ‘ ‘, 16, 0, 0, 0, 1, 0, 2, 0, 0x44, 0xAC, 0, 0, 0x10, 0xB1, 2, 0, 4, 0, 16, 0, ‘d’, ‘a’, ‘t’, ‘a’, 0, 0, 0, 0 };
f.write(h, 44);
}

//————————————–
void finalizeWav(File & f) {
uint32_t size = f.size();
uint32_t data = size – 44;
uint32_t riff = size – 8;
f.seek(4);
f.write((uint8_t*)&riff, 4);
f.seek(40);
f.write((uint8_t*)&data, 4);
}

//————————————-
int findNextRecNumber() {
for (int i = 1; i <= 9999; i++) { char n[13]; sprintf(n, "REC%04d.WAV", i); if (!SD.exists(n)) return i; } return 0; } //------------------------------------ void verifyWav() { Serial.println("\n=== WAV 头部校验 ==="); String input = Serial.readStringUntil('\n'); input.trim(); int fileIndex = -1; bool useCurrent = (input.length() == 0 || input == "v"); if (!useCurrent) { fileIndex = input.toInt() - 1; if (fileIndex < 0 || fileIndex >= numFiles) {
Serial.println(“无效的文件编号。”);
return;
}
} else {
if (currentFile < 0 || currentFile >= numFiles) {
Serial.println(“未选择任何文件。”);
return;
}
fileIndex = currentFile;
}

const char* filename = fileList[fileIndex];
Serial.print(“文件:”);
Serial.println(filename);

File file = SD.open(filename);
if (!file) {
Serial.println(“错误:无法打开文件。”);
return;
}

unsigned long fileSize = file.size();
Serial.print(“所报文件大小(SD):”);
Serial.print(fileSize);
Serial.println(” 字节”);

if (fileSize < 44) { Serial.println("错误:文件太小,无法包含 WAV 头部。"); file.close(); return; } uint8_t header[44]; file.read(header, 44); file.close(); bool valid = true; if (memcmp(header, "RIFF", 4) != 0) { valid = false; Serial.println("失败:缺少 'RIFF'"); } else Serial.println("OK:'RIFF' 块"); uint32_t riffSize = header[4] | (header[5] << 8) | (header[6] << 16) | (header[7] << 24); Serial.print("RIFF 大小字段:"); Serial.print(riffSize); Serial.print(" (0x"); Serial.print(riffSize, HEX); Serial.println(") 推断的总文件大小 = RIFF + 8"); if (memcmp(header + 8, "WAVE", 4) != 0) { valid = false; Serial.println("失败:缺少 'WAVE'"); } else Serial.println("OK:'WAVE' 格式"); // 跳过 fmt 检查以简化(保留你现有的即可) if (memcmp(header + 36, "data", 4) != 0) { valid = false; Serial.println("失败:缺少 'data' 块"); } else Serial.println("OK:存在 'data' 块"); uint32_t dataSize = header[40] | (header[41] << 8) | (header[42] << 16) | (header[43] << 24); Serial.print("数据大小字段:"); Serial.print(dataSize); Serial.print(" (0x"); Serial.print(dataSize, HEX); Serial.println(") 推断的音频字节数"); // 比较大小 long diff = riffSize - dataSize; Serial.print("差异(RIFF - 数据):"); Serial.print(diff); Serial.print(" 字节"); if (diff == 36) { Serial.println(" 恰好 36(标准,无额外分块)"); } else { Serial.print(" 不是 36("); if (diff > 36) Serial.print(“额外的块或填充”);
else Serial.print(“数据字段过小”);
Serial.println(“)”);
}

if (fileSize == 44) {
Serial.println(“错误:没有音频数据。只有头部”);
}

Serial.print(“\n=== 结果:”);
if (valid) {
Serial.println(“有效的 CD 质量 WAV 头部 ===”);
} else {
Serial.println(“无效头部 ===”);
}

}

//—————————————–
void updatePeakSweep() {
unsigned long now = millis();
if (now – lastSweepUpdate < 29) return; // ~34 Hz 更新 lastSweepUpdate = now; if (peakL.available() && peakR.available()) { float linear = max(peakL.read(), peakR.read()); // 0.01.0 // 转换为分贝(-60 dB 到 0 dB 范围) float db = (linear > 0.0001) ? 20.0 * log10(linear) : -60.0;

// 归一化到 0.0(安静)到 1.0(0 dB)
float normalized = max(0.0, (db + 60.0) / 60.0);

////float normalized = 0.0; // 测试:强制将底线设为0////

peakSweep[sweepIndex] = normalized;

sweepIndex++;
if (sweepIndex >= SWEEP_SAMPLES) {
sweepIndex = 0;
sweepFull = true;
}
}
}

// — 用剪裁高亮绘制 —
void drawPeakSweepMeter() {
const int x = 4;
const int y = 60; // 从下往上留出文本空间
const int w = SCREEN_WIDTH – 8;
const int h = 60; // 高度适合在文本下方

// 仅清除图形区域
tft.fillRect(x, y – 5, w, h, ST7735_BLACK);

// 绘制蓝色边框框
// tft.drawRect(x, y, w, h, ST7735_BLUE);

// 绘制越界外的蓝色边框
tft.drawRect(x – 1, y – 4, w + 4, h + 4, ST7735_BLUE);
////tft.drawRect(x – 1, y – 1, w + 2, h + 2, ST7735_BLUE); // 图形区域外边框

int samples = sweepFull ? SWEEP_SAMPLES : sweepIndex;
int start = sweepFull ? sweepIndex : 0;

for (int i = 1; i < samples; i++) { int idx1 = (start + i - 1) % SWEEP_SAMPLES; int idx2 = (start + i) % SWEEP_SAMPLES; int px1 = x + (i - 1) * w / SWEEP_SAMPLES; int px2 = x + i * w / SWEEP_SAMPLES; // 将安静线拉到底部上方 2 个像素处 int py1 = y + h - (int)(peakSweep[idx1] * h) - 2; int py2 = y + h - (int)(peakSweep[idx2] * h) - 2; uint16_t color = ST7735_YELLOW; if (peakSweep[idx2] >= 0.95) color = ST7735_RED;

tft.drawLine(px1, py1, px2, py2, color);
}

// 当前峰值标记,擦除旧点,绘制新点(无拖影)
if (samples > 0) {
static int lastX = -1;
static int lastY = -1;

int currentIdx = (start + samples – 1) % SWEEP_SAMPLES;
int currentX = x + (samples – 1) * w / SWEEP_SAMPLES;
int currentY = y + h – (int)(peakSweep[currentIdx] * h) – 2;

// 将标记保持在图内
currentX = constrain(currentX, x + 4, x + w – 4);
currentY = constrain(currentY, y + 4, y + h – 4);

// 擦除上一个标记
if (lastX != -1) {
tft.fillCircle(lastX, lastY, 5, ST7735_BLACK); // 尝试擦除
}

// 绘制新标记
uint16_t markerColor = (peakSweep[currentIdx] >= 0.95) ? ST7735_RED : ST7735_CYAN;
tft.fillCircle(currentX, currentY, 3, markerColor);

lastX = currentX;
lastY = currentY;
}
}

//——————————————-

void showStoppedUI() {
const int MAX_VISIBLE = 9;
const int LINE_HEIGHT = 12;
const int START_Y = 22;

// 自动居中选中项
int idealOffset = currentFile – MAX_VISIBLE / 2;
idealOffset = max(0, min(idealOffset, numFiles – MAX_VISIBLE));
int scrollOffset = idealOffset;

tft.fillRect(0, START_Y, SCREEN_WIDTH, SCREEN_HEIGHT – START_Y, ST7735_BLACK);

tft.setTextSize(1);
tft.setTextColor(ST7735_WHITE);

int y = START_Y;
int startIdx = scrollOffset;
int endIdx = min(numFiles, scrollOffset + MAX_VISIBLE);

for (int i = startIdx; i < endIdx; i++) { tft.setCursor(4, y); if (i == currentFile) { tft.setTextColor(ST7735_GREEN); tft.print("> “);
} else {
tft.setTextColor(ST7735_WHITE);
}

printNoExt(fileList[i]);
tft.println();

y += LINE_HEIGHT;
}

// 滚动指示
if (scrollOffset > 0) {
// 未完待续的部分被截断,保留原样
…
}

February 13, 2026 at 02:18AM