-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpectrumState.java
More file actions
86 lines (74 loc) · 2.52 KB
/
SpectrumState.java
File metadata and controls
86 lines (74 loc) · 2.52 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
package frc.spectrumLib;
import edu.wpi.first.wpilibj.event.EventLoop;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import java.util.HashMap;
import java.util.function.BooleanSupplier;
public class SpectrumState extends Trigger {
private static final HashMap<String, Boolean> stateConditions = new HashMap<>();
private String name;
private boolean value = false;
/**
* Create a new EventTrigger. This will run on the EventScheduler's event loop, which will be
* polled any time a path following command is running.
*
* @param name The name of the event. This will be the name of the event marker in the GUI
*/
public SpectrumState(String name) {
super(pollCondition(name));
this.name = name;
}
/**
* Create a new EventTrigger that gets polled by the given event loop instead of the
* EventScheduler
*
* @param eventLoop The event loop to poll this trigger
* @param name The name of the event. This will be the name of the event marker in the GUI
*/
public SpectrumState(EventLoop eventLoop, String name) {
super(eventLoop, pollCondition(name));
}
public Command set(boolean value) {
return Commands.runOnce(
() -> {
this.value = value;
setCondition(name, value);
});
}
public Command setTrue() {
return set(true);
}
public Command setFalse() {
return set(false);
}
public Command toggle() {
return Commands.runOnce(
() -> {
this.value = !this.value;
setCondition(name, value);
});
}
/**
* Create a boolean supplier that will poll(check) a condition.
*
* @param name The name of the event
* @return A boolean supplier to poll the event's condition
*/
private static BooleanSupplier pollCondition(String name) {
// Ensure there is a condition in the map for this name
if (!stateConditions.containsKey(name)) {
stateConditions.put(name, false);
}
return () -> stateConditions.get(name);
}
/**
* Set the value of an event condition
*
* @param name The name of the condition
* @param value The value of the condition
*/
protected static void setCondition(String name, boolean value) {
stateConditions.put(name, value);
}
}