InfiniTime/src/components/settings/Settings.cpp
joaquim.org 084123b752
Using littlefs (#438)
* add submodule littlefs
* base fs
* Save settings using littlefs
* Small fixes and suggestions from PR
* More small fixes from PR suggestions
* Code clean up
* Change SpiNorFlash functions to be private in FS
2021-07-11 15:06:06 +02:00

48 lines
1.1 KiB
C++

#include "Settings.h"
#include <cstdlib>
#include <cstring>
using namespace Pinetime::Controllers;
Settings::Settings(Pinetime::Controllers::FS& fs) : fs {fs} {
}
void Settings::Init() {
// Load default settings from Flash
LoadSettingsFromFile();
}
void Settings::SaveSettings() {
// verify if is necessary to save
if (settingsChanged) {
SaveSettingsToFile();
}
settingsChanged = false;
}
void Settings::LoadSettingsFromFile() {
SettingsData bufferSettings;
lfs_file_t settingsFile;
if ( fs.FileOpen(&settingsFile, "/settings.dat", LFS_O_RDWR | LFS_O_CREAT) != LFS_ERR_OK) {
return;
}
fs.FileRead(&settingsFile, reinterpret_cast<uint8_t*>(&bufferSettings), sizeof(settings));
fs.FileClose(&settingsFile);
if ( bufferSettings.version == settingsVersion ) {
settings = bufferSettings;
}
}
void Settings::SaveSettingsToFile() {
lfs_file_t settingsFile;
if ( fs.FileOpen(&settingsFile, "/settings.dat", LFS_O_RDWR | LFS_O_CREAT) != LFS_ERR_OK) {
return;
}
fs.FileWrite(&settingsFile, reinterpret_cast<uint8_t*>(&settings), sizeof(settings));
fs.FileClose(&settingsFile);
}