blob: 4784cdb2ff93a6145048034cbe2cf077166b547d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include "components/timer/Timer.h"
using namespace Pinetime::Controllers;
Timer::Timer(void* const timerData, TimerCallbackFunction_t timerCallbackFunction) {
timer = xTimerCreate("Timer", 1, pdFALSE, timerData, timerCallbackFunction);
}
void Timer::StartTimer(std::chrono::milliseconds duration) {
xTimerChangePeriod(timer, pdMS_TO_TICKS(duration.count()), 0);
xTimerStart(timer, 0);
expiry = xTimerGetExpiryTime(timer);
triggered = true;
}
// 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 = 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});
}
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() {
return (xTimerIsTimerActive(timer) == pdTRUE);
}
|