-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
88 lines (70 loc) · 1.46 KB
/
Copy pathTimer.cpp
File metadata and controls
88 lines (70 loc) · 1.46 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "Timer.h"
Timer *pHead = NULL;
uint32_t __sleepTimer = 0;
void TimerScan(){
Timer *ptr = pHead;
while (ptr != NULL){
if (ptr->IsEnabled && /* If we are enabled */
!ptr->IsExpired && /* And haven't already expired */
ptr->CurrentMS == 0){ /* And current MS just went to zero */
ptr->IsExpired = true;
if (ptr->Callback != NULL)
ptr->Callback->Call(); /* Execute the handler */
if (ptr->CallbackC != NULL)
ptr->CallbackC();
if (ptr->AutoReset){
ptr->CurrentMS = ptr->PeriodMS;
ptr->IsExpired = false;
}
else
ptr->IsExpired = true; /* Otherwise mark as expired */
}
ptr = ptr->pNext;
}
}
/* Called once per second on IRQ handler. */
extern "C" void TimerUpdate()
{
if (__sleepTimer > 0)
__sleepTimer--;
Timer *ptr = pHead;
while (ptr != NULL){
if (ptr->IsEnabled && ptr->CurrentMS > 0)
ptr->CurrentMS--;
ptr = ptr->pNext;
}
}
/* Forces a sleep */
extern "C" void SleepMS(uint32_t sleepMS){
__sleepTimer = sleepMS;
while (__sleepTimer > 0);
}
Timer::Timer(){
pNext = NULL;
Callback = NULL;
CallbackC = NULL;
IsExpired = false;
IsEnabled = false;
if (pHead == NULL)
pHead = this;
else{
Timer *ptr = pHead;
while (ptr->pNext != NULL)
ptr = ptr->pNext;
ptr->pNext = this;
}
}
void Timer::Enable()
{
CurrentMS = PeriodMS;
IsEnabled = true;
IsExpired = false;
}
void Timer::Disable(){
CurrentMS = 0;
IsEnabled = false;
IsExpired = false;
}
Timer::~Timer()
{
}