Factory reset (F-015): - InputManager gains MEDIUM_PRESSED state with mediumPressMs=5000 - 5s hold: wipe WiFi credentials only, restart on release - 10s hold: full factory reset (WiFi wipe already done at 5s mark) - New callbacks: setFactoryResetMediumPressCallback, setFactoryResetMediumPressReleaseCallback WiFi fallback chain (F-016): - Connection now cycles: saved creds → hardcoded failsafe → portal - MAX_WIFI_FAILURES reduced 3→2 per stage - Portal now reboots after saving credentials (fixes port 80 conflict between WiFiManager and AsyncWebServer) - AP SSID sourced from netConfig.apSsid instead of hardcoded pattern - clearWiFiCredentials() added to Networking for 5s reset gesture SettingsWebServer (F-017): - Instantiated in main.cpp alongside CommunicationRouter - begin() called when network becomes ready (both WiFi paths) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
294 lines
13 KiB
C++
294 lines
13 KiB
C++
#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, 5s medium press, 10s long press
|
|
_factoryResetButton.config = ButtonConfig(0, false, 50, 5000, 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.mediumPressTriggered = false;
|
|
_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::setFactoryResetMediumPressCallback(ButtonCallback callback) {
|
|
_factoryResetButton.config.onMediumPress = callback;
|
|
LOG_DEBUG(TAG, "InputManager: Factory reset medium press callback registered");
|
|
}
|
|
|
|
void InputManager::setFactoryResetMediumPressReleaseCallback(ButtonCallback callback) {
|
|
_factoryResetButton.config.onMediumPressRelease = callback;
|
|
LOG_DEBUG(TAG, "InputManager: Factory reset medium press release 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:
|
|
if (currentState && !button.lastRawState) {
|
|
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:
|
|
if (!currentState) {
|
|
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) {
|
|
button.state = ButtonState::LONG_PRESS_PENDING;
|
|
button.pressStartTime = now;
|
|
button.mediumPressTriggered = false;
|
|
button.longPressTriggered = false;
|
|
LOG_INFO(TAG, "InputManager: Button press confirmed on GPIO %d",
|
|
button.config.pin);
|
|
}
|
|
break;
|
|
|
|
case ButtonState::LONG_PRESS_PENDING:
|
|
if (!currentState) {
|
|
// Released before medium press threshold — short press
|
|
button.state = ButtonState::DEBOUNCING_RELEASE;
|
|
button.stateChangeTime = now;
|
|
LOG_INFO(TAG, "InputManager: Short press on GPIO %d (held %lums)",
|
|
button.config.pin, now - button.pressStartTime);
|
|
} else if (now - button.pressStartTime >= button.config.mediumPressMs) {
|
|
// Medium press threshold reached
|
|
button.state = ButtonState::MEDIUM_PRESSED;
|
|
button.mediumPressTriggered = true;
|
|
LOG_WARNING(TAG, "InputManager: MEDIUM PRESS on GPIO %d (held %lums)",
|
|
button.config.pin, now - button.pressStartTime);
|
|
if (button.config.onMediumPress) {
|
|
button.config.onMediumPress();
|
|
}
|
|
}
|
|
break;
|
|
|
|
case ButtonState::MEDIUM_PRESSED:
|
|
if (!currentState) {
|
|
// Released between medium and long threshold
|
|
button.state = ButtonState::DEBOUNCING_RELEASE;
|
|
button.stateChangeTime = now;
|
|
LOG_INFO(TAG, "InputManager: Medium press released on GPIO %d (held %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 on GPIO %d (held %lums)",
|
|
button.config.pin, now - button.pressStartTime);
|
|
if (button.config.onLongPress) {
|
|
button.config.onLongPress();
|
|
}
|
|
}
|
|
break;
|
|
|
|
case ButtonState::LONG_PRESSED:
|
|
if (!currentState) {
|
|
button.state = ButtonState::DEBOUNCING_RELEASE;
|
|
button.stateChangeTime = now;
|
|
LOG_INFO(TAG, "InputManager: Long press released on GPIO %d (held %lums)",
|
|
button.config.pin, now - button.pressStartTime);
|
|
}
|
|
break;
|
|
|
|
case ButtonState::DEBOUNCING_RELEASE:
|
|
if (currentState) {
|
|
// Re-pressed during release debounce
|
|
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) {
|
|
button.state = ButtonState::IDLE;
|
|
|
|
if (!button.mediumPressTriggered && !button.longPressTriggered) {
|
|
// Short press
|
|
if (button.config.onPress) {
|
|
LOG_INFO(TAG, "InputManager: Short press callback for GPIO %d", button.config.pin);
|
|
button.config.onPress();
|
|
}
|
|
} else if (button.mediumPressTriggered && !button.longPressTriggered) {
|
|
// Released between medium and long threshold
|
|
if (button.config.onMediumPressRelease) {
|
|
LOG_INFO(TAG, "InputManager: Medium press release callback for GPIO %d", button.config.pin);
|
|
button.config.onMediumPressRelease();
|
|
}
|
|
}
|
|
// Long press: action already fired at threshold, no release callback needed
|
|
|
|
LOG_DEBUG(TAG, "InputManager: Button release confirmed on GPIO %d", button.config.pin);
|
|
}
|
|
break;
|
|
}
|
|
|
|
button.lastRawState = currentState;
|
|
}
|