2026-07-15 15:57:37 +08:00
|
|
|
#include "config.h"
|
2026-07-15 16:14:30 +08:00
|
|
|
#include "rgb.h"
|
|
|
|
|
#include "event.h"
|
|
|
|
|
#include "app_manager.h"
|
|
|
|
|
#include "database.h"
|
|
|
|
|
#include "ui.h"
|
|
|
|
|
#include "system.h"
|
2026-07-15 15:57:37 +08:00
|
|
|
#include "../Drivers/rf.h"
|
2026-07-15 16:14:30 +08:00
|
|
|
#include "../Drivers/lcd.h"
|
2026-07-15 15:57:37 +08:00
|
|
|
|
|
|
|
|
/* =========================================================================
|
2026-07-15 16:14:30 +08:00
|
|
|
* 系统全局变量定义
|
2026-07-15 15:57:37 +08:00
|
|
|
* ========================================================================= */
|
|
|
|
|
SystemState current_state = STATE_NORMAL;
|
|
|
|
|
u8 current_hour = 10;
|
|
|
|
|
u8 current_min = 35;
|
|
|
|
|
u8 current_sec = 0;
|
|
|
|
|
volatile u8 clock_updated = 0;
|
|
|
|
|
|
|
|
|
|
u8 menu_select = 0;
|
|
|
|
|
u32 captured_addr = 0;
|
|
|
|
|
u8 captured_type = 0;
|
|
|
|
|
u8 alarm_sensor_slot = 0;
|
|
|
|
|
|
|
|
|
|
Sensor_Slot xdata sensor_list[16];
|
|
|
|
|
|
|
|
|
|
volatile u16 ms_tick = 0;
|
|
|
|
|
volatile u16 inactivity_timer = 0; // 无操作闲置计时器
|
|
|
|
|
volatile u32 pair_timeout_ms = 0;
|
|
|
|
|
volatile u16 alarm_timer_ms = 0;
|
|
|
|
|
volatile u16 motor_timer_ms = 0;
|
|
|
|
|
volatile bit alarm_flash_flag = 0;
|
|
|
|
|
|
|
|
|
|
volatile u16 key_scan_timer = 0;
|
|
|
|
|
volatile u16 key_up_hold = 0;
|
|
|
|
|
volatile u16 key_down_hold = 0;
|
|
|
|
|
volatile u16 key_confirm_hold = 0;
|
|
|
|
|
volatile u16 key_sos_hold = 0;
|
|
|
|
|
volatile u16 comb_hold = 0;
|
|
|
|
|
|
2026-07-15 17:05:22 +08:00
|
|
|
volatile u8 isr_p0_flag = 0;
|
|
|
|
|
volatile u8 isr_p2_flag = 0;
|
|
|
|
|
|
2026-07-15 15:57:37 +08:00
|
|
|
/* =========================================================================
|
2026-07-15 16:14:30 +08:00
|
|
|
* 串口调试指令解析与事件派发封装
|
|
|
|
|
* ========================================================================= */
|
|
|
|
|
void Debug_ProcessCommand(char cmd)
|
|
|
|
|
{
|
|
|
|
|
SystemEvent debug_evt;
|
|
|
|
|
inactivity_timer = 0; // 调试命令重置闲置计时器
|
|
|
|
|
debug_evt.extra_data = 0;
|
|
|
|
|
debug_evt.extra_size = 0;
|
|
|
|
|
|
|
|
|
|
if (cmd == 'u') {
|
|
|
|
|
debug_evt.key_event = KEY_EVENT_UP_CLICK;
|
|
|
|
|
debug_evt.priority = EVENT_PRIORITY_NORMAL;
|
|
|
|
|
EventQueue_Push(debug_evt);
|
|
|
|
|
Uart_SendString("[UART] KEY_UP Clicked\r\n");
|
|
|
|
|
} else if (cmd == 'U') {
|
|
|
|
|
debug_evt.key_event = KEY_EVENT_UP_LONG;
|
|
|
|
|
debug_evt.priority = EVENT_PRIORITY_NORMAL;
|
|
|
|
|
EventQueue_Push(debug_evt);
|
|
|
|
|
Uart_SendString("[UART] KEY_UP Long Pressed\r\n");
|
|
|
|
|
} else if (cmd == 'd') {
|
|
|
|
|
debug_evt.key_event = KEY_EVENT_DOWN_CLICK;
|
|
|
|
|
debug_evt.priority = EVENT_PRIORITY_NORMAL;
|
|
|
|
|
EventQueue_Push(debug_evt);
|
|
|
|
|
Uart_SendString("[UART] KEY_DOWN Clicked\r\n");
|
|
|
|
|
} else if (cmd == 'D') {
|
|
|
|
|
debug_evt.key_event = KEY_EVENT_DOWN_LONG;
|
|
|
|
|
debug_evt.priority = EVENT_PRIORITY_NORMAL;
|
|
|
|
|
EventQueue_Push(debug_evt);
|
|
|
|
|
Uart_SendString("[UART] KEY_DOWN Long Pressed\r\n");
|
|
|
|
|
} else if (cmd == 's') {
|
|
|
|
|
debug_evt.key_event = KEY_EVENT_SOS_CLICK;
|
|
|
|
|
debug_evt.priority = EVENT_PRIORITY_URGENT;
|
|
|
|
|
EventQueue_InsertFront(debug_evt);
|
|
|
|
|
Uart_SendString("[UART] KEY_SOS Clicked\r\n");
|
|
|
|
|
} else if (cmd == 'S') {
|
|
|
|
|
debug_evt.key_event = KEY_EVENT_SOS_LONG;
|
|
|
|
|
debug_evt.priority = EVENT_PRIORITY_URGENT;
|
|
|
|
|
EventQueue_InsertFront(debug_evt);
|
|
|
|
|
Uart_SendString("[UART] KEY_SOS Long Pressed\r\n");
|
|
|
|
|
} else if (cmd == 'c' || cmd == 'C') {
|
|
|
|
|
debug_evt.key_event = KEY_EVENT_UP_DOWN_COMB;
|
|
|
|
|
debug_evt.priority = EVENT_PRIORITY_NORMAL;
|
|
|
|
|
EventQueue_Push(debug_evt);
|
|
|
|
|
Uart_SendString("[UART] KEY_UP+DOWN Combined\r\n");
|
|
|
|
|
} else if (cmd == 'a' || cmd == 'A') {
|
|
|
|
|
u32 mock_addr = 0x123456;
|
|
|
|
|
u8 mock_slot;
|
|
|
|
|
if (!Check_Sensor_ID(mock_addr, &mock_slot)) {
|
|
|
|
|
Add_Sensor_With_Zone(mock_addr, 0, 1);
|
|
|
|
|
}
|
|
|
|
|
debug_evt.key_event = KEY_EVENT_NONE;
|
|
|
|
|
debug_evt.priority = EVENT_PRIORITY_HIGH;
|
|
|
|
|
debug_evt.extra_data = mock_addr;
|
|
|
|
|
debug_evt.extra_size = 4;
|
|
|
|
|
EventQueue_InsertFront(debug_evt);
|
|
|
|
|
Uart_SendString("[UART] Mock RF Alarm\r\n");
|
|
|
|
|
} else if (cmd == 'p' || cmd == 'P') {
|
|
|
|
|
if (AppManager_GetActiveAppID() == APP_ID_PAIR) {
|
|
|
|
|
debug_evt.key_event = KEY_EVENT_NONE;
|
|
|
|
|
debug_evt.priority = EVENT_PRIORITY_NORMAL;
|
|
|
|
|
debug_evt.extra_data = 0x789ABC;
|
|
|
|
|
debug_evt.extra_size = 4;
|
|
|
|
|
EventQueue_Push(debug_evt);
|
|
|
|
|
Uart_SendString("[UART] Mock RF Pair Signal\r\n");
|
|
|
|
|
} else {
|
|
|
|
|
Uart_SendString("[UART] Not in PAIR mode\r\n");
|
2026-07-15 15:57:37 +08:00
|
|
|
}
|
2026-07-15 16:14:30 +08:00
|
|
|
} else if (cmd == '1') {
|
|
|
|
|
if (AppManager_GetActiveAppID() == APP_ID_CLOCK) {
|
2026-07-15 15:57:37 +08:00
|
|
|
current_state = STATE_NORMAL;
|
|
|
|
|
UI_ShowClockPage(current_state, current_hour, current_min);
|
|
|
|
|
}
|
2026-07-15 16:14:30 +08:00
|
|
|
Uart_SendString("[UART] State: NORMAL\r\n");
|
|
|
|
|
} else if (cmd == '2') {
|
|
|
|
|
if (AppManager_GetActiveAppID() == APP_ID_CLOCK) {
|
2026-07-15 15:57:37 +08:00
|
|
|
current_state = STATE_ARMED;
|
2026-07-15 16:14:30 +08:00
|
|
|
UI_ShowClockPage(current_state, current_hour, current_min);
|
2026-07-15 15:57:37 +08:00
|
|
|
}
|
2026-07-15 16:14:30 +08:00
|
|
|
Uart_SendString("[UART] State: ARMED\r\n");
|
|
|
|
|
} else if (cmd == '3') {
|
|
|
|
|
if (AppManager_GetActiveAppID() == APP_ID_CLOCK) {
|
|
|
|
|
current_state = STATE_DISARMED;
|
|
|
|
|
UI_ShowClockPage(current_state, current_hour, current_min);
|
2026-07-15 15:57:37 +08:00
|
|
|
}
|
2026-07-15 16:14:30 +08:00
|
|
|
Uart_SendString("[UART] State: DISARMED\r\n");
|
|
|
|
|
} else if (cmd == 'z' || cmd == 'Z') {
|
|
|
|
|
inactivity_timer = 1000; // 模拟闲置超时,强制触发休眠
|
|
|
|
|
Uart_SendString("[UART] Force Entering Sleep mode!\r\n");
|
2026-07-15 15:57:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* =========================================================================
|
2026-07-15 16:14:30 +08:00
|
|
|
* 主函数 (入口)
|
2026-07-15 15:57:37 +08:00
|
|
|
* ========================================================================= */
|
|
|
|
|
void main(void)
|
|
|
|
|
{
|
|
|
|
|
char cmd;
|
|
|
|
|
|
|
|
|
|
WTST = 0;
|
|
|
|
|
EAXFR = 1;
|
|
|
|
|
CKCON = 0;
|
|
|
|
|
WDT_CONTR = 0x00;
|
|
|
|
|
|
|
|
|
|
GPIO_Init();
|
|
|
|
|
RF_Init();
|
|
|
|
|
Delay_ms(500);
|
|
|
|
|
SGM_SendPulse(27);
|
|
|
|
|
Delay_ms(50);
|
|
|
|
|
LCD_Init();
|
|
|
|
|
Uart1_Init();
|
|
|
|
|
Uart_SendString("Wristband System Initialized!\r\n");
|
|
|
|
|
Load_Database();
|
|
|
|
|
|
|
|
|
|
/* 步骤1: 初始化事件队列 */
|
|
|
|
|
EventQueue_Init();
|
|
|
|
|
|
|
|
|
|
/* 步骤2+5: 初始化应用管理器 */
|
|
|
|
|
AppManager_Init();
|
|
|
|
|
|
|
|
|
|
/* 启动1ms定时器中断 */
|
|
|
|
|
Timer1_Init();
|
|
|
|
|
|
|
|
|
|
Uart_SendString("[SYS] Event-driven framework ready\r\n");
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
/* 串口调试命令 → 以事件方式入队 */
|
|
|
|
|
cmd = Uart_RxChar();
|
|
|
|
|
if (cmd != '\0') {
|
2026-07-15 16:14:30 +08:00
|
|
|
Debug_ProcessCommand(cmd);
|
2026-07-15 15:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 步骤3: 事件分发 — 循环处理队列中所有事件 */
|
|
|
|
|
while (!EventQueue_IsEmpty()) {
|
|
|
|
|
Event_Dispatcher_Loop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 步骤5: 运行活跃应用 (含CPU占用控制) */
|
|
|
|
|
AppManager_RunActiveApp();
|
|
|
|
|
|
|
|
|
|
/* 步骤6: 超时自动进入深度休眠 */
|
|
|
|
|
if (inactivity_timer >= 1000) {
|
|
|
|
|
inactivity_timer = 0;
|
|
|
|
|
current_state = STATE_SLEEP;
|
|
|
|
|
Enter_Low_Power_Sleep();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* =========================================================================
|
2026-07-15 16:14:30 +08:00
|
|
|
* STC32G 端口中断 ISR
|
2026-07-15 15:57:37 +08:00
|
|
|
* ========================================================================= */
|
|
|
|
|
|
|
|
|
|
// Port0 中断服务函数 (由 isr_jump.asm 从向量 37 @ 0x012BH 跳转至此)
|
|
|
|
|
void Port0_Isr(void) interrupt 13
|
|
|
|
|
{
|
|
|
|
|
EAXFR = 1;
|
2026-07-15 17:05:22 +08:00
|
|
|
isr_p0_flag = P0INTF;
|
2026-07-15 15:57:37 +08:00
|
|
|
P0INTF = 0x00; // 清除 P0 端口中断悬挂标志
|
|
|
|
|
P0INTE = 0x00; // 立即禁用,防止低电平保持期间反复触发
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Port2 中断服务函数 (由 isr_jump.asm 从向量 39 @ 0x013BH 跳转至此)
|
|
|
|
|
void Port2_Isr(void) interrupt 15
|
|
|
|
|
{
|
|
|
|
|
EAXFR = 1;
|
2026-07-15 17:05:22 +08:00
|
|
|
isr_p2_flag = P2INTF;
|
2026-07-15 15:57:37 +08:00
|
|
|
P2INTF = 0x00; // 清除 P2 端口中断悬挂标志
|
|
|
|
|
P2INTE = 0x00; // 立即禁用,防止低电平保持期间反复触发
|
|
|
|
|
}
|