feat: 按钮去抖+配网界面美化+水浸离线+LED独立任务+报警全拓展(人身优先)+有人才检测

This commit is contained in:
yangzhijia
2026-07-31 15:58:09 +08:00
parent 49e3abbb34
commit 69e927c5f0
16 changed files with 1352 additions and 549 deletions

View File

@@ -1,10 +1,13 @@
/**
* @file button_manager.h
* @brief 三按钮管理器 - 去抖 + 长按/短按识别
* @brief 三按钮管理器 - 双向去抖 + 长按/短按识别 + 超时自愈
*
* 按钮1 (GPIO14): 长按3秒 → 配网
* 按钮2 (GPIO15): 短按 → 解除报警
* 按钮3 (GPIO16): 短按 → 翻页
* 按钮1 (GPIO14): 长按1.5秒 → 配网
* 按钮2 (GPIO13): 短按 → 解除报警
* 按钮3 (GPIO46): 短按 → 翻页
*
* 去抖策略: 状态变化需持续稳定 BTN_DEBOUNCE_MS 才确认
* 自愈策略: 按钮按下超过 10 秒强制复位,防止卡死
*/
#ifndef BUTTON_MANAGER_H
@@ -13,6 +16,8 @@
#include <Arduino.h>
#include "config.h"
#define BTN_STUCK_TIMEOUT_MS 10000 // 按钮卡死超时 (10秒强制复位)
typedef void (*ButtonCallback)(void);
class ButtonManager {
@@ -31,17 +36,32 @@ public:
void onPageSwitch(ButtonCallback cb) { _cbPage = cb; }
private:
/**
* 按钮状态机 (基于稳定读数而非边沿)
*
* 状态转换:
* IDLE ──[raw稳定LOW×BTN_DEBOUNCE_MS]──→ PRESSED (记录pressTime)
* PRESSED ──[hold≥BTN_LONG_PRESS_MS]──→ 触发长按回调 (仅限hasLong)
* PRESSED ──[raw稳定HIGH×BTN_DEBOUNCE_MS]──→ 检查hold时长触发短按 → IDLE
* PRESSED ──[hold≥BTN_STUCK_TIMEOUT_MS]──→ 强制复位 → IDLE (防卡死)
*/
struct ButtonState {
uint8_t pin;
bool pressed; // 当前是否在按下状态
uint32_t pressTime; // 按下时刻 (ms)
bool longTriggered; // 防止长按时重复触发短按
bool stableState; // 当前确认的稳定状态 (true=按下)
bool rawState; // 上一次 raw 读值
uint32_t changeTime; // raw 状态变化时刻
uint32_t pressTime; // 稳定按下时刻 (ms)
bool longTriggered; // 长按已触发
bool shortPending; // 等待释放以判定短按
};
/** 处理单个按钮的状态机 */
/** 处理单个按钮 (基于稳定状态而非瞬时边沿) */
void _processButton(ButtonState& btn, bool hasShort, bool hasLong);
bool _readPin(uint8_t pin);
/** 强制复位单个按钮 */
void _resetButton(ButtonState& btn);
ButtonState _btnProvision;
ButtonState _btnDismiss;
ButtonState _btnPage;