diff options
| author | mark9064 <30447455+mark9064@users.noreply.github.com> | 2025-11-09 18:17:51 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-09 19:17:51 +0100 |
| commit | 99ae2f368bd3f9ca5521d2d04a426addbb00b04b (patch) | |
| tree | 8218788ee2ba3a60d9512c3c6e420ac980d5a4ad | |
| parent | 716deff7d0456cdd9eb1d152d3b29f1f39c7a231 (diff) | |
Refactor Timer component to provide expiry information (#2365)
| -rw-r--r-- | src/components/timer/Timer.cpp | 23 | ||||
| -rw-r--r-- | src/components/timer/Timer.h | 10 | ||||
| -rw-r--r-- | src/displayapp/screens/Timer.cpp | 3 |
3 files changed, 30 insertions, 6 deletions
diff --git a/src/components/timer/Timer.cpp b/src/components/timer/Timer.cpp index 279178cd..4784cdb2 100644 --- a/src/components/timer/Timer.cpp +++ b/src/components/timer/Timer.cpp @@ -9,18 +9,33 @@ Timer::Timer(void* const timerData, TimerCallbackFunction_t timerCallbackFunctio void Timer::StartTimer(std::chrono::milliseconds duration) { xTimerChangePeriod(timer, pdMS_TO_TICKS(duration.count()), 0); xTimerStart(timer, 0); + expiry = xTimerGetExpiryTime(timer); + triggered = true; } -std::chrono::milliseconds Timer::GetTimeRemaining() { +// nullopt if timer stopped (StopTimer called / StartTimer not yet called) +// otherwise TimerStatus with the ticks until/since expiry (depending on state of expired flag) +std::optional<Timer::TimerStatus> Timer::GetTimerState() { if (IsRunning()) { - TickType_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount(); - return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ); + TickType_t remainingTime = expiry - xTaskGetTickCount(); + return std::make_optional<Timer::TimerStatus>( + {.distanceToExpiry = std::chrono::seconds(remainingTime / configTICK_RATE_HZ) + + std::chrono::milliseconds((remainingTime % configTICK_RATE_HZ) * 1000 / configTICK_RATE_HZ), + .expired = false}); } - return std::chrono::milliseconds(0); + if (triggered) { + TickType_t timeSinceExpiry = xTaskGetTickCount() - expiry; + return std::make_optional<Timer::TimerStatus>( + {.distanceToExpiry = std::chrono::seconds(timeSinceExpiry / configTICK_RATE_HZ) + + std::chrono::milliseconds((timeSinceExpiry % configTICK_RATE_HZ) * 1000 / configTICK_RATE_HZ), + .expired = true}); + } + return std::nullopt; } void Timer::StopTimer() { xTimerStop(timer, 0); + triggered = false; } bool Timer::IsRunning() { diff --git a/src/components/timer/Timer.h b/src/components/timer/Timer.h index 2469666f..a99f6c4d 100644 --- a/src/components/timer/Timer.h +++ b/src/components/timer/Timer.h @@ -4,23 +4,31 @@ #include <timers.h> #include <chrono> +#include <optional> namespace Pinetime { namespace Controllers { class Timer { public: + struct TimerStatus { + std::chrono::milliseconds distanceToExpiry; + bool expired; + }; + Timer(void* timerData, TimerCallbackFunction_t timerCallbackFunction); void StartTimer(std::chrono::milliseconds duration); void StopTimer(); - std::chrono::milliseconds GetTimeRemaining(); + std::optional<TimerStatus> GetTimerState(); bool IsRunning(); private: TimerHandle_t timer; + TickType_t expiry; + bool triggered = false; }; } } diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index d5f2a2b7..6f086e02 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -118,7 +118,8 @@ void Timer::Refresh() { } void Timer::DisplayTime() { - displaySeconds = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining()); + displaySeconds = + std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimerState().value_or(Controllers::Timer::TimerStatus {}).distanceToExpiry); if (displaySeconds.IsUpdated()) { minuteCounter.SetValue(displaySeconds.Get().count() / 60); secondCounter.SetValue(displaySeconds.Get().count() % 60); |
