Files
stc32g128k/App/main.c

203 lines
6.7 KiB
C

#include "config.h"
#include "rgb.h"
#include "event.h"
#include "app_manager.h"
#include "database.h"
#include "ui.h"
#include "system.h"
#include "../Drivers/rf.h"
#include "../Drivers/lcd.h"
/* =========================================================================
* 系统全局变量定义
* ========================================================================= */
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;
/* =========================================================================
* 串口调试指令解析与事件派发封装
* ========================================================================= */
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");
}
} else if (cmd == '1') {
if (AppManager_GetActiveAppID() == APP_ID_CLOCK) {
current_state = STATE_NORMAL;
UI_ShowClockPage(current_state, current_hour, current_min);
}
Uart_SendString("[UART] State: NORMAL\r\n");
} else if (cmd == '2') {
if (AppManager_GetActiveAppID() == APP_ID_CLOCK) {
current_state = STATE_ARMED;
UI_ShowClockPage(current_state, current_hour, current_min);
}
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);
}
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");
}
}
/* =========================================================================
* 主函数 (入口)
* ========================================================================= */
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') {
Debug_ProcessCommand(cmd);
}
/* 步骤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();
}
}
}
/* =========================================================================
* STC32G 端口中断 ISR
* ========================================================================= */
// Port0 中断服务函数 (由 isr_jump.asm 从向量 37 @ 0x012BH 跳转至此)
void Port0_Isr(void) interrupt 13
{
}
// Port2 中断服务函数 (由 isr_jump.asm 从向量 39 @ 0x013BH 跳转至此)
void Port2_Isr(void) interrupt 15
{
}