197 lines
10 KiB
C++
197 lines
10 KiB
C++
/*
|
|
* ═══════════════════════════════════════════════════════════════════════════════════
|
|
* INPUTMANAGER.HPP - Button and Input Handling System
|
|
* ═══════════════════════════════════════════════════════════════════════════════════
|
|
*
|
|
* 🎛️ INPUT HANDLING FOR VESPER 🎛️
|
|
*
|
|
* This class manages all physical input handling including button presses,
|
|
* long presses, and debouncing logic. It provides clean event-based callbacks
|
|
* for different input actions.
|
|
*
|
|
* 🏗️ ARCHITECTURE:
|
|
* • Non-blocking button state management
|
|
* • Software debouncing with configurable timing
|
|
* • Long press detection
|
|
* • Event-driven callbacks for actions
|
|
* • Easy expansion for multiple inputs
|
|
*
|
|
* 🔘 CURRENT INPUTS:
|
|
* • GPIO 0: Factory Reset Button (Long Press = 10s)
|
|
*
|
|
* ⚙️ FEATURES:
|
|
* • Debounce filtering (default 50ms)
|
|
* • Long press detection (configurable, default 10s)
|
|
* • Active-low button handling (pull-up enabled)
|
|
* • Non-blocking state machine
|
|
*
|
|
* 📋 VERSION: 1.0
|
|
* 📅 DATE: 2025
|
|
* 👨💻 AUTHOR: Advanced Bell Systems
|
|
* ═══════════════════════════════════════════════════════════════════════════════════
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
#include <functional>
|
|
|
|
class InputManager {
|
|
public:
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
// CALLBACK TYPES
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
using ButtonCallback = std::function<void()>;
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
// BUTTON CONFIGURATION
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
struct ButtonConfig {
|
|
uint8_t pin; // GPIO pin number
|
|
bool activeHigh; // true = active high, false = active low
|
|
uint32_t debounceMs; // Debounce time in milliseconds
|
|
uint32_t longPressMs; // Long press threshold in milliseconds
|
|
ButtonCallback onPress; // Callback for normal press
|
|
ButtonCallback onLongPress; // Callback for long press
|
|
|
|
ButtonConfig(uint8_t p = 0, bool ah = false, uint32_t db = 50, uint32_t lp = 10000)
|
|
: pin(p), activeHigh(ah), debounceMs(db), longPressMs(lp),
|
|
onPress(nullptr), onLongPress(nullptr) {}
|
|
};
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
// CONSTRUCTOR & INITIALIZATION
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
InputManager();
|
|
~InputManager();
|
|
|
|
/**
|
|
* @brief Initialize the InputManager and start the input task
|
|
* @return true if initialization successful
|
|
*/
|
|
bool begin();
|
|
|
|
/**
|
|
* @brief Stop the input task and cleanup
|
|
*/
|
|
void end();
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
// CALLBACK REGISTRATION
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
/**
|
|
* @brief Set callback for factory reset button press
|
|
* @param callback Function to call on short press
|
|
*/
|
|
void setFactoryResetPressCallback(ButtonCallback callback);
|
|
|
|
/**
|
|
* @brief Set callback for factory reset button long press
|
|
* @param callback Function to call on long press (10s)
|
|
*/
|
|
void setFactoryResetLongPressCallback(ButtonCallback callback);
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
// STATUS METHODS
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
/**
|
|
* @brief Check if factory reset button is currently pressed
|
|
* @return true if button is pressed
|
|
*/
|
|
bool isFactoryResetPressed() const;
|
|
|
|
/**
|
|
* @brief Get how long the factory reset button has been pressed
|
|
* @return Duration in milliseconds (0 if not pressed)
|
|
*/
|
|
uint32_t getFactoryResetPressDuration() const;
|
|
|
|
/**
|
|
* @brief Check if InputManager is healthy and functioning
|
|
* @return true if all inputs are properly configured
|
|
*/
|
|
bool isHealthy() const;
|
|
|
|
private:
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
// BUTTON STATE TRACKING
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
enum class ButtonState {
|
|
IDLE, // Button not pressed
|
|
DEBOUNCING_PRESS, // Waiting for debounce on press
|
|
PRESSED, // Button confirmed pressed (short)
|
|
LONG_PRESS_PENDING, // Button held, waiting for long press threshold
|
|
LONG_PRESSED, // Long press confirmed and triggered
|
|
DEBOUNCING_RELEASE // Waiting for debounce on release
|
|
};
|
|
|
|
struct ButtonData {
|
|
ButtonConfig config;
|
|
ButtonState state;
|
|
bool lastRawState; // Last raw reading from pin
|
|
uint32_t stateChangeTime; // When state last changed
|
|
uint32_t pressStartTime; // When button was first pressed
|
|
bool longPressTriggered; // Has long press callback been fired?
|
|
|
|
ButtonData() : state(ButtonState::IDLE), lastRawState(false),
|
|
stateChangeTime(0), pressStartTime(0),
|
|
longPressTriggered(false) {}
|
|
};
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
// MEMBER VARIABLES
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
ButtonData _factoryResetButton;
|
|
bool _initialized;
|
|
|
|
// FreeRTOS task management
|
|
TaskHandle_t _inputTaskHandle;
|
|
static constexpr uint32_t INPUT_TASK_STACK_SIZE = 4096;
|
|
static constexpr UBaseType_t INPUT_TASK_PRIORITY = 2;
|
|
static constexpr uint32_t INPUT_POLL_RATE_MS = 10; // Poll every 10ms
|
|
|
|
// Static instance for task callback
|
|
static InputManager* _instance;
|
|
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
// PRIVATE METHODS
|
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
/**
|
|
* @brief Configure a single button pin
|
|
* @param config Button configuration
|
|
*/
|
|
void configureButton(const ButtonConfig& config);
|
|
|
|
/**
|
|
* @brief Update state of a single button
|
|
* @param button Button data to update
|
|
*/
|
|
void updateButton(ButtonData& button);
|
|
|
|
/**
|
|
* @brief Read current state of button (handles active high/low)
|
|
* @param button Button to read
|
|
* @return true if button is currently pressed
|
|
*/
|
|
bool readButtonState(const ButtonData& button) const;
|
|
|
|
/**
|
|
* @brief Static task function for FreeRTOS
|
|
* @param parameter Pointer to InputManager instance
|
|
*/
|
|
static void inputTaskFunction(void* parameter);
|
|
|
|
/**
|
|
* @brief Internal update method called by task
|
|
*/
|
|
void update();
|
|
};
|