-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPausableTimer.cpp
More file actions
46 lines (39 loc) · 1.02 KB
/
PausableTimer.cpp
File metadata and controls
46 lines (39 loc) · 1.02 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
/** Implements a timer that outputs the time in milliseconds
* since the program has started, but can be paused by
* setting a flag
* @author Jordan Burklund
* @date Oct. 2015
**/
#include "PausableTimer.h"
/** Gets the time in milliseconds of the timer **/
unsigned long getPauseMillis() {
//Disable interrupts to copy the variable
unsigned long temp;
uint8_t SaveSREG = SREG; //Save the interrupt flags
cli(); //Disable interrupts
temp = _PAUSE_TIME_COUNT; //Copy the value
SREG = SaveSREG; //Re-enable interrupts
return temp;
}
/** Initialize the pausable timer **/
void initializePauseTimer() {
_SHOULD_PAUSE = false;
Timer1.initialize(1000); //1000 usec = 1 msec
Timer1.attachInterrupt(incrementPauseTime);
}
/** Increment the milliseconds count
* only if it is not paused
**/
void incrementPauseTime() {
if(!_SHOULD_PAUSE) {
_PAUSE_TIME_COUNT++;
}
}
/** Pause the timer **/
void pauseTimer() {
_SHOULD_PAUSE = true;
}
/** Unpause the timer **/
void resumeTimer() {
_SHOULD_PAUSE = false;
}