mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-06 07:37:37 +03:00
Refactor to use CrossPointState
This commit is contained in:
parent
396365c709
commit
0725e81500
@ -5,7 +5,7 @@
|
||||
#include <Serialization.h>
|
||||
|
||||
namespace {
|
||||
constexpr uint8_t STATE_FILE_VERSION = 1;
|
||||
constexpr uint8_t STATE_FILE_VERSION = 2;
|
||||
constexpr char STATE_FILE[] = "/.crosspoint/state.bin";
|
||||
} // namespace
|
||||
|
||||
@ -19,6 +19,7 @@ bool CrossPointState::saveToFile() const {
|
||||
|
||||
serialization::writePod(outputFile, STATE_FILE_VERSION);
|
||||
serialization::writeString(outputFile, openEpubPath);
|
||||
serialization::writePod(outputFile, lastSleepImage);
|
||||
outputFile.close();
|
||||
return true;
|
||||
}
|
||||
@ -31,13 +32,18 @@ bool CrossPointState::loadFromFile() {
|
||||
|
||||
uint8_t version;
|
||||
serialization::readPod(inputFile, version);
|
||||
if (version != STATE_FILE_VERSION) {
|
||||
if (version > STATE_FILE_VERSION) {
|
||||
Serial.printf("[%lu] [CPS] Deserialization failed: Unknown version %u\n", millis(), version);
|
||||
inputFile.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
serialization::readString(inputFile, openEpubPath);
|
||||
if (version >= 2) {
|
||||
serialization::readPod(inputFile, lastSleepImage);
|
||||
} else {
|
||||
lastSleepImage = 0;
|
||||
}
|
||||
|
||||
inputFile.close();
|
||||
return true;
|
||||
|
||||
@ -8,6 +8,7 @@ class CrossPointState {
|
||||
|
||||
public:
|
||||
std::string openEpubPath;
|
||||
uint8_t lastSleepImage;
|
||||
~CrossPointState() = default;
|
||||
|
||||
// Get singleton instance
|
||||
|
||||
@ -80,7 +80,13 @@ void SleepActivity::renderCustomSleepScreen() const {
|
||||
const auto numFiles = files.size();
|
||||
if (numFiles > 0) {
|
||||
// Generate a random number between 1 and numFiles
|
||||
const auto randomFileIndex = random(numFiles);
|
||||
auto randomFileIndex = random(numFiles);
|
||||
// If we picked the same image as last time, reroll
|
||||
while (numFiles > 1 && randomFileIndex == APP_STATE.lastSleepImage) {
|
||||
randomFileIndex = random(numFiles);
|
||||
}
|
||||
APP_STATE.lastSleepImage = randomFileIndex;
|
||||
APP_STATE.saveToFile();
|
||||
const auto filename = "/sleep/" + files[randomFileIndex];
|
||||
FsFile file;
|
||||
if (SdMan.openFileForRead("SLP", filename, file)) {
|
||||
|
||||
@ -303,6 +303,7 @@ void setup() {
|
||||
// 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.lastSleepImage = 0;
|
||||
APP_STATE.saveToFile();
|
||||
onGoToReader(path);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user