codex init
This commit is contained in:
45
OpticalChannelTester/SettingsStore.cpp
Normal file
45
OpticalChannelTester/SettingsStore.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "SettingsStore.h"
|
||||
#include "Config.h"
|
||||
#include <Preferences.h>
|
||||
|
||||
namespace { constexpr uint16_t SETTINGS_VERSION = 1; constexpr char NAMESPACE[] = "opt-test"; }
|
||||
|
||||
void SettingsStore::defaults(Settings &s) const {
|
||||
s = {SETTINGS_VERSION, static_cast<uint8_t>(Role::SOLO), 0, 4, 1, 2, 3, 2, 2, 0};
|
||||
s.checksum = settingsChecksum(s);
|
||||
}
|
||||
|
||||
bool SettingsStore::valid(const Settings &s) const {
|
||||
return s.version == SETTINGS_VERSION && s.role <= static_cast<uint8_t>(Role::SLAVE) &&
|
||||
s.startIndex < countOf(START_FREQ_OPTIONS_HZ) && s.endIndex < countOf(END_FREQ_OPTIONS_HZ) &&
|
||||
s.stepIndex < countOf(STEP_OPTIONS_HZ) && s.accuracyIndex < countOf(ACCURACY_OPTIONS_PCT) &&
|
||||
s.timeIndex < countOf(TEST_TIME_OPTIONS_MS) && s.repeatIndex < countOf(REPEAT_OPTIONS) &&
|
||||
s.dutyIndex < countOf(DUTY_OPTIONS_PCT) && s.checksum == settingsChecksum(s) &&
|
||||
END_FREQ_OPTIONS_HZ[s.endIndex] > START_FREQ_OPTIONS_HZ[s.startIndex];
|
||||
}
|
||||
|
||||
bool SettingsStore::load(Settings &s) {
|
||||
Preferences prefs;
|
||||
if (!prefs.begin(NAMESPACE, true)) { defaults(s); return false; }
|
||||
const size_t got = prefs.getBytes("settings", &s, sizeof(s));
|
||||
prefs.end();
|
||||
if (got != sizeof(s) || !valid(s)) { defaults(s); return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SettingsStore::save(Settings &s) {
|
||||
s.version = SETTINGS_VERSION; s.checksum = settingsChecksum(s);
|
||||
if (!valid(s)) return false;
|
||||
Preferences prefs;
|
||||
if (!prefs.begin(NAMESPACE, false)) return false;
|
||||
const bool ok = prefs.putBytes("settings", &s, sizeof(s)) == sizeof(s);
|
||||
prefs.end(); return ok;
|
||||
}
|
||||
|
||||
TestParams SettingsStore::params(const Settings &s) const {
|
||||
return {START_FREQ_OPTIONS_HZ[s.startIndex], END_FREQ_OPTIONS_HZ[s.endIndex],
|
||||
STEP_OPTIONS_HZ[s.stepIndex], ACCURACY_OPTIONS_PCT[s.accuracyIndex],
|
||||
TEST_TIME_OPTIONS_MS[s.timeIndex], REPEAT_OPTIONS[s.repeatIndex],
|
||||
DUTY_OPTIONS_PCT[s.dutyIndex]};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user