From ae2b9d30d625dfc67ece652d12b7781b7bbeb5ee Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Fri, 30 Jul 2021 21:43:44 +0300 Subject: [PATCH] Simplify systemtask states --- src/systemtask/SystemTask.cpp | 62 +++++++++++++++++------------------ src/systemtask/SystemTask.h | 9 +++-- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index f4eeffc3..ebd93f96 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -258,8 +258,7 @@ void SystemTask::Work() { nimbleController.RestartFastAdv(); } - isSleeping = false; - isWakingUp = false; + state = Running; isDimmed = false; break; case Messages::TouchWakeUp: { @@ -278,7 +277,7 @@ void SystemTask::Work() { if (doNotGoToSleep) { break; } - isGoingToSleep = true; + state = GoingToSleep; // Already set in PushMessage() NRF_LOG_INFO("[systemtask] Going to sleep"); xTimerStop(idleTimer, 0); xTimerStop(dimTimer, 0); @@ -294,7 +293,7 @@ void SystemTask::Work() { break; case Messages::OnNewNotification: if (settingsController.GetNotificationStatus() == Pinetime::Controllers::Settings::Notification::ON) { - if (isSleeping && !isWakingUp) { + if (state == Sleeping) { GoToRunning(); } else { ReloadIdleTimer(); @@ -303,14 +302,14 @@ void SystemTask::Work() { } break; case Messages::OnTimerDone: - if (isSleeping && !isWakingUp) { + if (state == Sleeping) { GoToRunning(); } motorController.RunForDuration(35); displayApp.PushMessage(Pinetime::Applications::Display::Messages::TimerDone); break; case Messages::SetOffAlarm: - if (isSleeping && !isWakingUp) { + if (state == Sleeping) { GoToRunning(); } motorController.StartRinging(); @@ -326,7 +325,7 @@ void SystemTask::Work() { break; case Messages::BleFirmwareUpdateStarted: doNotGoToSleep = true; - if (isSleeping && !isWakingUp) { + if (state == Sleeping) { GoToRunning(); } displayApp.PushMessage(Pinetime::Applications::Display::Messages::BleFirmwareUpdateStarted); @@ -341,8 +340,9 @@ void SystemTask::Work() { case Messages::StartFileTransfer: NRF_LOG_INFO("[systemtask] FS Started"); doNotGoToSleep = true; - if (isSleeping && !isWakingUp) + if (state == Sleeping) { GoToRunning(); + } // TODO add intent of fs access icon or something break; case Messages::StopFileTransfer: @@ -391,8 +391,7 @@ void SystemTask::Work() { touchPanel.Sleep(); } - isSleeping = true; - isGoingToSleep = false; + state = Sleeping; break; case Messages::OnNewDay: // We might be sleeping (with TWI device disabled. @@ -401,8 +400,9 @@ void SystemTask::Work() { break; case Messages::OnNewHour: using Pinetime::Controllers::AlarmController; - if (settingsController.GetChimeOption() == Controllers::Settings::ChimesOption::Hours && alarmController.State() != AlarmController::AlarmState::Alerting) { - if (isSleeping && !isWakingUp) { + if (settingsController.GetChimeOption() == Controllers::Settings::ChimesOption::Hours && + alarmController.State() != AlarmController::AlarmState::Alerting) { + if (state == Sleeping) { GoToRunning(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::Clock); } @@ -411,8 +411,9 @@ void SystemTask::Work() { break; case Messages::OnNewHalfHour: using Pinetime::Controllers::AlarmController; - if (settingsController.GetChimeOption() == Controllers::Settings::ChimesOption::HalfHours && alarmController.State() != AlarmController::AlarmState::Alerting) { - if (isSleeping && !isWakingUp) { + if (settingsController.GetChimeOption() == Controllers::Settings::ChimesOption::HalfHours && + alarmController.State() != AlarmController::AlarmState::Alerting) { + if (state == Sleeping) { GoToRunning(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::Clock); } @@ -423,7 +424,7 @@ void SystemTask::Work() { batteryController.ReadPowerState(); motorController.RunForDuration(15); ReloadIdleTimer(); - if (isSleeping && !isWakingUp) { + if (state == Sleeping) { GoToRunning(); } break; @@ -434,7 +435,7 @@ void SystemTask::Work() { nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining()); break; case Messages::OnPairing: - if (isSleeping && !isWakingUp) { + if (state == Sleeping) { GoToRunning(); } motorController.RunForDuration(35); @@ -475,14 +476,15 @@ void SystemTask::Work() { } void SystemTask::UpdateMotion() { - if (isGoingToSleep or isWakingUp) { + if (state == GoingToSleep || state == WakingUp) { return; } - if (isSleeping && !(settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) || - settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake))) { + if (state == Sleeping && !(settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) || + settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake))) { return; } + if (stepCounterMustBeReset) { motionSensor.ResetStepCounter(); stepCounterMustBeReset = false; @@ -494,7 +496,7 @@ void SystemTask::UpdateMotion() { motionController.Update(motionValues.x, motionValues.y, motionValues.z, motionValues.steps); if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist) && - motionController.Should_RaiseWake(isSleeping)) { + motionController.Should_RaiseWake(state == Sleeping)) { GoToRunning(); } if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake) && @@ -515,7 +517,7 @@ void SystemTask::HandleButtonAction(Controllers::ButtonActions action) { switch (action) { case Actions::Click: // If the first action after fast wakeup is a click, it should be ignored. - if (!fastWakeUpDone && !isGoingToSleep) { + if (!fastWakeUpDone && state != GoingToSleep) { displayApp.PushMessage(Applications::Display::Messages::ButtonPushed); } break; @@ -536,20 +538,16 @@ void SystemTask::HandleButtonAction(Controllers::ButtonActions action) { } void SystemTask::GoToRunning() { - if (isGoingToSleep or (not isSleeping) or isWakingUp) { - return; + if (state == Sleeping) { + state = WakingUp; + PushMessage(Messages::GoToRunning); } - isWakingUp = true; - PushMessage(Messages::GoToRunning); } void SystemTask::OnTouchEvent() { - if (isGoingToSleep) { - return; - } - if (!isSleeping) { + if (state == Running) { PushMessage(Messages::OnTouchEvent); - } else if (!isWakingUp) { + } else if (state == Sleeping) { if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap) or settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) { PushMessage(Messages::TouchWakeUp); @@ -559,7 +557,7 @@ void SystemTask::OnTouchEvent() { void SystemTask::PushMessage(System::Messages msg) { if (msg == Messages::GoToSleep && !doNotGoToSleep) { - isGoingToSleep = true; + state = GoingToSleep; } if (in_isr()) { @@ -594,7 +592,7 @@ void SystemTask::OnIdle() { } void SystemTask::ReloadIdleTimer() { - if (isSleeping || isGoingToSleep) { + if (state != Running) { return; } if (isDimmed) { diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index c5b03792..1a50f095 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -53,6 +53,7 @@ namespace Pinetime { namespace System { class SystemTask { public: + enum SystemTaskState { Sleeping, Running, GoingToSleep, WakingUp }; SystemTask(Drivers::SpiMaster& spi, Drivers::St7789& lcd, Pinetime::Drivers::SpiNorFlash& spiNorFlash, @@ -91,7 +92,7 @@ namespace Pinetime { }; bool IsSleeping() const { - return isSleeping; + return state == Sleeping || state == WakingUp; } private: @@ -110,10 +111,6 @@ namespace Pinetime { Pinetime::Controllers::TimerController& timerController; Pinetime::Controllers::AlarmController& alarmController; QueueHandle_t systemTasksMsgQueue; - std::atomic isSleeping {false}; - std::atomic isGoingToSleep {false}; - std::atomic isWakingUp {false}; - std::atomic isDimmed {false}; Pinetime::Drivers::Watchdog& watchdog; Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::MotorController& motorController; @@ -139,6 +136,8 @@ namespace Pinetime { TimerHandle_t idleTimer; TimerHandle_t measureBatteryTimer; bool doNotGoToSleep = false; + bool isDimmed = false; + SystemTaskState state = Running; void HandleButtonAction(Controllers::ButtonActions action); bool fastWakeUpDone = false;