Author: mark9064 <30447455+mark9064@users.noreply.github.com>
Use DirtyValue for timer
src/displayapp/screens/Timer.cpp | 19 +++++++++++-------- src/displayapp/screens/Timer.h | 3 +++
diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index a1ede6bed6d2a3c34c5d579e88f57d76f2bd2260..f6d5e73bdc84249a0a6ac74d21213db3e66083ea 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -104,9 +104,7 @@ } void Timer::Refresh() { if (timer.IsRunning()) { - auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining()); - minuteCounter.SetValue(secondsRemaining.count() / 60); - secondCounter.SetValue(secondsRemaining.count() % 60); + DisplayTime(); } else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) { lv_label_set_text_static(txtPlayPause, "Reset"); maskPosition += 15; @@ -119,6 +117,14 @@ } } } +void Timer::DisplayTime() { + displaySeconds = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining()); + if (displaySeconds.IsUpdated()) { + minuteCounter.SetValue(displaySeconds.Get().count() / 60); + secondCounter.SetValue(displaySeconds.Get().count() % 60); + } +} + void Timer::SetTimerRunning() { minuteCounter.HideControls(); secondCounter.HideControls(); @@ -133,9 +139,7 @@ } void Timer::ToggleRunning() { if (timer.IsRunning()) { - auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining()); - minuteCounter.SetValue(secondsRemaining.count() / 60); - secondCounter.SetValue(secondsRemaining.count() % 60); + DisplayTime(); timer.StopTimer(); SetTimerStopped(); } else if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) { @@ -147,7 +151,6 @@ } } void Timer::Reset() { - minuteCounter.SetValue(0); - secondCounter.SetValue(0); + DisplayTime(); SetTimerStopped(); } diff --git a/src/displayapp/screens/Timer.h b/src/displayapp/screens/Timer.h index 409cae1cd488e15896d0a0c180bcf96835a36e21..0cb2bb0802431a4899ca39813547f7524eb43ba4 100644 --- a/src/displayapp/screens/Timer.h +++ b/src/displayapp/screens/Timer.h @@ -5,6 +5,7 @@ #include "components/datetime/DateTimeController.h" #include "systemtask/SystemTask.h" #include "displayapp/LittleVgl.h" #include "displayapp/widgets/Counter.h" +#include "utility/DirtyValue.h" #include <lvgl/lvgl.h> #include "components/timer/Timer.h" @@ -26,6 +27,7 @@ private: void SetTimerRunning(); void SetTimerStopped(); void UpdateMask(); + void DisplayTime(); Pinetime::Controllers::Timer& timer; lv_obj_t* btnPlayPause; @@ -43,6 +45,7 @@ bool buttonPressing = false; lv_coord_t maskPosition = 0; TickType_t pressTime = 0; + Utility::DirtyValue<std::chrono::seconds> displaySeconds; }; }