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;
|
||||
}
|
||||
Reference in New Issue
Block a user