#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 menu_select = 0; // 主菜单当前中的高索引 (0~2) u32 captured_addr = 0; // 配或报时,捕获到的传感器 24 EV1527 射物理地 u8 captured_type = 0; // 正在对码或触发报的传感器设类 (0:门, 1:PIR...) u8 alarm_sensor_slot = 0; // 触发警报的传感器 sensor_list 数组的索引槽位号 // 传感器存储列表数库,位于单片机部 XDATA 存储空间,共 16 槽位 Sensor_Slot xdata sensor_list[16]; // 全局滴答与定时中计数变量,位 volatile 防编译器优 volatile u16 ms_tick = 0; // 定时 1ms 基准滴答加器 volatile u16 inactivity_timer = 0; // 用户闲置计时,用于无操作判定(单: 10ms volatile u32 pair_timeout_ms = 0; // 传感器配对码超时秒时 (上限 30000ms 30s) volatile u16 alarm_timer_ms = 0; // 入侵报马达震动时序数 volatile u16 motor_timer_ms = 0; // 立马达震动时长数 volatile bit alarm_flash_flag = 0; // 报时红周期闪烁亮标志 // 物理按键描与消抖长按计数 (10ms 描一) volatile u16 key_scan_timer = 0; // 10ms 描周期分频器 volatile u16 key_up_hold = 0; // (KEY_UP) 按下时长 volatile u16 key_down_hold = 0; // (KEY_DOWN) 按下时长 volatile u16 key_confirm_hold = 0; // (KEY_CONFIRM) 按下时长 volatile u16 key_sos_hold = 0; // SOS 按下时长 volatile u16 comb_hold = 0; // + 组合按下时长 /* ========================================================================= * 串口调试指令解析与事件派发封 * ========================================================================= */ /** * @brief 解析通过串口 1 发来的键盘及测试模拟指令 * @param cmd 接收到的 ASCII 字命 * @details 负责将字符指令转化应的系统按或射频事件,并推入事件队列以测试件架构: * - 'u'/'U': 模拟/长按 * - 'd'/'D': 模拟/长按 * - 's'/'S': 模拟/长按 SOS 物理(优先插入队首) * - 'c'/'C': 模拟组合触发 * - 'a'/'A': 模拟无线传感器入侵触发报信号,地固定 0x123456,zone=1 * - 'p'/'P': 模拟对码期间捕获 24 位无线码信号,地固定 0x789ABC * - '1'/'2'/'3': 模拟系统的常、已布防、已撤防状切 * - 'z'/'Z': 模拟超时,强行使闲置计数器置 6000,触发自动深度休眠 * - 't'/'T': 触发屏幕与无线控制引脚测试 */ /** * @brief ڵרRGB ɫͨŲ * @details ηʹ졢̡ÿɫ 2 룬Уʵɫӳϵ */ void RGB_Diagnostic_Test(void) { Uart_SendString("\r\n=== RGB LED Color Channel Test Starting ===\r\n"); // 1. Էͺɫ (G=0, R=150, B=0) Uart_SendString("[RGB-TEST] 1. Sending RED command -> G=0, R=150, B=0\r\n"); RGB_Send(0, 150, 0, 0, 150, 0); Delay_ms(2000); // Ϩ RGB_Send(0, 0, 0, 0, 0, 0); Delay_ms(300); // 2. Էɫ (G=150, R=0, B=0) Uart_SendString("[RGB-TEST] 2. Sending GREEN command -> G=150, R=0, B=0\r\n"); RGB_Send(150, 0, 0, 150, 0, 0); Delay_ms(2000); // Ϩ RGB_Send(0, 0, 0, 0, 0, 0); Delay_ms(300); // 3. Էɫ (G=0, R=0, B=150) Uart_SendString("[RGB-TEST] 3. Sending BLUE command -> G=0, R=0, B=150\r\n"); RGB_Send(0, 0, 150, 0, 0, 150); Delay_ms(2000); // Ϩ RGB_Send(0, 0, 0, 0, 0, 0); Uart_SendString("=== RGB LED Color Channel Test Ended ===\r\n"); } 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; // 如果地数库中没有匹配该射频,则强制登记进 1 号防 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; // 入侵报为 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') { // 有当手环切入 Pair 模式时,模拟对码信号才有 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') { // 模拟切换 正常撤防 (NORMAL) 状并刷新时钟 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') { // 模拟切换 已布 (ARMED) 状,通门磁可触发警报 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') { // 模拟切换 已撤 (DISARMED) 状,忽略通报 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 = 6000; // 模拟闲置 60 秒超时,强制触发主循深度休眠 Uart_SendString("[UART] Force Entering Sleep mode!\r\n"); } else if (cmd == 'q' || cmd == 'Q') { RGB_Diagnostic_Test(); // RGB ɫͨŲ } else if (cmd == 't' || cmd == 'T') { RF_DiagnosticMode('t'); // 触发射连发射诊断 (绿色爆闪) } else if (cmd == 'r' || cmd == 'R') { RF_DiagnosticMode('r'); // 触发 5 秒射频接收监诊断 } else if (cmd == 'l' || cmd == 'L') { Loopback_Test(); // 触发物理电气回环测试 ( P2.1 P3.6 ) } } /* ========================================================================= * 主函 (入口) * ========================================================================= */ void main(void) { char cmd; // ====== STC32G 特殊功能寄存器内核基初化 ====== WTST = 0; // 设置程序 Flash 访问等待时间 0 (高运) EAXFR = 1; // 允问扩展特殊功能寄存 (XSFR) CKCON = 0; // 外部总线时钟设定为最 WDT_CONTR = 0x00; // 关闭看门狗定时器 // ====== 件各模块基初化 ====== GPIO_Init(); // 配置各个引脚的推挽输/准双向模式及内部上拉 (锁存 PWR_HOLD=1) RGB_Send(0, 0, 0, 0, 0, 0); // 【修正:上电时间强制发 0 码关条,熄灭上电暂引起的随机白光 RF_Init(); // 初化射芯片各引脚控制 Delay_ms(500); // 动延时,等待供电电稳 LCD_Init(); // 初化 LHS114TC-IF03 (ST7789V) 屏幕控制寄存器序列并背光 Uart1_Init(); // 初化串口 1 波特 115200 供调试日 Uart_SendString("Wristband System Initialized!\r\n"); Self_Test(); // 执上电硬件自 (PWR_HOLD锁存、P0.3 ADC按键常P1.3 电池电量) Load_Database(); // IAP Flash 254 扇区读取有已绑定的传感器数据 RAM /* 步 1: 初化事件形缓冲区队列 */ EventQueue_Init(); /* 步 2+5: 初化应用管理器并登所有前台应,默认加 ClockApp */ AppManager_Init(); /* 动并挂载后台射监控应,开始侦无线传感器信 */ AppManager_AttachToBackground(&rf_monitor_app); /* 1ms 系统基准定时 Timer1 */ Timer1_Init(); /* 机自动加载并运待机时钟前台活跃应 */ AppManager_StartApp(APP_ID_CLOCK); Uart_SendString("[SYS] Event-driven framework ready\r\n"); // ====== 主循 ====== while (1) { /* 0. 查主按键定时标志 ( 1ms 位,主循响应清除并理) */ if (key_scan_flag) { key_scan_flag = 0; Event_KeyScan_Poll(); } /* 1. 串口非阻塞指令捕 -> 装配为事件方式入 */ cmd = Uart_RxChar(); if (cmd != '\0') { Debug_ProcessCommand(cmd); } /* 2. 事件分发总线:循询并处理队列的所有有效事 */ while (!EventQueue_IsEmpty()) { Event_Dispatcher_Loop(); } /* 3. 应用调度心:运当前于前台活跃状的应用(集成了 CPU 运时间超时挂起保护) */ AppManager_RunActiveApp(); /* 4. 动休眠判定:常无操作闲置达到 60 (6000 * 10ms = 60s) 时,切入低功耗停机休 */ if (inactivity_timer >= 6000) { inactivity_timer = 0; current_state = STATE_SLEEP; // 系统为休眠状 Enter_Low_Power_Sleep(); // 该函数是阻的,MCU 将在此挂起唤醒后将自动从其后继续执 current_state = STATE_NORMAL; // 唤醒后重新置为常待机状 AppManager_StartApp(APP_ID_CLOCK); // 重新时钟应用以刷新画面和背光 } } } // 唤醒源事件标志,由应的部 ISR 写入 volatile u8 p0_wakeup_flag = 0; // P0 口按唤醒标志 volatile u8 p2_wakeup_flag = 0; // P2 口按唤醒标志 volatile u8 p3_wakeup_flag = 0; // P3 口射频中唤醒标志 volatile u8 rf_wakeup_flag = 0; // 射中唤醒标志 /* ========================================================================= * STC32G 口掉电部唤醒服务程序 (ISR) * ========================================================================= */ /** * @brief Port0 口掉电中服务函数 ( isr_jump.asm 引定位至) * @details 负责处理 P0.1, P0.2, P0.3 按键在休眠模式下的物理触发,并立刻用防反复执行 */ void Port0_Isr(void) interrupt 13 { EAXFR = 1; // 使能访问扩展寄存 p0_wakeup_flag = P0INTF | 0x01;// 读取并份 P0 标志寄存器 (0x01作安全掩) P0INTF = 0x00; // 清除 P0 口中挂标志位 P0INTE = 0x00; // 【关保护】:立即关闭 P0 允,防按处于低电平期间反复触 ISR 导致堆栈溢出 } /** * @brief Port2 口掉电中服务函数 ( isr_jump.asm 引定位至) * @details 负责处理 P2.6 (SOS按键) P2.1 (射数) 在休眠模式下的物理跳变唤醒 */ void Port2_Isr(void) interrupt 15 { EAXFR = 1; // 使能扩展寄存 p2_wakeup_flag = P2INTF | 0x01;// 读取并份 P2 标志寄存区的 P2INTF = 0x00; // 清除 P2 口中挂标志位 P2INTE = 0x00; // 立即关闭 P2 通道,防低电平期间反复重 } /** * @brief Port3 口掉电中服务函数 ( isr_jump.asm 引定位至) * @details 负责处理 P3.6 (新射频数) 在休眠模式下的物理跳变唤醒 */ void Port3_Isr(void) interrupt 16 { EAXFR = 1; // 使能访问扩展寄存 p3_wakeup_flag = P3INTF | 0x01;// 读取并份 P3 标志寄存器 (0x01作安全掩) P3INTF = 0x00; // 清除 P3 口中挂标志位 P3INTE = 0x00; // 立即关闭 P3 通道,防止反复重 }