···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);
2728 // Ensure that the power button has been released to avoid immediately turning back on if you're holding it
2829 while (inputMgr.isPressed(BTN_POWER)) {
2930 delay(50);
3031 inputMgr.update();
3132 }
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);
3433 // Enter Deep Sleep
3534 esp_deep_sleep_start();
3635}
···4544 return digitalRead(UART0_RXD) == HIGH;
4645}
47464848-HalGPIO::WakeupReason HalGPIO::getWakeupReason() const {
4949- const bool usbConnected = isUsbConnected();
4747+bool HalGPIO::isWakeupByPowerButton() const {
5048 const auto wakeupCause = esp_sleep_get_wakeup_cause();
5149 const auto resetReason = esp_reset_reason();
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;
5050+ if (isUsbConnected()) {
5151+ return wakeupCause == ESP_SLEEP_WAKEUP_GPIO;
5252+ } else {
5353+ return (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_POWERON);
5654 }
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;
6262- }
6363- return WakeupReason::Other;
6464-}5555+}
+2-3
lib/hal/HalGPIO.h
···4747 // Check if USB is connected
4848 bool isUsbConnected() const;
49495050- enum class WakeupReason { PowerButton, AfterFlash, AfterUSBPower, Other };
5151-5252- WakeupReason getWakeupReason() const;
5050+ // Check if wakeup was caused by power button press
5151+ bool isWakeupByPowerButton() const;
53525453 // Button indices
5554 static constexpr uint8_t BTN_BACK = 0;
+4-16
src/main.cpp
···294294 SETTINGS.loadFromFile();
295295 KOREADER_STORE.loadFromFile();
296296297297- 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;
297297+ 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();
313301 }
314302315303 // First serial output only here to avoid timing inconsistencies for power button press duration verification