-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerOneMulti.cpp
More file actions
246 lines (212 loc) · 5.45 KB
/
TimerOneMulti.cpp
File metadata and controls
246 lines (212 loc) · 5.45 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "TimerOneMulti.h"
//Initialize static variables
TimerOneMulti* TimerOneMulti::singleton = NULL;
volatile bool TimerOneMulti::timerFirstShot = false;
//TODO remove (just useful for debug purposes
int ledState = LOW;
void toggleLight()
{
ledState = ! ledState;
digitalWrite(13,ledState);
}
void quickBeep()
{
for(unsigned long i=0;i<4000;i++)
{
digitalWrite(12,HIGH);
}
digitalWrite(12,LOW);
}
/********************************************
*TimerEvent class methods
*******************************************/
TimerEvent::TimerEvent(unsigned long period, void (*callback) (void*), bool periodic, void* arg)
{
this->period = period;
this->delta = period;
this->callback = callback;
this->periodic = periodic;
this->arg = arg;
this->next = NULL;
this->cancelled = false;
this->timeRemainingAfterTick=0;
}
void TimerEvent::add(TimerEvent* event)
{
if(next == NULL)
{
next = event;
if ( event->delta > MAX_PERIOD )
{
event->timeRemainingAfterTick = event->delta - MAX_PERIOD;
event->delta = MAX_PERIOD;
}
}
else if(event->delta < next->delta)
{
//insert it here (and update previous next's delta)
next->delta -= event->delta;
event->next = next;
next = event;
}
else
{
event->delta -= next->delta;
next->add(event);
}
}
bool TimerEvent::cancel(TimerEvent* event)
{
if ( event == this)
{
this->cancelled = true;
return true;
}
else if (this->next != NULL)
{
return this->next->cancel(event);
}
else
{
return false;
}
}
/*********************************************
*TimerOneMulti class methods
********************************************/
TimerOneMulti::TimerOneMulti()
{
events = NULL;
Timer1.initialize(MAX_PERIOD); // initialize timer1
}
TimerOneMulti* TimerOneMulti::getTimerController()
{
if(singleton == NULL)
singleton = new TimerOneMulti();
return singleton;
}
TimerEvent* TimerOneMulti::addEvent(unsigned long period, void (*callback) (void*), bool periodic /* Default = false */, void* arg /* Default = NULL */)
{
noInterrupts();
TimerEvent* event = new TimerEvent(period, callback, periodic, arg);
addEvent(event);
interrupts();
return event;
}
//This method should only be called with interrupts dissabled
void TimerOneMulti::addEvent(TimerEvent* event)
{
//Serial.print("Adding ");
//Serial.println((int)event,HEX);
bool queueWasEmpty = false;
//Add to events
if ( events == NULL)
{
if (event->delta > MAX_PERIOD)
{
event->timeRemainingAfterTick = event->delta - MAX_PERIOD;
event->delta = MAX_PERIOD;
}
events = event;
queueWasEmpty = true;
}
else
{
events->delta -= Timer1.read();
if ( events->delta > event->period)
{
//Insert at the beginning of the list
events->delta -= event->period;
event->next = events;
events = event;
}
else
{
event->delta -= events->delta;
events->add(event);
}
}
Timer1.setPeriod(events->delta);
if(queueWasEmpty)
{
timerFirstShot = true;
Timer1.start();
Timer1.attachInterrupt(tick); // attaches callback() as a timer overflow interrupt
}
//TODO figure out if any timer interrupts should have occurred while we were in the critical section
}
bool TimerOneMulti::cancelEvent(TimerEvent* event)
{
bool success = false;
//Critical section
noInterrupts();
{
if ( event != NULL && events != NULL )
{
success = events->cancel(event);
}
}
interrupts();
return success;
}
void TimerOneMulti::advanceTimer()
{
//Serial.print("Advancing ");
//Serial.println((int)events,HEX);
if (events != NULL)
{
do
{
if ( ! events->cancelled && events->timeRemainingAfterTick == 0)
{
events->callback(events->arg);
}
TimerEvent* oldEventsHead = events;
events = events->next;
//If we need to add the event back into the queue
if ( (oldEventsHead->periodic || oldEventsHead->timeRemainingAfterTick > 0 )&& ! oldEventsHead->cancelled)
{
//Note that in the case that its periodic and there is timeRemaining after tick, the fact that its periodic doesn't really matter right now
if ( oldEventsHead->timeRemainingAfterTick > 0 )
{
oldEventsHead->delta = oldEventsHead->timeRemainingAfterTick;
oldEventsHead->timeRemainingAfterTick = 0; //This will get set to the proper amount when we add it
}
else //Its periodic and the timer has expired (ie it doesn't have time remaining after tick)
{
//reset some things on the periodic event
oldEventsHead->delta = oldEventsHead->period;
}
//add it
oldEventsHead->next = NULL;
addEvent(oldEventsHead);
}
else
delete oldEventsHead;
}while(events != NULL && events->delta == 0);
if (events == NULL)
{
Timer1.stop();
}
else
{
timerFirstShot = true;
Timer1.setPeriod(events->delta);
Timer1.start();
Timer1.attachInterrupt(tick);
}
}
}
void TimerOneMulti::tick()
{
//Serial.println("tick");
//When you start the timer, it generates an interrupt immediately and then continues
//generating interrupts at the interval specified by period.
//But we don't want to do anything on the first shot, just on the second one
if ( timerFirstShot)
{
timerFirstShot = false;
return;
}
TimerOneMulti::getTimerController()->advanceTimer();
}