This commit is contained in:
Xuan-Son Nguyen 2026-01-22 12:18:07 +01:00 committed by GitHub
commit beeaf99b28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 6 deletions

View File

@ -54,6 +54,8 @@ class CrossPointSettings {
// Short power button press actions // Short power button press actions
enum SHORT_PWRBTN { IGNORE = 0, SLEEP = 1, PAGE_TURN = 2 }; enum SHORT_PWRBTN { IGNORE = 0, SLEEP = 1, PAGE_TURN = 2 };
static constexpr uint16_t SHORT_PRESS_DURATION_MS = 10;
static constexpr uint16_t LONG_PRESS_DURATION_MS = 400;
// Hide battery percentage // Hide battery percentage
enum HIDE_BATTERY_PERCENTAGE { HIDE_NEVER = 0, HIDE_READER = 1, HIDE_ALWAYS = 2 }; enum HIDE_BATTERY_PERCENTAGE { HIDE_NEVER = 0, HIDE_READER = 1, HIDE_ALWAYS = 2 };
@ -101,7 +103,7 @@ class CrossPointSettings {
static CrossPointSettings& getInstance() { return instance; } static CrossPointSettings& getInstance() { return instance; }
uint16_t getPowerButtonDuration() const { uint16_t getPowerButtonDuration() const {
return (shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::SLEEP) ? 10 : 400; return (shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::SLEEP) ? SHORT_PRESS_DURATION_MS : LONG_PRESS_DURATION_MS;
} }
int getReaderFontId() const; int getReaderFontId() const;

View File

@ -151,8 +151,15 @@ void enterNewActivity(Activity* activity) {
currentActivity->onEnter(); currentActivity->onEnter();
} }
// Verify long press on wake-up from deep sleep // Verify power button press duration on wake-up from deep sleep
void verifyWakeupLongPress() { // Pre-condition: isWakeupByPowerButton() == true
void verifyPowerButtonDuration() {
if (SETTINGS.getPowerButtonDuration() <= CrossPointSettings::SHORT_PRESS_DURATION_MS) {
// 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() // Give the user up to 1000ms to start holding the power button, and must hold for SETTINGS.getPowerButtonDuration()
const auto start = millis(); const auto start = millis();
bool abort = false; bool abort = false;
@ -165,6 +172,7 @@ void verifyWakeupLongPress() {
inputManager.update(); inputManager.update();
// Verify the user has actually pressed // Verify the user has actually pressed
// Needed because inputManager.isPressed() may take up to ~500ms to return the correct state
while (!inputManager.isPressed(InputManager::BTN_POWER) && millis() - start < 1000) { while (!inputManager.isPressed(InputManager::BTN_POWER) && millis() - start < 1000) {
delay(10); // only wait 10ms each iteration to not delay too much in case of short configured duration. delay(10); // only wait 10ms each iteration to not delay too much in case of short configured duration.
inputManager.update(); inputManager.update();
@ -288,6 +296,11 @@ bool isWakeupAfterFlashing() {
return isUsbConnected() && (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_UNKNOWN); return isUsbConnected() && (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_UNKNOWN);
} }
bool isWakeupByPowerButton() {
const auto wakeupCause = esp_sleep_get_wakeup_cause();
return wakeupCause == ESP_SLEEP_WAKEUP_GPIO;
}
void setup() { void setup() {
t1 = millis(); t1 = millis();
@ -322,9 +335,10 @@ void setup() {
SETTINGS.loadFromFile(); SETTINGS.loadFromFile();
KOREADER_STORE.loadFromFile(); KOREADER_STORE.loadFromFile();
if (!isWakeupAfterFlashing()) { if (isWakeupByPowerButton()) {
// For normal wakeups (not immediately after flashing), verify long press // For normal wakeups, verify power button press duration
verifyWakeupLongPress(); Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis());
verifyPowerButtonDuration();
} }
// First serial output only here to avoid timing inconsistencies for power button press duration verification // First serial output only here to avoid timing inconsistencies for power button press duration verification