-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyFixedTimer.java
More file actions
executable file
·168 lines (160 loc) · 5.18 KB
/
myFixedTimer.java
File metadata and controls
executable file
·168 lines (160 loc) · 5.18 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
/*
* myFixedTimer.java
* Fixed signal timing timer
*
* Created on February 27, 2007, 2:26 PM
*/
/**
* @author Chen-Fu Liao
* Sr. Systems Engineer
* ITS Institute, ITS Laboratory
* Center For Transportation Studies
* University of Minnesota
* 200 Transportation and Safety Building
* 511 Washington Ave. SE
* Minneapolis, MN 55455
*/
public class myFixedTimer {
public myTimer greenTimer = new myTimer() ;
public myTimer yellowTimer = new myTimer() ;
public myTimer redTimer = new myTimer() ;
public int phase_stage = 0 ; // 1 - green, 2 - yellow, 3 - red
private Runnable runThread0 ;
private Thread tTimer ;
private boolean timerPaused = true ;
private boolean timesup = false ;
private boolean stepSim_flag = false ; // sec by sec simulation
/** Creates a new instance of myFixedTimer */
public myFixedTimer() {
init() ;
}
public boolean getStepSim() {
return stepSim_flag ;
}
public void setStepSim(boolean _state) {
stepSim_flag = _state ;
}
public myFixedTimer(float green, float yellow, float red) {
greenTimer.setTime(green) ;
yellowTimer.setTime(yellow) ;
redTimer.setTime(red) ;
init() ;
}
public void setDurations(float green, float yellow, float red) {
greenTimer.setTime(green) ;
yellowTimer.setTime(yellow) ;
redTimer.setTime(red) ;
// System.out.println("timer G Y R = "+greenTimer.getTime()+","+yellowTimer.getTime()+","+redTimer.getTime()) ;
}
private void init() {
runThread0 = new Runnable() {
public void run() {
while (true) {
if (!timerPaused){
// go thru green yellow & red sequence
if (phase_stage==0) {
phase_stage=1 ; // get green phase started
greenTimer.start() ;
//System.out.println("greenTimer starts...") ;
} else if (phase_stage==1 && greenTimer.isTimeUp()) {
phase_stage=2 ;
yellowTimer.start() ;
//System.out.println("yellowTimer starts...") ;
} else if (phase_stage==2 && yellowTimer.isTimeUp()) {
phase_stage=3 ;
redTimer.start() ;
//System.out.println("redTimer starts...") ;
} else if (phase_stage==3 && redTimer.isTimeUp()) {
timesup = true ;
phase_stage=-1 ;
}
try {Thread.sleep(100) ;}
catch (InterruptedException ie) {} ;
} else { // motion paused
// Get current time
//tLast = System.currentTimeMillis();
//tTimer.yield();
try {Thread.sleep(200) ;}
catch (InterruptedException ie) {} ;
}
}
} // void run
} ; // runThread 0
tTimer = new Thread(runThread0, "FixedTimer") ;
tTimer.start() ;
}
public void start() {
greenTimer.stepSim = stepSim_flag ;
yellowTimer.stepSim = stepSim_flag ;
redTimer.stepSim = stepSim_flag ;
phase_stage = 0 ;
timesup = false ;
timerPaused = false ;
}
public void resume() {
greenTimer.stepSim = stepSim_flag ;
yellowTimer.stepSim = stepSim_flag ;
redTimer.stepSim = stepSim_flag ;
timerPaused = false ;
switch (phase_stage) {
case 1:
greenTimer.resume() ;
break ;
case 2:
yellowTimer.resume() ;
break ;
case 3:
redTimer.resume() ;
break ;
} // switch
} // resume
public void pause() {
timerPaused = true ;
switch (phase_stage) {
case 1:
greenTimer.pause() ;
break ;
case 2:
yellowTimer.pause() ;
break ;
case 3:
redTimer.pause() ;
break ;
} // switch
}
// reset timer
public void reset() {
timesup = false ;
phase_stage = 0 ;
greenTimer.reset() ;
yellowTimer.reset() ;
redTimer.reset() ;
}
public boolean isGreenTimeUp() {
return greenTimer.isTimeUp() ;
}
public boolean isYellowTimeUp() {
return yellowTimer.isTimeUp() ;
}
public boolean isRedTimeUp() {
return redTimer.isTimeUp() ;
}
public boolean isTimeUp() {
return timesup ;
}
public void clear() {
timesup = false ;
}
public boolean getTimerPaused() {
return timerPaused ;
}
public void setTimerPaused(boolean status) {
timerPaused = status ;
}
public boolean getTimesup() {
return timesup ;
}
public void setTimesup(boolean status) {
timesup = status ;
}
}