2021-02-24 14:40:24 -05:00
|
|
|
#include "Settings.h"
|
2021-04-03 22:08:51 -04:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2021-02-24 14:40:24 -05:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
2021-07-11 09:06:06 -04:00
|
|
|
Settings::Settings(Pinetime::Controllers::FS& fs) : fs {fs} {
|
2021-04-18 13:28:14 -04:00
|
|
|
}
|
2021-02-24 14:40:24 -05:00
|
|
|
|
|
|
|
void Settings::Init() {
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
// Load default settings from Flash
|
2021-07-11 09:06:06 -04:00
|
|
|
LoadSettingsFromFile();
|
2021-04-03 22:08:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::SaveSettings() {
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
// verify if is necessary to save
|
|
|
|
if (settingsChanged) {
|
2021-07-11 09:06:06 -04:00
|
|
|
SaveSettingsToFile();
|
2021-04-18 13:28:14 -04:00
|
|
|
}
|
|
|
|
settingsChanged = false;
|
2021-04-03 22:08:51 -04:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:06:06 -04:00
|
|
|
void Settings::LoadSettingsFromFile() {
|
|
|
|
SettingsData bufferSettings;
|
|
|
|
lfs_file_t settingsFile;
|
2021-04-18 13:28:14 -04:00
|
|
|
|
2021-07-11 09:06:06 -04:00
|
|
|
if ( fs.FileOpen(&settingsFile, "/settings.dat", LFS_O_RDWR | LFS_O_CREAT) != LFS_ERR_OK) {
|
|
|
|
return;
|
2021-04-18 13:28:14 -04:00
|
|
|
}
|
2021-07-11 09:06:06 -04:00
|
|
|
fs.FileRead(&settingsFile, reinterpret_cast<uint8_t*>(&bufferSettings), sizeof(settings));
|
|
|
|
fs.FileClose(&settingsFile);
|
|
|
|
if ( bufferSettings.version == settingsVersion ) {
|
|
|
|
settings = bufferSettings;
|
2021-04-18 13:28:14 -04:00
|
|
|
}
|
2021-04-03 22:08:51 -04:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:06:06 -04:00
|
|
|
void Settings::SaveSettingsToFile() {
|
|
|
|
lfs_file_t settingsFile;
|
2021-04-18 13:28:14 -04:00
|
|
|
|
2021-07-11 09:06:06 -04:00
|
|
|
if ( fs.FileOpen(&settingsFile, "/settings.dat", LFS_O_RDWR | LFS_O_CREAT) != LFS_ERR_OK) {
|
|
|
|
return;
|
2021-04-18 13:28:14 -04:00
|
|
|
}
|
2021-07-11 09:06:06 -04:00
|
|
|
fs.FileWrite(&settingsFile, reinterpret_cast<uint8_t*>(&settings), sizeof(settings));
|
|
|
|
fs.FileClose(&settingsFile);
|
2021-04-03 22:08:51 -04:00
|
|
|
}
|