From 8ac36022e0b6d5d5603c806d332ee31a2c147723 Mon Sep 17 00:00:00 2001 From: fruchti Date: Sun, 20 Sep 2020 18:00:35 +0200 Subject: [PATCH] Also save to NVS from time to time --- build-number.txt | 2 +- src/animation.c | 8 ++++++++ src/animation.h | 4 ++++ src/main.c | 4 ++-- src/nvs.c | 6 +++++- src/nvs.h | 5 +++-- 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/build-number.txt b/build-number.txt index 4930863..de2a00c 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -455 +457 diff --git a/src/animation.c b/src/animation.c index 6c50ff3..72dbb8b 100644 --- a/src/animation.c +++ b/src/animation.c @@ -90,6 +90,14 @@ void Animation_Poll(void) NVS_Data->animation_step_top, LightSensor_RelativeBrightness); LED_Commit(); + + static unsigned int store_counter = 0; + store_counter++; + if(store_counter >= ANIMATION_NVS_STORE_INTERVAL * ANIMATION_REFRESH_RATE) + { + store_counter = 0; + NVS_Save(false); + } } void TIM17_IRQHandler(void) diff --git a/src/animation.h b/src/animation.h index 144291e..04e2a73 100644 --- a/src/animation.h +++ b/src/animation.h @@ -15,6 +15,10 @@ #define ANIMATION_CYCLE_TIME_TOP \ (8 * 24 * 60 * 60) +// Interval for saving the current animation step to NVS (in seconds) +#define ANIMATION_NVS_STORE_INTERVAL \ + (60 * 60) + extern const unsigned int Animation_LEDOrder[LED_COUNT]; void Animation_Init(void); diff --git a/src/main.c b/src/main.c index d41caa5..d921b37 100644 --- a/src/main.c +++ b/src/main.c @@ -26,13 +26,13 @@ int main(void) if(LightSensor_RelativeBrightness == 0 && !powered_down) { LED_Suspend(); - NVS_Save(); + NVS_Save(true); powered_down = true; } if(powered_down && LightSensor_RelativeBrightness > 0) { powered_down = false; - NVS_Save(); + NVS_Save(true); LED_WakeUp(); } } diff --git a/src/nvs.c b/src/nvs.c index 8f7c0b8..ef19b0e 100644 --- a/src/nvs.c +++ b/src/nvs.c @@ -148,7 +148,7 @@ bool NVS_Load(void) } } -void NVS_Save(void) +void NVS_Save(bool erase_if_needed) { NVS_UnlockFlash(); @@ -160,6 +160,10 @@ void NVS_Save(void) if(current_block == NULL || next_block > NVS_Area + NVS_BLOCK_COUNT || !NVS_BlockEmpty(next_block)) { + if(!erase_if_needed) + { + return; + } NVS_EraseArea(); next_block = &NVS_Area[0]; current_block = NULL; diff --git a/src/nvs.h b/src/nvs.h index 47ba48a..ad03133 100644 --- a/src/nvs.h +++ b/src/nvs.h @@ -17,7 +17,8 @@ extern NVS_Data_t *const NVS_Data; // defaults were restored instead bool NVS_Load(void); -// Stores the current contents of NVS_Data to flash -void NVS_Save(void); +// Stores the current contents of NVS_Data to flash. Pass `false` as a parameter +// to skip saving unless it can be done without a flash page erase. +void NVS_Save(bool erase_if_needed);