mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2025-12-19 15:47:40 +03:00
## Summary
* This PR drastically reshapes the structure of the codebase, moving
from the concept of "Screens" to "Activities", restructing the files and
setting up the concept of subactivities.
* This should help with keep the main file clean and containing all
functional logic in the relevant activity.
* CrossPointState is now also a global singleton which should help with
accessing it from within activities.
## Additional Context
* This is probably going to be a bit disruptive for people with open
PRs, sorry 😞
68 lines
1.9 KiB
C++
68 lines
1.9 KiB
C++
#include "CrossPointSettings.h"
|
|
|
|
#include <HardwareSerial.h>
|
|
#include <SD.h>
|
|
#include <Serialization.h>
|
|
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
|
|
// Initialize the static instance
|
|
CrossPointSettings CrossPointSettings::instance;
|
|
|
|
namespace {
|
|
constexpr uint8_t SETTINGS_FILE_VERSION = 1;
|
|
constexpr uint8_t SETTINGS_COUNT = 2;
|
|
constexpr char SETTINGS_FILE[] = "/sd/.crosspoint/settings.bin";
|
|
} // namespace
|
|
|
|
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;
|
|
}
|
|
|
|
bool CrossPointSettings::loadFromFile() {
|
|
if (!SD.exists(SETTINGS_FILE + 3)) { // +3 to skip "/sd" prefix
|
|
Serial.printf("[%lu] [CPS] Settings file does not exist, using defaults\n", millis());
|
|
return false;
|
|
}
|
|
|
|
std::ifstream inputFile(SETTINGS_FILE);
|
|
|
|
uint8_t version;
|
|
serialization::readPod(inputFile, version);
|
|
if (version != SETTINGS_FILE_VERSION) {
|
|
Serial.printf("[%lu] [CPS] Deserialization failed: Unknown version %u\n", millis(), version);
|
|
inputFile.close();
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
}
|