Merge branch 'develop' of

https://github.com/JF002/InfiniTime into StepsApp
This commit is contained in:
Joaquim
2021-04-26 21:29:48 +01:00
177 changed files with 10107 additions and 10659 deletions

View File

@@ -5,110 +5,107 @@
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
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} {}
Settings::Settings(Pinetime::Drivers::SpiNorFlash& spiNorFlash) : spiNorFlash {spiNorFlash} {
}
void Settings::Init() {
// Load default settings from Flash
LoadSettingsFromFlash();
// Load default settings from Flash
LoadSettingsFromFlash();
}
void Settings::SaveSettings() {
// verify if is necessary to save
if ( settingsChanged ) {
SaveSettingsToFlash();
}
settingsChanged = false;
// verify if is necessary to save
if (settingsChanged) {
SaveSettingsToFlash();
}
settingsChanged = false;
}
bool Settings::FindHeader() {
SettingsHeader settingsHeader;
uint8_t bufferHead[sizeof(settingsHeader)];
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;
}
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;
}
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));
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));
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::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));
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();
}
if (settingsFlashBlock == 99) {
// Find current Block, if can't find use default settings and set block to 0 ans save !
if (FindHeader()) {
ReadSettingsData();
} else {
// Read Settings from flash...
// never used :)
ReadSettingsData();
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
// calculate where to save...
// mark current to inactive
// erase the new location and save
// set settingsFlashBlock
if ( settingsFlashBlock != 99 ) {
SetHeader( false );
}
// if first time hever, only saves to block 0 and set settingsFlashBlock
settingsFlashBlock++;
if ( settingsFlashBlock > 9 ) settingsFlashBlock = 0;
if (settingsFlashBlock != 99) {
SetHeader(false);
}
EraseBlock();
SetHeader( true );
SaveSettingsData();
settingsFlashBlock++;
if (settingsFlashBlock > 9)
settingsFlashBlock = 0;
EraseBlock();
SetHeader(true);
SaveSettingsData();
}

View File

@@ -8,104 +8,129 @@
namespace Pinetime {
namespace Controllers {
class Settings {
public:
enum class ClockType {H24, H12};
enum class Vibration {ON, OFF};
enum class WakeUpMode {None, SingleTap, DoubleTap, RaiseWrist};
public:
enum class ClockType { H24, H12 };
enum class Vibration { ON, OFF };
enum class WakeUpMode { None, SingleTap, DoubleTap, RaiseWrist };
Settings( Pinetime::Drivers::SpiNorFlash &spiNorFlash );
Settings(Pinetime::Drivers::SpiNorFlash& spiNorFlash);
void Init();
void SaveSettings();
void Init();
void SaveSettings();
void SetClockFace( uint8_t face ) {
if ( face != settings.clockFace ) settingsChanged = true;
settings.clockFace = face;
};
uint8_t GetClockFace() const { return settings.clockFace; };
void SetClockFace(uint8_t face) {
if (face != settings.clockFace)
settingsChanged = true;
settings.clockFace = face;
};
uint8_t GetClockFace() const {
return settings.clockFace;
};
void SetAppMenu( uint8_t menu ) { appMenu = menu; };
uint8_t GetAppMenu() { return appMenu; };
void SetAppMenu(uint8_t menu) {
appMenu = menu;
};
uint8_t GetAppMenu() {
return appMenu;
};
void SetSettingsMenu( uint8_t menu ) { settingsMenu = menu; };
uint8_t GetSettingsMenu() const { return settingsMenu; };
void SetSettingsMenu(uint8_t menu) {
settingsMenu = menu;
};
uint8_t GetSettingsMenu() const {
return settingsMenu;
};
void SetClockType( ClockType clocktype ) {
if ( clocktype != settings.clockType ) settingsChanged = true;
settings.clockType = clocktype;
};
ClockType GetClockType() const { return settings.clockType; };
void SetClockType(ClockType clocktype) {
if (clocktype != settings.clockType)
settingsChanged = true;
settings.clockType = clocktype;
};
ClockType GetClockType() const {
return settings.clockType;
};
void SetVibrationStatus( Vibration status ) {
if ( status != settings.vibrationStatus ) settingsChanged = true;
settings.vibrationStatus = status;
};
Vibration GetVibrationStatus() const { return settings.vibrationStatus; };
void SetVibrationStatus(Vibration status) {
if (status != settings.vibrationStatus)
settingsChanged = true;
settings.vibrationStatus = status;
};
Vibration GetVibrationStatus() const {
return settings.vibrationStatus;
};
void SetScreenTimeOut( uint32_t timeout ) {
if ( timeout != settings.screenTimeOut ) settingsChanged = true;
settings.screenTimeOut = timeout;
};
uint32_t GetScreenTimeOut() const { return settings.screenTimeOut; };
void SetScreenTimeOut(uint32_t timeout) {
if (timeout != settings.screenTimeOut)
settingsChanged = true;
settings.screenTimeOut = timeout;
};
uint32_t GetScreenTimeOut() const {
return settings.screenTimeOut;
};
void setWakeUpMode( WakeUpMode wakeUp ) {
if ( wakeUp != settings.wakeUpMode ) settingsChanged = true;
settings.wakeUpMode = wakeUp;
};
WakeUpMode getWakeUpMode() const { return settings.wakeUpMode; };
void setWakeUpMode(WakeUpMode wakeUp) {
if (wakeUp != settings.wakeUpMode)
settingsChanged = true;
settings.wakeUpMode = wakeUp;
};
WakeUpMode getWakeUpMode() const {
return settings.wakeUpMode;
};
void SetBrightness( Controllers::BrightnessController::Levels level ) {
if ( level != settings.brightLevel ) settingsChanged = true;
settings.brightLevel = level;
};
Controllers::BrightnessController::Levels GetBrightness() const { return settings.brightLevel; };
void SetBrightness(Controllers::BrightnessController::Levels level) {
if (level != settings.brightLevel)
settingsChanged = true;
settings.brightLevel = level;
};
Controllers::BrightnessController::Levels GetBrightness() const {
return settings.brightLevel;
};
void SetStepsGoal( uint32_t goal ) {
if ( goal != settings.stepsGoal ) settingsChanged = true;
settings.stepsGoal = goal;
};
uint32_t GetStepsGoal() const { return settings.stepsGoal; };
void SetStepsGoal( uint32_t goal ) {
if ( goal != settings.stepsGoal )
settingsChanged = true;
settings.stepsGoal = goal;
};
uint32_t GetStepsGoal() const { return settings.stepsGoal; };
private:
private:
Pinetime::Drivers::SpiNorFlash& spiNorFlash;
struct SettingsData {
Pinetime::Drivers::SpiNorFlash& spiNorFlash;
struct SettingsData {
ClockType clockType = ClockType::H24;
Vibration vibrationStatus = Vibration::ON;
ClockType clockType = ClockType::H24;
Vibration vibrationStatus = Vibration::ON;
uint8_t clockFace = 0;
uint8_t clockFace = 0;
uint32_t stepsGoal = 10000;
uint32_t screenTimeOut = 15000;
uint32_t stepsGoal = 10000;
uint32_t screenTimeOut = 15000;
WakeUpMode wakeUpMode = WakeUpMode::None;
WakeUpMode wakeUpMode = WakeUpMode::None;
Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium;
};
Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium;
SettingsData settings;
bool settingsChanged = false;
};
uint8_t appMenu = 0;
uint8_t settingsMenu = 0;
SettingsData settings;
bool settingsChanged = false;
// There are 10 blocks of reserved flash to save settings
// to minimize wear, the recording is done in a rotating way by the 10 blocks
uint8_t settingsFlashBlock = 99; // default to indicate it needs to find the active block
uint8_t appMenu = 0;
uint8_t settingsMenu = 0;
// There are 10 blocks of reserved flash to save settings
// to minimize wear, the recording is done in a rotating way by the 10 blocks
uint8_t settingsFlashBlock = 99; // default to indicate it needs to find the active block
static constexpr uint32_t settingsBaseAddr = 0x3F6000; // Flash Settings Location
static constexpr uint16_t settingsVersion = 0x0100; // Flash Settings Version
bool FindHeader();
void ReadSettingsData();
void EraseBlock();
void SetHeader( bool state );
void SaveSettingsData();
void LoadSettingsFromFlash();
void SaveSettingsToFlash();
static constexpr uint32_t settingsBaseAddr = 0x3F6000; // Flash Settings Location
static constexpr uint16_t settingsVersion = 0x0100; // Flash Settings Version
bool FindHeader();
void ReadSettingsData();
void EraseBlock();
void SetHeader(bool state);
void SaveSettingsData();
void LoadSettingsFromFlash();
void SaveSettingsToFlash();
};
}
}