-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimerSys.cpp
More file actions
88 lines (75 loc) · 1.75 KB
/
TimerSys.cpp
File metadata and controls
88 lines (75 loc) · 1.75 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
/*
* TimerSys.cpp
*
* This class represents the system timer.
* Encapsulates a timer thread.
*/
#include "TimerSys.h"
/**
* Function: Constructor
* Description: Initalizes the timer.
*
* @param duration: The total duration of the timer, the point it starts decrementing.
* @param sim_spped: The simulation speed.
*/
TimerSys::TimerSys( int duration, float sim_speed ){
// Define length of the down counter:
timerVal = duration;
// Define speed at which the timer will decrement:
speed = sim_speed;
// Disable timer:
timerOn = false;
inRunMode = false;
// Instantiate the pthread for later use:
pthread_mutex_init(&mutex, NULL);
}// TimerSys()
TimerSys::~TimerSys(){/* Unused */}
/**
* Function: startTimer()
* Description: Starts the timer and creats the thread.
*/
void TimerSys::startTimer(){
timerOn = true;
inRunMode = true;
pthread_create(&thread, NULL, TimerThreadFunc, this);
}
/**
* Function: readTimer()
* Description: Reads the current time of the timer
*
* @return int current value of the timer
*/
int TimerSys::readTimer(){
return timerVal;
}
/**
* Function: stopTimer()
* Description: Stops the timer and cleans the timer thread.
*/
void TimerSys::stopTimer(){
timerOn = false;
pthread_join(thread,NULL);
}
/**
* Function: isRunning()
* Description: Checks whether the timer is still running
*
* @return bool true if timer is still running
*/
bool TimerSys::isRunning(){
return inRunMode;
}
/**
* Function: timerDecrementer()
* Description: Decrements the timer. This is the function passed into
* the thread. Stops when timer is shut off.
*/
void TimerSys::timerDecrementer(){
while(timerOn == true){
usleep(speed);
timerVal-=TIME_INCR;
if(timerVal <= 0){
inRunMode = false;
}
}
}// timerDecrementer()