Skip to content

Commit 99ae2f3

Browse files
authored
Refactor Timer component to provide expiry information (#2365)
1 parent 716deff commit 99ae2f3

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/components/timer/Timer.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,33 @@ Timer::Timer(void* const timerData, TimerCallbackFunction_t timerCallbackFunctio
99
void 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

2236
void Timer::StopTimer() {
2337
xTimerStop(timer, 0);
38+
triggered = false;
2439
}
2540

2641
bool Timer::IsRunning() {

src/components/timer/Timer.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,31 @@
44
#include <timers.h>
55

66
#include <chrono>
7+
#include <optional>
78

89
namespace Pinetime {
910
namespace Controllers {
1011
class Timer {
1112
public:
13+
struct TimerStatus {
14+
std::chrono::milliseconds distanceToExpiry;
15+
bool expired;
16+
};
17+
1218
Timer(void* timerData, TimerCallbackFunction_t timerCallbackFunction);
1319

1420
void StartTimer(std::chrono::milliseconds duration);
1521

1622
void StopTimer();
1723

18-
std::chrono::milliseconds GetTimeRemaining();
24+
std::optional<TimerStatus> GetTimerState();
1925

2026
bool IsRunning();
2127

2228
private:
2329
TimerHandle_t timer;
30+
TickType_t expiry;
31+
bool triggered = false;
2432
};
2533
}
2634
}

src/displayapp/screens/Timer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ void Timer::Refresh() {
118118
}
119119

120120
void Timer::DisplayTime() {
121-
displaySeconds = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining());
121+
displaySeconds =
122+
std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimerState().value_or(Controllers::Timer::TimerStatus {}).distanceToExpiry);
122123
if (displaySeconds.IsUpdated()) {
123124
minuteCounter.SetValue(displaySeconds.Get().count() / 60);
124125
secondCounter.SetValue(displaySeconds.Get().count() % 60);

0 commit comments

Comments
 (0)