**What is the goal of this PR?** This PR introduces Internationalization (i18n) support, enabling users to switch the UI language dynamically. **What changes are included?** - Core Logic: Added I18n class (`lib/I18n/I18n.h/cpp`) to manage language state and string retrieval. - Data Structures: - `lib/I18n/I18nStrings.h/cpp`: Static string arrays for each supported language. - `lib/I18n/I18nKeys.h`: Enum definitions for type-safe string access. - `lib/I18n/translations.csv`: single source of truth. - Documentation: Added `docs/i18n.md` detailing the workflow for developers and translators. - New Settings activity: `src/activities/settings/LanguageSelectActivity.h/cpp` This implementation (building on concepts from #505) prioritizes performance and memory efficiency. The core approach is to store all localized strings for each language in dedicated arrays and access them via enums. This provides O(1) access with zero runtime overhead, and avoids the heap allocations, hashing, and collision handling required by `std::map` or `std::unordered_map`. The main trade-off is that enums and string arrays must remain perfectly synchronized—any mismatch would result in incorrect strings being displayed in the UI. To eliminate this risk, I added a Python script that automatically generates `I18nStrings.h/.cpp` and `I18nKeys.h` from a CSV file, which will serve as the single source of truth for all translations. The full design and workflow are documented in `docs/i18n.md`. - [x] Python script `generate_i18n.py` to auto-generate C++ files from CSV - [x] Populate translations.csv with initial translations. Currently available translations: English, Español, Français, Deutsch, Čeština, Português (Brasil), Русский, Svenska. Thanks, community! **Status:** EDIT: ready to be merged. As a proof of concept, the SPANISH strings currently mirror the English ones, but are fully uppercased. --- Did you use AI tools to help write this code? _**< PARTIALLY >**_ I used AI for the black work of replacing strings with I18n references across the project, and for generating the documentation. EDIT: also some help with merging changes from master. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: yeyeto2788 <juanernestobiondi@gmail.com>
468 lines
18 KiB
C++
468 lines
18 KiB
C++
#include <Arduino.h>
|
|
#include <Epub.h>
|
|
#include <GfxRenderer.h>
|
|
#include <HalDisplay.h>
|
|
#include <HalGPIO.h>
|
|
#include <HalPowerManager.h>
|
|
#include <HalStorage.h>
|
|
#include <I18n.h>
|
|
#include <Logging.h>
|
|
#include <SPI.h>
|
|
#include <builtinFonts/all.h>
|
|
|
|
#include <cstring>
|
|
|
|
#include "Battery.h"
|
|
#include "CrossPointSettings.h"
|
|
#include "CrossPointState.h"
|
|
#include "KOReaderCredentialStore.h"
|
|
#include "MappedInputManager.h"
|
|
#include "RecentBooksStore.h"
|
|
#include "activities/boot_sleep/BootActivity.h"
|
|
#include "activities/boot_sleep/SleepActivity.h"
|
|
#include "activities/browser/OpdsBookBrowserActivity.h"
|
|
#include "activities/home/HomeActivity.h"
|
|
#include "activities/home/MyLibraryActivity.h"
|
|
#include "activities/home/RecentBooksActivity.h"
|
|
#include "activities/network/CrossPointWebServerActivity.h"
|
|
#include "activities/reader/ReaderActivity.h"
|
|
#include "activities/settings/SettingsActivity.h"
|
|
#include "activities/util/FullScreenMessageActivity.h"
|
|
#include "components/UITheme.h"
|
|
#include "fontIds.h"
|
|
#include "util/ButtonNavigator.h"
|
|
|
|
HalDisplay display;
|
|
HalGPIO gpio;
|
|
HalPowerManager powerManager;
|
|
MappedInputManager mappedInputManager(gpio);
|
|
GfxRenderer renderer(display);
|
|
Activity* currentActivity;
|
|
|
|
// Fonts
|
|
#ifndef OMIT_BOOKERLY
|
|
EpdFont bookerly14RegularFont(&bookerly_14_regular);
|
|
EpdFont bookerly14BoldFont(&bookerly_14_bold);
|
|
EpdFont bookerly14ItalicFont(&bookerly_14_italic);
|
|
EpdFont bookerly14BoldItalicFont(&bookerly_14_bolditalic);
|
|
EpdFontFamily bookerly14FontFamily(&bookerly14RegularFont, &bookerly14BoldFont, &bookerly14ItalicFont,
|
|
&bookerly14BoldItalicFont);
|
|
#endif // OMIT_BOOKERLY
|
|
#ifndef OMIT_FONTS
|
|
#ifndef OMIT_BOOKERLY
|
|
EpdFont bookerly12RegularFont(&bookerly_12_regular);
|
|
EpdFont bookerly12BoldFont(&bookerly_12_bold);
|
|
EpdFont bookerly12ItalicFont(&bookerly_12_italic);
|
|
EpdFont bookerly12BoldItalicFont(&bookerly_12_bolditalic);
|
|
EpdFontFamily bookerly12FontFamily(&bookerly12RegularFont, &bookerly12BoldFont, &bookerly12ItalicFont,
|
|
&bookerly12BoldItalicFont);
|
|
EpdFont bookerly16RegularFont(&bookerly_16_regular);
|
|
EpdFont bookerly16BoldFont(&bookerly_16_bold);
|
|
EpdFont bookerly16ItalicFont(&bookerly_16_italic);
|
|
EpdFont bookerly16BoldItalicFont(&bookerly_16_bolditalic);
|
|
EpdFontFamily bookerly16FontFamily(&bookerly16RegularFont, &bookerly16BoldFont, &bookerly16ItalicFont,
|
|
&bookerly16BoldItalicFont);
|
|
EpdFont bookerly18RegularFont(&bookerly_18_regular);
|
|
EpdFont bookerly18BoldFont(&bookerly_18_bold);
|
|
EpdFont bookerly18ItalicFont(&bookerly_18_italic);
|
|
EpdFont bookerly18BoldItalicFont(&bookerly_18_bolditalic);
|
|
EpdFontFamily bookerly18FontFamily(&bookerly18RegularFont, &bookerly18BoldFont, &bookerly18ItalicFont,
|
|
&bookerly18BoldItalicFont);
|
|
#endif // OMIT_BOOKERLY
|
|
|
|
#ifndef OMIT_NOTOSANS
|
|
EpdFont notosans12RegularFont(¬osans_12_regular);
|
|
EpdFont notosans12BoldFont(¬osans_12_bold);
|
|
EpdFont notosans12ItalicFont(¬osans_12_italic);
|
|
EpdFont notosans12BoldItalicFont(¬osans_12_bolditalic);
|
|
EpdFontFamily notosans12FontFamily(¬osans12RegularFont, ¬osans12BoldFont, ¬osans12ItalicFont,
|
|
¬osans12BoldItalicFont);
|
|
EpdFont notosans14RegularFont(¬osans_14_regular);
|
|
EpdFont notosans14BoldFont(¬osans_14_bold);
|
|
EpdFont notosans14ItalicFont(¬osans_14_italic);
|
|
EpdFont notosans14BoldItalicFont(¬osans_14_bolditalic);
|
|
EpdFontFamily notosans14FontFamily(¬osans14RegularFont, ¬osans14BoldFont, ¬osans14ItalicFont,
|
|
¬osans14BoldItalicFont);
|
|
EpdFont notosans16RegularFont(¬osans_16_regular);
|
|
EpdFont notosans16BoldFont(¬osans_16_bold);
|
|
EpdFont notosans16ItalicFont(¬osans_16_italic);
|
|
EpdFont notosans16BoldItalicFont(¬osans_16_bolditalic);
|
|
EpdFontFamily notosans16FontFamily(¬osans16RegularFont, ¬osans16BoldFont, ¬osans16ItalicFont,
|
|
¬osans16BoldItalicFont);
|
|
EpdFont notosans18RegularFont(¬osans_18_regular);
|
|
EpdFont notosans18BoldFont(¬osans_18_bold);
|
|
EpdFont notosans18ItalicFont(¬osans_18_italic);
|
|
EpdFont notosans18BoldItalicFont(¬osans_18_bolditalic);
|
|
EpdFontFamily notosans18FontFamily(¬osans18RegularFont, ¬osans18BoldFont, ¬osans18ItalicFont,
|
|
¬osans18BoldItalicFont);
|
|
#endif // OMIT_NOTOSANS
|
|
|
|
#ifndef OMIT_OPENDYSLEXIC
|
|
EpdFont opendyslexic8RegularFont(&opendyslexic_8_regular);
|
|
EpdFont opendyslexic8BoldFont(&opendyslexic_8_bold);
|
|
EpdFont opendyslexic8ItalicFont(&opendyslexic_8_italic);
|
|
EpdFont opendyslexic8BoldItalicFont(&opendyslexic_8_bolditalic);
|
|
EpdFontFamily opendyslexic8FontFamily(&opendyslexic8RegularFont, &opendyslexic8BoldFont, &opendyslexic8ItalicFont,
|
|
&opendyslexic8BoldItalicFont);
|
|
EpdFont opendyslexic10RegularFont(&opendyslexic_10_regular);
|
|
EpdFont opendyslexic10BoldFont(&opendyslexic_10_bold);
|
|
EpdFont opendyslexic10ItalicFont(&opendyslexic_10_italic);
|
|
EpdFont opendyslexic10BoldItalicFont(&opendyslexic_10_bolditalic);
|
|
EpdFontFamily opendyslexic10FontFamily(&opendyslexic10RegularFont, &opendyslexic10BoldFont, &opendyslexic10ItalicFont,
|
|
&opendyslexic10BoldItalicFont);
|
|
EpdFont opendyslexic12RegularFont(&opendyslexic_12_regular);
|
|
EpdFont opendyslexic12BoldFont(&opendyslexic_12_bold);
|
|
EpdFont opendyslexic12ItalicFont(&opendyslexic_12_italic);
|
|
EpdFont opendyslexic12BoldItalicFont(&opendyslexic_12_bolditalic);
|
|
EpdFontFamily opendyslexic12FontFamily(&opendyslexic12RegularFont, &opendyslexic12BoldFont, &opendyslexic12ItalicFont,
|
|
&opendyslexic12BoldItalicFont);
|
|
EpdFont opendyslexic14RegularFont(&opendyslexic_14_regular);
|
|
EpdFont opendyslexic14BoldFont(&opendyslexic_14_bold);
|
|
EpdFont opendyslexic14ItalicFont(&opendyslexic_14_italic);
|
|
EpdFont opendyslexic14BoldItalicFont(&opendyslexic_14_bolditalic);
|
|
EpdFontFamily opendyslexic14FontFamily(&opendyslexic14RegularFont, &opendyslexic14BoldFont, &opendyslexic14ItalicFont,
|
|
&opendyslexic14BoldItalicFont);
|
|
#endif // OMIT_OPENDYSLEXIC
|
|
#endif // OMIT_FONTS
|
|
|
|
EpdFont smallFont(¬osans_8_regular);
|
|
EpdFontFamily smallFontFamily(&smallFont);
|
|
|
|
EpdFont ui10RegularFont(&ubuntu_10_regular);
|
|
EpdFont ui10BoldFont(&ubuntu_10_bold);
|
|
EpdFontFamily ui10FontFamily(&ui10RegularFont, &ui10BoldFont);
|
|
|
|
EpdFont ui12RegularFont(&ubuntu_12_regular);
|
|
EpdFont ui12BoldFont(&ubuntu_12_bold);
|
|
EpdFontFamily ui12FontFamily(&ui12RegularFont, &ui12BoldFont);
|
|
|
|
// measurement of power button press duration calibration value
|
|
unsigned long t1 = 0;
|
|
unsigned long t2 = 0;
|
|
|
|
void exitActivity() {
|
|
if (currentActivity) {
|
|
currentActivity->onExit();
|
|
delete currentActivity;
|
|
currentActivity = nullptr;
|
|
}
|
|
}
|
|
|
|
void enterNewActivity(Activity* activity) {
|
|
currentActivity = activity;
|
|
currentActivity->onEnter();
|
|
}
|
|
|
|
// Verify power button press duration on wake-up from deep sleep
|
|
// Pre-condition: isWakeupByPowerButton() == true
|
|
void verifyPowerButtonDuration() {
|
|
if (SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::SLEEP) {
|
|
// 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()
|
|
const auto start = millis();
|
|
bool abort = false;
|
|
// Subtract the current time, because inputManager only starts counting the HeldTime from the first update()
|
|
// This way, we remove the time we already took to reach here from the duration,
|
|
// assuming the button was held until now from millis()==0 (i.e. device start time).
|
|
const uint16_t calibration = start;
|
|
const uint16_t calibratedPressDuration =
|
|
(calibration < SETTINGS.getPowerButtonDuration()) ? SETTINGS.getPowerButtonDuration() - calibration : 1;
|
|
|
|
gpio.update();
|
|
// Needed because inputManager.isPressed() may take up to ~500ms to return the correct state
|
|
while (!gpio.isPressed(HalGPIO::BTN_POWER) && millis() - start < 1000) {
|
|
delay(10); // only wait 10ms each iteration to not delay too much in case of short configured duration.
|
|
gpio.update();
|
|
}
|
|
|
|
t2 = millis();
|
|
if (gpio.isPressed(HalGPIO::BTN_POWER)) {
|
|
do {
|
|
delay(10);
|
|
gpio.update();
|
|
} while (gpio.isPressed(HalGPIO::BTN_POWER) && gpio.getHeldTime() < calibratedPressDuration);
|
|
abort = gpio.getHeldTime() < calibratedPressDuration;
|
|
} else {
|
|
abort = true;
|
|
}
|
|
|
|
if (abort) {
|
|
// Button released too early. Returning to sleep.
|
|
// IMPORTANT: Re-arm the wakeup trigger before sleeping again
|
|
powerManager.startDeepSleep(gpio);
|
|
}
|
|
}
|
|
|
|
void waitForPowerRelease() {
|
|
gpio.update();
|
|
while (gpio.isPressed(HalGPIO::BTN_POWER)) {
|
|
delay(50);
|
|
gpio.update();
|
|
}
|
|
}
|
|
|
|
// Enter deep sleep mode
|
|
void enterDeepSleep() {
|
|
APP_STATE.lastSleepFromReader = currentActivity && currentActivity->isReaderActivity();
|
|
APP_STATE.saveToFile();
|
|
exitActivity();
|
|
enterNewActivity(new SleepActivity(renderer, mappedInputManager));
|
|
|
|
display.deepSleep();
|
|
LOG_DBG("MAIN", "Power button press calibration value: %lu ms", t2 - t1);
|
|
LOG_DBG("MAIN", "Entering deep sleep");
|
|
|
|
powerManager.startDeepSleep(gpio);
|
|
}
|
|
|
|
void onGoHome();
|
|
void onGoToMyLibraryWithPath(const std::string& path);
|
|
void onGoToRecentBooks();
|
|
void onGoToReader(const std::string& initialEpubPath) {
|
|
exitActivity();
|
|
enterNewActivity(
|
|
new ReaderActivity(renderer, mappedInputManager, initialEpubPath, onGoHome, onGoToMyLibraryWithPath));
|
|
}
|
|
|
|
void onGoToFileTransfer() {
|
|
exitActivity();
|
|
enterNewActivity(new CrossPointWebServerActivity(renderer, mappedInputManager, onGoHome));
|
|
}
|
|
|
|
void onGoToSettings() {
|
|
exitActivity();
|
|
enterNewActivity(new SettingsActivity(renderer, mappedInputManager, onGoHome));
|
|
}
|
|
|
|
void onGoToMyLibrary() {
|
|
exitActivity();
|
|
enterNewActivity(new MyLibraryActivity(renderer, mappedInputManager, onGoHome, onGoToReader));
|
|
}
|
|
|
|
void onGoToRecentBooks() {
|
|
exitActivity();
|
|
enterNewActivity(new RecentBooksActivity(renderer, mappedInputManager, onGoHome, onGoToReader));
|
|
}
|
|
|
|
void onGoToMyLibraryWithPath(const std::string& path) {
|
|
exitActivity();
|
|
enterNewActivity(new MyLibraryActivity(renderer, mappedInputManager, onGoHome, onGoToReader, path));
|
|
}
|
|
|
|
void onGoToBrowser() {
|
|
exitActivity();
|
|
enterNewActivity(new OpdsBookBrowserActivity(renderer, mappedInputManager, onGoHome));
|
|
}
|
|
|
|
void onGoHome() {
|
|
exitActivity();
|
|
enterNewActivity(new HomeActivity(renderer, mappedInputManager, onGoToReader, onGoToMyLibrary, onGoToRecentBooks,
|
|
onGoToSettings, onGoToFileTransfer, onGoToBrowser));
|
|
}
|
|
|
|
void setupDisplayAndFonts() {
|
|
display.begin();
|
|
renderer.begin();
|
|
LOG_DBG("MAIN", "Display initialized");
|
|
#ifndef OMIT_BOOKERLY
|
|
renderer.insertFont(BOOKERLY_14_FONT_ID, bookerly14FontFamily);
|
|
#endif
|
|
#ifndef OMIT_FONTS
|
|
#ifndef OMIT_BOOKERLY
|
|
renderer.insertFont(BOOKERLY_12_FONT_ID, bookerly12FontFamily);
|
|
renderer.insertFont(BOOKERLY_16_FONT_ID, bookerly16FontFamily);
|
|
renderer.insertFont(BOOKERLY_18_FONT_ID, bookerly18FontFamily);
|
|
#endif // OMIT_BOOKERLY
|
|
|
|
#ifndef OMIT_NOTOSANS
|
|
renderer.insertFont(NOTOSANS_12_FONT_ID, notosans12FontFamily);
|
|
renderer.insertFont(NOTOSANS_14_FONT_ID, notosans14FontFamily);
|
|
renderer.insertFont(NOTOSANS_16_FONT_ID, notosans16FontFamily);
|
|
renderer.insertFont(NOTOSANS_18_FONT_ID, notosans18FontFamily);
|
|
#endif // OMIT_NOTOSANS
|
|
#ifndef OMIT_OPENDYSLEXIC
|
|
renderer.insertFont(OPENDYSLEXIC_8_FONT_ID, opendyslexic8FontFamily);
|
|
renderer.insertFont(OPENDYSLEXIC_10_FONT_ID, opendyslexic10FontFamily);
|
|
renderer.insertFont(OPENDYSLEXIC_12_FONT_ID, opendyslexic12FontFamily);
|
|
renderer.insertFont(OPENDYSLEXIC_14_FONT_ID, opendyslexic14FontFamily);
|
|
#endif // OMIT_OPENDYSLEXIC
|
|
#endif // OMIT_FONTS
|
|
renderer.insertFont(UI_10_FONT_ID, ui10FontFamily);
|
|
renderer.insertFont(UI_12_FONT_ID, ui12FontFamily);
|
|
renderer.insertFont(SMALL_FONT_ID, smallFontFamily);
|
|
LOG_DBG("MAIN", "Fonts setup");
|
|
}
|
|
|
|
void setup() {
|
|
t1 = millis();
|
|
|
|
gpio.begin();
|
|
powerManager.begin();
|
|
|
|
// Only start serial if USB connected
|
|
if (gpio.isUsbConnected()) {
|
|
Serial.begin(115200);
|
|
// Wait up to 3 seconds for Serial to be ready to catch early logs
|
|
unsigned long start = millis();
|
|
while (!Serial && (millis() - start) < 3000) {
|
|
delay(10);
|
|
}
|
|
}
|
|
|
|
// SD Card Initialization
|
|
// We need 6 open files concurrently when parsing a new chapter
|
|
if (!Storage.begin()) {
|
|
LOG_ERR("MAIN", "SD card initialization failed");
|
|
setupDisplayAndFonts();
|
|
exitActivity();
|
|
enterNewActivity(new FullScreenMessageActivity(renderer, mappedInputManager, "SD card error", EpdFontFamily::BOLD));
|
|
return;
|
|
}
|
|
|
|
SETTINGS.loadFromFile();
|
|
I18N.loadSettings();
|
|
KOREADER_STORE.loadFromFile();
|
|
UITheme::getInstance().reload();
|
|
ButtonNavigator::setMappedInputManager(mappedInputManager);
|
|
|
|
switch (gpio.getWakeupReason()) {
|
|
case HalGPIO::WakeupReason::PowerButton:
|
|
// For normal wakeups, verify power button press duration
|
|
LOG_DBG("MAIN", "Verifying power button press duration");
|
|
verifyPowerButtonDuration();
|
|
break;
|
|
case HalGPIO::WakeupReason::AfterUSBPower:
|
|
// If USB power caused a cold boot, go back to sleep
|
|
LOG_DBG("MAIN", "Wakeup reason: After USB Power");
|
|
powerManager.startDeepSleep(gpio);
|
|
break;
|
|
case HalGPIO::WakeupReason::AfterFlash:
|
|
// After flashing, just proceed to boot
|
|
case HalGPIO::WakeupReason::Other:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
// First serial output only here to avoid timing inconsistencies for power button press duration verification
|
|
LOG_DBG("MAIN", "Starting CrossPoint version " CROSSPOINT_VERSION);
|
|
|
|
setupDisplayAndFonts();
|
|
|
|
exitActivity();
|
|
enterNewActivity(new BootActivity(renderer, mappedInputManager));
|
|
|
|
APP_STATE.loadFromFile();
|
|
RECENT_BOOKS.loadFromFile();
|
|
|
|
// Boot to home screen if no book is open, last sleep was not from reader, back button is held, or reader activity
|
|
// crashed (indicated by readerActivityLoadCount > 0)
|
|
if (APP_STATE.openEpubPath.empty() || !APP_STATE.lastSleepFromReader ||
|
|
mappedInputManager.isPressed(MappedInputManager::Button::Back) || APP_STATE.readerActivityLoadCount > 0) {
|
|
onGoHome();
|
|
} else {
|
|
// Clear app state to avoid getting into a boot loop if the epub doesn't load
|
|
const auto path = APP_STATE.openEpubPath;
|
|
APP_STATE.openEpubPath = "";
|
|
APP_STATE.readerActivityLoadCount++;
|
|
APP_STATE.saveToFile();
|
|
onGoToReader(path);
|
|
}
|
|
|
|
// Ensure we're not still holding the power button before leaving setup
|
|
waitForPowerRelease();
|
|
}
|
|
|
|
void loop() {
|
|
static unsigned long maxLoopDuration = 0;
|
|
const unsigned long loopStartTime = millis();
|
|
static unsigned long lastMemPrint = 0;
|
|
|
|
gpio.update();
|
|
|
|
renderer.setFadingFix(SETTINGS.fadingFix);
|
|
|
|
if (Serial && millis() - lastMemPrint >= 10000) {
|
|
LOG_INF("MEM", "Free: %d bytes, Total: %d bytes, Min Free: %d bytes", ESP.getFreeHeap(), ESP.getHeapSize(),
|
|
ESP.getMinFreeHeap());
|
|
lastMemPrint = millis();
|
|
}
|
|
|
|
// Handle incoming serial commands,
|
|
// nb: we use logSerial from logging to avoid deprecation warnings
|
|
if (logSerial.available() > 0) {
|
|
String line = logSerial.readStringUntil('\n');
|
|
if (line.startsWith("CMD:")) {
|
|
String cmd = line.substring(4);
|
|
cmd.trim();
|
|
if (cmd == "SCREENSHOT") {
|
|
logSerial.printf("SCREENSHOT_START:%d\n", HalDisplay::BUFFER_SIZE);
|
|
uint8_t* buf = display.getFrameBuffer();
|
|
logSerial.write(buf, HalDisplay::BUFFER_SIZE);
|
|
logSerial.printf("SCREENSHOT_END\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check for any user activity (button press or release) or active background work
|
|
static unsigned long lastActivityTime = millis();
|
|
if (gpio.wasAnyPressed() || gpio.wasAnyReleased() || (currentActivity && currentActivity->preventAutoSleep())) {
|
|
lastActivityTime = millis(); // Reset inactivity timer
|
|
powerManager.setPowerSaving(false); // Restore normal CPU frequency on user activity
|
|
}
|
|
|
|
const unsigned long sleepTimeoutMs = SETTINGS.getSleepTimeoutMs();
|
|
if (millis() - lastActivityTime >= sleepTimeoutMs) {
|
|
LOG_DBG("SLP", "Auto-sleep triggered after %lu ms of inactivity", sleepTimeoutMs);
|
|
enterDeepSleep();
|
|
// This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start
|
|
return;
|
|
}
|
|
|
|
if (gpio.isPressed(HalGPIO::BTN_POWER) && gpio.getHeldTime() > SETTINGS.getPowerButtonDuration()) {
|
|
enterDeepSleep();
|
|
// This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start
|
|
return;
|
|
}
|
|
|
|
const unsigned long activityStartTime = millis();
|
|
if (currentActivity) {
|
|
currentActivity->loop();
|
|
}
|
|
const unsigned long activityDuration = millis() - activityStartTime;
|
|
|
|
const unsigned long loopDuration = millis() - loopStartTime;
|
|
if (loopDuration > maxLoopDuration) {
|
|
maxLoopDuration = loopDuration;
|
|
if (maxLoopDuration > 50) {
|
|
LOG_DBG("LOOP", "New max loop duration: %lu ms (activity: %lu ms)", maxLoopDuration, activityDuration);
|
|
}
|
|
}
|
|
|
|
// Re-check preventAutoSleep: the activity may have changed during loop() above
|
|
// (e.g., HomeActivity transitioned to EpubReaderActivity with pending section work).
|
|
if (currentActivity && currentActivity->preventAutoSleep()) {
|
|
lastActivityTime = millis();
|
|
powerManager.setPowerSaving(false);
|
|
}
|
|
|
|
// Add delay at the end of the loop to prevent tight spinning
|
|
// When an activity requests skip loop delay (e.g., webserver running), use yield() for faster response
|
|
// Otherwise, use longer delay to save power
|
|
if (currentActivity && currentActivity->skipLoopDelay()) {
|
|
yield(); // Give FreeRTOS a chance to run tasks, but return immediately
|
|
} else {
|
|
if (millis() - lastActivityTime >= HalPowerManager::IDLE_POWER_SAVING_MS) {
|
|
// If we've been inactive for a while, increase the delay to save power
|
|
powerManager.setPowerSaving(true); // Lower CPU frequency after extended inactivity
|
|
delay(50);
|
|
} else {
|
|
// Short delay to prevent tight loop while still being responsive
|
|
delay(10);
|
|
}
|
|
}
|
|
}
|