From a8f8525bc0697376be2f754e3872df917d416a14 Mon Sep 17 00:00:00 2001 From: Xuan Son Nguyen Date: Tue, 27 Jan 2026 17:01:20 +0100 Subject: [PATCH] move isWakeupByPowerButton to HAL --- lib/hal/HalGPIO.cpp | 10 ++++++++++ lib/hal/HalGPIO.h | 3 +++ src/main.cpp | 12 +----------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/hal/HalGPIO.cpp b/lib/hal/HalGPIO.cpp index 6d2510a5..803efba0 100644 --- a/lib/hal/HalGPIO.cpp +++ b/lib/hal/HalGPIO.cpp @@ -43,3 +43,13 @@ bool HalGPIO::isUsbConnected() const { // U0RXD/GPIO20 reads HIGH when USB is connected return digitalRead(UART0_RXD) == HIGH; } + +bool HalGPIO::isWakeupByPowerButton() const { + const auto wakeupCause = esp_sleep_get_wakeup_cause(); + const auto resetReason = esp_reset_reason(); + if (isUsbConnected()) { + return wakeupCause == ESP_SLEEP_WAKEUP_GPIO; + } else { + return (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_POWERON); + } +} diff --git a/lib/hal/HalGPIO.h b/lib/hal/HalGPIO.h index 83244f5b..11ffb22e 100644 --- a/lib/hal/HalGPIO.h +++ b/lib/hal/HalGPIO.h @@ -47,6 +47,9 @@ class HalGPIO { // Check if USB is connected bool isUsbConnected() const; + // Check if wakeup was caused by power button press + bool isWakeupByPowerButton() const; + // Button indices static constexpr uint8_t BTN_BACK = 0; static constexpr uint8_t BTN_CONFIRM = 1; diff --git a/src/main.cpp b/src/main.cpp index 0637388b..2308f0a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -266,16 +266,6 @@ void setupDisplayAndFonts() { Serial.printf("[%lu] [ ] Fonts setup\n", millis()); } -bool isWakeupByPowerButton() { - const auto wakeupCause = esp_sleep_get_wakeup_cause(); - const auto resetReason = esp_reset_reason(); - if (gpio.isUsbConnected()) { - return wakeupCause == ESP_SLEEP_WAKEUP_GPIO; - } else { - return (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_POWERON); - } -} - void setup() { t1 = millis(); @@ -304,7 +294,7 @@ void setup() { SETTINGS.loadFromFile(); KOREADER_STORE.loadFromFile(); - if (isWakeupByPowerButton()) { + if (gpio.isWakeupByPowerButton()) { // For normal wakeups, verify power button press duration Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis()); verifyPowerButtonDuration();