-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: LED Framework * refactor: clean up modifiers, loops * feat: add wave pattern * refactor: change led pattern structure * feat: add alternating group sizes * feat: Fire and breathing LED patterns :D * feat: RSL LEDs * feat: allow hue selection based on Color * refactor: clean up pattern files * feat: add triaging * feat: fix bindings, add led tuning mode --------- Co-authored-by: David :D <[email protected]>
- Loading branch information
1 parent
e6fc7ae
commit 7ceb9aa
Showing
13 changed files
with
515 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/org/team1540/robot2024/subsystems/led/LedTriager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.team1540.robot2024.subsystems.led; | ||
|
||
import org.team1540.robot2024.subsystems.led.patterns.LedPattern; | ||
import org.team1540.robot2024.subsystems.led.patterns.LedPatternFlame; | ||
import org.team1540.robot2024.subsystems.led.patterns.LedPatternRainbow; | ||
|
||
|
||
public class LedTriager { | ||
private final LedPattern[] patterns = new LedPattern[Leds.CRITICALITY_COUNT]; | ||
private final LedPattern defaultPattern = new LedPatternRainbow(1); | ||
private boolean isNew = true; | ||
public LedPattern getPattern() { | ||
for (int i = patterns.length -1; i >= 0; i--) { | ||
if (patterns[i] != null) { | ||
return patterns[i]; | ||
} | ||
} | ||
return defaultPattern; | ||
} | ||
|
||
public boolean shouldRefresh() { | ||
final boolean val = isNew || getPattern().isDynamic(); | ||
isNew = false; | ||
return val; | ||
} | ||
|
||
public void clearPattern(Leds.PatternCriticality criticality) { | ||
patterns[criticality.ordinal()] = null; | ||
isNew = true; | ||
} | ||
|
||
public boolean addPattern(LedPattern pattern, Leds.PatternCriticality criticality) { | ||
patterns[criticality.ordinal()] = pattern; | ||
isNew = true; | ||
return getPattern() == pattern; | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/org/team1540/robot2024/subsystems/led/Leds.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.team1540.robot2024.subsystems.led; | ||
|
||
import edu.wpi.first.wpilibj.AddressableLED; | ||
import edu.wpi.first.wpilibj.AddressableLEDBuffer; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
|
||
import org.team1540.robot2024.subsystems.led.patterns.LedPattern; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static org.team1540.robot2024.Constants.Leds.*; | ||
|
||
public class Leds extends SubsystemBase { | ||
private final AddressableLEDBuffer ledBuffer = new AddressableLEDBuffer(LED_STRIP_LENGTH); | ||
private final AddressableLED strip = new AddressableLED(LED_STRIP_PORT_PWM); | ||
private final ZonedAddressableLEDBuffer[] buffers = new ZonedAddressableLEDBuffer[ZONE_COUNT]; | ||
private final LedTriager[] patterns = new LedTriager[ZONE_COUNT]; | ||
|
||
public Leds() { | ||
strip.setLength(ledBuffer.getLength()); | ||
strip.setData(ledBuffer); | ||
strip.start(); | ||
|
||
buffers[Zone.ELEVATOR_BACK.ordinal()] = new ZonedAddressableLEDBuffer(ledBuffer, 1, 41, false); | ||
for (int i = 0; i < ZONE_COUNT;i++) { | ||
patterns[i] = new LedTriager(); | ||
} | ||
} | ||
|
||
@Override | ||
public void periodic() { | ||
for (int i = 0; i < ZONE_COUNT;i++) { | ||
if (patterns[i].shouldRefresh()) { | ||
patterns[i].getPattern().apply(buffers[i]); | ||
} | ||
} | ||
strip.setData(ledBuffer); | ||
} | ||
|
||
public void setPattern(Zone zone, LedPattern pattern, PatternCriticality criticality) { | ||
patterns[zone.ordinal()].addPattern(pattern, criticality); | ||
pattern.setLength(buffers[zone.ordinal()].getLength()); | ||
} | ||
|
||
public void setPattern(Zone zone, LedPattern pattern) { | ||
setPattern(zone, pattern, PatternCriticality.INFO); | ||
} | ||
|
||
public void clearPattern(Zone zone, PatternCriticality criticality) { | ||
patterns[zone.ordinal()].clearPattern(criticality); | ||
} | ||
|
||
public void setFatalPattern(Supplier<LedPattern> patternSupplier) { | ||
for (int i = 0; i<ZONE_COUNT;i++) { | ||
LedPattern pattern = patternSupplier.get(); | ||
patterns[i].addPattern(pattern, PatternCriticality.FATAL); | ||
pattern.setLength(buffers[i].getLength()); | ||
} | ||
} | ||
|
||
public void clearFatalPattern() { | ||
for (int i = 0; i<ZONE_COUNT;i++) { | ||
patterns[i].clearPattern(PatternCriticality.FATAL); | ||
} | ||
} | ||
|
||
|
||
private static final int ZONE_COUNT=Zone.values().length; | ||
public enum Zone { | ||
ELEVATOR_BACK, | ||
} | ||
static final int CRITICALITY_COUNT=PatternCriticality.values().length; | ||
public enum PatternCriticality { | ||
DECORATION, | ||
INFO, | ||
CRITICAL, | ||
FATAL | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/org/team1540/robot2024/subsystems/led/ZonedAddressableLEDBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.team1540.robot2024.subsystems.led; | ||
|
||
import edu.wpi.first.wpilibj.AddressableLEDBuffer; | ||
import edu.wpi.first.wpilibj.DriverStation; | ||
import edu.wpi.first.wpilibj.util.Color; | ||
import edu.wpi.first.wpilibj.util.Color8Bit; | ||
|
||
public class ZonedAddressableLEDBuffer { | ||
private final boolean isInverted; | ||
private final int start; | ||
private final int length; | ||
private final AddressableLEDBuffer buffer; | ||
|
||
public ZonedAddressableLEDBuffer(AddressableLEDBuffer buffer, int start, int end, boolean isInverted) { | ||
if (start > end) { | ||
throw new IllegalArgumentException("start must be less than end"); | ||
} | ||
this.buffer = buffer; | ||
this.isInverted = isInverted; | ||
this.start = start; | ||
this.length = end - start; | ||
} | ||
|
||
public void setRGB(int index, int r, int g, int b) { | ||
buffer.setRGB(this.getAbsoluteIndex(index), r, g, b); | ||
} | ||
|
||
public void setHSV(int index, int h, int s, int v) { | ||
buffer.setHSV(this.getAbsoluteIndex(index), h, s, v); | ||
} | ||
|
||
public void setLED(int index, Color color) { | ||
buffer.setLED(this.getAbsoluteIndex(index), color); | ||
} | ||
|
||
public void setLED(int index, Color8Bit color) { | ||
buffer.setLED(this.getAbsoluteIndex(index), color); | ||
} | ||
|
||
public Color getLED(int index) { | ||
return buffer.getLED(this.getAbsoluteIndex(index)); | ||
} | ||
|
||
public Color8Bit getLED8Bit(int index) { | ||
return buffer.getLED8Bit(this.getAbsoluteIndex(index)); | ||
} | ||
|
||
public int getLength() { | ||
return this.length; | ||
} | ||
|
||
private int getAbsoluteIndex(int index) { | ||
if (index >= length) { | ||
DriverStation.reportWarning("led index out of bounds", false); | ||
return 0; | ||
} | ||
if (this.isInverted) { | ||
return this.start + length - 1 - index; | ||
} else { | ||
return this.start + index; | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/org/team1540/robot2024/subsystems/led/patterns/LedPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.team1540.robot2024.subsystems.led.patterns; | ||
|
||
import edu.wpi.first.wpilibj.util.Color; | ||
import org.team1540.robot2024.subsystems.led.ZonedAddressableLEDBuffer; | ||
|
||
public abstract class LedPattern { | ||
private final boolean isDynamic; | ||
|
||
protected LedPattern(boolean isDynamic) { | ||
this.isDynamic = isDynamic; | ||
} | ||
|
||
public final boolean isDynamic() { | ||
return isDynamic; | ||
} | ||
|
||
public abstract void apply(ZonedAddressableLEDBuffer buffer); | ||
public void setLength(int length) {} | ||
|
||
protected static int getHue(Color color) { | ||
final int red = (int) color.red * 255; | ||
final int green = (int) color.green * 255; | ||
final int blue = (int) color.blue * 255; | ||
float min = Math.min(Math.min(red, green), blue); | ||
float max = Math.max(Math.max(red, green), blue); | ||
|
||
if (min == max) { | ||
return 0; | ||
} | ||
|
||
float hue = 0f; | ||
if (max == red) { | ||
hue = (green - blue) / (max - min); | ||
|
||
} else if (max == green) { | ||
hue = 2f + (blue - red) / (max - min); | ||
|
||
} else { | ||
hue = 4f + (red - green) / (max - min); | ||
} | ||
|
||
hue = hue * 60; | ||
if (hue < 0) hue = hue + 360; | ||
|
||
return Math.round(hue); | ||
} | ||
} |
Oops, something went wrong.