aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/timer
diff options
context:
space:
mode:
authormark9064 <30447455+mark9064@users.noreply.github.com>2025-11-09 18:17:51 +0000
committerGitHub <noreply@github.com>2025-11-09 19:17:51 +0100
commit99ae2f368bd3f9ca5521d2d04a426addbb00b04b (patch)
tree8218788ee2ba3a60d9512c3c6e420ac980d5a4ad /src/components/timer
parent716deff7d0456cdd9eb1d152d3b29f1f39c7a231 (diff)
Refactor Timer component to provide expiry information (#2365)
Diffstat (limited to 'src/components/timer')
-rw-r--r--src/components/timer/Timer.cpp23
-rw-r--r--src/components/timer/Timer.h10
2 files changed, 28 insertions, 5 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;
};
}
}