From 79b54b3a7554024d29047fe71cb46bfb2cf60a4a Mon Sep 17 00:00:00 2001 From: jpirnay Date: Thu, 12 Mar 2026 00:37:13 +0100 Subject: [PATCH] fix: Init lastSleepImage (edge case) (#1360) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary * **What is the goal of this PR?** fix edge case for definition ## Additional Context If loadFromFile() returns false (no state file exists — first boot, or SD missing), lastSleepImage is never set and contains garbage. [SleepActivity.cpp:83] then uses it in a while comparison to avoid repeating the same image. The JSON path (doc["lastSleepImage"] | (uint8_t)0) handles it, but only if the file exists. --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _** NO **_ --- src/CrossPointState.cpp | 2 +- src/CrossPointState.h | 3 ++- src/JsonSettingsIO.cpp | 2 +- src/activities/boot_sleep/SleepActivity.cpp | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/CrossPointState.cpp b/src/CrossPointState.cpp index 232860cd..af8bdda5 100644 --- a/src/CrossPointState.cpp +++ b/src/CrossPointState.cpp @@ -63,7 +63,7 @@ bool CrossPointState::loadFromBinaryFile() { if (version >= 2) { serialization::readPod(inputFile, lastSleepImage); } else { - lastSleepImage = 0; + lastSleepImage = UINT8_MAX; } if (version >= 3) { diff --git a/src/CrossPointState.h b/src/CrossPointState.h index 35a9a871..1de89838 100644 --- a/src/CrossPointState.h +++ b/src/CrossPointState.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include @@ -8,7 +9,7 @@ class CrossPointState { public: std::string openEpubPath; - uint8_t lastSleepImage; + uint8_t lastSleepImage = UINT8_MAX; // UINT8_MAX = unset sentinel uint8_t readerActivityLoadCount = 0; bool lastSleepFromReader = false; ~CrossPointState() = default; diff --git a/src/JsonSettingsIO.cpp b/src/JsonSettingsIO.cpp index 4a563142..a0458043 100644 --- a/src/JsonSettingsIO.cpp +++ b/src/JsonSettingsIO.cpp @@ -87,7 +87,7 @@ bool JsonSettingsIO::loadState(CrossPointState& s, const char* json) { } s.openEpubPath = doc["openEpubPath"] | std::string(""); - s.lastSleepImage = doc["lastSleepImage"] | (uint8_t)0; + s.lastSleepImage = doc["lastSleepImage"] | (uint8_t)UINT8_MAX; s.readerActivityLoadCount = doc["readerActivityLoadCount"] | (uint8_t)0; s.lastSleepFromReader = doc["lastSleepFromReader"] | false; return true; diff --git a/src/activities/boot_sleep/SleepActivity.cpp b/src/activities/boot_sleep/SleepActivity.cpp index 0b2c1825..d6a566df 100644 --- a/src/activities/boot_sleep/SleepActivity.cpp +++ b/src/activities/boot_sleep/SleepActivity.cpp @@ -80,7 +80,7 @@ void SleepActivity::renderCustomSleepScreen() const { // Generate a random number between 1 and numFiles auto randomFileIndex = random(numFiles); // If we picked the same image as last time, reroll - while (numFiles > 1 && randomFileIndex == APP_STATE.lastSleepImage) { + while (numFiles > 1 && APP_STATE.lastSleepImage != UINT8_MAX && randomFileIndex == APP_STATE.lastSleepImage) { randomFileIndex = random(numFiles); } APP_STATE.lastSleepImage = randomFileIndex;