Also save to NVS from time to time

This commit is contained in:
fruchti 2020-09-20 18:00:35 +02:00
parent a3799b02a0
commit 8ac36022e0
6 changed files with 23 additions and 6 deletions

View file

@ -1 +1 @@
455 457

View file

@ -90,6 +90,14 @@ void Animation_Poll(void)
NVS_Data->animation_step_top, NVS_Data->animation_step_top,
LightSensor_RelativeBrightness); LightSensor_RelativeBrightness);
LED_Commit(); 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) void TIM17_IRQHandler(void)

View file

@ -15,6 +15,10 @@
#define ANIMATION_CYCLE_TIME_TOP \ #define ANIMATION_CYCLE_TIME_TOP \
(8 * 24 * 60 * 60) (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]; extern const unsigned int Animation_LEDOrder[LED_COUNT];
void Animation_Init(void); void Animation_Init(void);

View file

@ -26,13 +26,13 @@ int main(void)
if(LightSensor_RelativeBrightness == 0 && !powered_down) if(LightSensor_RelativeBrightness == 0 && !powered_down)
{ {
LED_Suspend(); LED_Suspend();
NVS_Save(); NVS_Save(true);
powered_down = true; powered_down = true;
} }
if(powered_down && LightSensor_RelativeBrightness > 0) if(powered_down && LightSensor_RelativeBrightness > 0)
{ {
powered_down = false; powered_down = false;
NVS_Save(); NVS_Save(true);
LED_WakeUp(); LED_WakeUp();
} }
} }

View file

@ -148,7 +148,7 @@ bool NVS_Load(void)
} }
} }
void NVS_Save(void) void NVS_Save(bool erase_if_needed)
{ {
NVS_UnlockFlash(); NVS_UnlockFlash();
@ -160,6 +160,10 @@ void NVS_Save(void)
if(current_block == NULL || next_block > NVS_Area + NVS_BLOCK_COUNT if(current_block == NULL || next_block > NVS_Area + NVS_BLOCK_COUNT
|| !NVS_BlockEmpty(next_block)) || !NVS_BlockEmpty(next_block))
{ {
if(!erase_if_needed)
{
return;
}
NVS_EraseArea(); NVS_EraseArea();
next_block = &NVS_Area[0]; next_block = &NVS_Area[0];
current_block = NULL; current_block = NULL;

View file

@ -17,7 +17,8 @@ extern NVS_Data_t *const NVS_Data;
// defaults were restored instead // defaults were restored instead
bool NVS_Load(void); bool NVS_Load(void);
// Stores the current contents of NVS_Data to flash // Stores the current contents of NVS_Data to flash. Pass `false` as a parameter
void NVS_Save(void); // to skip saving unless it can be done without a flash page erase.
void NVS_Save(bool erase_if_needed);