-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightStrip.java
More file actions
58 lines (52 loc) · 2.14 KB
/
LightStrip.java
File metadata and controls
58 lines (52 loc) · 2.14 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
package frc.robot.subsystems;
import edu.wpi.first.wpilibj.motorcontrol.Spark;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.constants.Constants;
import frc.robot.utils.BlinkinPattern;
import frc.robot.utils.smartshuffleboard.SmartShuffleboard;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BooleanSupplier;
public class LightStrip extends SubsystemBase {
private final Spark colorSensorPort;
private final AtomicReference<BlinkinPattern> pattern = new AtomicReference<>(BlinkinPattern.BLACK);
private final ConcurrentHashMap<BooleanSupplier, BlinkinPattern> predicateLightEvents = new ConcurrentHashMap<>();
private final AtomicBoolean running = new AtomicBoolean(true);
public LightStrip(int port) {
this.colorSensorPort = new Spark(port);
new Thread(() -> {
while (running.get()){
for (BooleanSupplier supplier : predicateLightEvents.keySet()){
if (supplier.getAsBoolean()){
setPattern(predicateLightEvents.get(supplier));
predicateLightEvents.remove(supplier);
break;
}
}
try {
Thread.sleep(40);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
}
public void setPattern(BlinkinPattern pattern) {
this.pattern.set(pattern);
colorSensorPort.set(pattern.getPwm());
if (Constants.LED_DEBUG){
SmartShuffleboard.put("Lightstrip", "pwm", pattern.getPwm());
SmartShuffleboard.put("Lightstrip", "pattern", pattern.toString());
}
}
public BlinkinPattern getPattern() {
return pattern.get();
}
public void scheduleOnTrue(BooleanSupplier callable, BlinkinPattern pattern) {
predicateLightEvents.put(callable, pattern);
}
public void setRunning(boolean running) {
this.running.set(running);
}
}