-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyTimer.java
More file actions
executable file
·187 lines (173 loc) · 5.72 KB
/
myTimer.java
File metadata and controls
executable file
·187 lines (173 loc) · 5.72 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
/*
* myTimer.java
* Basic count up or down timer used for fixed green/yellow/red phases
* Created on June 22, 2006, 9:59 AM
*/
/**
* @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 myTimer {
private int flag = 0 ; // 0-count down, >=1-count up
private float countValue =0f ; // Count up/down time
private float currentCount=0f ; // current timer count
private long tLast, tNow ;
private Runnable runThread0 ;
private Thread tTimer ;
private boolean timerPaused = true ;
private boolean timesup = false ;
public boolean stepSim = false ;
private boolean first_start = true ;
public float elapsedTimeSec = 0f ;
public boolean step_finished = true ;
/** Creates a new instance of myTimer */
public myTimer() {
init() ;
}
public myTimer(int dir) {
flag = dir ;
init() ;
}
private void init() {
runThread0 = new Runnable() {
public void run() {
while (true) {
if (!timerPaused){
step_finished = false ;
// Get elapsed time in milliseconds
long tNow = System.currentTimeMillis() ;
if (!first_start) {
long elapsedTimeMillis = tNow-tLast;
// Get elapsed time in seconds
elapsedTimeSec = elapsedTimeMillis/1000f;
if (stepSim) {
elapsedTimeSec = 1f ; // 1 seconf step simulation
}
if (flag==0) { // count down
currentCount -= elapsedTimeSec ;
//System.out.println("cur="+currentCount+",elapse="+elapsedTimeSec) ;
if (currentCount<=0f) {
// time is up
currentCount=0f ;
timesup = true ; // set time up flag
timerPaused = true ; // pause timer
}
} else { // count up
currentCount += elapsedTimeSec ;
if (currentCount>=countValue) {
// time is up
timesup = true ; // set time up flag
timerPaused = true ; // pause timer
} // end if currentCount>=countValue?
} // if flag==0
} else {
first_start = false ;
}
tLast = tNow ;
if (stepSim) {
timerPaused = true ;
step_finished = true ;
}
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, "Timer-X") ;
tTimer.start() ;
}
public void setTime(float _time) {
countValue = _time ;
}
public float getTime() {
return countValue ;
}
public void start() {
first_start = true ;
timesup = false ;
if (flag==0) { // count down
currentCount = countValue ;
} else { // count up
currentCount = 0f ;
}
timerPaused = false ;
tLast = System.currentTimeMillis();
}
public void resume() {
timerPaused = false ;
tLast = System.currentTimeMillis();
}
public void clear() {
timesup = false ;
}
public void pause() {
timerPaused = true ;
}
// reset timer
public void reset() {
timesup = false ;
if (flag==0) { // count down
currentCount = countValue ;
} else { // count up
currentCount = 0f ;
}
}
public boolean isTimeUp() {
return timesup ;
}
public float getSetCount() {
return countValue ;
}
public float getCount() {
return currentCount ;
}
public void setCount(float _value) {
currentCount = _value ;
}
public float getCountValue() {
return countValue ;
}
public void setCountValue(float _value) {
countValue = _value ;
}
public int getFlag() {
return flag ;
}
public void setFlag(int _flag) {
flag = _flag ;
}
public boolean getTimerPaused() {
return timerPaused ;
}
public void setTimerPaused(boolean status) {
timerPaused = status ;
}
public boolean getTimesup() {
return timesup ;
}
public void setTimesup(boolean status) {
timesup = status ;
}
public boolean getFirstStart() {
return first_start ;
}
public void setFirstStart(boolean status) {
first_start = status ;
}
//public void destroy() {
// tTimer.destroy() ;
//}
}