Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions src/systemtask/SystemTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<uint8_t>(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<uint8_t*>(&oldSteps), sizeof(oldSteps));
fs.FileSeek(&stepsFile, 0);

steps += oldSteps;

fs.FileWrite(&stepsFile, reinterpret_cast<uint8_t*>(&steps), sizeof(steps));
fs.FileClose(&stepsFile);
return steps;
}

void SystemTask::OnButtonPushed() {
if (isGoingToSleep)
return;
Expand Down
6 changes: 5 additions & 1 deletion src/systemtask/SystemTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace Pinetime {
void Start();
void PushMessage(Messages msg);

uint32_t SavingSteps(uint32_t steps);
void OnButtonPushed();
void OnTouchEvent();

Expand Down Expand Up @@ -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;

Expand Down