From c3db4c05035cc2a80a7ed58d45e019b443fe9c59 Mon Sep 17 00:00:00 2001 From: Jonas Diemer Date: Mon, 15 Dec 2025 12:49:35 +0100 Subject: [PATCH] Rework after feedback --- lib/Epub/Epub/Section.cpp | 2 +- src/CrossPointSettings.cpp | 24 +++++++++++++++++++----- src/CrossPointSettings.h | 14 +++++++------- src/main.cpp | 1 - src/screens/SettingsScreen.cpp | 3 ++- src/screens/SettingsScreen.h | 5 +++-- src/screens/SleepScreen.h | 5 +---- 7 files changed, 33 insertions(+), 21 deletions(-) diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index aa92b22..9c05208 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -9,7 +9,7 @@ #include "Page.h" #include "parsers/ChapterHtmlSlimParser.h" -constexpr uint8_t SECTION_FILE_VERSION = 4; +constexpr uint8_t SECTION_FILE_VERSION = 5; void Section::onPageComplete(std::unique_ptr page) { const auto filePath = cachePath + "/page_" + std::to_string(pageCount) + ".bin"; diff --git a/src/CrossPointSettings.cpp b/src/CrossPointSettings.cpp index a740405..d5de7a9 100644 --- a/src/CrossPointSettings.cpp +++ b/src/CrossPointSettings.cpp @@ -4,24 +4,27 @@ #include #include +#include #include // Initialize the static instance CrossPointSettings CrossPointSettings::instance; constexpr uint8_t SETTINGS_FILE_VERSION = 1; +constexpr uint8_t SETTINGS_COUNT = 2; constexpr char SETTINGS_FILE[] = "/sd/.crosspoint/settings.bin"; bool CrossPointSettings::saveToFile() const { // Make sure the directory exists SD.mkdir("/.crosspoint"); - + std::ofstream outputFile(SETTINGS_FILE); serialization::writePod(outputFile, SETTINGS_FILE_VERSION); + serialization::writePod(outputFile, SETTINGS_COUNT); serialization::writePod(outputFile, whiteSleepScreen); serialization::writePod(outputFile, extraParagraphSpacing); outputFile.close(); - + Serial.printf("[%lu] [CPS] Settings saved to file\n", millis()); return true; } @@ -42,10 +45,21 @@ bool CrossPointSettings::loadFromFile() { return false; } - serialization::readPod(inputFile, whiteSleepScreen); - serialization::readPod(inputFile, extraParagraphSpacing); + uint8_t fileSettingsCount = 0; + serialization::readPod(inputFile, fileSettingsCount); + + // load settings that exist + switch (fileSettingsCount) { + case 1: + serialization::readPod(inputFile, whiteSleepScreen); + break; + case 2: + serialization::readPod(inputFile, whiteSleepScreen); + serialization::readPod(inputFile, extraParagraphSpacing); + break; + } inputFile.close(); Serial.printf("[%lu] [CPS] Settings loaded from file\n", millis()); return true; -} \ No newline at end of file +} diff --git a/src/CrossPointSettings.h b/src/CrossPointSettings.h index 8924121..9b8747d 100644 --- a/src/CrossPointSettings.h +++ b/src/CrossPointSettings.h @@ -5,21 +5,21 @@ class CrossPointSettings { private: // Private constructor for singleton CrossPointSettings() = default; - + // Static instance static CrossPointSettings instance; - + public: // Delete copy constructor and assignment CrossPointSettings(const CrossPointSettings&) = delete; CrossPointSettings& operator=(const CrossPointSettings&) = delete; - + // Sleep screen settings bool whiteSleepScreen = false; - + // Text rendering settings - bool extraParagraphSpacing = false; - + bool extraParagraphSpacing = true; + ~CrossPointSettings() = default; // Get singleton instance @@ -32,4 +32,4 @@ class CrossPointSettings { }; // Helper macro to access settings -#define SETTINGS CrossPointSettings::getInstance() \ No newline at end of file +#define SETTINGS CrossPointSettings::getInstance() diff --git a/src/main.cpp b/src/main.cpp index 22b0c74..7d9286b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -133,7 +133,6 @@ void waitForPowerRelease() { // Enter deep sleep mode void enterDeepSleep() { exitScreen(); - SETTINGS.saveToFile(); enterNewScreen(new SleepScreen(renderer, inputManager)); Serial.printf("[%lu] [ ] Power button released after a long press. Entering deep sleep.\n", millis()); diff --git a/src/screens/SettingsScreen.cpp b/src/screens/SettingsScreen.cpp index e06a81d..da744b9 100644 --- a/src/screens/SettingsScreen.cpp +++ b/src/screens/SettingsScreen.cpp @@ -6,6 +6,7 @@ #include "config.h" // Define the static settings list + const SettingInfo SettingsScreen::settingsList[SettingsScreen::settingsCount] = { {"White Sleep Screen", &CrossPointSettings::whiteSleepScreen}, {"Extra Paragraph Spacing", &CrossPointSettings::extraParagraphSpacing} @@ -134,7 +135,7 @@ void SettingsScreen::render() const { } // Draw help text - renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 40, + renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 30, "Press OK to toggle, BACK to save & exit"); // Always use standard refresh for settings screen diff --git a/src/screens/SettingsScreen.h b/src/screens/SettingsScreen.h index fd66de4..c7d6a5e 100644 --- a/src/screens/SettingsScreen.h +++ b/src/screens/SettingsScreen.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -13,7 +14,7 @@ class CrossPointSettings; // Structure to hold setting information struct SettingInfo { const char* name; // Display name of the setting - bool CrossPointSettings::* valuePtr; // Pointer to member in CrossPointSettings + uint8_t CrossPointSettings::* valuePtr; // Pointer to member in CrossPointSettings }; class SettingsScreen final : public Screen { @@ -22,7 +23,7 @@ class SettingsScreen final : public Screen { bool updateRequired = false; int selectedSettingIndex = 0; // Currently selected setting const std::function onGoHome; - + // Static settings list static constexpr int settingsCount = 2; // Number of settings static const SettingInfo settingsList[settingsCount]; diff --git a/src/screens/SleepScreen.h b/src/screens/SleepScreen.h index 918ac0c..b280087 100644 --- a/src/screens/SleepScreen.h +++ b/src/screens/SleepScreen.h @@ -1,11 +1,8 @@ #pragma once #include "Screen.h" -class CrossPointSettings; - class SleepScreen final : public Screen { public: - explicit SleepScreen(GfxRenderer& renderer, InputManager& inputManager) - : Screen(renderer, inputManager) {} + explicit SleepScreen(GfxRenderer& renderer, InputManager& inputManager) : Screen(renderer, inputManager) {} void onEnter() override; };