···2424unsigned long HalGPIO::getHeldTime() const { return inputMgr.getHeldTime(); }
25252626void HalGPIO::startDeepSleep() {
2727- esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
2827 // Ensure that the power button has been released to avoid immediately turning back on if you're holding it
2928 while (inputMgr.isPressed(BTN_POWER)) {
3029 delay(50);
3130 inputMgr.update();
3231 }
3232+ // Arm the wakeup trigger *after* the button is released
3333+ esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
3334 // Enter Deep Sleep
3435 esp_deep_sleep_start();
3536}
···4445 return digitalRead(UART0_RXD) == HIGH;
4546}
46474747-bool HalGPIO::isWakeupByPowerButton() const {
4848+HalGPIO::WakeupReason HalGPIO::getWakeupReason() const {
4949+ const bool usbConnected = isUsbConnected();
4850 const auto wakeupCause = esp_sleep_get_wakeup_cause();
4951 const auto resetReason = esp_reset_reason();
5050- if (isUsbConnected()) {
5151- return wakeupCause == ESP_SLEEP_WAKEUP_GPIO;
5252- } else {
5353- return (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_POWERON);
5252+5353+ if ((wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_POWERON && !usbConnected) ||
5454+ (wakeupCause == ESP_SLEEP_WAKEUP_GPIO && resetReason == ESP_RST_DEEPSLEEP && usbConnected)) {
5555+ return WakeupReason::PowerButton;
5656+ }
5757+ if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_UNKNOWN && usbConnected) {
5858+ return WakeupReason::AfterFlash;
5959+ }
6060+ if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_POWERON && usbConnected) {
6161+ return WakeupReason::AfterUSBPower;
5462 }
5555-}
6363+ return WakeupReason::Other;
6464+}
+3-2
lib/hal/HalGPIO.h
···4747 // Check if USB is connected
4848 bool isUsbConnected() const;
49495050- // Check if wakeup was caused by power button press
5151- bool isWakeupByPowerButton() const;
5050+ enum class WakeupReason { PowerButton, AfterFlash, AfterUSBPower, Other };
5151+5252+ WakeupReason getWakeupReason() const;
52535354 // Button indices
5455 static constexpr uint8_t BTN_BACK = 0;
+16-4
src/main.cpp
···294294 SETTINGS.loadFromFile();
295295 KOREADER_STORE.loadFromFile();
296296297297- if (gpio.isWakeupByPowerButton()) {
298298- // For normal wakeups, verify power button press duration
299299- Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis());
300300- verifyPowerButtonDuration();
297297+ switch (gpio.getWakeupReason()) {
298298+ case HalGPIO::WakeupReason::PowerButton:
299299+ // For normal wakeups, verify power button press duration
300300+ Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis());
301301+ verifyPowerButtonDuration();
302302+ break;
303303+ case HalGPIO::WakeupReason::AfterUSBPower:
304304+ // If USB power caused a cold boot, go back to sleep
305305+ Serial.printf("[%lu] [ ] Wakeup reason: After USB Power\n", millis());
306306+ gpio.startDeepSleep();
307307+ break;
308308+ case HalGPIO::WakeupReason::AfterFlash:
309309+ // After flashing, just proceed to boot
310310+ case HalGPIO::WakeupReason::Other:
311311+ default:
312312+ break;
301313 }
302314303315 // First serial output only here to avoid timing inconsistencies for power button press duration verification