ESP32-S3-ePaper-3.97可以用arduino编写程序,实现自己想要的功能。本文是写一个简单网络电音机的过程。

arduino的安装和配置

参照安装和配置 Arduino IDE ,
1 安装arduino
2 安装 ESP32 开发板库

ESP32版本选3.2.0-cn(最新版本可能有兼容性问题)。
3 安装库

安装ESP32-audioI2S-master,v3.3.0

4 连接开发板

这里是对应微雪开发板的选项,务必选对。

简单的网络收音机

新建一个项目,命名为iRadio,会打开一个.ino文件,把以下内容复制进去。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include "Audio.h"
#include "es8311.h"

// ==================== 引脚定义 ====================
#define I2C_SDA 41
#define I2C_SCL 42
#define I2S_MCK_PIN 13
#define I2S_BCK_PIN 14
#define I2S_LRCK_PIN 47
#define I2S_DOUT_PIN 48
#define PA_CTRL 39

#define BOOT_BTN 0 // BOOT 按键

// ==================== WiFi 与电台 ====================

const char* ssid = "wifi名称";
const char* password = "wifi密码";
const char* radioUrl = "https://lhttp-hw.qtfm.cn/live/20849/64k.mp3";
// ==================== ES8311 配置 ====================
#define EXAMPLE_SAMPLE_RATE (48000)
#define EXAMPLE_MCLK_MULTIPLE (256)
#define EXAMPLE_MCLK_FREQ_HZ (EXAMPLE_SAMPLE_RATE * EXAMPLE_MCLK_MULTIPLE)
#define EXAMPLE_VOICE_VOLUME (70)

Audio audio;
bool isPlaying = false; // 当前播放状态

// ==================== ES8311 初始化 ====================
static esp_err_t es8311_codec_init(void) {
es8311_handle_t es_handle = es8311_create(I2C_NUM_0, ES8311_ADDRRES_0);
if (!es_handle) return ESP_FAIL;

const es8311_clock_config_t es_clk = {
.mclk_inverted = false,
.sclk_inverted = false,
.mclk_from_mclk_pin = true,
.mclk_frequency = EXAMPLE_MCLK_FREQ_HZ,
.sample_frequency = EXAMPLE_SAMPLE_RATE
};

esp_err_t ret = es8311_init(es_handle, &es_clk, ES8311_RESOLUTION_16, ES8311_RESOLUTION_16);
if (ret != ESP_OK) return ret;

return es8311_voice_volume_set(es_handle, EXAMPLE_VOICE_VOLUME, NULL);
}

// ==================== Setup ====================
void setup() {
Serial.begin(115200);
delay(500);

// 按键初始化(内部上拉,按下为低)
pinMode(BOOT_BTN, INPUT_PULLUP);

Wire.begin(I2C_SDA, I2C_SCL);
pinMode(PA_CTRL, OUTPUT);
digitalWrite(PA_CTRL, HIGH);
delay(10);

if (es8311_codec_init() != ESP_OK) {
Serial.println("ES8311 init failed");
while (1) delay(1000);
}

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 15000) {
delay(500); Serial.print(".");
}
if (WiFi.status() != WL_CONNECTED) { ESP.restart(); }
Serial.printf("\nWiFi OK, IP: %s\n", WiFi.localIP().toString().c_str());

audio.setPinout(I2S_BCK_PIN, I2S_LRCK_PIN, I2S_DOUT_PIN, I2S_MCK_PIN);
audio.setVolume(18);

// 上电默认自动播放
audio.connecttohost(radioUrl);
isPlaying = true;
Serial.println("Auto play started");
}

// ==================== Loop ====================
void loop() {
audio.loop();

// 检测 BOOT 按键(下降沿触发,带简单消抖)
static bool lastBtnState = HIGH;
bool btnState = digitalRead(BOOT_BTN);

if (lastBtnState == HIGH && btnState == LOW) {
delay(20); // 消抖
if (digitalRead(BOOT_BTN) == LOW) {
if (isPlaying) {
audio.stopSong();
isPlaying = false;
Serial.println("Stopped by BOOT");
} else {
audio.connecttohost(radioUrl);
isPlaying = true;
Serial.println("Playing by BOOT");
}
// 等待按键释放,避免连击
while (digitalRead(BOOT_BTN) == LOW) {
delay(10);
}
}
}
lastBtnState = btnState;

if (WiFi.status() != WL_CONNECTED) WiFi.reconnect();
}

为了简单起见,wifi名和密码都写进代码,电台url也是固定的。
es8311.h文件在官方示例程序包 Arduino\examples\01_Audio_Test下,es8311开头的三个文件都复制到项目目录下。
点击上传按钮,编译无误后就刷进了开发板。
上电开始播放,按BOOT键开始、停止播放。
此项目没有界面,不会刷新屏幕。
epaper是一种特殊的屏幕,刷新需要加电压,断电后屏幕内容会保留,屏幕也不能长时间加电,否则会损坏。

直播源的获取

打开蜻蜓FM https://www.qtfm.cn/ ,打开电台页,选择一个电台,点播放,打开开发者工具侧边栏,

标出的地方就是电台url,后面的字符串没有用。有的站点的网络电台url上有身份验证和时间戳,不能直接使用。

改进版

加上界面刷新,wifi配置,电量显示,深度睡眠等功能。
安装库:WiFiManager v2.0.17 ,XPowersLib v0.3.3
有些包含文件在官方示例包中,背景图是用python生成的800*480大小的黑白图,在background.h里。
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include <WiFiManager.h>
#include "Audio.h"
#include "es8311.h"
#include <Preferences.h>
#define XPOWERS_CHIP_AXP2101
#include <XPowersLib.h>
#include "EPD_3in97.h"
#include "GUI_Paint.h"
#include "fonts.h"
#include "background.h"
#include "driver/rtc_io.h"
// ==================== 引脚 ====================
#define I2C_SDA 41
#define I2C_SCL 42
#define I2S_MCK_PIN 13
#define I2S_BCK_PIN 14
#define I2S_LRCK_PIN 47
#define I2S_DOUT_PIN 48
#define PA_CTRL 39
#define BOOT_BTN 0
#define KEY_FUNC 5

// ==================== 超时 ====================
#define IDLE_WIFI_OFF_MS (5UL * 60 * 1000)
#define IDLE_SLEEP_MS (10UL * 60 * 1000)

// ==================== 电台 ====================
struct Station {
const char *url;
const char *name;
};
Station stations[] = {
{ "https://lhttp-hw.qtfm.cn/live/20849/64k.mp3", "Qinhuangdao Traffic Radio" },
{ "https://lhttp-hw.qtfm.cn/live/20859/64k.mp3", "Qinhuangdao 1038 Private Car Radio" },
{ "https://lhttp-hw.qtfm.cn/live/20855/64k.mp3", "Qinhuangdao News Radio" },
{ "https://lhttp-hw.qtfm.cn/live/20835/64k.mp3", "Qinhuangdao Music Radio" },
{ "https://lhttp-hw.qtfm.cn/live/1644/64k.mp3", "Hebei News Comprehensive Radio" },
{ "https://lhttp-hw.qtfm.cn/live/15318703/64k.mp3", "Western music FM88.7" },
{ "https://lhttp-hw.qtfm.cn/live/20212269/64k.mp3", "Classical music FM90.5" },
{ "https://lhttp-hw.qtfm.cn/live/4915/64k.mp3", "Morning Music Channel" },
{ "https://ngcdn001.cnr.cn/live/zgzs/index.m3u8", "Voice of China" },
{ "https://sk.cri.cn/am846.m3u8", "English News Radio" }
};
const size_t STATION_COUNT = sizeof(stations) / sizeof(stations[0]);

// ==================== 全局状态 ====================
struct RadioState {
bool playing = false;
bool idle = true;
bool wifiOff = false;
bool screenOff = false;
bool screenOk = false;
int station = 0;
int partialCount = 0;
unsigned long idleStart = 0;
} st;

// ==================== WiFi ====================
WiFiManager wm;

// ==================== ES8311 ====================
#define SAMPLE_RATE 48000
#define MCLK_FREQ (SAMPLE_RATE * 256)
Audio audio;

// ==================== 屏幕 ====================
#define SCREEN_W EPD_3IN97_WIDTH
#define SCREEN_H EPD_3IN97_HEIGHT
#define STATUS_Y 100
#define STATUS_H 60
#define BYTES_PER_ROW (SCREEN_W / 8)
#define IMG_BYTES (BYTES_PER_ROW * SCREEN_H)
#define STATUS_BYTES (BYTES_PER_ROW * STATUS_H)
#define PARTIAL_REFRESH_MAX 15

UBYTE *BlackImage = nullptr;
UBYTE *StatusImage = nullptr;
char statusLine[80] = "Booting...";

Preferences prefs;
static XPowersAXP2101 PMU;
static bool pmu_ok = false;

// ==================== ES8311 初始化 ====================
static esp_err_t es8311_codec_init(void) {
es8311_handle_t h = es8311_create(I2C_NUM_0, ES8311_ADDRRES_0);
if (!h) return ESP_FAIL;
const es8311_clock_config_t clk = {
.mclk_inverted = false, .sclk_inverted = false,
.mclk_from_mclk_pin = true, .mclk_frequency = MCLK_FREQ,
.sample_frequency = SAMPLE_RATE
};
esp_err_t ret = es8311_init(h, &clk, ES8311_RESOLUTION_16, ES8311_RESOLUTION_16);
if (ret != ESP_OK) return ret;
return es8311_voice_volume_set(h, 70, NULL);
}

// ==================== 电量 ====================
static int get_battery_percent() {
if (!pmu_ok) return -1;
int pct = PMU.getBatteryPercent();
if (pct < 0 || pct > 100) return -1;
return pct;
}

// ==================== 电池图标 ====================
static void draw_battery_icon(int x, int y, int w, int h, int pct) {
if (pct < 0) pct = 0;
if (pct > 100) pct = 100;

Paint_DrawRectangle(x, y, x + w, y + h,
BLACK, DOT_PIXEL_1X1, DRAW_FILL_EMPTY);

int cap_w = w / 8;
int cap_h = h / 3;
if (cap_w < 2) cap_w = 2;
if (cap_h < 2) cap_h = 2;
int cap_x = x + w;
int cap_y = y + (h - cap_h) / 2;
Paint_DrawRectangle(cap_x, cap_y, cap_x + cap_w, cap_y + cap_h,
BLACK, DOT_PIXEL_1X1, DRAW_FILL_FULL);

int inner_x = x + 2;
int inner_y = y + 2;
int inner_w = w - 4;
int inner_h = h - 4;
if (inner_w <= 0 || inner_h <= 0) return;

int fill_w = inner_w * pct / 100;
if (fill_w > 0) {
Paint_DrawRectangle(inner_x, inner_y,
inner_x + fill_w, inner_y + inner_h,
BLACK, DOT_PIXEL_1X1, DRAW_FILL_FULL);
}
}

// ==================== 屏幕初始化 ====================
void initScreen() {
Serial.println("screen: malloc...");
BlackImage = (UBYTE *)malloc(IMG_BYTES);
StatusImage = (UBYTE *)malloc(STATUS_BYTES);
if (!BlackImage || !StatusImage) {
Serial.println("malloc FAIL");
return;
}

Serial.println("screen: EPD_Init...");
EPD_3IN97_Init();
EPD_3IN97_Clear();
Serial.println("screen: EPD_Init done");

Paint_NewImage(BlackImage, SCREEN_W, SCREEN_H, 0, WHITE);
Paint_SelectImage(BlackImage);
Paint_Clear(WHITE);
st.screenOk = true;
}

// ==================== 全刷背景 ====================
void showBackground() {
if (!st.screenOk) return;
Serial.println("screen: display bg...");

EPD_3IN97_Init();

// 关键:全刷前把画布切回整屏,避免被 updateStatusBar 改成 800x60
Paint_NewImage(BlackImage, SCREEN_W, SCREEN_H, 0, WHITE);
Paint_SelectImage(BlackImage);

memcpy(BlackImage, gImage_800x480, IMG_BYTES);

int bat = get_battery_percent();
if (bat < 0) bat = 0;

char num[8];
snprintf(num, sizeof(num), "%d", bat);

const int ICON_W = 40;
const int ICON_H = 18;
const int GAP = 6;
const int MARGIN = 8;

int num_w = strlen(num) * courbd36.Width;
int num_h = courbd36.Height;

int total_w = ICON_W + GAP + num_w;
int total_h = (ICON_H > num_h) ? ICON_H : num_h;

int base_x = SCREEN_W - total_w - MARGIN;
int base_y = SCREEN_H - total_h - MARGIN;
if (base_x < 0) base_x = 0;
if (base_y < 0) base_y = 0;

int icon_y = base_y + (total_h - ICON_H) / 2;
draw_battery_icon(base_x, icon_y, ICON_W, ICON_H, bat);

int num_x = base_x + ICON_W + GAP;
int num_y = base_y + (total_h - num_h) / 2;
if (num_y < 0) num_y = 0;
Paint_DrawString_EN(num_x, num_y, num, &courbd36, WHITE, BLACK);

EPD_3IN97_Display_Base(BlackImage);
Serial.println("screen: bg done");
delay(300);
}

// ==================== 状态栏(局刷) ====================
void updateStatusBar(const char *msg) {
if (!st.screenOk || st.screenOff) return;

if (st.partialCount >= PARTIAL_REFRESH_MAX) {
st.partialCount = 0;
showBackground(); // 全刷后继续画状态栏
}

Paint_NewImage(StatusImage, SCREEN_W, STATUS_H, 0, WHITE);
Paint_SelectImage(StatusImage);
Paint_Clear(WHITE);

char safeMsg[80];
strncpy(safeMsg, msg, sizeof(safeMsg) - 1);
safeMsg[sizeof(safeMsg) - 1] = '\0';

int maxChars = (SCREEN_W - 20) / courbd36.Width;
if (maxChars < 1) maxChars = 1;
if (strlen(safeMsg) > (size_t)maxChars) safeMsg[maxChars] = '\0';

Paint_DrawString_EN(10, 5, safeMsg, &courbd36, WHITE, BLACK);

// 局刷窗口对齐到 8 像素;y 用 STATUS_Y / STATUS_H
EPD_3IN97_Display_Partial(StatusImage,
0, STATUS_Y,
SCREEN_W, STATUS_Y + STATUS_H);
st.partialCount++;
}

// ==================== WiFi ====================
bool ensureWiFi(unsigned long timeout_ms = 15000) {
if (WiFi.status() == WL_CONNECTED) return true;

Serial.println("WiFi: connecting / starting config portal...");
updateStatusBar("WiFi: Connecting or Config Radio-Setup");

wm.setConfigPortalTimeout(180);
bool connected = wm.autoConnect("Radio-Setup");

if (connected && WiFi.status() == WL_CONNECTED) {
Serial.printf("WiFi: OK %s\n", WiFi.localIP().toString().c_str());
st.wifiOff = false;
updateStatusBar("WiFi: Connected");
return true;
}

Serial.println("WiFi: config portal timeout / failed");
updateStatusBar("ERROR: WiFi Config Timeout");
return false;
}

// ==================== 播放控制 ====================
void switchStation(int idx) {
if (!ensureWiFi(10000)) {
Serial.println("switchStation: no WiFi");
updateStatusBar("ERROR: No WiFi");
return;
}
if (idx < 0) idx = STATION_COUNT - 1;
if (idx >= (int)STATION_COUNT) idx = 0;
st.station = idx;

// NVS:只在值变化时写
prefs.begin("radio", false);
int saved = prefs.getInt("stationId", -1);
if (saved != idx) prefs.putInt("stationId", idx);
prefs.end();

audio.stopSong();
delay(100); // 等 stopSong 释放 socket

digitalWrite(PA_CTRL, HIGH);
delay(10);

bool ok = audio.connecttohost(stations[idx].url);
if (!ok) {
Serial.println("connecttohost FAIL");
updateStatusBar("ERROR: Connect Failed");
st.playing = false;
st.idle = true;
st.idleStart = millis();
digitalWrite(PA_CTRL, LOW);
return;
}

st.playing = true;
st.idle = false;

snprintf(statusLine, sizeof(statusLine), "PLAY: %s", stations[idx].name);
Serial.printf("PLAY: %s\n", stations[idx].name);
updateStatusBar(statusLine);
}

void togglePlayStop() {
if (st.playing) {
audio.stopSong();
st.playing = false;
st.idle = true;
st.idleStart = millis();
digitalWrite(PA_CTRL, LOW); // 停止播放时关功放
snprintf(statusLine, sizeof(statusLine), "STOP: %s", stations[st.station].name);
Serial.println("STOP");
} else {
if (!ensureWiFi(10000)) {
Serial.println("PLAY: no WiFi");
updateStatusBar("ERROR: No WiFi");
return;
}
digitalWrite(PA_CTRL, HIGH);
delay(10);
bool ok = audio.connecttohost(stations[st.station].url);
if (!ok) {
updateStatusBar("ERROR: Connect Failed");
digitalWrite(PA_CTRL, LOW);
return;
}
st.playing = true;
st.idle = false;
snprintf(statusLine, sizeof(statusLine), "PLAY: %s", stations[st.station].name);
Serial.println("PLAY");
}
updateStatusBar(statusLine);
}

// ==================== 深度睡眠 ====================
void enterDeepSleep() {
Serial.println("[SYS] deep sleep");
if (st.screenOk && !st.screenOff) {
updateStatusBar("SLEEPING (Hold BOOT)");
delay(500);
EPD_3IN97_Sleep();
}
digitalWrite(PA_CTRL, LOW);
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);

rtc_gpio_pullup_en(GPIO_NUM_0);
rtc_gpio_pulldown_dis(GPIO_NUM_0);
//esp_sleep_enable_ext0_wakeup(GPIO_NUM_0, 0);
esp_sleep_enable_ext1_wakeup(1ULL << GPIO_NUM_0, ESP_EXT1_WAKEUP_ANY_LOW);
Serial.flush();
esp_deep_sleep_start();
}

void wakeUp() {
if (!st.screenOff) return;
Serial.println("wakeUp...");
digitalWrite(PA_CTRL, HIGH);
delay(10);
if (st.screenOk) {
EPD_3IN97_Init();
showBackground();
}
st.screenOff = false;
st.idleStart = millis();
st.idle = false;

if (!ensureWiFi(15000)) {
updateStatusBar("ERROR: WiFi");
st.playing = false;
st.idle = true;
st.idleStart = millis();
digitalWrite(PA_CTRL, LOW);
return;
}

switchStation(st.station);
Serial.println("wakeUp done");
}

// ==================== Setup ====================
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\n=== BOOT START ===");

pinMode(BOOT_BTN, INPUT_PULLUP);
pinMode(KEY_FUNC, INPUT_PULLUP);
pinMode(PA_CTRL, OUTPUT);
digitalWrite(PA_CTRL, HIGH);
Serial.println("pins ok");

Serial.println("DEV_Module_Init...");
DEV_Module_Init();
Serial.println("DEV_Module_Init done");

Wire.begin(I2C_SDA, I2C_SCL);
Serial.println("i2c ok");

if (PMU.begin(Wire, AXP2101_SLAVE_ADDRESS, I2C_SDA, I2C_SCL)) {
PMU.enableBattVoltageMeasure();
pmu_ok = true;
Serial.printf("[PMU] OK, bat=%d%%, %dmV\r\n",
PMU.getBatteryPercent(), PMU.getBattVoltage());
} else {
pmu_ok = false;
Serial.println("[PMU] init failed");
}

initScreen();
Serial.printf("screenOk=%d\n", st.screenOk);
showBackground();

Serial.println("es8311 init...");
if (es8311_codec_init() != ESP_OK) {
Serial.println("ES8311 FAIL (continue)");
updateStatusBar("ERROR: ES8311");
} else {
Serial.println("es8311 ok");
}

Serial.println("wifi connect...");
if (!ensureWiFi(15000)) {
Serial.println("WiFi FAIL");
updateStatusBar("ERROR: WiFi");
st.playing = false;
st.idle = true;
st.idleStart = millis();
digitalWrite(PA_CTRL, LOW);
} else {
updateStatusBar("WiFi OK");
audio.setPinout(I2S_BCK_PIN, I2S_LRCK_PIN, I2S_DOUT_PIN, I2S_MCK_PIN);
audio.setVolume(18);

prefs.begin("radio", true);
st.station = prefs.getInt("stationId", 0);
prefs.end();
if (st.station < 0 || st.station >= (int)STATION_COUNT) st.station = 0;

switchStation(st.station);
}

Serial.println("=== SETUP DONE ===");
}

// ==================== Loop ====================
void loop() {
// 只在播放时跑音频解码,空闲时省 CPU
if (st.playing) audio.loop();

static unsigned long lastBtnTime = 0;
static unsigned long bootPressStart = 0;
static bool bootPressed = false, funcPressed = false, longHandled = false;

bool bootNow = (digitalRead(BOOT_BTN) == LOW);
bool funcNow = (digitalRead(KEY_FUNC) == LOW);

if (bootNow && !bootPressed) {
bootPressed = true;
bootPressStart = millis();
longHandled = false;
}
if (bootNow && bootPressed && st.screenOff && !longHandled && (millis() - bootPressStart >= 800)) {
longHandled = true;
Serial.println("long press -> wakeUp");
wakeUp();
}
if (!bootNow && bootPressed) {
bootPressed = false;
unsigned long hold = millis() - bootPressStart;
if (!st.screenOff && hold < 800) togglePlayStop();
}

if (funcNow && !funcPressed) {
funcPressed = true;
if (st.screenOff) wakeUp();
if (millis() - lastBtnTime > 250) {
switchStation(st.station + 1);
lastBtnTime = millis();
}
}
if (!funcNow) funcPressed = false;

// 空闲省电:先关 WiFi,再深度睡眠
if (!st.playing && st.idle && !st.screenOff) {
unsigned long idle = millis() - st.idleStart;

if (!st.wifiOff && idle > IDLE_WIFI_OFF_MS) {
Serial.println("idle -> wifi off");
WiFi.disconnect(false);
WiFi.mode(WIFI_OFF);
st.wifiOff = true;
updateStatusBar("WIFI OFF (Idle)");
}

if (idle > IDLE_SLEEP_MS) {
Serial.println("idle -> deep sleep");
enterDeepSleep();
}
}

static unsigned long hb = 0;
if (millis() - hb > 5000) {
hb = millis();
//Serial.printf("[HB] play=%d idle=%d wifiOff=%d screenOff=%d\n",
// st.playing, st.idle, st.wifiOff, st.screenOff);
}
}

// ==================== 音频回调 ====================
void audio_showstation(const char *info) {}
void audio_eof_mp3(const char *info) {
if (st.playing) {
st.playing = false;
st.idle = true;
st.idleStart = millis();
digitalWrite(PA_CTRL, LOW);
updateStatusBar("ERROR: Stream Lost");
}
}


wifi配置:打开 http://192.168.4.1 进行配网。
按左侧拨轮换台,按BOOT播放和停止,停止播放5分钟关wifi,10分钟后进入深度睡眠,长按BOOT唤醒。
源码:https://nas.qs100371.top:5667/s/58f9831696b949c7a6

代码在deepseek网页版下完成。局部刷新走了很长时间弯路,正常写文字都是左上角是(0, 0),从上到下,局刷却是左下角,从下到上。