mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2025-12-18 15:17:42 +03:00
Added settings class with de/serialization and whiteSleepScreen setting to control inverting the sleep screen
This commit is contained in:
parent
3262d8d2b7
commit
1c92c3f2bb
46
src/CrossPointSettings.cpp
Normal file
46
src/CrossPointSettings.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include "CrossPointSettings.h"
|
||||||
|
|
||||||
|
#include <HardwareSerial.h>
|
||||||
|
#include <SD.h>
|
||||||
|
#include <Serialization.h>
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
constexpr uint8_t SETTINGS_FILE_VERSION = 1;
|
||||||
|
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, whiteSleepScreen);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
serialization::readPod(inputFile, whiteSleepScreen);
|
||||||
|
|
||||||
|
inputFile.close();
|
||||||
|
Serial.printf("[%lu] [CPS] Settings loaded from file\n", millis());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
13
src/CrossPointSettings.h
Normal file
13
src/CrossPointSettings.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iosfwd>
|
||||||
|
|
||||||
|
class CrossPointSettings {
|
||||||
|
public:
|
||||||
|
// Sleep screen settings
|
||||||
|
bool whiteSleepScreen = false;
|
||||||
|
|
||||||
|
~CrossPointSettings() = default;
|
||||||
|
|
||||||
|
bool saveToFile() const;
|
||||||
|
bool loadFromFile();
|
||||||
|
};
|
||||||
@ -14,6 +14,7 @@
|
|||||||
#include <builtinFonts/ubuntu_bold_10.h>
|
#include <builtinFonts/ubuntu_bold_10.h>
|
||||||
|
|
||||||
#include "Battery.h"
|
#include "Battery.h"
|
||||||
|
#include "CrossPointSettings.h"
|
||||||
#include "CrossPointState.h"
|
#include "CrossPointState.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "screens/BootLogoScreen.h"
|
#include "screens/BootLogoScreen.h"
|
||||||
@ -41,6 +42,7 @@ InputManager inputManager;
|
|||||||
GfxRenderer renderer(einkDisplay);
|
GfxRenderer renderer(einkDisplay);
|
||||||
Screen* currentScreen;
|
Screen* currentScreen;
|
||||||
CrossPointState appState;
|
CrossPointState appState;
|
||||||
|
CrossPointSettings appSettings;
|
||||||
|
|
||||||
// Fonts
|
// Fonts
|
||||||
EpdFont bookerlyFont(&bookerly_2b);
|
EpdFont bookerlyFont(&bookerly_2b);
|
||||||
@ -131,7 +133,8 @@ void waitForPowerRelease() {
|
|||||||
// Enter deep sleep mode
|
// Enter deep sleep mode
|
||||||
void enterDeepSleep() {
|
void enterDeepSleep() {
|
||||||
exitScreen();
|
exitScreen();
|
||||||
enterNewScreen(new SleepScreen(renderer, inputManager));
|
appSettings.saveToFile();
|
||||||
|
enterNewScreen(new SleepScreen(renderer, inputManager, appSettings));
|
||||||
|
|
||||||
Serial.printf("[%lu] [ ] Power button released after a long press. Entering deep sleep.\n", millis());
|
Serial.printf("[%lu] [ ] Power button released after a long press. Entering deep sleep.\n", millis());
|
||||||
delay(1000); // Allow Serial buffer to empty and display to update
|
delay(1000); // Allow Serial buffer to empty and display to update
|
||||||
@ -199,6 +202,7 @@ void setup() {
|
|||||||
// SD Card Initialization
|
// SD Card Initialization
|
||||||
SD.begin(SD_SPI_CS, SPI, SPI_FQ);
|
SD.begin(SD_SPI_CS, SPI, SPI_FQ);
|
||||||
|
|
||||||
|
appSettings.loadFromFile();
|
||||||
appState.loadFromFile();
|
appState.loadFromFile();
|
||||||
if (!appState.openEpubPath.empty()) {
|
if (!appState.openEpubPath.empty()) {
|
||||||
auto epub = loadEpub(appState.openEpubPath);
|
auto epub = loadEpub(appState.openEpubPath);
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "images/CrossLarge.h"
|
#include "images/CrossLarge.h"
|
||||||
|
#include "CrossPointSettings.h"
|
||||||
|
|
||||||
void SleepScreen::onEnter() {
|
void SleepScreen::onEnter() {
|
||||||
const auto pageWidth = GfxRenderer::getScreenWidth();
|
const auto pageWidth = GfxRenderer::getScreenWidth();
|
||||||
@ -13,6 +14,11 @@ void SleepScreen::onEnter() {
|
|||||||
renderer.drawImage(CrossLarge, (pageWidth - 128) / 2, (pageHeight - 128) / 2, 128, 128);
|
renderer.drawImage(CrossLarge, (pageWidth - 128) / 2, (pageHeight - 128) / 2, 128, 128);
|
||||||
renderer.drawCenteredText(UI_FONT_ID, pageHeight / 2 + 70, "CrossPoint", true, BOLD);
|
renderer.drawCenteredText(UI_FONT_ID, pageHeight / 2 + 70, "CrossPoint", true, BOLD);
|
||||||
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight / 2 + 95, "SLEEPING");
|
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight / 2 + 95, "SLEEPING");
|
||||||
// renderer.invertScreen();
|
|
||||||
|
// Apply white screen if enabled in settings
|
||||||
|
if (!settings.whiteSleepScreen) {
|
||||||
|
renderer.invertScreen();
|
||||||
|
}
|
||||||
|
|
||||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Screen.h"
|
#include "Screen.h"
|
||||||
|
|
||||||
|
class CrossPointSettings;
|
||||||
|
|
||||||
class SleepScreen final : public Screen {
|
class SleepScreen final : public Screen {
|
||||||
public:
|
public:
|
||||||
explicit SleepScreen(GfxRenderer& renderer, InputManager& inputManager) : Screen(renderer, inputManager) {}
|
explicit SleepScreen(GfxRenderer& renderer, InputManager& inputManager, const CrossPointSettings& settings)
|
||||||
|
: Screen(renderer, inputManager), settings(settings) {}
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const CrossPointSettings& settings;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user