feat: two-tier factory reset, WiFi fallback chain, SettingsWebServer

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>
This commit is contained in:
2026-06-30 09:52:17 +03:00
co-authored by Claude Sonnet 4.6
parent ee73d4d2c4
commit df8426fce6
5 changed files with 183 additions and 124 deletions
+64 -39
View File
@@ -14,8 +14,8 @@ 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);
// GPIO 0, Active LOW (pull-up), 50ms debounce, 5s medium press, 10s long press
_factoryResetButton.config = ButtonConfig(0, false, 50, 5000, 10000);
_instance = this;
}
@@ -40,6 +40,7 @@ bool InputManager::begin() {
_factoryResetButton.lastRawState = false;
_factoryResetButton.stateChangeTime = millis();
_factoryResetButton.pressStartTime = 0;
_factoryResetButton.mediumPressTriggered = false;
_factoryResetButton.longPressTriggered = false;
// Create FreeRTOS task for input polling
@@ -81,6 +82,16 @@ void InputManager::setFactoryResetPressCallback(ButtonCallback 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");
@@ -175,94 +186,108 @@ bool InputManager::readButtonState(const ButtonData& button) const {
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",
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)",
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.mediumPressTriggered = false;
button.longPressTriggered = false;
LOG_INFO(TAG, "InputManager: Button press confirmed on GPIO %d",
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
// Released before medium press threshold — short press
button.state = ButtonState::DEBOUNCING_RELEASE;
button.stateChangeTime = now;
LOG_INFO(TAG, "InputManager: Short press detected on GPIO %d (held for %lums)",
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 DETECTED on GPIO %d (held for %lums)",
LOG_WARNING(TAG, "InputManager: LONG PRESS on GPIO %d (held %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)",
LOG_INFO(TAG, "InputManager: Long press released on GPIO %d (held %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
// Re-pressed during release debounce
button.state = ButtonState::LONG_PRESS_PENDING;
LOG_DEBUG(TAG, "InputManager: Button re-pressed during release debounce on GPIO %d",
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();
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();
}
}
LOG_DEBUG(TAG, "InputManager: Button release confirmed on GPIO %d",
button.config.pin);
// 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;
}
// Store last raw state for edge detection
button.lastRawState = currentState;
}
+27 -12
View File
@@ -52,13 +52,16 @@ public:
uint8_t pin; // GPIO pin number
bool activeHigh; // true = active high, false = active low
uint32_t debounceMs; // Debounce time in milliseconds
uint32_t mediumPressMs; // Medium press threshold 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) {}
ButtonCallback onPress; // Callback for short press (released before medium threshold)
ButtonCallback onMediumPress; // Callback fired when medium threshold is crossed (while still held)
ButtonCallback onMediumPressRelease;// Callback fired on release between medium and long threshold
ButtonCallback onLongPress; // Callback fired when long threshold is crossed (while still held)
ButtonConfig(uint8_t p = 0, bool ah = false, uint32_t db = 50, uint32_t mp = 5000, uint32_t lp = 10000)
: pin(p), activeHigh(ah), debounceMs(db), mediumPressMs(mp), longPressMs(lp),
onPress(nullptr), onMediumPress(nullptr), onMediumPressRelease(nullptr), onLongPress(nullptr) {}
};
// ═══════════════════════════════════════════════════════════════════════════════
@@ -89,6 +92,16 @@ public:
*/
void setFactoryResetPressCallback(ButtonCallback callback);
/**
* @brief Set callback for factory reset button medium press (fired at 5s while still held)
*/
void setFactoryResetMediumPressCallback(ButtonCallback callback);
/**
* @brief Set callback for factory reset button medium press release (released between 5s and 10s)
*/
void setFactoryResetMediumPressReleaseCallback(ButtonCallback callback);
/**
* @brief Set callback for factory reset button long press
* @param callback Function to call on long press (10s)
@@ -126,22 +139,24 @@ private:
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_PRESS_PENDING, // Button held, waiting for medium/long press threshold
MEDIUM_PRESSED, // Medium press confirmed and triggered, still held
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 mediumPressTriggered; // Has medium press callback been fired?
bool longPressTriggered; // Has long press callback been fired?
ButtonData() : state(ButtonState::IDLE), lastRawState(false),
stateChangeTime(0), pressStartTime(0),
longPressTriggered(false) {}
ButtonData() : state(ButtonState::IDLE), lastRawState(false),
stateChangeTime(0), pressStartTime(0),
mediumPressTriggered(false), longPressTriggered(false) {}
};
// ═══════════════════════════════════════════════════════════════════════════════