-
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.
- Loading branch information
Showing
7 changed files
with
185 additions
and
1 deletion.
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
34 changes: 34 additions & 0 deletions
34
src/main/java/org/team1540/robot2024/subsystems/led/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,34 @@ | ||
package org.team1540.robot2024.subsystems.led; | ||
|
||
import edu.wpi.first.wpilibj.AddressableLEDBuffer; | ||
import edu.wpi.first.wpilibj.util.Color; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public class LedPattern { | ||
private final Consumer<ZonedAddressableLEDBuffer> applier; | ||
public final boolean isDynamic; | ||
LedPattern(Consumer<ZonedAddressableLEDBuffer> applier, boolean isDynamic) { | ||
this.applier = applier; | ||
this.isDynamic = isDynamic; | ||
} | ||
|
||
void apply(ZonedAddressableLEDBuffer buffer) { | ||
this.applier.accept(buffer); | ||
} | ||
|
||
public static LedPattern solid(Color color) { | ||
return new LedPattern((buffer) -> { | ||
for (int i = 0; i<buffer.getLength();i++) { | ||
buffer.setLED(i, color); | ||
} | ||
}, false); | ||
} | ||
public static LedPattern alternating(Color a, Color b) { | ||
return new LedPattern((buffer) -> { | ||
for (int i = 0; i<buffer.getLength();i++) { | ||
buffer.setLED(i, i % 2 == 0 ? a : b); | ||
} | ||
}, false); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/team1540/robot2024/subsystems/led/LedPatternRainbow.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,23 @@ | ||
package org.team1540.robot2024.subsystems.led; | ||
|
||
import static java.lang.Math.round; | ||
|
||
public class LedPatternRainbow extends LedPattern { | ||
private final int speed; | ||
int initialHue = 0; | ||
public LedPatternRainbow(int speed) { | ||
super(null, true); | ||
this.speed = speed; | ||
} | ||
|
||
@Override | ||
void apply(ZonedAddressableLEDBuffer buffer) { | ||
for (int i = 0; i < buffer.getLength(); i++) { | ||
int hue = (initialHue + (i * 180 / buffer.getLength())) % 180; | ||
buffer.setHSV(i, hue, 255, 128); | ||
} | ||
initialHue += speed; | ||
initialHue %= 180; | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
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.Constants; | ||
|
||
|
||
import static org.team1540.robot2024.Constants.LED_STRIP_PORT_PWM; | ||
|
||
public class Leds extends SubsystemBase { | ||
|
||
private final AddressableLEDBuffer ledBuffer = new AddressableLEDBuffer(Constants.LED_STRIP_LENGTH); | ||
private final AddressableLED strip = new AddressableLED(LED_STRIP_PORT_PWM); | ||
private final ZonedAddressableLEDBuffer[] buffers = new ZonedAddressableLEDBuffer[ZONE_COUNT]; | ||
private final LedPattern[] patterns = new LedPattern[ZONE_COUNT]; | ||
|
||
public Leds() { | ||
strip.setLength(ledBuffer.getLength()); | ||
strip.setData(ledBuffer); | ||
strip.start(); | ||
|
||
buffers[Zone.ZONE1.ordinal()] = new ZonedAddressableLEDBuffer(ledBuffer, 0, 40, false); | ||
buffers[Zone.ZONE2.ordinal()] = new ZonedAddressableLEDBuffer(ledBuffer, 40, 80, false); | ||
} | ||
|
||
@Override | ||
public void periodic() { | ||
for (int i = 0; i < ZONE_COUNT;i++) { | ||
if (patterns[i].isDynamic) { | ||
patterns[i].apply(buffers[i]); | ||
} | ||
} | ||
strip.setData(ledBuffer); | ||
} | ||
|
||
public void setPattern(Zone zone, LedPattern pattern) { | ||
patterns[zone.ordinal()] = pattern; | ||
if (!pattern.isDynamic) { | ||
pattern.apply(buffers[zone.ordinal()]); | ||
} | ||
} | ||
|
||
|
||
private static final int ZONE_COUNT=Zone.values().length; | ||
public enum Zone { | ||
ZONE1, | ||
ZONE2; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
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; | ||
|
||
class ZonedAddressableLEDBuffer { | ||
private final AddressableLEDBuffer buffer; | ||
public final boolean isInverted; | ||
public final int start; | ||
public final int length; | ||
|
||
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) { | ||
System.out.println(this.getAbsoluteIndex(index)); | ||
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; | ||
} | ||
} | ||
} |