diff options
| author | Joaquim <joaquim.org@gmail.com> | 2021-04-04 02:08:51 (GMT) |
|---|---|---|
| committer | Joaquim <joaquim.org@gmail.com> | 2021-04-04 02:08:51 (GMT) |
| commit | 1d3742e14f09316a1d795527713eb8f9742f0ffb (patch) | |
| tree | 6bc6343538506b68256aa057121e063d22f8ed1a /src/components/settings/Settings.cpp | |
| parent | 58a2d000c4d49d96121894d6dd6bb861d7564bea (diff) | |
Big UI and navigation Rewrite
new navigation
add some color to the apps
redesign menus
new settings menu
new quick settings
code clean up
size reduction by converting navigation images to font
and more...
Diffstat (limited to 'src/components/settings/Settings.cpp')
| -rw-r--r-- | src/components/settings/Settings.cpp | 110 |
1 files changed, 104 insertions, 6 deletions
diff --git a/src/components/settings/Settings.cpp b/src/components/settings/Settings.cpp index 0c6cf61..454df57 100644 --- a/src/components/settings/Settings.cpp +++ b/src/components/settings/Settings.cpp @@ -1,16 +1,114 @@ #include "Settings.h" +#include <cstdlib> +#include <cstring> using namespace Pinetime::Controllers; +struct SettingsHeader { + uint8_t isActive; // 0xF1 = Block is active, 0xF0 = Block is inactive + uint16_t version; // Current version, to verify if the saved data is for the current Version +}; + +#define HEADER_SIZE sizeof(SettingsHeader) + + +Settings::Settings( Pinetime::Drivers::SpiNorFlash &spiNorFlash ) : spiNorFlash{spiNorFlash} {} -// TODO (team): -// Read and write the settings to Flash -// void Settings::Init() { - // default Clock face - clockFace = 0; - clockType = ClockType::H24; + // Load default settings from Flash + LoadSettingsFromFlash(); + +} + +void Settings::SaveSettings() { + + // verify if is necessary to save + if ( settingsChanged ) { + SaveSettingsToFlash(); + } + settingsChanged = false; +} + + +bool Settings::FindHeader() { + SettingsHeader settingsHeader; + uint8_t bufferHead[sizeof(settingsHeader)]; + + for (uint8_t block = 0; block < 10; block++) { + + spiNorFlash.Read( settingsBaseAddr + (block * 0x1000), bufferHead, sizeof(settingsHeader) ); + std::memcpy(&settingsHeader, bufferHead, sizeof(settingsHeader)); + if ( settingsHeader.isActive == 0xF1 && settingsHeader.version == settingsVersion ) { + settingsFlashBlock = block; + return true; + } + } + return false; +} + +void Settings::ReadSettingsData() { + uint8_t bufferSettings[sizeof(settings)]; + spiNorFlash.Read( settingsBaseAddr + (settingsFlashBlock * 0x1000) + HEADER_SIZE, bufferSettings, sizeof(settings) ); + std::memcpy(&settings, bufferSettings, sizeof(settings)); +} + +void Settings::EraseBlock() { + spiNorFlash.SectorErase(settingsBaseAddr + (settingsFlashBlock * 0x1000)); } +void Settings::SetHeader( bool state ) { + SettingsHeader settingsHeader; + uint8_t bufferHead[sizeof(settingsHeader)]; + settingsHeader.isActive = state ? 0xF1 : 0xF0; + settingsHeader.version = settingsVersion; + + std::memcpy(bufferHead, &settingsHeader, sizeof(settingsHeader)); + spiNorFlash.Write(settingsBaseAddr + (settingsFlashBlock * 0x1000), bufferHead, sizeof(settingsHeader)); + +} + +void Settings::SaveSettingsData() { + uint8_t bufferSettings[sizeof(settings)]; + std::memcpy(bufferSettings, &settings, sizeof(settings)); + spiNorFlash.Write(settingsBaseAddr + (settingsFlashBlock * 0x1000) + HEADER_SIZE, bufferSettings, sizeof(settings)); +} + +void Settings::LoadSettingsFromFlash() { + + if ( settingsFlashBlock == 99 ) { + // Find current Block, if can't find use default settings and set block to 0 ans save ! + if ( FindHeader() ) { + ReadSettingsData(); + } else { + SaveSettingsToFlash(); + } + } else { + // Read Settings from flash... + // never used :) + ReadSettingsData(); + } + +} + +void Settings::SaveSettingsToFlash() { + + // calculate where to save... + // mark current to inactive + // erase the new location and save + // set settingsFlashBlock + + // if first time hever, only saves to block 0 and set settingsFlashBlock + + if ( settingsFlashBlock != 99 ) { + SetHeader( false ); + } + + settingsFlashBlock++; + if ( settingsFlashBlock > 9 ) settingsFlashBlock = 0; + + EraseBlock(); + SetHeader( true ); + SaveSettingsData(); +} |
