@@ -9,18 +9,33 @@ Timer::Timer(void* const timerData, TimerCallbackFunction_t timerCallbackFunctio
99void Timer::StartTimer (std::chrono::milliseconds duration) {
1010 xTimerChangePeriod (timer, pdMS_TO_TICKS (duration.count ()), 0 );
1111 xTimerStart (timer, 0 );
12+ expiry = xTimerGetExpiryTime (timer);
13+ triggered = true ;
1214}
1315
14- std::chrono::milliseconds Timer::GetTimeRemaining () {
16+ // nullopt if timer stopped (StopTimer called / StartTimer not yet called)
17+ // otherwise TimerStatus with the ticks until/since expiry (depending on state of expired flag)
18+ std::optional<Timer::TimerStatus> Timer::GetTimerState () {
1519 if (IsRunning ()) {
16- TickType_t remainingTime = xTimerGetExpiryTime (timer) - xTaskGetTickCount ();
17- return std::chrono::milliseconds (remainingTime * 1000 / configTICK_RATE_HZ);
20+ TickType_t remainingTime = expiry - xTaskGetTickCount ();
21+ return std::make_optional<Timer::TimerStatus>(
22+ {.distanceToExpiry = std::chrono::seconds (remainingTime / configTICK_RATE_HZ) +
23+ std::chrono::milliseconds ((remainingTime % configTICK_RATE_HZ) * 1000 / configTICK_RATE_HZ),
24+ .expired = false });
1825 }
19- return std::chrono::milliseconds (0 );
26+ if (triggered) {
27+ TickType_t timeSinceExpiry = xTaskGetTickCount () - expiry;
28+ return std::make_optional<Timer::TimerStatus>(
29+ {.distanceToExpiry = std::chrono::seconds (timeSinceExpiry / configTICK_RATE_HZ) +
30+ std::chrono::milliseconds ((timeSinceExpiry % configTICK_RATE_HZ) * 1000 / configTICK_RATE_HZ),
31+ .expired = true });
32+ }
33+ return std::nullopt ;
2034}
2135
2236void Timer::StopTimer () {
2337 xTimerStop (timer, 0 );
38+ triggered = false ;
2439}
2540
2641bool Timer::IsRunning () {
0 commit comments