Skip to content

Commit

Permalink
add color fading
Browse files Browse the repository at this point in the history
took way too long to debug
  • Loading branch information
quackitsquinn committed Apr 1, 2024
1 parent 805f266 commit 6a3dcd9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ public static class LightStrips {
/** The count of LEDs for the underglow light strip. */
public static final int UNDERGLOW_LED_COUNT = 150;

/** The speed of the fade animation. [0, 1] */
public static final double STRIP_FADE_AMOUNT = 0.1;

public static final class Colors {
/** The color for the lights when the robot is disabled. */
public static final Color DISABLED_COLOR = Color.fromHSV(15, 255, 100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public void initialize() {

// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {}
public void execute() {
robotLights.updateRainbow();
}

// Called once the command ends or is interrupted.
@Override
Expand Down
42 changes: 37 additions & 5 deletions src/main/java/frc/robot/subsystems/AddressableLightStrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
import edu.wpi.first.wpilibj.AddressableLEDBuffer;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
import org.jspecify.annotations.Nullable;

public class AddressableLightStrip extends SubsystemBase {

private final AddressableLED ledStrip;
private final AddressableLEDBuffer buffer;
private int rainbowHue = 0;
@Nullable private Color targetStripColor = null;

/**
* Creates a new AddressableLightStrip
Expand All @@ -32,6 +35,7 @@ public AddressableLightStrip(int pwmPort, int lightCount) {
this.ledStrip = new AddressableLED(pwmPort);
this.buffer = new AddressableLEDBuffer(lightCount);
this.ledStrip.setLength(lightCount);
updateRainbow();
}

/**
Expand All @@ -43,6 +47,7 @@ public AddressableLightStrip(int pwmPort, int lightCount) {
* @param b The blue component
*/
public void setRgbLight(int lightNumber, int r, int g, int b) {
this.targetStripColor = null;
this.buffer.setRGB(lightNumber, r, g, b);
}

Expand All @@ -55,6 +60,7 @@ public void setRgbLight(int lightNumber, int r, int g, int b) {
* @param v The value component
*/
public void setHSVLight(int lightNumber, int h, int s, int v) {
this.targetStripColor = null;
this.buffer.setHSV(lightNumber, h, s, v);
}

Expand All @@ -65,6 +71,7 @@ public void setHSVLight(int lightNumber, int h, int s, int v) {
* @param color
*/
public void setColorLight(int lightNumber, Color color) {
this.targetStripColor = null;
this.buffer.setLED(lightNumber, color);
}

Expand All @@ -74,15 +81,16 @@ public void setColorLight(int lightNumber, Color color) {
* @param color The color to set the strip to.
*/
public void setStripColor(Color color) {
for (int i = 0; i < this.buffer.getLength(); i++) {
this.buffer.setLED(i, color);
}
targetStripColor = color;
}

private static double lerp(double v0, double v1, double amount) {
if (v0 == v1) {
return v0;
}
if (v0 < v1) {
return (1 - amount) * v1 + amount * v0;
}
return (1 - amount) * v0 + amount * v1;
}

Expand Down Expand Up @@ -125,17 +133,41 @@ public void stopLights() {
this.ledStrip.stop();
}

/** Updates the LED rainbow pattern. Must be ran for it to animate. */
public void updateRainbow() {
this.targetStripColor = null;
rainbowHue++;
for (int led = 0; led < buffer.getLength(); led++) {
this.buffer.setHSV(led, (rainbowHue + led) % 180, 255, 255);
}
this.flush();
}

private boolean stripAtTargetColor() {
if (this.targetStripColor == null) {
// targetStripColor being null means that the light strip has been manually changed, and we
// should not touch the values.
return true;
}
for (int led = 0; led < this.buffer.getLength(); led++) {
// This is a kinda gross way to do this, but it removes the like 10 Math.floors or (int)s
if (this.targetStripColor.toHexString() != this.buffer.getLED(led).toHexString()) {
return false;
}
}
return true;
}

@Override
public void periodic() {
updateRainbow();
// This method will be called once per scheduler run
if (!stripAtTargetColor()) {
for (int led = 0; led < this.buffer.getLength(); led++) {
Color c =
lerpColors(
this.buffer.getLED(led), targetStripColor, Constants.LightStrips.STRIP_FADE_AMOUNT);
this.buffer.setLED(led, c);
}
this.flush();
}
}
}

0 comments on commit 6a3dcd9

Please sign in to comment.