/****************************************************************************** * @file app_function.c * @brief 功能总装:喷淋键启停、出厂重置、任务上下文落地配对/OTA * @author cyWu <1917507415@qq.com> * @date 2026.09.09 * @version V1.1.0 * @history * - V1.1.0, 2026.09.09, cyWu, P1 加 VM 落盘收口(app_nvm_flush) * - V1.0.0, 2026.09.09, cyWu, P0 首次发布(无 VM/联动/计划;P1+ 再扩展) * * @note ⚠ 硬约束:写 flash(清绑定)、BLE 广播切换(配对/OTA)禁止在 * 20ms 中断上下文直跑,一律 app_defer_request 后由本模块在 * usr_timer_loop(任务上下文)落地。 ******************************************************************************/ #include "system/includes.h" /* 必须先于 app_main.h:u8/u16/u32 等类型由 SDK 定义 */ #include "app_function.h" #include "app_pair.h" #include "app_defer.h" #include "app_led.h" #include "app_spray.h" #include "app_nvm.h" #include "app_arbiter.h" #include "app_runrec.h" #include "app_linkage.h" #include "app_timer_task.h" #include "app_main.h" #include "usr_le_api.h" #include "jb_product.h" #include "board_pin.h" #define LOG_TAG_CONST APP #define LOG_TAG "[APP_FUNC]" #define LOG_INFO_ENABLE #include "debug.h" /** * @brief 初始化功能模块 * @return 无 */ void app_function_init(void) { } /** * @brief 任务上下文周期处理(配对 / OTA / 写 VM 只准在此落地) * @return 无 */ void app_function_loop(void) { app_nvm_flush(); /* VM 防抖到点的落盘(任务上下文) */ if (app_defer_take(APP_DEFER_OTA)) { app_defer_cancel(APP_DEFER_PAIR); usr_app_ota_init(); app_led_request_ota(); log_info("pair key hold 12s -> ota\n"); } else if (app_defer_take(APP_DEFER_PAIR)) { usr_goto_pair_mode(); app_pair_start(); app_led_clear_pair_hint(); log_info("pair key released after 5s -> pair\n"); } } /** * @brief DP24 出厂重置(规格书第 15 章):清业务 VM + RAM 复位 * @note 保留 BLE 配对、不断开、不软复位。泵停走请求(下一 tick 生效); * userInit 把 gDevData 全量回默认(DP7=10/3/3、计划全 0、plan_en=0)。 * @return 无 */ void app_function_factory_reset(void) { app_spray_req_stop(); /* 泵停,DP1=0(不自动喷) */ app_nvm_reset(); /* 清 4 个业务 item(保留配对三元组) */ userInit(); /* gDevData 全量回默认 */ app_runrec_reset(); /* 31 天记录清零 */ app_linkage_init(); /* 作废标志/湿度有效位清掉 */ app_timer_init(); /* 计划队列清掉 */ log_info("factory reset done, business vm cleared\n"); } /** * @brief 同步 DP1 = 喷淋作业态(整轮含间隔等待为 1) * @return 无 */ void app_function_poll(void) { #if BOARD_SPRAY_ENABLE gDevData.spray_switch = app_spray_is_running(); #endif } /** * @brief 喷淋键短按:空闲用上次有效配置开一轮,作业中立刻停(OTA 中忽略) * @return 无 */ void usr_on_key_short(void) { if (app_led_is_ota()) { return; /* OTA 进行中忽略(规格书 5.3) */ } app_arbiter_manual_intervene(); /* 开/停都是手动抢权,作废联动 */ if (app_spray_is_running()) { app_spray_req_stop(); log_info("spray key short -> stop\n"); } else { app_spray_req_start(); log_info("spray key short -> start\n"); } } /** * @brief 配网键按住满 5s:红灯 100ms 快闪提示(不碰绿灯,不清绑定) * @return 无 */ void usr_on_key_pair_hint(void) { if (app_led_is_ota() || app_pair_is_active()) { return; /* OTA / 配对会话中忽略再次 5s(规格书 5.3) */ } app_led_hint_pair(); log_info("pair key hold 5s, red fast blink (release to pair, keep to 12s ota)\n"); } /** * @brief 配网键按满 5s 后松手:置配对标志(清绑定在任务上下文) * @return 无 */ void usr_on_key_pair_start(void) { if (app_led_is_ota() || app_pair_is_active()) { return; /* OTA / 配对会话中忽略(规格书 5.3) */ } app_defer_request(APP_DEFER_PAIR); log_info("pair key 5s released, pair pending\n"); } /** * @brief 配网键按住满 12s:置 OTA 标志(BLE 切换在任务上下文) * @return 无 */ void usr_on_key_ota_start(void) { if (app_led_is_ota() || app_pair_is_active()) { return; /* OTA / 配对会话中忽略(规格书 5.3) */ } app_defer_request(APP_DEFER_OTA); log_info("pair key hold 12s, ota pending\n"); }