This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelayCommand.java
47 lines (38 loc) · 1.57 KB
/
DelayCommand.java
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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.SECONDS;
public class DelayCommand {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
//To run a method as a runnable event, do this:
/*
Runnable targetMethod = new Runnable() {
public void run() {
myObject.objectMethod();
}};
*/
//As a novice programmer, I'm not sure about what this really does and how safe it is.
//Delay is in milliseconds. TimeUnit can be changed.
//In event, input your method converted to a Runnable.
public void delay(Runnable event, int delay){
executorService.schedule(event, delay, TimeUnit.MILLISECONDS);
}
public void loop(Runnable event, int rate, LinearOpMode opMode){
final ScheduledFuture<?> beeperHandle =
executorService.scheduleAtFixedRate(event, 0, rate, TimeUnit.MILLISECONDS);
Runnable end = new Runnable() {
@Override
public void run() {
if(!opMode.opModeIsActive()){
beeperHandle.cancel(true);
}
}
};
final ScheduledFuture<?> cancelhandle =
executorService.scheduleAtFixedRate(event, 0, rate, TimeUnit.MILLISECONDS);
}
}