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;
WiFiManager wm;
#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;
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();
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);
EPD_3IN97_Display_Partial(StatusImage, 0, STATUS_Y, SCREEN_W, STATUS_Y + STATUS_H); st.partialCount++; }
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;
prefs.begin("radio", false); int saved = prefs.getInt("stationId", -1); if (saved != idx) prefs.putInt("stationId", idx); prefs.end();
audio.stopSong(); delay(100);
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_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"); }
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 ==="); }
void loop() { 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;
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(); } }
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"); } }
|