diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 8915ce7448..5c889b9c8d 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -320,11 +320,6 @@ void SystemTask::Work() { isSleeping = true; isGoingToSleep = false; break; - case Messages::OnNewDay: - // We might be sleeping (with TWI device disabled. - // Remember we'll have to reset the counter next time we're awake - stepCounterMustBeReset = true; - break; case Messages::OnChargingEvent: motorController.SetDuration(15); // Battery level is updated on every message - there's no need to do anything @@ -370,22 +365,53 @@ void SystemTask::UpdateMotion() { if (isSleeping) twiMaster.Wakeup(); - if (stepCounterMustBeReset) { + if (isSleeping) + twiMaster.Sleep(); + + auto motionValues = motionSensor.Process(); + stepCounterUnsavedCount += motionValues.steps; + motionSensor.ResetStepCounter(); + + // As the UpdateMotion is triggered every 100ms or so we will not save + // the value every tick. With the code below we will save and restore + // values every 10th second. (100 * 100ms = 10s) + if (stepCounterSaveTimer > 100 && stepCounterUnsavedCount > 0) { motionSensor.ResetStepCounter(); - stepCounterMustBeReset = false; + stepCounterSavedCount = SavingSteps(stepCounterUnsavedCount); + stepCounterSaveTimer = 0; + stepCounterUnsavedCount = 0; } - auto motionValues = motionSensor.Process(); - if (isSleeping) - twiMaster.Sleep(); + stepCounterSaveTimer++; motionController.IsSensorOk(motionSensor.IsOk()); - motionController.Update(motionValues.x, motionValues.y, motionValues.z, motionValues.steps); + motionController.Update(motionValues.x, motionValues.y, motionValues.z, stepCounterSavedCount + stepCounterUnsavedCount); + if (motionController.ShouldWakeUp(isSleeping)) { GoToRunning(); } } +uint32_t SystemTask::SavingSteps(uint32_t steps) { + lfs_file_t stepsFile; + uint32_t oldSteps; + + char fileName[40]; + snprintf(fileName, sizeof(fileName), "/steps_%.4d%.2d%.2d.dat", dateTimeController.Year(), static_cast(dateTimeController.Month()), dateTimeController.Day()); + + if ( fs.FileOpen(&stepsFile, fileName, LFS_O_RDWR | LFS_O_CREAT) != LFS_ERR_OK) { + return steps; + } + fs.FileRead(&stepsFile, reinterpret_cast(&oldSteps), sizeof(oldSteps)); + fs.FileSeek(&stepsFile, 0); + + steps += oldSteps; + + fs.FileWrite(&stepsFile, reinterpret_cast(&steps), sizeof(steps)); + fs.FileClose(&stepsFile); + return steps; +} + void SystemTask::OnButtonPushed() { if (isGoingToSleep) return; diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index ba4342987b..1530c22e77 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -67,6 +67,7 @@ namespace Pinetime { void Start(); void PushMessage(Messages msg); + uint32_t SavingSteps(uint32_t steps); void OnButtonPushed(); void OnTouchEvent(); @@ -129,13 +130,16 @@ namespace Pinetime { void ReloadIdleTimer(); bool isBleDiscoveryTimerRunning = false; uint8_t bleDiscoveryTimer = 0; + int stepCounterSaveTimer = 0; + int stepCounterSavedCount = 0; + int stepCounterUnsavedCount = 0; + TimerHandle_t dimTimer; TimerHandle_t idleTimer; bool doNotGoToSleep = false; void GoToRunning(); void UpdateMotion(); - bool stepCounterMustBeReset = false; static constexpr TickType_t batteryNotificationPeriod = 1000 * 60 * 10; // 1 tick ~= 1ms. 1ms * 60 * 10 = 10 minutes TickType_t batteryNotificationTick = 0;