-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWaitForDigitalInterruptCommand.java
68 lines (57 loc) · 2.11 KB
/
WaitForDigitalInterruptCommand.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
package com.team254.frc2024.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import edu.wpi.first.wpilibj.AsynchronousInterrupt;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj2.command.Command;
/**
* Command for tripping banner sensors
*/
public class WaitForDigitalInterruptCommand extends Command {
public enum Edges {
RISING,
FALLING,
BOTH
}
private List<Consumer<Edges>> usafeActions = new ArrayList<>();
private AtomicBoolean tripped = new AtomicBoolean(false);
private AsynchronousInterrupt interrupt;
public WaitForDigitalInterruptCommand(DigitalInput dio, Edges triggerMode) {
setName("WaitForDigitalInterruptCommand");
interrupt = new AsynchronousInterrupt(dio, (triggeredByRisingEdge, triggeredByFallingEdge) -> {
System.out.println("Got tripped: " + triggeredByRisingEdge + ", " + triggeredByFallingEdge);
Edges trigger = triggeredByRisingEdge ? Edges.RISING : Edges.FALLING;
if (triggerMode == Edges.BOTH || trigger == triggerMode) {
synchronized (this) {
this.usafeActions.forEach(action -> action.accept(trigger));
}
this.tripped.set(true);
}
});
interrupt.setInterruptEdges(triggerMode == Edges.BOTH || triggerMode == Edges.RISING,
triggerMode == Edges.BOTH || triggerMode == Edges.FALLING);
}
@Override
public void initialize() {
tripped.set(false);
interrupt.enable();
}
@Override
public boolean isFinished() {
return tripped.get();
}
@Override
public void end(boolean interrupted) {
interrupt.disable();
}
/**
* Adds an immediate (not thread safe, not scheduled) execution to the
* interrupt. Use this with caution.
*/
public synchronized WaitForDigitalInterruptCommand addUnsafeImmediateAction(Consumer<Edges> triggerAction) {
usafeActions.add(triggerAction);
return this;
}
}