Baji_Rtc_Toy_Key/main/dzbj/device_mode.c
Rdzleo 3a05c3c7d5 双模设备电子吧唧按键功能完整实现 + BLE设备间图片传输 + 模式切换防护
1、按键驱动重构:dzbj_button改为iot_button组件,支持BOOT/KEY2单击/双击/长按,回调通过xTaskCreate派发避免阻塞esp_timer;
2、新增按键导航管理器(key_nav):9种上下文状态机,统一分发按键事件到对应界面;
3、BLE改造:广播默认关闭由按键触发启停,新增设备间GATT Client图片传输(扫描→连接→MTU协商→分包发送),WRITE_EVT区分APP/设备传图跳转不同界面;
4、新增模式切换按键抑制:NVS标志+2秒时间窗,防止AI↔吧唧切换时幽灵按键触发配网或白屏;
5、AI模式BOOT单击回调添加抑制检查,吧唧模式key_nav_boot_click同步添加;
6、新增6个UI界面:配对(Peiwang)、更新(Update)、发送等待/接收等待/发送中/接收中;
7、新增电池指示器组件(battery_ui):多界面真实电量显示+3秒渐隐;
8、Home/Img界面重构:移除触摸手势事件,改为airhub背景+按键导航;
9、禁用触摸(DZBJ_ENABLE_TOUCH=0),保留代码可恢复;
10、sleep_mgr简化:按键唤醒由key_nav统一处理;

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-26 17:44:46 +08:00

71 lines
2.0 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 "device_mode.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_timer.h"
#define TAG "DeviceMode"
#define NVS_NAMESPACE "device"
#define NVS_KEY "mode"
#define NVS_KEY_SWITCHED "switched"
// 模式切换标志首次调用时从NVS读取并清除
static bool switch_flag_checked = false;
static bool mode_was_switched = false;
device_mode_t device_mode_get(void) {
nvs_handle_t h;
int32_t mode = DEVICE_MODE_AI;
if (nvs_open(NVS_NAMESPACE, NVS_READONLY, &h) == ESP_OK) {
nvs_get_i32(h, NVS_KEY, &mode);
nvs_close(h);
}
return (device_mode_t)mode;
}
void device_mode_set(device_mode_t mode) {
nvs_handle_t h;
if (nvs_open(NVS_NAMESPACE, NVS_READWRITE, &h) == ESP_OK) {
nvs_set_i32(h, NVS_KEY, (int32_t)mode);
// 设置切换标志,重启后用于按键抑制
nvs_set_u8(h, NVS_KEY_SWITCHED, 1);
nvs_commit(h);
nvs_close(h);
}
ESP_LOGI(TAG, "模式切换为 %s即将重启...",
mode == DEVICE_MODE_BADGE ? "吧唧" : "AI");
vTaskDelay(pdMS_TO_TICKS(500));
esp_restart();
}
bool device_mode_is_badge(void) {
return device_mode_get() == DEVICE_MODE_BADGE;
}
bool device_mode_in_switch_suppress(void) {
// 首次调用时从NVS读取切换标志并清除
if (!switch_flag_checked) {
switch_flag_checked = true;
nvs_handle_t h;
uint8_t val = 0;
if (nvs_open(NVS_NAMESPACE, NVS_READWRITE, &h) == ESP_OK) {
nvs_get_u8(h, NVS_KEY_SWITCHED, &val);
if (val) {
nvs_set_u8(h, NVS_KEY_SWITCHED, 0);
nvs_commit(h);
ESP_LOGI(TAG, "检测到模式切换重启启用2秒按键抑制");
}
nvs_close(h);
}
mode_was_switched = (val == 1);
}
if (!mode_was_switched) return false;
// 重启后2秒内抑制按键组合键释放产生的幽灵事件
return esp_timer_get_time() < 2000000;
}