docs: add per-subsystem README files
Add inline documentation for all src/ subsystems covering purpose, responsibilities, public API, and usage notes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
# InputManager
|
||||
|
||||
## Summary
|
||||
|
||||
InputManager handles all physical button inputs on the Vesper hardware. It implements a non-blocking state machine with software debouncing and long-press detection, firing registered callbacks on press or long-press events. Currently it manages one input: the factory reset button on GPIO 0. The subsystem runs a dedicated FreeRTOS task that polls GPIO state at 10 ms intervals, keeping input handling entirely off the main loop and isolated from time-critical subsystems.
|
||||
|
||||
---
|
||||
|
||||
## Public API
|
||||
|
||||
| Method | Description |
|
||||
|---|---|
|
||||
| `bool begin()` | Configures GPIO pins and starts the polling task. Returns `false` if task creation fails. |
|
||||
| `void end()` | Deletes the polling task and marks the subsystem uninitialized. |
|
||||
| `void setFactoryResetPressCallback(ButtonCallback cb)` | Registers a callback fired on a confirmed short press of the factory reset button (released before the long-press threshold). |
|
||||
| `void setFactoryResetLongPressCallback(ButtonCallback cb)` | Registers a callback fired when the factory reset button is held for 10 seconds. |
|
||||
| `bool isFactoryResetPressed() const` | Returns `true` if the factory reset button is in any pressed state (debouncing, held, or long-pressed). |
|
||||
| `uint32_t getFactoryResetPressDuration() const` | Returns how long the factory reset button has been held, in milliseconds. Returns 0 if not pressed. |
|
||||
| `bool isHealthy() const` | Returns `true` if `begin()` completed successfully and the task is running. |
|
||||
|
||||
`ButtonCallback` is `std::function<void()>`.
|
||||
|
||||
---
|
||||
|
||||
## Initialization
|
||||
|
||||
InputManager is instantiated as a global in `main.cpp` and initialized near the top of `setup()`, immediately after `configManager.begin()`:
|
||||
|
||||
```cpp
|
||||
inputManager.begin();
|
||||
inputManager.setFactoryResetLongPressCallback(handleFactoryReset);
|
||||
```
|
||||
|
||||
`handleFactoryReset` calls `configManager.resetAllToDefaults()` then `ESP.restart()`, so `ConfigManager` must be initialized before the callback is registered. No other subsystem dependencies are required for `begin()` itself.
|
||||
|
||||
The subsystem does not need to be initialized before networking, communication, or any other subsystem. Its only constraint is that `ConfigManager` is ready if the long-press callback touches it.
|
||||
|
||||
---
|
||||
|
||||
## Dependencies
|
||||
|
||||
| Dependency | Reason |
|
||||
|---|---|
|
||||
| Arduino framework (`Arduino.h`) | `pinMode`, `digitalRead`, `millis` |
|
||||
| FreeRTOS (`xTaskCreate`, `vTaskDelayUntil`) | Polling task lifecycle |
|
||||
| `Logging/Logging.hpp` | `LOG_*` macros |
|
||||
| `ConfigManager` | Indirect — required by the `handleFactoryReset` callback registered in `main.cpp`, not by InputManager itself |
|
||||
|
||||
No external libraries, I2C peripherals, SD card, or network stack are required.
|
||||
|
||||
---
|
||||
|
||||
## Gotchas / Notes
|
||||
|
||||
**Singleton pattern.** The constructor assigns `this` to the static `InputManager::_instance` pointer. Only one instance should exist at a time. Constructing a second instance will overwrite the pointer, which is harmless in practice but indicates a design assumption of a single global instance.
|
||||
|
||||
**Short-press fires on release.** The `onPress` callback is invoked after the button is released and the release debounce passes, not on the initial press. The `onLongPress` callback fires immediately when the 10-second threshold is reached, while the button is still held.
|
||||
|
||||
**Long press suppresses short press.** If the long-press threshold is crossed, `onPress` is never called for that press cycle. The two callbacks are mutually exclusive per press.
|
||||
|
||||
**Callbacks execute in the polling task context.** Both `onPress` and `onLongPress` are called directly from the `InputTask` FreeRTOS task (stack: 4096 bytes, priority 2). The factory reset callback blocks this task while `configManager.resetAllToDefaults()` runs and then calls `ESP.restart()`, so blocking behavior is acceptable for that use case. Any future callbacks should be kept short or should defer work to a separate task.
|
||||
|
||||
**GPIO 0 is the ESP32 boot strapping pin.** Holding GPIO 0 low during reset enters the ESP32 bootloader. This is intentional for factory reset but means the button must not be pressed during power-on or OTA reboot if the bootloader is not desired.
|
||||
|
||||
**No ISR — polling only.** Input detection is purely poll-based at 10 ms intervals. The debounce window is 50 ms (5 poll cycles), which is sufficient to filter typical contact bounce. There is no interrupt-driven wakeup from deep sleep.
|
||||
|
||||
**Not thread-safe for callback registration.** `setFactoryResetPressCallback` and `setFactoryResetLongPressCallback` write directly to the `ButtonConfig` struct that the polling task reads. Register callbacks before calling `begin()`, or accept that a race is possible if registering after the task starts.
|
||||
Reference in New Issue
Block a user