Files
stc32g128k/Docs/50_module-breakdown/mod-sys.md

49 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 模块拆分 - 系统状态机与 App 框架 (Docs/50_module-breakdown/mod-sys.md)
本模块描述手环的软件事件管理器、App 调度引擎(生命周期回调)以及 IAP 持久化防区数据库。
## 1. 模块职责说明
* **职责范围**
1. **维护 App 生命周期**:提供 `StartApp(app_id)``CloseApp(app_id)` 的系统级接口,调度应用层组件的切入与切出。
2. **事件路由器**主循环不断查询事件环形缓冲区Event Queue。若有事件分发至当前活跃 App 的 `onEvent` 接口。
3. **后台应用调度**:当有传感器入侵事件触发时,不论当前处于什么 App 页面,后台监测进程会自动唤醒并强占调用警报 AppAPP_ALARM
4. **Flash 数据库管理**:维护 `0xFE0000` (第 254 扇区) 物理闪存的读写擦除,管理 16 个已配对传感器的配置数据。
---
## 2. 核心结构体与接口定义
### 2.1 应用程序结构体原型 (WristbandApp)
```c
typedef struct {
u8 app_id; // 应用程序 ID
void (*OnStart)(void); // 启动接口:渲染初始画面、装载应用资源
void (*onRun)(void); // 循环接口:主循环轮空调用,执行非阻塞业务逻辑
void (*onClose)(void); // 关闭接口:退出前台,保存状态、复位相关驱动
void (*onEvent)(KeyEvent evt); // 事件处理:接收并消耗事件队列分配的按键/射频事件
} WristbandApp;
```
### 2.2 APP 管理器全局状态
```c
extern WristbandApp* active_app; // 指向当前前台活跃应用的指针
extern u16 inactivity_timer; // 无操作自动睡眠计时器
```
---
## 3. 接口与函数说明
* `void App_Manager_Init(void)`注册所有支持的应用实例待机时钟、主菜单、时间设置、对码、布撤防、主动SOS、被动报警默认设置 `active_app` 指向时钟应用并执行 `active_app->OnStart()`
* `void StartApp(u8 app_id)`
1. 执行当前 `active_app->onClose()` 清理现场。
2. 切换 `active_app` 指向目标 `app_id` 实例。
3. 执行新应用的 `active_app->OnStart()`,并置 `Redraw = 1`
* `void Load_Database(void)`:读取 `0xFE0000` 扇区,载入 16 组 `Sensor_Slot` 数据。
* `void Save_Database(void)`:擦除 IAP 扇区并写回当前缓存的配对记录。
* `void Add_Sensor(u32 addr, u8 type, u8 suffix_idx)`:添加配对并持久化。
* `bit Check_Sensor_ID(u32 addr, u8 *out_slot_index)`:在数据库中匹配已对码的传感器 ID获取槽位。
<!-- Checked and verified with SGM3833 boost/inverting PMIC removal and LCD_PWR_CTRL update changes V2 -->