This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoTransitioner.java
73 lines (63 loc) · 2.98 KB
/
AutoTransitioner.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
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
package org.firstinspires.ftc.teamcode;
import android.util.Log;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
//import org.firstinspires.ftc.robotcontroller.internal.FtcRobotControllerActivity;
import org.firstinspires.ftc.robotcore.internal.opmode.OpModeManagerImpl;
/**
* Created by KNO3 Robotics
* AutoTransitioner is a utility to automatically initialize the teleop program of your choice
* after the autonomous period ends. To use AutoTransitioner, go to your OpMode/LinearOpMode,
* and place the following line of code in your init() method or before your waitForStart()
* (for OpMode and LinearOpMode, respectively):
* AutoTransitioner.transitionOnStop(this, "Robot Teleop");
* Where 'Robot Teleop' is replaced with the NAME of your teleop program. See full documentation
* on kno3.net/resources for more info.
*/
public class AutoTransitioner extends Thread {
private static final AutoTransitioner INSTANCE = new AutoTransitioner(); //Create singleton instance
private OpMode onStop;
private String transitionTo;
private OpModeManagerImpl opModeManager;
private AutoTransitioner() {
this.start(); //Start the watcher thread
}
@Override
public void run() {
try {
while (true) { //Loop
synchronized (this) { //Synchronized to prevent weird conditions
//If there is a transition set up and the active op mode is no longer the one
//the transition was set up with, proceed with the transition
if (onStop != null && opModeManager.getActiveOpMode() != onStop) {
Thread.sleep(1000); //Wait 1 second to prevent weird conditions
opModeManager.initActiveOpMode(transitionTo); //Request initialization of the teleop
reset(); //Reset the AutoTransitioner
}
}
Thread.sleep(50); //Sleep 50 seconds to minimize performance impact to the rest of your program
}
} catch (InterruptedException ex) {
//Log.e(FtcRobotControllerActivity.TAG, "AutoTransitioner shutdown, thread interrupted");
}
}
private void setNewTransition(OpMode onStop, String transitionTo) {
synchronized (this) { //Synchronized to prevent wierd conditions
this.onStop = onStop;
this.transitionTo = transitionTo;
this.opModeManager = (OpModeManagerImpl) onStop.internalOpModeServices; //Store OpModeManagerImpl
}
}
private void reset() {
this.onStop = null;
this.transitionTo = null;
this.opModeManager = null;
}
/**
* Setup the next transition
* @param onStop The program you'll be transitioning from (usually 'this')
* @param transitionTo The name of the program you want to transition to
*/
public static void transitionOnStop(OpMode onStop, String transitionTo) {
INSTANCE.setNewTransition(onStop, transitionTo);
}
}