165 lines
5.6 KiB
C++
165 lines
5.6 KiB
C++
// nex_buddy —— 圆环交互固件(自研,烧录在 app1)
|
||
//
|
||
// 主文件只做四件事:初始化硬件、注册应用与服务、驱动主循环、把启动过程演给人看。
|
||
// 分层:hal(硬件)/ core(框架)/ ui(圆环控件与导航)/ apps(业务)/ services(后台能力)
|
||
#include <Arduino.h>
|
||
#include <lvgl.h>
|
||
|
||
#include "src/config/AppConfig.h"
|
||
#include "src/core/Log.h"
|
||
#include "src/core/Settings.h"
|
||
#include "src/core/AppManager.h"
|
||
#include "src/hal/Display.h"
|
||
#include "src/hal/Storage.h"
|
||
#include "src/hal/Power.h"
|
||
#include "src/hal/Encoder.h"
|
||
#include "src/hal/Haptic.h"
|
||
#include "src/ui/Theme.h"
|
||
#include "src/ui/Shell.h"
|
||
#include "src/ui/Splash.h"
|
||
#include "src/apps/ClockApp.h"
|
||
#include "src/apps/GalleryApp.h"
|
||
#include "src/apps/MusicApp.h"
|
||
#include "src/apps/AstroApp.h"
|
||
#include "src/apps/UserApp.h"
|
||
#include "src/apps/WeatherApp.h"
|
||
#include "src/apps/SettingsApp.h"
|
||
#include "src/services/NetService.h"
|
||
#include "src/services/WebPortal.h"
|
||
#include "src/services/AstroService.h"
|
||
#include "src/services/WeatherService.h"
|
||
#include "src/services/PlatformService.h"
|
||
#include "src/services/OtaService.h"
|
||
#include "src/services/LinkService.h"
|
||
#include "src/services/AudioLink.h"
|
||
#include "src/services/MediaSync.h"
|
||
#include "src/services/RemoteDebug.h"
|
||
|
||
static void onSettingChanged(const char* key) {
|
||
if (strcmp(key, "sys.bright") == 0) {
|
||
hal::setBrightness((uint8_t)settings.getI(key));
|
||
} else if (strcmp(key, "sys.accent") == 0) {
|
||
theme::applyAccent((uint8_t)settings.getI(key));
|
||
lv_obj_invalidate(lv_scr_act());
|
||
} else if (strcmp(key, "sys.haptic") == 0) {
|
||
hal::hapticSetLevel((uint8_t)settings.getI(key));
|
||
} else if (strcmp(key, "sys.tz") == 0) {
|
||
svc::net.applyTz(); // 改时区立即生效,不用重启
|
||
}
|
||
}
|
||
|
||
// 背光从暗到亮,开机第一秒的观感差别很大
|
||
static void fadeBacklight(uint8_t target, uint32_t ms) {
|
||
uint32_t t0 = millis();
|
||
while (millis() - t0 < ms) {
|
||
float k = (float)(millis() - t0) / (float)ms;
|
||
uint8_t b = (uint8_t)(5 + (target - 5) * k);
|
||
hal::setBrightness(b);
|
||
lv_timer_handler();
|
||
delay(6);
|
||
}
|
||
hal::setBrightness(target);
|
||
}
|
||
|
||
// 主循环跑在独立任务里,而不是 Arduino 默认的 loop()。
|
||
// 原因:默认 loopTask 只给 8KB 栈,图片解码、TLS 握手这类操作会直接爆栈。
|
||
// 这里给 20KB,并且只在一个任务里碰 LVGL(LVGL 不是线程安全的)。
|
||
static void knobTask(void*) {
|
||
for (;;) {
|
||
hal::displayLoop();
|
||
ui::shell.loop(millis());
|
||
|
||
svc::link.loop();
|
||
svc::audioLink.loop();
|
||
svc::net.loop();
|
||
svc::platform.loop();
|
||
svc::mediaSync.loop(); // TF 卡媒体增量同步:到点自己派任务,不阻塞 UI
|
||
svc::astro.loop();
|
||
svc::weather.loop();
|
||
svc::ota.loop(); // 进度合并 + 到点重启;必须有人每帧推进
|
||
svc::webPortal.loop();
|
||
svc::remoteDbg.loop();
|
||
|
||
delay(3);
|
||
}
|
||
}
|
||
|
||
void setup() {
|
||
Serial.begin(115200);
|
||
delay(200);
|
||
LOGF("=== %s %s ===", KNOBOS_NAME, KNOBOS_VERSION);
|
||
|
||
settings.begin();
|
||
settings.onChange(onSettingChanged);
|
||
|
||
// 开机守卫要尽量早:升级后连崩三次固件,就在进任何 UI 之前切回上一个槽。
|
||
// 放在 settings.begin() 之后是因为它要读写 NVS。
|
||
svc::ota.begin();
|
||
|
||
// 屏幕初始化内部会调用 lv_init(),必须先于任何 LVGL 样式操作
|
||
if (!hal::displayBegin()) {
|
||
LOGF("FATAL: display init failed");
|
||
while (true) delay(1000);
|
||
}
|
||
|
||
theme::init(); // 依赖 lv_init() 建立的内存池
|
||
|
||
// ---- 启动动画:先亮屏,再逐步把真实初始化进度画出来 ----
|
||
ui::splash.build();
|
||
fadeBacklight((uint8_t)settings.getI("sys.bright"), 420);
|
||
ui::splash.step(8, "点亮屏幕", 60);
|
||
|
||
hal::storageBegin(); // 没插卡也继续跑
|
||
ui::splash.step(28, "挂载存储", 80);
|
||
|
||
hal::powerBegin();
|
||
hal::hapticBegin();
|
||
ui::splash.step(44, "检查电源", 90);
|
||
|
||
hal::encoderBegin(nullptr);
|
||
ui::splash.step(54, "读取旋钮", 70);
|
||
|
||
// 应用注册顺序 = 主菜单顺序 = 设置里"开机启动"的编号
|
||
appMgr.add(&apps::clockApp);
|
||
appMgr.add(&apps::galleryApp);
|
||
appMgr.add(&apps::musicApp);
|
||
appMgr.add(&apps::astroApp);
|
||
appMgr.add(&apps::userApp);
|
||
appMgr.add(&apps::weatherApp);
|
||
appMgr.add(&apps::settingsApp);
|
||
ui::splash.step(68, "加载应用", 90);
|
||
|
||
ui::shell.begin();
|
||
ui::splash.step(82, "装配界面", 80);
|
||
|
||
// 后台服务
|
||
svc::link.begin();
|
||
svc::audioLink.begin(); // 同一条 UART1:音轨走 921600 推给副控
|
||
svc::net.begin();
|
||
svc::platform.begin(); // 读 NVS 里的设备 token,决定是否已绑定
|
||
ui::splash.step(94, "连接网络", 70);
|
||
|
||
svc::astro.begin();
|
||
svc::weather.begin();
|
||
svc::mediaSync.begin(); // 读 TF 本地清单,等绑定+联网后自动增量同步
|
||
svc::webPortal.begin();
|
||
svc::remoteDbg.begin();
|
||
ui::splash.step(100, "准备就绪", 160);
|
||
|
||
ui::splash.finish();
|
||
|
||
// 启动自检:相册是最容易"看不见"的一环(屏上看不到就等于没证据),
|
||
// 开机把解码链路跑一次并留日志,远程也能判断问题出在扫描还是解码。
|
||
apps::galleryApp.diagDecode();
|
||
|
||
LOGF("boot done, heap=%u psram=%u",
|
||
(unsigned)ESP.getFreeHeap(), (unsigned)ESP.getFreePsram());
|
||
|
||
xTaskCreatePinnedToCore(knobTask, "knobos", 20480, nullptr, 2, nullptr, 1);
|
||
}
|
||
|
||
void loop() {
|
||
// 主循环已经交给 knobos 任务,这里只留一个空转,避免两边同时操作 LVGL
|
||
delay(1000);
|
||
}
|