diff --git a/lib/hal/HalGPIO.cpp b/lib/hal/HalGPIO.cpp index b150dc8c..67367cd1 100644 --- a/lib/hal/HalGPIO.cpp +++ b/lib/hal/HalGPIO.cpp @@ -225,29 +225,17 @@ unsigned long HalGPIO::getHeldTime() const { return inputMgr.getHeldTime(); } unsigned long HalGPIO::getPowerButtonHeldTime() const { return inputMgr.getPowerButtonHeldTime(); } -void HalGPIO::startDeepSleep() { - // Ensure that the power button has been released to avoid immediately turning back on if you're holding it - while (inputMgr.isPressed(BTN_POWER)) { - delay(50); - inputMgr.update(); - } - // Arm the wakeup trigger *after* the button is released - esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW); - // Enter Deep Sleep - esp_deep_sleep_start(); -} - -void HalGPIO::verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPressAllowed) { +bool HalGPIO::verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPressAllowed) { if (shortPressAllowed) { // Fast path - no duration check needed - return; + return true; } // TODO: Intermittent edge case remains: a single tap followed by another single tap // can still power on the device. Tighten wake debounce/state handling here. - // Calibrate: subtract boot time already elapsed, assuming button held since boot - const uint16_t calibration = millis(); - const uint16_t calibratedDuration = (calibration < requiredDurationMs) ? (requiredDurationMs - calibration) : 1; + // Calibrate: subtract boot time already elapsed, assuming button held since boot. + const unsigned long calibration = millis(); + const unsigned long calibratedDuration = (calibration < requiredDurationMs) ? (requiredDurationMs - calibration) : 1; const auto start = millis(); inputMgr.update(); @@ -262,11 +250,12 @@ void HalGPIO::verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPre inputMgr.update(); } while (inputMgr.isPressed(BTN_POWER) && inputMgr.getPowerButtonHeldTime() < calibratedDuration); if (inputMgr.getPowerButtonHeldTime() < calibratedDuration) { - startDeepSleep(); + return false; } } else { - startDeepSleep(); + return false; } + return true; } bool HalGPIO::isUsbConnected() const { diff --git a/lib/hal/HalGPIO.h b/lib/hal/HalGPIO.h index 94e9bcd7..9456dcb8 100644 --- a/lib/hal/HalGPIO.h +++ b/lib/hal/HalGPIO.h @@ -72,13 +72,10 @@ class HalGPIO { unsigned long getHeldTime() const; unsigned long getPowerButtonHeldTime() const; - // Setup wake up GPIO and enter deep sleep - void startDeepSleep(); - // Verify power button was held long enough after wakeup. - // If verification fails, enters deep sleep and does not return. + // Returns true if verification succeeded, false if device should return to sleep. // Should only be called when wakeup reason is PowerButton. - void verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPressAllowed); + bool verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPressAllowed); // Check if USB is connected bool isUsbConnected() const; diff --git a/src/main.cpp b/src/main.cpp index 13ba129b..36a960e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -159,49 +159,6 @@ void silentRestartToReader() { ESP.restart(); } -// Verify power button press duration on wake-up from deep sleep -// Pre-condition: isWakeupByPowerButton() == true -void verifyPowerButtonDuration() { - if (SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::SLEEP) { - // Fast path for short press - // Needed because inputManager.isPressed() may take up to ~500ms to return the correct state - return; - } - - // Give the user up to 1000ms to start holding the power button, and must hold for SETTINGS.getPowerButtonDuration() - const auto start = millis(); - bool abort = false; - // Subtract the current time, because inputManager only starts counting the HeldTime from the first update() - // This way, we remove the time we already took to reach here from the duration, - // assuming the button was held until now from millis()==0 (i.e. device start time). - const uint16_t calibration = start; - const uint16_t calibratedPressDuration = - (calibration < SETTINGS.getPowerButtonDuration()) ? SETTINGS.getPowerButtonDuration() - calibration : 1; - - gpio.update(); - // Needed because inputManager.isPressed() may take up to ~500ms to return the correct state - while (!gpio.isPressed(HalGPIO::BTN_POWER) && millis() - start < 1000) { - delay(10); // only wait 10ms each iteration to not delay too much in case of short configured duration. - gpio.update(); - } - - t2 = millis(); - if (gpio.isPressed(HalGPIO::BTN_POWER)) { - do { - delay(10); - gpio.update(); - } while (gpio.isPressed(HalGPIO::BTN_POWER) && gpio.getPowerButtonHeldTime() < calibratedPressDuration); - abort = gpio.getPowerButtonHeldTime() < calibratedPressDuration; - } else { - abort = true; - } - - if (abort) { - // Button released too early. Returning to sleep. - // IMPORTANT: Re-arm the wakeup trigger before sleeping again - powerManager.startDeepSleep(gpio); - } -} void waitForPowerRelease() { gpio.update(); while (gpio.isPressed(HalGPIO::BTN_POWER)) { @@ -357,8 +314,10 @@ void setup() { switch (wakeupReason) { case HalGPIO::WakeupReason::PowerButton: LOG_DBG("MAIN", "Verifying power button press duration"); - gpio.verifyPowerButtonWakeup(SETTINGS.getPowerButtonDuration(), - SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::SLEEP); + if (!gpio.verifyPowerButtonWakeup(SETTINGS.getPowerButtonDuration(), + SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::SLEEP)) { + powerManager.startDeepSleep(gpio); + } break; case HalGPIO::WakeupReason::AfterUSBPower: // If USB power caused a cold boot, go back to sleep