refactor: Fixed the file hierarchy and switched to platform.io
This commit is contained in:
@@ -0,0 +1,268 @@
|
||||
#include "InputManager.hpp"
|
||||
|
||||
#define TAG "InputManager"
|
||||
#include "../Logging/Logging.hpp"
|
||||
|
||||
// Static instance pointer
|
||||
InputManager* InputManager::_instance = nullptr;
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
// CONSTRUCTOR & DESTRUCTOR
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
InputManager::InputManager()
|
||||
: _initialized(false)
|
||||
, _inputTaskHandle(nullptr) {
|
||||
// Initialize factory reset button configuration
|
||||
// GPIO 0, Active LOW (pull-up), 50ms debounce, 10s long press
|
||||
_factoryResetButton.config = ButtonConfig(0, false, 50, 10000);
|
||||
|
||||
_instance = this;
|
||||
}
|
||||
|
||||
InputManager::~InputManager() {
|
||||
end();
|
||||
_instance = nullptr;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
// INITIALIZATION
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
bool InputManager::begin() {
|
||||
LOG_INFO(TAG, "InputManager: Initializing input handling system");
|
||||
|
||||
// Configure factory reset button
|
||||
configureButton(_factoryResetButton.config);
|
||||
|
||||
// Initialize button state
|
||||
_factoryResetButton.state = ButtonState::IDLE;
|
||||
_factoryResetButton.lastRawState = false;
|
||||
_factoryResetButton.stateChangeTime = millis();
|
||||
_factoryResetButton.pressStartTime = 0;
|
||||
_factoryResetButton.longPressTriggered = false;
|
||||
|
||||
// Create FreeRTOS task for input polling
|
||||
BaseType_t result = xTaskCreate(
|
||||
inputTaskFunction, // Task function
|
||||
"InputTask", // Task name
|
||||
INPUT_TASK_STACK_SIZE, // Stack size
|
||||
this, // Parameter (this instance)
|
||||
INPUT_TASK_PRIORITY, // Priority
|
||||
&_inputTaskHandle // Task handle
|
||||
);
|
||||
|
||||
if (result != pdPASS) {
|
||||
LOG_ERROR(TAG, "InputManager: Failed to create input task!");
|
||||
return false;
|
||||
}
|
||||
|
||||
_initialized = true;
|
||||
|
||||
LOG_INFO(TAG, "InputManager: Initialization complete - Factory Reset on GPIO 0 (Task running)");
|
||||
return true;
|
||||
}
|
||||
|
||||
void InputManager::end() {
|
||||
if (_inputTaskHandle != nullptr) {
|
||||
vTaskDelete(_inputTaskHandle);
|
||||
_inputTaskHandle = nullptr;
|
||||
LOG_INFO(TAG, "InputManager: Input task stopped");
|
||||
}
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
// CALLBACK REGISTRATION
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
void InputManager::setFactoryResetPressCallback(ButtonCallback callback) {
|
||||
_factoryResetButton.config.onPress = callback;
|
||||
LOG_DEBUG(TAG, "InputManager: Factory reset press callback registered");
|
||||
}
|
||||
|
||||
void InputManager::setFactoryResetLongPressCallback(ButtonCallback callback) {
|
||||
_factoryResetButton.config.onLongPress = callback;
|
||||
LOG_DEBUG(TAG, "InputManager: Factory reset long press callback registered");
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
// STATUS METHODS
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
bool InputManager::isFactoryResetPressed() const {
|
||||
return _factoryResetButton.state != ButtonState::IDLE;
|
||||
}
|
||||
|
||||
uint32_t InputManager::getFactoryResetPressDuration() const {
|
||||
if (_factoryResetButton.state == ButtonState::IDLE) {
|
||||
return 0;
|
||||
}
|
||||
return millis() - _factoryResetButton.pressStartTime;
|
||||
}
|
||||
|
||||
bool InputManager::isHealthy() const {
|
||||
if (!_initialized) {
|
||||
LOG_DEBUG(TAG, "InputManager: Unhealthy - not initialized");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Could add more health checks here if needed
|
||||
return true;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
// FREERTOS TASK
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
void InputManager::inputTaskFunction(void* parameter) {
|
||||
InputManager* manager = static_cast<InputManager*>(parameter);
|
||||
|
||||
LOG_INFO(TAG, "InputManager: Input task started (polling every %dms)", INPUT_POLL_RATE_MS);
|
||||
|
||||
TickType_t lastWakeTime = xTaskGetTickCount();
|
||||
const TickType_t pollInterval = pdMS_TO_TICKS(INPUT_POLL_RATE_MS);
|
||||
|
||||
while (true) {
|
||||
// Update button states
|
||||
manager->update();
|
||||
|
||||
// Wait for next poll interval
|
||||
vTaskDelayUntil(&lastWakeTime, pollInterval);
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
// UPDATE LOGIC (Called by task)
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
void InputManager::update() {
|
||||
if (!_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Update factory reset button
|
||||
updateButton(_factoryResetButton);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
// PRIVATE METHODS
|
||||
// ═══════════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
void InputManager::configureButton(const ButtonConfig& config) {
|
||||
// Configure pin as input with pull-up (for active-low buttons)
|
||||
if (config.activeHigh) {
|
||||
pinMode(config.pin, INPUT);
|
||||
} else {
|
||||
pinMode(config.pin, INPUT_PULLUP);
|
||||
}
|
||||
|
||||
LOG_DEBUG(TAG, "InputManager: Configured GPIO %d as input (%s)",
|
||||
config.pin, config.activeHigh ? "active-high" : "active-low");
|
||||
}
|
||||
|
||||
bool InputManager::readButtonState(const ButtonData& button) const {
|
||||
bool rawState = digitalRead(button.config.pin);
|
||||
|
||||
// Invert reading if active-low
|
||||
if (!button.config.activeHigh) {
|
||||
rawState = !rawState;
|
||||
}
|
||||
|
||||
return rawState;
|
||||
}
|
||||
|
||||
void InputManager::updateButton(ButtonData& button) {
|
||||
uint32_t now = millis();
|
||||
bool currentState = readButtonState(button);
|
||||
|
||||
// State machine for button handling
|
||||
switch (button.state) {
|
||||
case ButtonState::IDLE:
|
||||
// Waiting for button press
|
||||
if (currentState && !button.lastRawState) {
|
||||
// Button just pressed - start debouncing
|
||||
button.state = ButtonState::DEBOUNCING_PRESS;
|
||||
button.stateChangeTime = now;
|
||||
LOG_DEBUG(TAG, "InputManager: Button press detected on GPIO %d - debouncing",
|
||||
button.config.pin);
|
||||
}
|
||||
break;
|
||||
|
||||
case ButtonState::DEBOUNCING_PRESS:
|
||||
// Debouncing press
|
||||
if (!currentState) {
|
||||
// Button released during debounce - false trigger
|
||||
button.state = ButtonState::IDLE;
|
||||
LOG_DEBUG(TAG, "InputManager: False trigger on GPIO %d (released during debounce)",
|
||||
button.config.pin);
|
||||
} else if (now - button.stateChangeTime >= button.config.debounceMs) {
|
||||
// Debounce time passed - confirm press
|
||||
button.state = ButtonState::LONG_PRESS_PENDING;
|
||||
button.pressStartTime = now;
|
||||
button.longPressTriggered = false;
|
||||
LOG_INFO(TAG, "InputManager: Button press confirmed on GPIO %d",
|
||||
button.config.pin);
|
||||
}
|
||||
break;
|
||||
|
||||
case ButtonState::LONG_PRESS_PENDING:
|
||||
// Button is pressed, waiting to see if it's a long press
|
||||
if (!currentState) {
|
||||
// Button released before long press threshold - it's a short press
|
||||
button.state = ButtonState::DEBOUNCING_RELEASE;
|
||||
button.stateChangeTime = now;
|
||||
LOG_INFO(TAG, "InputManager: Short press detected on GPIO %d (held for %lums)",
|
||||
button.config.pin, now - button.pressStartTime);
|
||||
} else if (now - button.pressStartTime >= button.config.longPressMs) {
|
||||
// Long press threshold reached
|
||||
button.state = ButtonState::LONG_PRESSED;
|
||||
button.longPressTriggered = true;
|
||||
|
||||
LOG_WARNING(TAG, "InputManager: LONG PRESS DETECTED on GPIO %d (held for %lums)",
|
||||
button.config.pin, now - button.pressStartTime);
|
||||
|
||||
// Trigger long press callback
|
||||
if (button.config.onLongPress) {
|
||||
button.config.onLongPress();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ButtonState::LONG_PRESSED:
|
||||
// Long press has been triggered, waiting for release
|
||||
if (!currentState) {
|
||||
button.state = ButtonState::DEBOUNCING_RELEASE;
|
||||
button.stateChangeTime = now;
|
||||
LOG_INFO(TAG, "InputManager: Long press released on GPIO %d (total duration: %lums)",
|
||||
button.config.pin, now - button.pressStartTime);
|
||||
}
|
||||
break;
|
||||
|
||||
case ButtonState::DEBOUNCING_RELEASE:
|
||||
// Debouncing release
|
||||
if (currentState) {
|
||||
// Button pressed again during release debounce - go back to pressed state
|
||||
button.state = ButtonState::LONG_PRESS_PENDING;
|
||||
LOG_DEBUG(TAG, "InputManager: Button re-pressed during release debounce on GPIO %d",
|
||||
button.config.pin);
|
||||
} else if (now - button.stateChangeTime >= button.config.debounceMs) {
|
||||
// Debounce time passed - confirm release
|
||||
button.state = ButtonState::IDLE;
|
||||
|
||||
// If it was a short press (not long press), trigger the press callback
|
||||
if (!button.longPressTriggered && button.config.onPress) {
|
||||
LOG_INFO(TAG, "InputManager: Triggering press callback for GPIO %d",
|
||||
button.config.pin);
|
||||
button.config.onPress();
|
||||
}
|
||||
|
||||
LOG_DEBUG(TAG, "InputManager: Button release confirmed on GPIO %d",
|
||||
button.config.pin);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Store last raw state for edge detection
|
||||
button.lastRawState = currentState;
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* ═══════════════════════════════════════════════════════════════════════════════════
|
||||
* 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();
|
||||
};
|
||||
Reference in New Issue
Block a user