-
-
Notifications
You must be signed in to change notification settings - Fork 984
/
Copy pathTimer.cpp
59 lines (46 loc) · 1.3 KB
/
Timer.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "components/timer/Timer.h"
using namespace Pinetime::Controllers;
Timer::Timer(void* const timerData, TimerCallbackFunction_t timerCallbackFunction) {
pausedAtTimer = std::chrono::seconds(0);
state = Stopped;
timer = xTimerCreate("Timer", 1, pdFALSE, timerData, timerCallbackFunction);
}
std::chrono::milliseconds Timer::GetTimeRemaining() {
if (IsRunning()) {
const TickType_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount();
return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ);
}
if (state == Paused)
return pausedAtTimer;
return std::chrono::milliseconds(0);
}
Timer::TimerState Timer::GetState() {
return state;
}
void Timer::Start(const std::chrono::milliseconds duration) {
TimerStart(duration);
state = Running;
}
void Timer::Stop() {
TimerStop();
state = Stopped;
}
void Timer::Pause() {
pausedAtTimer = GetTimeRemaining();
TimerStop();
state = Paused;
}
void Timer::Resume() {
TimerStart(pausedAtTimer);
state = Running;
}
bool Timer::IsRunning() {
return (xTimerIsTimerActive(timer) == pdTRUE);
};
void Timer::TimerStart(const std::chrono::milliseconds duration) {
xTimerChangePeriod(timer, pdMS_TO_TICKS(duration.count()), 0);
xTimerStart(timer, 0);
}
void Timer::TimerStop() {
xTimerStop(timer, 0);
}