fix: Init lastSleepImage (edge case) (#1360)
## 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 **_
This commit is contained in:
@@ -63,7 +63,7 @@ bool CrossPointState::loadFromBinaryFile() {
|
|||||||
if (version >= 2) {
|
if (version >= 2) {
|
||||||
serialization::readPod(inputFile, lastSleepImage);
|
serialization::readPod(inputFile, lastSleepImage);
|
||||||
} else {
|
} else {
|
||||||
lastSleepImage = 0;
|
lastSleepImage = UINT8_MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (version >= 3) {
|
if (version >= 3) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <cstdint>
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -8,7 +9,7 @@ class CrossPointState {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
std::string openEpubPath;
|
std::string openEpubPath;
|
||||||
uint8_t lastSleepImage;
|
uint8_t lastSleepImage = UINT8_MAX; // UINT8_MAX = unset sentinel
|
||||||
uint8_t readerActivityLoadCount = 0;
|
uint8_t readerActivityLoadCount = 0;
|
||||||
bool lastSleepFromReader = false;
|
bool lastSleepFromReader = false;
|
||||||
~CrossPointState() = default;
|
~CrossPointState() = default;
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ bool JsonSettingsIO::loadState(CrossPointState& s, const char* json) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.openEpubPath = doc["openEpubPath"] | std::string("");
|
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.readerActivityLoadCount = doc["readerActivityLoadCount"] | (uint8_t)0;
|
||||||
s.lastSleepFromReader = doc["lastSleepFromReader"] | false;
|
s.lastSleepFromReader = doc["lastSleepFromReader"] | false;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ void SleepActivity::renderCustomSleepScreen() const {
|
|||||||
// Generate a random number between 1 and numFiles
|
// Generate a random number between 1 and numFiles
|
||||||
auto randomFileIndex = random(numFiles);
|
auto randomFileIndex = random(numFiles);
|
||||||
// If we picked the same image as last time, reroll
|
// 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);
|
randomFileIndex = random(numFiles);
|
||||||
}
|
}
|
||||||
APP_STATE.lastSleepImage = randomFileIndex;
|
APP_STATE.lastSleepImage = randomFileIndex;
|
||||||
|
|||||||
Reference in New Issue
Block a user