Skip to content

Commit 3d23ee3

Browse files
author
Kevin Harrington
committed
adding pausable time
1 parent c2aedca commit 3d23ee3

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.neuronrobotics.sdk.pid;
2+
3+
public interface IPauseTimeListener {
4+
5+
public void pause(boolean val);
6+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.neuronrobotics.sdk.pid;
2+
3+
import java.util.ArrayList;
4+
5+
public class PausableTime {
6+
private static long timePaused = 0;
7+
private static long durationPaused = 0;
8+
private static boolean paused =false;
9+
private static ArrayList<IPauseTimeListener> listeners = new ArrayList<IPauseTimeListener>();
10+
11+
public static long currentTimeMillis() {
12+
if(!paused)
13+
return System.currentTimeMillis()-durationPaused;
14+
return timePaused;
15+
}
16+
17+
public static void pause(boolean val) {
18+
if(val)
19+
timePaused=System.currentTimeMillis();
20+
else
21+
durationPaused+=(System.currentTimeMillis()-timePaused);
22+
23+
paused=val;
24+
for(int i=0;i<listeners.size();i++)
25+
listeners.get(i).pause(val);
26+
}
27+
28+
29+
public static void step(long ms) {
30+
new Thread(()->{
31+
boolean start = paused;
32+
pause(false);
33+
sleep(ms);
34+
pause(start);
35+
}).start();
36+
}
37+
38+
public static void sleep(long durationMS) {
39+
try {
40+
Thread.sleep(durationMS);
41+
} catch (InterruptedException e) {
42+
throw new RuntimeException(e);
43+
}
44+
while(paused) {
45+
try {
46+
Thread.sleep(1);
47+
} catch (InterruptedException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
51+
}
52+
53+
public static void addIPauseTimeListener(IPauseTimeListener l) {
54+
if(listeners.contains(l))
55+
return;
56+
listeners.add(l);
57+
}
58+
public static void removeIPauseTimeListener(IPauseTimeListener l) {
59+
if(listeners.contains(l))
60+
listeners.remove(l);
61+
}
62+
}

0 commit comments

Comments
 (0)