From 85502b417e793225eed6fd32a37403c4bd3fb4b2 Mon Sep 17 00:00:00 2001 From: Dave Allie Date: Fri, 5 Dec 2025 17:47:23 +1100 Subject: [PATCH] Speedup boot by not waiting for Serial --- src/Input.cpp | 14 +++++++------- src/Input.h | 2 +- src/main.cpp | 32 +++++++++++++++----------------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/Input.cpp b/src/Input.cpp index 7b2bee6..235416d 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -29,15 +29,15 @@ Button getPressedButton() { return NONE; } -Input getInput(const bool skipWait) { +Input getInput(const long maxHoldMs) { const Button button = getPressedButton(); if (button == NONE) return {NONE, 0}; - if (skipWait) { - return {button, 0}; - } - const auto start = millis(); - while (getPressedButton() == button) delay(50); - return {button, millis() - start}; + unsigned long held = 0; + while (getPressedButton() == button && (maxHoldMs < 0 || held < maxHoldMs)) { + delay(50); + held = millis() - start; + } + return {button, held}; } diff --git a/src/Input.h b/src/Input.h index f245b11..da24385 100644 --- a/src/Input.h +++ b/src/Input.h @@ -25,4 +25,4 @@ struct Input { void setupInputPinModes(); Button getPressedButton(); -Input getInput(bool skipWait = false); +Input getInput(long maxHoldMs = -1); diff --git a/src/main.cpp b/src/main.cpp index 7af0a8a..1ad9a17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -67,10 +67,10 @@ void enterNewScreen(Screen* screen) { void verifyWakeupLongPress() { // Give the user up to 1000ms to start holding the power button, and must hold for POWER_BUTTON_WAKEUP_MS const auto start = millis(); - auto input = getInput(); + auto input = getInput(POWER_BUTTON_WAKEUP_MS); while (input.button != POWER && millis() - start < 1000) { delay(50); - input = getInput(); + input = getInput(POWER_BUTTON_WAKEUP_MS); } if (input.button != POWER || input.pressTime < POWER_BUTTON_WAKEUP_MS) { @@ -81,6 +81,12 @@ void verifyWakeupLongPress() { } } +void waitForNoButton() { + while (getInput().button != NONE) { + delay(50); + } +} + // Enter deep sleep mode void enterDeepSleep() { enterNewScreen(new FullScreenMessageScreen(renderer, "Sleeping", true, false, true)); @@ -97,20 +103,6 @@ void enterDeepSleep() { esp_deep_sleep_start(); } -void setupSerial() { - Serial.begin(115200); - // Wait for serial monitor - const unsigned long start = millis(); - while (!Serial && (millis() - start) < 3000) { - delay(10); - } - - if (Serial) { - // delay for monitor to start reading - delay(1000); - } -} - void onGoHome(); void onSelectEpubFile(const std::string& path) { enterNewScreen(new FullScreenMessageScreen(renderer, "Loading...")); @@ -129,7 +121,8 @@ void onGoHome() { enterNewScreen(new FileSelectionScreen(renderer, onSelectEpubF void setup() { setupInputPinModes(); verifyWakeupLongPress(); - setupSerial(); + Serial.begin(115200); + Serial.println("Booting..."); // Initialize pins pinMode(BAT_GPIO0, INPUT); @@ -154,11 +147,16 @@ void setup() { Epub* epub = loadEpub(appState->openEpubPath); if (epub) { enterNewScreen(new EpubReaderScreen(renderer, epub, onGoHome)); + // Ensure we're not still holding the power button before leaving setup + waitForNoButton(); return; } } enterNewScreen(new FileSelectionScreen(renderer, onSelectEpubFile)); + + // Ensure we're not still holding the power button before leaving setup + waitForNoButton(); } void loop() {