refactor(event): replace global 60s inactivity timer with per-second tick event

Adds KEY_EVENT_SECOND_TICK, generated once per real second from
Clock_IncMS() via a new second_tick_flag and pushed through the normal
event queue, so every foreground App's onEvent() receives it and
decides independently whether to act. Removes the old global
inactivity_timer (60s sleep from anywhere) entirely. HomeApp now
accumulates its own idle-second counter and sleeps after 10s idle
specifically while on the home screen; MenuApp's existing 10s
auto-close is converted from onRun() polling to the same tick+reset
pattern for consistency. Any non-tick key event resets each app's own
counter to 0.
This commit is contained in:
edisondeng
2026-07-31 18:49:03 +08:00
parent cbd480eea3
commit 2a83079007
8 changed files with 70 additions and 54 deletions

View File

@@ -6,7 +6,6 @@
#include "system.h"
#include "database.h"
#include "language.h"
#include "clock.h"
#include "../Drivers/rf.h"
// 外部全局变量引用
@@ -16,12 +15,11 @@ extern u8 menu_select;
// 菜单闲置自动关闭超时 (秒):无任何按键操作达到这个时长,自动退出菜单返回时钟主页
#define MENU_IDLE_TIMEOUT_SEC 10
// 本应用私有变量:记录最近一次按键活动时的秒计时快照
static u8 menu_idle_start_sec = 0;
// 本应用私有变量:累计未按任何键的秒数 (由 KEY_EVENT_SECOND_TICK 驱动,任意按键事件清零)
static u8 menu_idle_sec = 0;
// 声明本应用的生命周期回调函数
static void MenuApp_OnStart(void);
static void MenuApp_onRun(void);
static void MenuApp_onClose(void);
static EventResult MenuApp_onEvent(SystemEvent *evt);
@@ -30,7 +28,7 @@ WristbandApp menu_app = {
APP_ID_MENU,
"Menu",
MenuApp_OnStart,
MenuApp_onRun,
NULL,
MenuApp_onClose,
MenuApp_onEvent
};
@@ -41,7 +39,7 @@ WristbandApp menu_app = {
static void MenuApp_OnStart(void)
{
menu_select = 0;
menu_idle_start_sec = current_sec;
menu_idle_sec = 0;
current_state = STATE_PAIR_MENU; // 系统运行状态设为对码配置菜单
RF_SetMode(0); // 关闭射频接收芯片,防止接收杂波干扰屏幕绘制
UI_ShowMainMenu(menu_select); // 绘制主菜单
@@ -49,20 +47,6 @@ static void MenuApp_OnStart(void)
XTELL_LOG("[MenuApp] Started\r\n");
}
/**
* @brief MenuApp 轮询主回调:检测闲置超时,无操作达到 MENU_IDLE_TIMEOUT_SEC 秒自动退出
*/
static void MenuApp_onRun(void)
{
u8 elapsed = (current_sec >= menu_idle_start_sec)
? (current_sec - menu_idle_start_sec)
: (60 + current_sec - menu_idle_start_sec);
if (elapsed >= MENU_IDLE_TIMEOUT_SEC) {
XTELL_LOG("[MenuApp] Idle timeout, closing.\r\n");
AppManager_SwitchToForeground(&home_app);
}
}
/**
* @brief MenuApp 退出清理回调
*/
@@ -78,7 +62,19 @@ static void MenuApp_onClose(void)
*/
static EventResult MenuApp_onEvent(SystemEvent *evt)
{
menu_idle_start_sec = current_sec; // 任意按键活动都刷新闲置计时起点
// 0. 整秒滴答:累计闲置秒数,达到门限就自动关闭菜单退回待机主页
if (evt->key_event == KEY_EVENT_SECOND_TICK) {
menu_idle_sec++;
if (menu_idle_sec >= MENU_IDLE_TIMEOUT_SEC) {
menu_idle_sec = 0;
XTELL_LOG("[MenuApp] Idle timeout, closing.\r\n");
AppManager_SwitchToForeground(&home_app);
}
return EVENT_HANDLED;
}
// 收到任意其它按键事件,说明用户仍在操作,清零闲置计数
menu_idle_sec = 0;
if (evt->key_event == KEY_EVENT_UP_CLICK) {
// 短按 ▲ 键向上移动焦点,允许循环切换 (到顶绕到底)