diff --git a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java index c9c329b5..41fe8cbc 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -40,7 +40,7 @@ public static void initiateAddressableLED(int port, int totalAmountOfLEDs) { } /** - * Constructs a new AddressableLEDStrip. Before any commands are sent to the LED strip, the setAddressableLED and setAddressableLEDBuffer methods must be called. + * Constructs a new AddressableLEDStrip. Before any commands are sent to the LED strip, the {@link AddressableLEDStrip#initiateAddressableLED(int, int)} method must be called. * * @param inverted whether the LED strip is inverted * @param numberOfLEDs the amount of LEDs in the strip @@ -58,12 +58,12 @@ public void periodic() { } @Override - void clearLEDColors() { - staticColor(Color.kBlack); + protected void clearLEDColors() { + setStaticColor(Color.kBlack); } @Override - void blink(Color firstColor, double speed) { + protected void blink(Color color, double speed) { final double correctedSpeed = 1 - speed; final double currentTime = Timer.getTimestamp(); @@ -73,19 +73,19 @@ void blink(Color firstColor, double speed) { } if (isLEDAnimationChanged) { - staticColor(firstColor); + setStaticColor(color); return; } clearLEDColors(); } @Override - void staticColor(Color color) { - setLEDColors(color, 0, numberOfLEDs - 1); + protected void staticColor(Color color) { + setStaticColor(color); } @Override - void breathe(Color color, int breathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + protected void breathe(Color color, int numberOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { clearLEDColors(); final boolean correctedInverted = this.inverted != inverted; final double moveLEDTimeSeconds = 1 - speed; @@ -99,12 +99,12 @@ void breathe(Color color, int breathingLEDs, double speed, boolean inverted, Lar lastBreatheLED++; } - checkIfBreathingHasHitEnd(breathingLEDs, correctedInverted, bounceMode); - setBreathingLEDs(color, breathingLEDs, bounceMode); + checkIfBreathingHasHitEnd(numberOfBreathingLEDs, correctedInverted, bounceMode); + setBreathingLEDs(color, numberOfBreathingLEDs, bounceMode); } @Override - void colorFlow(Color color, double speed, boolean inverted) { + protected void colorFlow(Color color, double speed, boolean inverted) { clearLEDColors(); final boolean correctedInverted = this.inverted != inverted; final double moveLEDTimeSeconds = 1 - speed; @@ -123,13 +123,13 @@ void colorFlow(Color color, double speed, boolean inverted) { } @Override - void alternateColor(Color firstColor, Color secondColor) { + protected void alternateColor(Color firstColor, Color secondColor) { for (int i = 0; i < numberOfLEDs; i++) LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? firstColor : secondColor); } @Override - void rainbow(double brightness, double speed, boolean inverted) { + protected void rainbow(double brightness, double speed, boolean inverted) { final boolean correctedInverted = this.inverted != inverted; final int adjustedBrightness = (int) (brightness * 255); final int hueIncrement = (int) (speed * 8); @@ -150,9 +150,9 @@ void rainbow(double brightness, double speed, boolean inverted) { } @Override - void sectionColor(Supplier[] colors) { + protected void sectionColor(Supplier[] colors) { final int amountOfSections = colors.length; - final int ledsPerSection = (int) Math.floor(numberOfLEDs / amountOfSections); + final int ledsPerSection = (int) Math.floor((double) numberOfLEDs / amountOfSections); for (int i = 0; i < amountOfSections; i++) setLEDColors( @@ -163,7 +163,7 @@ void sectionColor(Supplier[] colors) { } @Override - void resetLEDSettings() { + protected void resetLEDSettings() { lastBreatheLED = indexOffset; lastLEDAnimationChangeTime = Timer.getTimestamp(); rainbowFirstPixelHue = 0; @@ -171,6 +171,10 @@ void resetLEDSettings() { amountOfColorFlowLEDs = 0; } + private void setStaticColor(Color color) { + setLEDColors(color, 0, numberOfLEDs - 1); + } + private void checkIfBreathingHasHitEnd(int amountOfBreathingLEDs, boolean inverted, LarsonAnimation.BounceMode bounceMode) { final int bounceModeAddition = switch (bounceMode) { case Back -> amountOfBreathingLEDs; diff --git a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java index a59dfdf2..a9157c6f 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -21,7 +21,7 @@ public class CANdleLEDStrip extends LEDStrip { * @param candle the CANdle instance to be used */ public static void setCANdle(CANdle candle) { - if (CANDLE == null) + if (CANDLE == null && !RobotHardwareStats.isSimulation()) CANDLE = candle; } @@ -37,7 +37,7 @@ public static void setTotalAmountOfLEDs(int totalAmountOfLEDs) { } /** - * Constructs a new CANdleLEDStrip. Before any commands are sent to the LED strip, the setCANdle method must be called. + * Constructs a new CANdleLEDStrip. Before any commands are sent to the LED strip, the {@link CANdleLEDStrip#setCANdle(CANdle)} method must be called. * * @param inverted whether the LED strip is inverted * @param numberOfLEDs the amount of LEDs in the strip @@ -50,17 +50,17 @@ public static void setTotalAmountOfLEDs(int totalAmountOfLEDs) { } @Override - void clearLEDColors() { + protected void clearLEDColors() { CANDLE.clearAnimation(animationSlot); } @Override - void blink(Color firstColor, double speed) { + protected void blink(Color color, double speed) { CANDLE.animate( new SingleFadeAnimation( - (int) firstColor.red, - (int) firstColor.green, - (int) firstColor.blue, + (int) (color.red * 255), + (int) (color.green * 255), + (int) (color.blue * 255), 0, speed, this.numberOfLEDs, @@ -71,22 +71,29 @@ void blink(Color firstColor, double speed) { } @Override - void staticColor(Color color) { - CANDLE.setLEDs((int) color.red, (int) color.green, (int) color.blue, 0, indexOffset, numberOfLEDs); + protected void staticColor(Color color) { + CANDLE.setLEDs( + ((int) color.red * 255), + ((int) color.green * 255), + ((int) color.blue * 255), + 0, + indexOffset, + numberOfLEDs + ); } @Override - void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + protected void breathe(Color color, int numberOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { CANDLE.animate( new LarsonAnimation( - (int) color.red, - (int) color.green, - (int) color.blue, + (int) (color.red * 255), + (int) (color.green * 255), + (int) (color.blue * 255), 0, speed, this.numberOfLEDs, bounceMode, - amountOfBreathingLEDs, + numberOfBreathingLEDs, indexOffset ), animationSlot @@ -94,12 +101,12 @@ void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inver } @Override - void alternateColor(Color firstColor, Color secondColor) { + protected void alternateColor(Color firstColor, Color secondColor) { for (int i = 0; i < numberOfLEDs; i++) CANDLE.setLEDs( - (int) (isEven(i) ? firstColor.red : secondColor.red), - (int) (isEven(i) ? firstColor.green : secondColor.green), - (int) (isEven(i) ? firstColor.blue : secondColor.blue), + (int) ((isEven(i) ? firstColor.red : secondColor.red) * 255), + (int) ((isEven(i) ? firstColor.green : secondColor.green) * 255), + (int) ((isEven(i) ? firstColor.blue : secondColor.blue) * 255), 0, i + indexOffset, 1 @@ -107,13 +114,13 @@ void alternateColor(Color firstColor, Color secondColor) { } @Override - void colorFlow(Color color, double speed, boolean inverted) { + protected void colorFlow(Color color, double speed, boolean inverted) { final boolean correctedInverted = this.inverted != inverted; CANDLE.animate( new ColorFlowAnimation( - (int) color.red, - (int) color.green, - (int) color.blue, + (int) (color.red * 255), + (int) (color.green * 255), + (int) (color.blue * 255), 0, speed, this.numberOfLEDs, @@ -125,7 +132,7 @@ void colorFlow(Color color, double speed, boolean inverted) { } @Override - void rainbow(double brightness, double speed, boolean inverted) { + protected void rainbow(double brightness, double speed, boolean inverted) { final boolean correctedInverted = this.inverted != inverted; CANDLE.animate( new RainbowAnimation( @@ -140,17 +147,17 @@ void rainbow(double brightness, double speed, boolean inverted) { } @Override - void sectionColor(Supplier[] colors) { - final int ledsPerSection = (int) Math.floor(numberOfLEDs / colors.length); + protected void sectionColor(Supplier[] colors) { + final int ledsPerSection = (int) Math.floor((double) numberOfLEDs / colors.length); setSectionColor(colors.length, ledsPerSection, colors); } private void setSectionColor(int amountOfSections, int ledsPerSection, Supplier[] colors) { for (int i = 0; i < amountOfSections; i++) { CANDLE.setLEDs( - (int) (inverted ? colors[amountOfSections - i - 1].get().red : colors[i].get().red), - (int) (inverted ? colors[amountOfSections - i - 1].get().green : colors[i].get().green), - (int) (inverted ? colors[amountOfSections - i - 1].get().blue : colors[i].get().blue), + (int) ((inverted ? colors[amountOfSections - i - 1].get().red : colors[i].get().red) * 255), + (int) ((inverted ? colors[amountOfSections - i - 1].get().green : colors[i].get().green) * 255), + (int) ((inverted ? colors[amountOfSections - i - 1].get().blue : colors[i].get().blue) * 255), 0, ledsPerSection * i + indexOffset, i == amountOfSections - 1 ? numberOfLEDs - 1 : ledsPerSection * (i + 1) - 1 diff --git a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java index 9d7c2372..126f483e 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -8,21 +8,36 @@ import java.util.function.Consumer; import java.util.function.Supplier; -/** - * A class that contains static functions for getting LED commands. These commands work with both types of LEDStrips. - */ public class LEDCommands { /** - * Gets a command that sets the color of the LED strip to the given color. + * Gets a command that applies the specified animation settings to the LED strips. * - * @param color the color to set the LED strip to + * @param settings the settings for the desired animation + * @param ledStrips the LED strips to be used + * @return the command + */ + public static Command getAnimateCommand(LEDStripAnimationSettings.LEDAnimationSettings settings, LEDStrip... ledStrips) { + return new StartEndCommand( + () -> { + runForLEDs(LEDStrip::clearLEDColors, ledStrips); + runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(LEDStrip.applyAnimation(ledStrip, settings)), ledStrips); + }, + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips + ).ignoringDisable(true); + } + + /** + * Gets a command that sets LED strips to a specific color + * + * @param color the color the LED strips will be set to * @param ledStrips the LED strips to be used * @return the command */ public static Command getStaticColorCommand(Color color, LEDStrip... ledStrips) { return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip::clearLEDColors, ledStrips); runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(() -> ledStrip.staticColor(color)), ledStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), @@ -31,18 +46,18 @@ public static Command getStaticColorCommand(Color color, LEDStrip... ledStrips) } /** - * Gets a command that blinks the LED strip with a specific color. + * Gets a command that sets the LED strips to blink a single color on and off. * - * @param firstColor the color to blink - * @param speed how fast the LED strip should blink on a scale between 0 and 1 - * @param ledStrips the LED strips to be used + * @param color the color to blink on and off + * @param speed the speed at which the LED strips will alternate between being on and off on a scale between 0 and 1 + * @param ledStrips the LED strips to be used * @return the command */ - public static Command getBlinkingCommand(Color firstColor, double speed, LEDStrip... ledStrips) { + public static Command getBlinkCommand(Color color, double speed, LEDStrip... ledStrips) { return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); - runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(() -> ledStrip.blink(firstColor, speed)), ledStrips); + runForLEDs(LEDStrip::clearLEDColors, ledStrips); + runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(() -> ledStrip.blink(color, speed)), ledStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), ledStrips @@ -50,21 +65,20 @@ public static Command getBlinkingCommand(Color firstColor, double speed, LEDStri } /** - * Gets a command that "breathes" a pocket of light across the LED strip. + * Gets a command that "breathes" LEDs along the LED strips. * - * @param color the color to breathe - * @param amountOfBreathingLEDs the amount of breathing LEDs between 1 and 7 - * @param speed the speed of the breathing on a scale between 0 and 1 + * @param color the color of the breathing LEDs + * @param numberOfBreathingLEDs the amount of breathing LEDs + * @param speed the speed at which the color should travel throughout the strip on a scale between 0 and 1 * @param inverted whether the breathing should be inverted - * @param bounceMode when the pocket of LEDs should restart to the start of the strip - * @param ledStrips the LED strips to be used + * @param bounceMode when the breathing LEDs should restart their cycle throughout the strip * @param ledStrips the LED strips to be used * @return the command */ - public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... ledStrips) { + public static Command getBreatheCommand(Color color, int numberOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... ledStrips) { return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); - runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(() -> ledStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode)), ledStrips); + runForLEDs(LEDStrip::clearLEDColors, ledStrips); + runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(() -> ledStrip.breathe(color, numberOfBreathingLEDs, speed, inverted, bounceMode)), ledStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), ledStrips @@ -72,18 +86,18 @@ public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, } /** - * Gets a command that flows a color through the LED strip. + * Gets a command that flows a single color through the LED strips. * - * @param color the color to flow through the LED strip - * @param speed how fast should the color travel the strip on a scale between 0 and 1 - * @param inverted whether the flow should be inverted + * @param color the color that flows through the LED strips + * @param speed the speed at which the color flows through the LED strips on a scale between 0 and 1 + * @param inverted whether the breathing should be inverted * @param ledStrips the LED strips to be used * @return the command */ public static Command getColorFlowCommand(Color color, double speed, boolean inverted, LEDStrip... ledStrips) { return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip::clearLEDColors, ledStrips); runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(() -> ledStrip.colorFlow(color, speed, inverted)), ledStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), @@ -92,7 +106,7 @@ public static Command getColorFlowCommand(Color color, double speed, boolean inv } /** - * Gets a command that displays two colors in an alternating pattern on the LED strips. + * Gets a command that displays 2 colors in an alternating pattern on the LED strips. * * @param firstColor the first color * @param secondColor the second color @@ -102,7 +116,7 @@ public static Command getColorFlowCommand(Color color, double speed, boolean inv public static Command getAlternateColorCommand(Color firstColor, Color secondColor, LEDStrip... ledStrips) { return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip::clearLEDColors, ledStrips); runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(() -> ledStrip.alternateColor(firstColor, secondColor)), ledStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), @@ -111,16 +125,16 @@ public static Command getAlternateColorCommand(Color firstColor, Color secondCol } /** - * Gets a command that splits the LED strip into different sections. + * Gets a command that sections the LED strips into multiple different colors. * - * @param colors an array of colors to set the sections to. The length of the array dictates the amount of sections + * @param colors an array of the colors to color the sections with. The length of the array dictates the amount of sections * @param ledStrips the LED strips to be used * @return the command */ public static Command getSectionColorCommand(Supplier[] colors, LEDStrip... ledStrips) { return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip::clearLEDColors, ledStrips); runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(() -> ledStrip.sectionColor(colors)), ledStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), @@ -129,18 +143,17 @@ public static Command getSectionColorCommand(Supplier[] colors, LEDStrip. } /** - * Gets a command that displays a rainbow pattern on the LED strips. + * Gets a command that sets the LED strips to a moving rainbow pattern. * * @param brightness the brightness of the rainbow on a scale from 0 to 1 - * @param speed the speed of the rainbow's movement on a scale from 0 to 1 - * @param inverted whether the rainbow should be inverted + * @param speed the speed of the rainbow's movement on a between 0 and 1 * @param ledStrips the LED strips to be used * @return the command */ public static Command getRainbowCommand(double brightness, double speed, boolean inverted, LEDStrip... ledStrips) { return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip::clearLEDColors, ledStrips); runForLEDs(ledStrip -> ledStrip.setCurrentAnimation(() -> ledStrip.rainbow(brightness, speed, inverted)), ledStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), @@ -148,7 +161,13 @@ public static Command getRainbowCommand(double brightness, double speed, boolean ).ignoringDisable(true); } + /** + * Runs an action on all LED strips. + */ private static void runForLEDs(Consumer action, LEDStrip... ledStrips) { + if (ledStrips.length == 0) + ledStrips = LEDStrip.LED_STRIPS; + for (LEDStrip ledStrip : ledStrips) action.accept(ledStrip); } diff --git a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java index c2802a0a..a1350b4a 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -2,7 +2,6 @@ import com.ctre.phoenix.led.LarsonAnimation; import edu.wpi.first.wpilibj.util.Color; -import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.SubsystemBase; import org.trigon.hardware.RobotHardwareStats; @@ -46,16 +45,23 @@ public static LEDStrip createCANdleLEDStrip(boolean inverted, int numberOfLEDs, } /** - * Sets the default command for all LED strips. + * Sets the default animation for all LED strips. * - * @param command the default command to be set + * @param defaultAnimationSettings the default animation settings to be set */ - public static void setDefaultCommandForAllLEDS(Command command) { + public static void setDefaultAnimationForAllLEDS(LEDStripAnimationSettings.LEDAnimationSettings defaultAnimationSettings) { for (LEDStrip ledStrip : LED_STRIPS) - ledStrip.setDefaultCommand(command); + ledStrip.setDefaultCommand(LEDCommands.getAnimateCommand(defaultAnimationSettings, ledStrip)); } - public LEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + /** + * Applies the correct animation based on the type of LEDAnimationSettings. + */ + static Runnable applyAnimation(LEDStrip ledStrip, LEDStripAnimationSettings.LEDAnimationSettings settings) { + return () -> settings.apply(ledStrip); + } + + protected LEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { this.inverted = inverted; this.numberOfLEDs = numberOfLEDs; this.indexOffset = indexOffset; @@ -67,74 +73,29 @@ public int getNumberOfLEDS() { return numberOfLEDs; } - void setCurrentAnimation(Runnable currentAnimation) { + protected void setCurrentAnimation(Runnable currentAnimation) { this.currentAnimation = currentAnimation; currentAnimation.run(); } - void resetLEDSettings() { + protected void resetLEDSettings() { } - /** - * Sets the color of the LED strip to the given color. - * - * @param color the color to set the LED strip to - */ - abstract void staticColor(Color color); + protected abstract void clearLEDColors(); - /** - * Blinks the LED strip with a specific color. - * - * @param firstColor the color to blink - * @param speed how fast the LED strip should blink on a scale between 0 and 1 - */ - abstract void blink(Color firstColor, double speed); + protected abstract void staticColor(Color color); - /** - * "Breathes" a pocket of LEDs with a given color. - * - * @param color the color of the breathing LEDs - * @param breathingLEDs the amount of breathing LEDs - * @param speed how fast should the color travel the strip on a scale between 0 and 1 - * @param inverted whether the breathing should be inverted - * @param bounceMode when the breathing LEDs should restart at the start of the strip - */ - abstract void breathe(Color color, int breathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode); + protected abstract void blink(Color color, double speed); - /** - * Flows a color through the LED strip. - * - * @param color the color to flow through the LED strip - * @param speed how fast should the color travel the strip on a scale between 0 and 1 - * @param inverted whether the color flow should be inverted - */ - abstract void colorFlow(Color color, double speed, boolean inverted); + protected abstract void breathe(Color color, int numberOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode); - /** - * Displays two colors in an alternating pattern on the LED strip. - * - * @param firstColor the first color - * @param secondColor the second color - */ - abstract void alternateColor(Color firstColor, Color secondColor); + protected abstract void colorFlow(Color color, double speed, boolean inverted); - /** - * Splits the LED strip into different sections. - * - * @param colors an array of the colors to color the sections with. The length of the array dictates the amount of sections - */ - abstract void sectionColor(Supplier[] colors); + protected abstract void alternateColor(Color firstColor, Color secondColor); - /** - * Displays a rainbow pattern on the LED strip. - * - * @param brightness the brightness of the rainbow on a scale from 0 to 1 - * @param speed the speed of the rainbow's movement on a scale from 0 to 1 - * @param inverted whether the rainbow should be inverted - */ - abstract void rainbow(double brightness, double speed, boolean inverted); + protected abstract void sectionColor(Supplier[] colors); - abstract void clearLEDColors(); + protected abstract void rainbow(double brightness, double speed, boolean inverted); private void addLEDStripToLEDStripsArray(LEDStrip ledStrip) { final LEDStrip[] newLEDStrips = new LEDStrip[LED_STRIPS.length + 1]; diff --git a/src/main/java/org/trigon/hardware/misc/leds/LEDStripAnimationSettings.java b/src/main/java/org/trigon/hardware/misc/leds/LEDStripAnimationSettings.java new file mode 100644 index 00000000..52f96082 --- /dev/null +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStripAnimationSettings.java @@ -0,0 +1,110 @@ +package org.trigon.hardware.misc.leds; + +import com.ctre.phoenix.led.LarsonAnimation; +import edu.wpi.first.wpilibj.util.Color; + +import java.util.function.Supplier; + +/** + * A class that contains settings for the LED commands. + */ +public class LEDStripAnimationSettings { + public interface LEDAnimationSettings { + void apply(LEDStrip ledStrip); + } + + /** + * The settings for a command that sets an LED strip to a single color. + * + * @param color the color to set the LED strip to + */ + public record StaticColorSettings(Color color) implements LEDAnimationSettings { + @Override + public void apply(LEDStrip ledStrip) { + ledStrip.staticColor(color); + } + } + + /** + * The settings for a command that blinks a single color on and off on an LED strip. + * + * @param color the color to blink + * @param speed the speed at which the LED strip should blink on a scale between 0 and 1 + */ + public record BlinkSettings(Color color, double speed) implements LEDAnimationSettings { + @Override + public void apply(LEDStrip ledStrip) { + ledStrip.blink(color, speed); + } + } + + /** + * The settings for a command that "breathes" LEDs along an LED strip. + * + * @param color the color of the breathing LEDs + * @param numberOfBreathingLEDs the amount of breathing LEDs + * @param speed the speed at which the color should travel throughout the strip on a scale between 0 and 1 + * @param inverted whether the breathing should be inverted + * @param bounceMode when the breathing LEDs should restart their cycle throughout the strip + */ + public record BreatheSettings(Color color, int numberOfBreathingLEDs, double speed, boolean inverted, + LarsonAnimation.BounceMode bounceMode) implements LEDAnimationSettings { + @Override + public void apply(LEDStrip ledStrip) { + ledStrip.breathe(color, numberOfBreathingLEDs, speed, inverted, bounceMode); + } + } + + /** + * The settings for a command that flows a color throughout a LED strip. + * + * @param color the color to flow throughout the LED strip + * @param speed the speed at which the color should travel throughout the strip on a scale between 0 and 1 + * @param inverted whether the color flow should be inverted + */ + public record ColorFlowSettings(Color color, double speed, boolean inverted) implements LEDAnimationSettings { + @Override + public void apply(LEDStrip ledStrip) { + ledStrip.colorFlow(color, speed, inverted); + } + } + + /** + * The settings for a command that displays 2 colors in an alternating pattern. + * + * @param firstColor the first color + * @param secondColor the second color + */ + public record AlternateColorSettings(Color firstColor, Color secondColor) implements LEDAnimationSettings { + @Override + public void apply(LEDStrip ledStrip) { + ledStrip.alternateColor(firstColor, secondColor); + } + } + + /** + * The settings for a command that splits the LED strip into different colored sections. + * + * @param colors an array of the colors to color the sections with. The length of the array dictates the amount of sections + */ + public record SectionColorSettings(Supplier[] colors) implements LEDAnimationSettings { + @Override + public void apply(LEDStrip ledStrip) { + ledStrip.sectionColor(colors); + } + } + + /** + * The settings for a command that animates an LED strip as a rainbow. + * + * @param brightness the brightness of the rainbow on a scale from 0 to 1 + * @param speed the speed of the rainbow's movement on a scale between 0 and 1 + * @param inverted whether the rainbow should be inverted + */ + public record RainbowSettings(double brightness, double speed, boolean inverted) implements LEDAnimationSettings { + @Override + public void apply(LEDStrip ledStrip) { + ledStrip.rainbow(brightness, speed, inverted); + } + } +} \ No newline at end of file