Files
health-wellness-device-2/include/button_manager.h
yangzhijia 49e3abbb34 feat: init
2026-07-22 11:27:37 +08:00

55 lines
1.4 KiB
C++

/**
* @file button_manager.h
* @brief 三按钮管理器 - 去抖 + 长按/短按识别
*
* 按钮1 (GPIO14): 长按3秒 → 配网
* 按钮2 (GPIO15): 短按 → 解除报警
* 按钮3 (GPIO16): 短按 → 翻页
*/
#ifndef BUTTON_MANAGER_H
#define BUTTON_MANAGER_H
#include <Arduino.h>
#include "config.h"
typedef void (*ButtonCallback)(void);
class ButtonManager {
public:
ButtonManager();
/** 初始化三个按钮引脚 */
void begin();
/** 主循环轮询 (每 10ms 调用) */
void loop();
/** 注册回调 */
void onProvisionLongPress(ButtonCallback cb) { _cbProvision = cb; }
void onAlarmDismiss(ButtonCallback cb) { _cbDismiss = cb; }
void onPageSwitch(ButtonCallback cb) { _cbPage = cb; }
private:
struct ButtonState {
uint8_t pin;
bool pressed; // 当前是否在按下状态
uint32_t pressTime; // 按下时刻 (ms)
bool longTriggered; // 防止长按时重复触发短按
};
/** 处理单个按钮的状态机 */
void _processButton(ButtonState& btn, bool hasShort, bool hasLong);
bool _readPin(uint8_t pin);
ButtonState _btnProvision;
ButtonState _btnDismiss;
ButtonState _btnPage;
ButtonCallback _cbProvision;
ButtonCallback _cbDismiss;
ButtonCallback _cbPage;
};
#endif // BUTTON_MANAGER_H