Files
stc32g128k/App/app_menu.c
edisondeng 2a83079007 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.
2026-07-31 18:49:03 +08:00

119 lines
4.3 KiB
C
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.

#include "app_manager.h"
#include "xtell.h"
#include "ui.h"
#include "rgb.h"
#include "event.h"
#include "system.h"
#include "database.h"
#include "language.h"
#include "../Drivers/rf.h"
// 外部全局变量引用
extern SystemState current_state;
extern u8 menu_select;
// 菜单闲置自动关闭超时 (秒):无任何按键操作达到这个时长,自动退出菜单返回时钟主页
#define MENU_IDLE_TIMEOUT_SEC 10
// 本应用私有变量:累计未按任何键的秒数 (由 KEY_EVENT_SECOND_TICK 驱动,任意按键事件清零)
static u8 menu_idle_sec = 0;
// 声明本应用的生命周期回调函数
static void MenuApp_OnStart(void);
static void MenuApp_onClose(void);
static EventResult MenuApp_onEvent(SystemEvent *evt);
// 实体化定义菜单应用控制块 (顶层 4 项列表:语言切换/时间设置/传感器配对/摄像头绑定)
WristbandApp menu_app = {
APP_ID_MENU,
"Menu",
MenuApp_OnStart,
NULL,
MenuApp_onClose,
MenuApp_onEvent
};
/**
* @brief MenuApp 启动回调
*/
static void MenuApp_OnStart(void)
{
menu_select = 0;
menu_idle_sec = 0;
current_state = STATE_PAIR_MENU; // 系统运行状态设为对码配置菜单
RF_SetMode(0); // 关闭射频接收芯片,防止接收杂波干扰屏幕绘制
UI_ShowMainMenu(menu_select); // 绘制主菜单
RGB_Send(0, 0, 0, 0, 0, 0); // 熄灭所有灯条
XTELL_LOG("[MenuApp] Started\r\n");
}
/**
* @brief MenuApp 退出清理回调
*/
static void MenuApp_onClose(void)
{
RGB_Send(0, 0, 0, 0, 0, 0); // 熄灭 RGB
RF_SetMode(1); // 重新开启无线接收模式
XTELL_LOG("[MenuApp] Closed\r\n");
}
/**
* @brief MenuApp 接收按键与控制事件回调 (0-语言切换, 1-HORLOGE, 2-APPAIRAGE, 3-LIAISON)
*/
static EventResult MenuApp_onEvent(SystemEvent *evt)
{
// 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) {
// 短按 ▲ 键向上移动焦点,允许循环切换 (到顶绕到底)
menu_select = (menu_select == 0) ? 3 : (menu_select - 1);
UI_ShowMainMenu(menu_select);
return EVENT_HANDLED;
}
if (evt->key_event == KEY_EVENT_DOWN_CLICK) {
// 短按 ▼ 键向下移动焦点,允许循环切换 (到底绕到顶)
menu_select = (menu_select == 3) ? 0 : (menu_select + 1);
UI_ShowMainMenu(menu_select);
return EVENT_HANDLED;
}
if (evt->key_event == KEY_EVENT_CONFIRM_CLICK) {
// 短按 ■ 确认键,进入选定功能的子页面
if (menu_select == 0) {
// 功能 0语言切换。原地在 FR/EN 间切换,立即保存并用新语言重绘当前菜单
Language new_lang = (Lang_Current() == LANG_FR) ? LANG_EN : LANG_FR;
Lang_Set(new_lang);
Save_Database(); // 语言字节与传感器数据库共用同一扇区持久化
MOTOR = 1; Delay_ms(50); MOTOR = 0; // 短震反馈,与其它确认操作风格一致
UI_ShowMainMenu(menu_select);
} else if (menu_select == 1) {
// 功能 1设置时间。切换到独立的时间设置子 App
AppManager_SwitchToForeground(&set_clock_app);
} else if (menu_select == 2) {
// 功能 2传感器学习。切换到独立的对码子 App (类型选择是它的第一个状态)
AppManager_SwitchToForeground(&pair_app);
} else if (menu_select == 3) {
// 功能 3相机配对。切换到独立的摄像头绑定子 App
AppManager_SwitchToForeground(&ipc_bind_app);
}
return EVENT_HANDLED;
}
if (evt->key_event == KEY_EVENT_CONFIRM_LONG) {
// 长按 ■ 确认键,退出设置菜单直接返回待机主页
AppManager_SwitchToForeground(&home_app);
return EVENT_HANDLED;
}
return EVENT_IGNORED;
}