From a5d7d3a35006a365a2686001c43aad29bd0c3de8 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Sun, 1 Aug 2021 10:55:32 +0200 Subject: [PATCH 1/4] Keep steps through reboots. --- src/systemtask/SystemTask.cpp | 38 ++++++++++++++++++++++++----------- src/systemtask/SystemTask.h | 1 + 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 8915ce7448..ad6d8c0de0 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,41 @@ void SystemTask::UpdateMotion() { if (isSleeping) twiMaster.Wakeup(); - if (stepCounterMustBeReset) { - motionSensor.ResetStepCounter(); - stepCounterMustBeReset = false; - } - - auto motionValues = motionSensor.Process(); if (isSleeping) twiMaster.Sleep(); + auto motionValues = motionSensor.Process(); + motionSensor.ResetStepCounter(); + uint32_t steps = SavingSteps(motionValues.steps); + motionController.IsSensorOk(motionSensor.IsOk()); - motionController.Update(motionValues.x, motionValues.y, motionValues.z, motionValues.steps); + motionController.Update(motionValues.x, motionValues.y, motionValues.z, steps); if (motionController.ShouldWakeUp(isSleeping)) { GoToRunning(); } } +uint32_t SystemTask::SavingSteps(uint32_t steps) { + lfs_file_t stepsFile; + uint32_t oldSteps; + + char buff[40]; + snprintf(buff, sizeof(buff), "/steps_%.4d%.2d%.2d.dat", dateTimeController.Year(), static_cast(dateTimeController.Month()), dateTimeController.Day()); + std::string fileName = buff; + + if ( fs.FileOpen(&stepsFile, fileName.c_str(), 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..862d1d4072 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(); From 66bda5e2f6ff3089d12dd56a3b9fa372ed566d40 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Mon, 2 Aug 2021 11:37:20 +0200 Subject: [PATCH 2/4] Change to update file storage only every 10 seconds. --- src/systemtask/SystemTask.cpp | 17 +++++++++++++++-- src/systemtask/SystemTask.h | 5 ++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index ad6d8c0de0..f71ade8091 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -369,11 +369,24 @@ void SystemTask::UpdateMotion() { twiMaster.Sleep(); auto motionValues = motionSensor.Process(); + stepCounterUnsavedCount += motionValues.steps; motionSensor.ResetStepCounter(); - uint32_t steps = SavingSteps(motionValues.steps); + + // 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) { + motionSensor.ResetStepCounter(); + stepCounterSavedCount = SavingSteps(stepCounterUnsavedCount); + stepCounterSaveTimer = 0; + stepCounterUnsavedCount = 0; + } + + stepCounterSaveTimer++; motionController.IsSensorOk(motionSensor.IsOk()); - motionController.Update(motionValues.x, motionValues.y, motionValues.z, steps); + motionController.Update(motionValues.x, motionValues.y, motionValues.z, stepCounterSavedCount + stepCounterUnsavedCount); + if (motionController.ShouldWakeUp(isSleeping)) { GoToRunning(); } diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 862d1d4072..1530c22e77 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -130,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; From 9677a5a31fc79ddde9a9cf29ef3e6654172b4ed2 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Mon, 2 Aug 2021 23:34:08 +0200 Subject: [PATCH 3/4] Don't update if there is no steps unsaved. --- src/systemtask/SystemTask.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index f71ade8091..6b8a470a5b 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -375,7 +375,7 @@ void SystemTask::UpdateMotion() { // 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) { + if (stepCounterSaveTimer > 100 && stepCounterUnsavedCount > 0) { motionSensor.ResetStepCounter(); stepCounterSavedCount = SavingSteps(stepCounterUnsavedCount); stepCounterSaveTimer = 0; From 319e3a2b5a9d940678f9d74cba86064d7c130733 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Fri, 6 Aug 2021 10:59:20 +0200 Subject: [PATCH 4/4] Changes after review. --- src/systemtask/SystemTask.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 6b8a470a5b..5c889b9c8d 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -396,11 +396,10 @@ uint32_t SystemTask::SavingSteps(uint32_t steps) { lfs_file_t stepsFile; uint32_t oldSteps; - char buff[40]; - snprintf(buff, sizeof(buff), "/steps_%.4d%.2d%.2d.dat", dateTimeController.Year(), static_cast(dateTimeController.Month()), dateTimeController.Day()); - std::string fileName = buff; + 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.c_str(), LFS_O_RDWR | LFS_O_CREAT) != LFS_ERR_OK) { + if ( fs.FileOpen(&stepsFile, fileName, LFS_O_RDWR | LFS_O_CREAT) != LFS_ERR_OK) { return steps; } fs.FileRead(&stepsFile, reinterpret_cast(&oldSteps), sizeof(oldSteps));