`armor/` 是它的 Rust 改写版,这一份留着当参考:ADC 阈值的 2000 就是从它标定出来的 (`.env.example` 里那句 "same as the reference firmware" 指的就是它),而且它的 README 记着实测设备与 MAC,换硬件时是有用的现场记录。 只收了 20 个源文件。它自带的 `.gitignore` 已经排掉 `build/`(211 MB 的 ESP-IDF 构建产物) 与生成的 `sdkconfig`,所以仓库里不会多出这些东西。 两处顺带处理: - `reference/armor/.git` 是一个**空的**仓库壳(`git init` 过,零提交、零 remote、零 ref), 而外层 git 正因为它的存在而拒绝索引 `reference/` 下的任何文件 (`error: 'reference/armor/' does not have a commit checked out`)。它被挪到 `desktop/.dev/backup/reference-armor-git`(可逆,且该目录已被忽略),不是删除。 - 它的 README 指着 `../armor-platformio-backup/`,那份备份并不存在。改成明说"没有进仓库", 免得以后有人照着找一个不存在的目录。 另外把 `/docs/` 加进 .gitignore:那是工作区里另起的一个 Astro 文档站,与本仓库无关。
79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
/**
|
|
* @brief Button gesture detector, ported from OneButton 2.6.1.
|
|
*
|
|
* Only the parts the firmware actually uses are kept (single click and long
|
|
* press start); the state machine, the debounce rule and the default timings
|
|
* are a line-by-line port of `OneButton::_fsm()` from
|
|
* https://github.com/mathertel/OneButton (BSD-3-Clause) so gesture behaviour
|
|
* matches the original Arduino build exactly.
|
|
*
|
|
* The unused OneButton features (double click, multi click, press, long press
|
|
* stop/during, idle) were never attached in the original firmware and are
|
|
* therefore not reproduced here.
|
|
*/
|
|
class Button {
|
|
public:
|
|
using callback_t = void (*)();
|
|
|
|
/**
|
|
* @param gpio GPIO the button is attached to.
|
|
* @param active_low true when pressing connects the pin to GND
|
|
* (the original used `OneButton button(9, true)`).
|
|
*/
|
|
Button(int gpio, bool active_low);
|
|
|
|
void attachClick(callback_t cb);
|
|
void attachLongPressStart(callback_t cb);
|
|
|
|
/** Milliseconds before a click is considered stable (OneButton setClickMs). */
|
|
void setClickMs(uint32_t ms);
|
|
|
|
/** Milliseconds before the press counts as a long press (setPressMs). */
|
|
void setPressMs(uint32_t ms);
|
|
|
|
/** Poll the pin and advance the state machine. Call periodically (10 ms). */
|
|
void tick();
|
|
|
|
private:
|
|
enum state_t {
|
|
OCS_INIT = 0,
|
|
OCS_DOWN = 1,
|
|
OCS_UP = 2,
|
|
OCS_COUNT = 3,
|
|
OCS_PRESS = 6,
|
|
OCS_PRESSEND = 7,
|
|
};
|
|
|
|
bool debounce(bool value);
|
|
void fsm(bool active_level);
|
|
void reset();
|
|
|
|
int _pin;
|
|
bool _active_low;
|
|
|
|
state_t _state = OCS_INIT;
|
|
int _nClicks = 0;
|
|
|
|
// Debounce state (OneButton: _debounce_ms defaults to 50 ms).
|
|
int _debounce_ms = 50;
|
|
bool debouncedLevel = false;
|
|
bool _lastDebounceLevel = false;
|
|
unsigned long _lastDebounceTime = 0;
|
|
unsigned long now = 0;
|
|
|
|
unsigned long _startTime = 0;
|
|
|
|
// `setClickMs(50)` / `setPressMs(500)` are applied by the application;
|
|
// these are the OneButton defaults.
|
|
unsigned int _click_ms = 400;
|
|
unsigned int _press_ms = 800;
|
|
int _maxClicks = 1;
|
|
|
|
callback_t _clickFunc = nullptr;
|
|
callback_t _longPressStartFunc = nullptr;
|
|
};
|