Move all the preference in seperate h file

This commit is contained in:
Carsten Graf
2025-08-18 17:57:43 +02:00
parent a768783640
commit 55eb062d2c
3 changed files with 101 additions and 81 deletions

View File

@@ -15,3 +15,6 @@ v2.0
- ADD option point for location (read from online table and select the location via dropdown) DONE - ADD option point for location (read from online table and select the location via dropdown) DONE
- ADD option to enter a name, age DONE - ADD option to enter a name, age DONE
- ADD upload to a Online Database () DONE - ADD upload to a Online Database () DONE
- Redo Database Backend -> New SQL Server and deploy backend to edge functions?! Maybe host evrythin myself in a VM!

View File

@@ -22,90 +22,11 @@
#include <timesync.h> #include <timesync.h>
#include <webserverrouter.h> #include <webserverrouter.h>
#include <wificlass.h> #include <wificlass.h>
#include <preferencemanager.h>
const char *firmwareversion = "1.0.0"; // Version der Firmware const char *firmwareversion = "1.0.0"; // Version der Firmware
void saveButtonConfig() { // moved to preferencemanager.h
preferences.begin("buttons", false);
preferences.putBytes("config", &buttonConfigs, sizeof(buttonConfigs));
preferences.end();
}
void loadButtonConfig() {
preferences.begin("buttons", true);
size_t schLen = preferences.getBytesLength("config");
if (schLen == sizeof(buttonConfigs)) {
preferences.getBytes("config", &buttonConfigs, schLen);
}
preferences.end();
}
void saveBestTimes() {
preferences.begin("times", false);
preferences.putULong("best1", timerData.bestTime1);
preferences.putULong("best2", timerData.bestTime2);
preferences.end();
}
void loadBestTimes() {
preferences.begin("times", true);
timerData.bestTime1 = preferences.getULong("best1", 0);
timerData.bestTime2 = preferences.getULong("best2", 0);
preferences.end();
}
void saveSettings() {
preferences.begin("settings", false);
preferences.putULong("maxTime", maxTimeBeforeReset);
preferences.putULong("maxTimeDisplay", maxTimeDisplay);
preferences.putUInt("gamemode", gamemode);
preferences.end();
}
void loadSettings() {
preferences.begin("settings", true);
maxTimeBeforeReset = preferences.getULong("maxTime", 300000);
maxTimeDisplay = preferences.getULong("maxTimeDisplay", 20000);
gamemode = preferences.getUInt("gamemode", 0);
preferences.end();
}
void saveWifiSettings() {
preferences.begin("wifi", false);
preferences.putString("ssid", ssidSTA);
preferences.putString("password", passwordSTA);
preferences.end();
delay(500); // Warte 2 Sekunden, bevor der Neustart erfolgt
ESP.restart(); // Neustart des ESP32
}
void loadLocationSettings() {
preferences.begin("location", true);
masterlocation = preferences.getString("location", "");
preferences.end();
}
void saveLocationSettings() {
preferences.begin("location", false);
preferences.putString("location", masterlocation);
preferences.end();
}
void loadWifiSettings() {
preferences.begin("wifi", true);
String ssid = preferences.getString("ssid", "");
String password = preferences.getString("password", "");
ssidSTA = strdup(ssid.c_str());
passwordSTA = strdup(password.c_str());
preferences.end();
}
int checkLicence() {
loadLicenceFromPrefs();
String id = getUniqueDeviceID();
int tier = getLicenseTier(id, licence); // licence = stored or entered key
return tier;
}

96
src/preferencemanager.h Normal file
View File

@@ -0,0 +1,96 @@
#pragma once
#include <Arduino.h>
#include <Preferences.h>
#include <master.h>
#include <licenceing.h>
// Persist and load button configuration
void saveButtonConfig() {
preferences.begin("buttons", false);
preferences.putBytes("config", &buttonConfigs, sizeof(buttonConfigs));
preferences.end();
}
void loadButtonConfig() {
preferences.begin("buttons", true);
size_t schLen = preferences.getBytesLength("config");
if (schLen == sizeof(buttonConfigs)) {
preferences.getBytes("config", &buttonConfigs, schLen);
}
preferences.end();
}
// Persist and load best times
void saveBestTimes() {
preferences.begin("times", false);
preferences.putULong("best1", timerData.bestTime1);
preferences.putULong("best2", timerData.bestTime2);
preferences.end();
}
void loadBestTimes() {
preferences.begin("times", true);
timerData.bestTime1 = preferences.getULong("best1", 0);
timerData.bestTime2 = preferences.getULong("best2", 0);
preferences.end();
}
// Persist and load general settings
void saveSettings() {
preferences.begin("settings", false);
preferences.putULong("maxTime", maxTimeBeforeReset);
preferences.putULong("maxTimeDisplay", maxTimeDisplay);
preferences.putUInt("gamemode", gamemode);
preferences.end();
}
void loadSettings() {
preferences.begin("settings", true);
maxTimeBeforeReset = preferences.getULong("maxTime", 300000);
maxTimeDisplay = preferences.getULong("maxTimeDisplay", 20000);
gamemode = preferences.getUInt("gamemode", 0);
preferences.end();
}
// Persist and load WiFi settings
void saveWifiSettings() {
preferences.begin("wifi", false);
preferences.putString("ssid", ssidSTA);
preferences.putString("password", passwordSTA);
preferences.end();
delay(500);
ESP.restart();
}
void loadWifiSettings() {
preferences.begin("wifi", true);
String ssid = preferences.getString("ssid", "");
String password = preferences.getString("password", "");
ssidSTA = strdup(ssid.c_str());
passwordSTA = strdup(password.c_str());
preferences.end();
}
// Persist and load location settings
void loadLocationSettings() {
preferences.begin("location", true);
masterlocation = preferences.getString("location", "");
preferences.end();
}
void saveLocationSettings() {
preferences.begin("location", false);
preferences.putString("location", masterlocation);
preferences.end();
}
// Licence helper
int checkLicence() {
loadLicenceFromPrefs();
String id = getUniqueDeviceID();
int tier = getLicenseTier(id, licence);
return tier;
}