From 8962edee57e7f10f58b49a6d6877d76c2af0a1d3 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Fri, 8 Nov 2024 11:33:31 +0200 Subject: [PATCH 01/31] implemented AddressableLed and most of CANdleLed --- .../misc/leds/AddressableLEDStrip.java | 160 ++++++++++++++++++ .../hardware/misc/leds/CANdleLEDStrip.java | 102 +++++++++++ .../trigon/hardware/misc/leds/LEDStrip.java | 54 ++++++ 3 files changed, 316 insertions(+) create mode 100644 src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java create mode 100644 src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java create mode 100644 src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java diff --git a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java new file mode 100644 index 00000000..a7ce07a8 --- /dev/null +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -0,0 +1,160 @@ +package org.trigon.hardware.misc.leds; + +import com.ctre.phoenix.led.LarsonAnimation; +import com.ctre.phoenix.led.TwinkleAnimation; +import edu.wpi.first.wpilibj.AddressableLED; +import edu.wpi.first.wpilibj.AddressableLEDBuffer; +import edu.wpi.first.wpilibj.Timer; + +import java.awt.*; +import java.util.function.Supplier; + +public class AddressableLEDStrip extends LEDStrip { + private static AddressableLED LED; + private static AddressableLEDBuffer LED_BUFFER; + private int lastBreatheLED; + private double lastBreatheMovementTime = 0; + private double rainbowFirstPixelHue = 0; + private boolean areLEDsOnForBlinking = false; + private double lastBlinkTime = 0; + private boolean alternateColor = true; + private double lastAlternateColorTime = 0; + + public static void setLED(AddressableLED led) { + LED = led; + } + + public static void setLEDBuffer(AddressableLEDBuffer ledBuffer) { + LED_BUFFER = ledBuffer; + } + + public AddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + super(inverted, numberOfLEDs, indexOffset); + resetLEDSettings(); + } + + @Override + public void periodic() { + LED.setData(LED_BUFFER); + } + + @Override + void clearLEDColors() { + staticColor(Color.BLACK); + } + + @Override + void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { + double currentTime = Timer.getFPGATimestamp(); + if (currentTime - lastBlinkTime > blinkingIntervalSeconds) { + lastBlinkTime = currentTime; + areLEDsOnForBlinking = !areLEDsOnForBlinking; + } + if (areLEDsOnForBlinking) + staticColor(firstColor); + else + staticColor(secondColor); + } + + @Override + void staticColor(Color color) { + setLEDColors(color, 0, numberOfLEDs - 1); + } + + @Override + void breath(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + clearLEDColors(); + inverted = this.inverted != inverted; + double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; + double currentTime = Timer.getFPGATimestamp(); + if (currentTime - lastBreatheMovementTime > moveLEDTimeSeconds) { + lastBreatheMovementTime = currentTime; + if (inverted) + lastBreatheLED--; + else + lastBreatheLED++; + } + if (inverted ? (lastBreatheLED < indexOffset) : (lastBreatheLED >= numberOfLEDs + indexOffset)) { + if (!shouldLoop) { + getDefaultCommand().schedule(); + return; + } + lastBreatheLED = inverted ? indexOffset + numberOfLEDs : indexOffset; + } + for (int i = 0; i < breathingLEDs; i++) { + if (lastBreatheLED - i >= indexOffset && lastBreatheLED - i < indexOffset + numberOfLEDs) + LED_BUFFER.setLED(lastBreatheLED - i, convertToColor(color)); + else if (lastBreatheLED - i < indexOffset + numberOfLEDs) + LED_BUFFER.setLED(lastBreatheLED - i + numberOfLEDs, convertToColor(color)); + } + } + + @Override + void twinkle(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { + if (Timer.getFPGATimestamp() - lastAlternateColorTime > intervalSeconds) { + alternateColor = !alternateColor; + lastAlternateColorTime = Timer.getFPGATimestamp(); + } + if (alternateColor) { + for (int i = 0; i < numberOfLEDs; i++) + LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? convertToColor(firstColor) : convertToColor(secondColor)); + return; + } + for (int i = 0; i < numberOfLEDs; i++) + LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? convertToColor(secondColor) : convertToColor(firstColor)); + } + + + @Override + void rainbow(double brightness, double speed) { + for (int led = 0; led < numberOfLEDs; led++) { + final int hue = (int) (rainbowFirstPixelHue + (led * 180 / numberOfLEDs) % 180); + LED_BUFFER.setHSV(led + indexOffset, hue, 255, 128); + } + if (inverted) { + rainbowFirstPixelHue -= 3; + if (rainbowFirstPixelHue < 0) + rainbowFirstPixelHue += 180; + return; + } + rainbowFirstPixelHue += 3; + rainbowFirstPixelHue %= 180; + } + + @Override + void sectionColor(int amountOfSections, Supplier[] colors) { + if (amountOfSections != colors.length) + throw new IllegalArgumentException("Amount of sections must be equal to the amount of colors"); + final int LEDSPerSection = (int) Math.floor(numberOfLEDs / amountOfSections); + setSectionColor(amountOfSections, LEDSPerSection, colors); + } + + private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier[] colors) { + if (inverted) { + for (int i = 0; i < amountOfSections; i++) + setLEDColors(colors[amountOfSections - i - 1].get(), LEDSPerSection * i, i == amountOfSections - 1 ? numberOfLEDs - 1 : LEDSPerSection * (i + 1) - 1); + return; + } + for (int i = 0; i < amountOfSections; i++) + setLEDColors(colors[i].get(), LEDSPerSection * i, i == amountOfSections - 1 ? numberOfLEDs - 1 : LEDSPerSection * (i + 1) - 1); + } + + private void setLEDColors(Color color, int startIndex, int endIndex) { + for (int i = 0; i <= endIndex - startIndex; i++) + LED_BUFFER.setLED(startIndex + indexOffset + i, convertToColor(color)); + } + + private void resetLEDSettings() { + lastBreatheLED = indexOffset; + lastBreatheMovementTime = Timer.getFPGATimestamp(); + rainbowFirstPixelHue = 0; + areLEDsOnForBlinking = false; + lastBlinkTime = 0; + alternateColor = true; + lastAlternateColorTime = 0; + } + + private edu.wpi.first.wpilibj.util.Color convertToColor(Color color) { + return new edu.wpi.first.wpilibj.util.Color(color.getRed(), color.getGreen(), color.getBlue()); + } +} diff --git a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java new file mode 100644 index 00000000..c24b0a79 --- /dev/null +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -0,0 +1,102 @@ +package org.trigon.hardware.misc.leds; + +import com.ctre.phoenix.led.*; + +import java.awt.*; +import java.util.function.Supplier; + +public class CANdleLEDStrip extends LEDStrip { + private static CANdle CANDLE; + private static int LAST_CREATED_LED_STRIP_ANIMATION_SLOT = 0; + private final int animationSlot; + + public static void setCandle(CANdle candle) { + CANDLE = candle; + } + + public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + super(inverted, numberOfLEDs, indexOffset); + LAST_CREATED_LED_STRIP_ANIMATION_SLOT++; + animationSlot = LAST_CREATED_LED_STRIP_ANIMATION_SLOT; + } + + @Override + void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { + CANDLE.animate( + new SingleFadeAnimation( + firstColor.getRed(), + firstColor.getGreen(), + firstColor.getBlue(), + 0, + blinkingIntervalSeconds, + this.numberOfLEDs, + indexOffset + ), + animationSlot + ); + } + + @Override + void staticColor(Color color) { + CANDLE.setLEDs(color.getRed(), color.getGreen(), color.getBlue(), 0, indexOffset, numberOfLEDs); + } + + @Override + void breath(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + CANDLE.animate( + new LarsonAnimation( + color.getRed(), + color.getGreen(), + color.getBlue(), + 0, + cycleTimeSeconds, + this.numberOfLEDs, + bounceMode, + breathingLEDs, + indexOffset + ), + animationSlot + ); + } + + @Override + void twinkle(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { + CANDLE.animate( + new TwinkleAnimation( + firstColor.getRed(), + firstColor.getGreen(), + firstColor.getBlue(), + 0, + intervalSeconds, + this.numberOfLEDs, + divider, + indexOffset + ), + animationSlot + ); + } + + @Override + void sectionColor(int amountOfSections, Supplier[] colors) { + + } + + @Override + void rainbow(double brightness, double speed) { + CANDLE.animate( + new RainbowAnimation( + brightness, + speed, + this.numberOfLEDs, + inverted, + indexOffset + ), + animationSlot + ); + } + + @Override + void clearLEDColors() { + CANDLE.clearAnimation(animationSlot); + } +} diff --git a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java new file mode 100644 index 00000000..c5aa7919 --- /dev/null +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -0,0 +1,54 @@ +package org.trigon.hardware.misc.leds; + +import com.ctre.phoenix.led.LarsonAnimation; +import com.ctre.phoenix.led.TwinkleAnimation; +import edu.wpi.first.wpilibj2.command.Command; +import edu.wpi.first.wpilibj2.command.SubsystemBase; + +import java.awt.*; +import java.util.function.Supplier; + +public abstract class LEDStrip extends SubsystemBase { + public static LEDStrip[] LED_STRIPS = new LEDStrip[0]; + final int indexOffset; + final boolean inverted; + final int numberOfLEDs; + + public LEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + this.indexOffset = indexOffset; + this.inverted = inverted; + this.numberOfLEDs = numberOfLEDs; + + addLEDStripToLEDStripsArray(this); + } + + public static void setDefaultCommandForAllLEDS(Command command) { + for (LEDStrip ledStrip : LED_STRIPS) + ledStrip.setDefaultCommand(command); + } + + public int getNumberOfLEDS() { + return numberOfLEDs; + } + + abstract void staticColor(Color color); + + abstract void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds); + + abstract void breath(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode); + + abstract void twinkle(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider); + + abstract void sectionColor(int amountOfSections, Supplier[] colors); + + abstract void rainbow(double brightness, double speed); + + abstract void clearLEDColors(); + + private void addLEDStripToLEDStripsArray(LEDStrip ledStrip) { + final LEDStrip[] newLEDStrips = new LEDStrip[LED_STRIPS.length + 1]; + System.arraycopy(LED_STRIPS, 0, newLEDStrips, 0, LED_STRIPS.length); + newLEDStrips[LED_STRIPS.length] = ledStrip; + LED_STRIPS = newLEDStrips; + } +} From f248c819ae1dddb56cb4c497fec7731dfd0e3643 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Fri, 8 Nov 2024 14:28:14 +0200 Subject: [PATCH 02/31] finished CANdleLEDStrip and added LEDCommands --- .../hardware/misc/leds/CANdleLEDStrip.java | 42 +++++++++++-- .../hardware/misc/leds/LEDCommands.java | 59 +++++++++++++++++++ 2 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java 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 c24b0a79..51c95d75 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -20,6 +20,11 @@ public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { animationSlot = LAST_CREATED_LED_STRIP_ANIMATION_SLOT; } + @Override + void clearLEDColors() { + CANDLE.clearAnimation(animationSlot); + } + @Override void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { CANDLE.animate( @@ -76,10 +81,6 @@ void twinkle(Color firstColor, Color secondColor, double intervalSeconds, Twinkl ); } - @Override - void sectionColor(int amountOfSections, Supplier[] colors) { - - } @Override void rainbow(double brightness, double speed) { @@ -96,7 +97,36 @@ void rainbow(double brightness, double speed) { } @Override - void clearLEDColors() { - CANDLE.clearAnimation(animationSlot); + void sectionColor(int amountOfSections, Supplier[] colors) { + if (amountOfSections != colors.length) + throw new IllegalArgumentException("Amount of sections must be equal to the amount of colors"); + final int LEDSPerSection = (int) Math.floor(numberOfLEDs / amountOfSections); + setSectionColor(amountOfSections, LEDSPerSection, colors); + } + + private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier[] colors) { + if (inverted) { + for (int i = 0; i < amountOfSections; i++) { + CANDLE.setLEDs( + colors[amountOfSections - i - 1].get().getRed(), + colors[amountOfSections - i - 1].get().getGreen(), + colors[amountOfSections - i - 1].get().getBlue(), + 0, + LEDSPerSection * i + indexOffset, + i == amountOfSections - 1 ? numberOfLEDs - 1 : LEDSPerSection * (i + 1) - 1 + ); + } + return; + } + for (int i = 0; i < amountOfSections; i++) { + CANDLE.setLEDs( + colors[i].get().getRed(), + colors[i].get().getGreen(), + colors[i].get().getBlue(), + 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 new file mode 100644 index 00000000..6be081e9 --- /dev/null +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -0,0 +1,59 @@ +package org.trigon.hardware.misc.leds; + +import com.ctre.phoenix.led.LarsonAnimation; +import com.ctre.phoenix.led.TwinkleAnimation; +import edu.wpi.first.wpilibj2.command.Command; +import org.trigon.commands.ExecuteEndCommand; + +import java.awt.*; +import java.util.function.Consumer; +import java.util.function.Supplier; + +public class LEDCommands { + public static Command getStaticColorCommand(Color color, LEDStrip... ledStrips) { + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + ).ignoringDisable(true); + } + + public static Command getBlinkingCommand(Color firstColor, Color secondColor, double blinkingIntervalSeconds, LEDStrip... ledStrips) { + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + ).ignoringDisable(true); + } + + public static Command getBreathingCommand(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... ledStrips) { + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.breath(color, breathingLEDs, cycleTimeSeconds, shouldLoop, inverted, bounceMode)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + ).ignoringDisable(true); + } + + public static Command getTwinkleCommand(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { + return new ExecuteEndCommand( + () -> runForLEDs(LEDStrip -> LEDStrip.twinkle(firstColor, secondColor, intervalSeconds, divider), LEDStrip.LED_STRIPS), + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrip.LED_STRIPS) + ).ignoringDisable(true); + } + + public static Command getSectionColorCommand(int amountOfSections, Supplier[] colors, LEDStrip... ledStrips) { + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip) -> LEDStrip.sectionColor(amountOfSections, colors), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + ).ignoringDisable(true); + } + + public static Command getRainbowCommand(double brightness, double speed, LEDStrip... ledStrips) { + return new ExecuteEndCommand( + () -> runForLEDs(LEDStrip -> LEDStrip.rainbow(brightness, speed), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + ).ignoringDisable(true); + } + + public static void runForLEDs(Consumer action, LEDStrip... ledStrips) { + for (LEDStrip LEDStrip : ledStrips) + action.accept(LEDStrip); + } +} From 92d91dc625426ae4afdb9681136140c36ec3a608 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Sat, 9 Nov 2024 19:52:51 +0200 Subject: [PATCH 03/31] added colorFLowCommand --- .../misc/leds/AddressableLEDStrip.java | 57 ++++++++++++++----- .../hardware/misc/leds/CANdleLEDStrip.java | 47 ++++++++++++--- .../hardware/misc/leds/LEDCommands.java | 34 ++++++++--- .../trigon/hardware/misc/leds/LEDStrip.java | 9 ++- .../phoenix6/talonfx/TalonFXMotor.java | 2 +- 5 files changed, 118 insertions(+), 31 deletions(-) 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 a7ce07a8..aa6ffe2b 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -19,6 +19,8 @@ public class AddressableLEDStrip extends LEDStrip { private double lastBlinkTime = 0; private boolean alternateColor = true; private double lastAlternateColorTime = 0; + private double lastColorFlowTime = 0; + private int amountOfColorFlowLEDs = 0; public static void setLED(AddressableLED led) { LED = led; @@ -62,7 +64,7 @@ void staticColor(Color color) { } @Override - void breath(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { clearLEDColors(); inverted = this.inverted != inverted; double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; @@ -90,7 +92,33 @@ else if (lastBreatheLED - i < indexOffset + numberOfLEDs) } @Override - void twinkle(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { + void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted) { + clearLEDColors(); + inverted = this.inverted != inverted; + double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; + if (Timer.getFPGATimestamp() - lastColorFlowTime > moveLEDTimeSeconds) { + lastColorFlowTime = Timer.getFPGATimestamp(); + if (inverted) + amountOfColorFlowLEDs--; + else + amountOfColorFlowLEDs++; + } + if (inverted ? amountOfColorFlowLEDs < 0 : amountOfColorFlowLEDs >= numberOfLEDs) { + if (!shouldLoop) { + getDefaultCommand().schedule(); + return; + } + amountOfColorFlowLEDs = inverted ? numberOfLEDs : 0; + } + if (inverted) { + setLEDColors(color, numberOfLEDs - amountOfColorFlowLEDs, numberOfLEDs - 1); + return; + } + setLEDColors(color, 0, amountOfColorFlowLEDs); + } + + @Override + void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { if (Timer.getFPGATimestamp() - lastAlternateColorTime > intervalSeconds) { alternateColor = !alternateColor; lastAlternateColorTime = Timer.getFPGATimestamp(); @@ -129,6 +157,19 @@ void sectionColor(int amountOfSections, Supplier[] colors) { setSectionColor(amountOfSections, LEDSPerSection, colors); } + @Override + void resetLEDSettings() { + lastBreatheLED = indexOffset; + lastBreatheMovementTime = Timer.getFPGATimestamp(); + rainbowFirstPixelHue = 0; + areLEDsOnForBlinking = false; + lastBlinkTime = 0; + alternateColor = true; + lastAlternateColorTime = 0; + lastColorFlowTime = 0; + amountOfColorFlowLEDs = 0; + } + private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier[] colors) { if (inverted) { for (int i = 0; i < amountOfSections; i++) @@ -144,17 +185,7 @@ private void setLEDColors(Color color, int startIndex, int endIndex) { LED_BUFFER.setLED(startIndex + indexOffset + i, convertToColor(color)); } - private void resetLEDSettings() { - lastBreatheLED = indexOffset; - lastBreatheMovementTime = Timer.getFPGATimestamp(); - rainbowFirstPixelHue = 0; - areLEDsOnForBlinking = false; - lastBlinkTime = 0; - alternateColor = true; - lastAlternateColorTime = 0; - } - private edu.wpi.first.wpilibj.util.Color convertToColor(Color color) { return new edu.wpi.first.wpilibj.util.Color(color.getRed(), color.getGreen(), color.getBlue()); } -} +} \ No newline at end of file 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 51c95d75..ab5c99f0 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -1,6 +1,7 @@ package org.trigon.hardware.misc.leds; import com.ctre.phoenix.led.*; +import edu.wpi.first.wpilibj.Timer; import java.awt.*; import java.util.function.Supplier; @@ -9,6 +10,8 @@ public class CANdleLEDStrip extends LEDStrip { private static CANdle CANDLE; private static int LAST_CREATED_LED_STRIP_ANIMATION_SLOT = 0; private final int animationSlot; + private boolean alternateColor = true; + private double lastAlternateColorTime = 0; public static void setCandle(CANdle candle) { CANDLE = candle; @@ -47,7 +50,7 @@ void staticColor(Color color) { } @Override - void breath(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { CANDLE.animate( new LarsonAnimation( color.getRed(), @@ -65,23 +68,51 @@ void breath(Color color, int breathingLEDs, double cycleTimeSeconds, boolean sho } @Override - void twinkle(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { - CANDLE.animate( - new TwinkleAnimation( + void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { + if (Timer.getFPGATimestamp() - lastAlternateColorTime > intervalSeconds) { + alternateColor = !alternateColor; + lastAlternateColorTime = Timer.getFPGATimestamp(); + } + if (alternateColor) { + for (int i = 0; i < numberOfLEDs; i++) + CANDLE.setLEDs( firstColor.getRed(), firstColor.getGreen(), firstColor.getBlue(), 0, - intervalSeconds, + i + indexOffset, + 1 + ); + return; + } + for (int i = 0; i < numberOfLEDs; i++) + CANDLE.setLEDs( + firstColor.getRed(), + firstColor.getGreen(), + firstColor.getBlue(), + 0, + i + indexOffset, + 1 + ); + } + + @Override + void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted) { + CANDLE.animate( + new ColorFlowAnimation( + color.getRed(), + color.getGreen(), + color.getBlue(), + 0, + cycleTimeSeconds, this.numberOfLEDs, - divider, + inverted ? ColorFlowAnimation.Direction.Backward : ColorFlowAnimation.Direction.Forward, indexOffset ), animationSlot ); } - @Override void rainbow(double brightness, double speed) { CANDLE.animate( @@ -129,4 +160,4 @@ private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier< ); } } -} +} \ No newline at end of file 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 6be081e9..95ebee7e 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -3,6 +3,7 @@ import com.ctre.phoenix.led.LarsonAnimation; import com.ctre.phoenix.led.TwinkleAnimation; import edu.wpi.first.wpilibj2.command.Command; +import edu.wpi.first.wpilibj2.command.FunctionalCommand; import org.trigon.commands.ExecuteEndCommand; import java.awt.*; @@ -24,16 +25,35 @@ public static Command getBlinkingCommand(Color firstColor, Color secondColor, do ).ignoringDisable(true); } - public static Command getBreathingCommand(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... ledStrips) { - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.breath(color, breathingLEDs, cycleTimeSeconds, shouldLoop, inverted, bounceMode)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + public static Command getBreatheCommand(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... ledStrips) { + return new FunctionalCommand( + () -> { + if (!shouldLoop) + runForLEDs(LEDStrip::resetLEDSettings, ledStrips); + }, + () -> runForLEDs((LEDStrip) -> LEDStrip.breathe(color, breathingLEDs, cycleTimeSeconds, shouldLoop, inverted, bounceMode), ledStrips), + (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + () -> false, + ledStrips + ).ignoringDisable(true); + } + + public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LEDStrip... ledStrips) { + return new FunctionalCommand( + () -> { + if (!shouldLoop) + runForLEDs(LEDStrip::resetLEDSettings, ledStrips); + }, + () -> runForLEDs((LEDStrip) -> LEDStrip.colorFlow(color, cycleTimeSeconds, shouldLoop, inverted), ledStrips), + (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + () -> false, + ledStrips ).ignoringDisable(true); } - public static Command getTwinkleCommand(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { + public static Command getAlternateColorCommand(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { return new ExecuteEndCommand( - () -> runForLEDs(LEDStrip -> LEDStrip.twinkle(firstColor, secondColor, intervalSeconds, divider), LEDStrip.LED_STRIPS), + () -> runForLEDs(LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds, divider), LEDStrip.LED_STRIPS), () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrip.LED_STRIPS) ).ignoringDisable(true); } @@ -56,4 +76,4 @@ public static void runForLEDs(Consumer action, LEDStrip... ledStrips) for (LEDStrip LEDStrip : ledStrips) action.accept(LEDStrip); } -} +} \ No newline at end of file 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 c5aa7919..cace1cc4 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -31,13 +31,18 @@ public int getNumberOfLEDS() { return numberOfLEDs; } + void resetLEDSettings() { + } + abstract void staticColor(Color color); abstract void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds); - abstract void breath(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode); + abstract void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode); + + abstract void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted); - abstract void twinkle(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider); + abstract void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider); abstract void sectionColor(int amountOfSections, Supplier[] colors); diff --git a/src/main/java/org/trigon/hardware/phoenix6/talonfx/TalonFXMotor.java b/src/main/java/org/trigon/hardware/phoenix6/talonfx/TalonFXMotor.java index bf0b786a..d933b626 100644 --- a/src/main/java/org/trigon/hardware/phoenix6/talonfx/TalonFXMotor.java +++ b/src/main/java/org/trigon/hardware/phoenix6/talonfx/TalonFXMotor.java @@ -110,4 +110,4 @@ private TalonFXIO generateIO(int id, String canbus) { return new SimulationTalonFXIO(id); return new RealTalonFXIO(id, canbus); } -} +} \ No newline at end of file From eb220527c73179c278dd2d85f22bf659a160aa44 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Sun, 10 Nov 2024 14:41:26 +0200 Subject: [PATCH 04/31] made code more efficient --- .../misc/leds/AddressableLEDStrip.java | 18 +++----- .../hardware/misc/leds/CANdleLEDStrip.java | 46 +++++++++---------- .../hardware/misc/leds/LEDCommands.java | 4 +- .../trigon/hardware/misc/leds/LEDStrip.java | 2 +- 4 files changed, 33 insertions(+), 37 deletions(-) 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 aa6ffe2b..77e363c4 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -5,8 +5,8 @@ import edu.wpi.first.wpilibj.AddressableLED; import edu.wpi.first.wpilibj.AddressableLEDBuffer; import edu.wpi.first.wpilibj.Timer; +import edu.wpi.first.wpilibj.util.Color; -import java.awt.*; import java.util.function.Supplier; public class AddressableLEDStrip extends LEDStrip { @@ -42,7 +42,7 @@ public void periodic() { @Override void clearLEDColors() { - staticColor(Color.BLACK); + staticColor(Color.kBlack); } @Override @@ -85,9 +85,9 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean sh } for (int i = 0; i < breathingLEDs; i++) { if (lastBreatheLED - i >= indexOffset && lastBreatheLED - i < indexOffset + numberOfLEDs) - LED_BUFFER.setLED(lastBreatheLED - i, convertToColor(color)); + LED_BUFFER.setLED(lastBreatheLED - i, color); else if (lastBreatheLED - i < indexOffset + numberOfLEDs) - LED_BUFFER.setLED(lastBreatheLED - i + numberOfLEDs, convertToColor(color)); + LED_BUFFER.setLED(lastBreatheLED - i + numberOfLEDs, color); } } @@ -125,11 +125,11 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, } if (alternateColor) { for (int i = 0; i < numberOfLEDs; i++) - LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? convertToColor(firstColor) : convertToColor(secondColor)); + LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? firstColor : secondColor); return; } for (int i = 0; i < numberOfLEDs; i++) - LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? convertToColor(secondColor) : convertToColor(firstColor)); + LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? secondColor : firstColor); } @@ -182,10 +182,6 @@ private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier< private void setLEDColors(Color color, int startIndex, int endIndex) { for (int i = 0; i <= endIndex - startIndex; i++) - LED_BUFFER.setLED(startIndex + indexOffset + i, convertToColor(color)); - } - - private edu.wpi.first.wpilibj.util.Color convertToColor(Color color) { - return new edu.wpi.first.wpilibj.util.Color(color.getRed(), color.getGreen(), color.getBlue()); + LED_BUFFER.setLED(startIndex + indexOffset + i, color); } } \ No newline at end of file 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 ab5c99f0..885a6b52 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -2,8 +2,8 @@ import com.ctre.phoenix.led.*; import edu.wpi.first.wpilibj.Timer; +import edu.wpi.first.wpilibj.util.Color; -import java.awt.*; import java.util.function.Supplier; public class CANdleLEDStrip extends LEDStrip { @@ -32,9 +32,9 @@ void clearLEDColors() { void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { CANDLE.animate( new SingleFadeAnimation( - firstColor.getRed(), - firstColor.getGreen(), - firstColor.getBlue(), + (int) firstColor.red, + (int) firstColor.green, + (int) firstColor.blue, 0, blinkingIntervalSeconds, this.numberOfLEDs, @@ -46,16 +46,16 @@ void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) @Override void staticColor(Color color) { - CANDLE.setLEDs(color.getRed(), color.getGreen(), color.getBlue(), 0, indexOffset, numberOfLEDs); + CANDLE.setLEDs((int) color.red, (int) color.green, (int) color.blue, 0, indexOffset, numberOfLEDs); } @Override void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { CANDLE.animate( new LarsonAnimation( - color.getRed(), - color.getGreen(), - color.getBlue(), + (int) color.red, + (int) color.green, + (int) color.blue, 0, cycleTimeSeconds, this.numberOfLEDs, @@ -76,9 +76,9 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, if (alternateColor) { for (int i = 0; i < numberOfLEDs; i++) CANDLE.setLEDs( - firstColor.getRed(), - firstColor.getGreen(), - firstColor.getBlue(), + (int) firstColor.red, + (int) firstColor.green, + (int) firstColor.blue, 0, i + indexOffset, 1 @@ -87,9 +87,9 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, } for (int i = 0; i < numberOfLEDs; i++) CANDLE.setLEDs( - firstColor.getRed(), - firstColor.getGreen(), - firstColor.getBlue(), + (int) firstColor.red, + (int) firstColor.green, + (int) firstColor.blue, 0, i + indexOffset, 1 @@ -100,9 +100,9 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted) { CANDLE.animate( new ColorFlowAnimation( - color.getRed(), - color.getGreen(), - color.getBlue(), + (int) color.red, + (int) color.green, + (int) color.blue, 0, cycleTimeSeconds, this.numberOfLEDs, @@ -139,9 +139,9 @@ private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier< if (inverted) { for (int i = 0; i < amountOfSections; i++) { CANDLE.setLEDs( - colors[amountOfSections - i - 1].get().getRed(), - colors[amountOfSections - i - 1].get().getGreen(), - colors[amountOfSections - i - 1].get().getBlue(), + (int) colors[amountOfSections - i - 1].get().red, + (int) colors[amountOfSections - i - 1].get().green, + (int) colors[amountOfSections - i - 1].get().blue, 0, LEDSPerSection * i + indexOffset, i == amountOfSections - 1 ? numberOfLEDs - 1 : LEDSPerSection * (i + 1) - 1 @@ -151,9 +151,9 @@ private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier< } for (int i = 0; i < amountOfSections; i++) { CANDLE.setLEDs( - colors[i].get().getRed(), - colors[i].get().getGreen(), - colors[i].get().getBlue(), + (int) colors[i].get().red, + (int) colors[i].get().green, + (int) colors[i].get().blue, 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 95ebee7e..f6e384b4 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -2,11 +2,11 @@ import com.ctre.phoenix.led.LarsonAnimation; import com.ctre.phoenix.led.TwinkleAnimation; +import edu.wpi.first.wpilibj.util.Color; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.FunctionalCommand; import org.trigon.commands.ExecuteEndCommand; -import java.awt.*; import java.util.function.Consumer; import java.util.function.Supplier; @@ -72,7 +72,7 @@ public static Command getRainbowCommand(double brightness, double speed, LEDStri ).ignoringDisable(true); } - public static void runForLEDs(Consumer action, LEDStrip... ledStrips) { + private static void runForLEDs(Consumer action, LEDStrip... ledStrips) { 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 cace1cc4..de5bf11f 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -2,10 +2,10 @@ import com.ctre.phoenix.led.LarsonAnimation; import com.ctre.phoenix.led.TwinkleAnimation; +import edu.wpi.first.wpilibj.util.Color; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.SubsystemBase; -import java.awt.*; import java.util.function.Supplier; public abstract class LEDStrip extends SubsystemBase { From 46bb512b5f9b2b758a12322d1d87bb16fa5e4a7c Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Mon, 11 Nov 2024 09:59:11 +0200 Subject: [PATCH 05/31] cleaned --- .../misc/leds/AddressableLEDStrip.java | 31 ++++++++----------- .../hardware/misc/leds/CANdleLEDStrip.java | 2 +- .../hardware/misc/leds/LEDCommands.java | 5 ++- .../trigon/hardware/misc/leds/LEDStrip.java | 3 +- 4 files changed, 17 insertions(+), 24 deletions(-) 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 77e363c4..9bf8c7d8 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -1,7 +1,6 @@ package org.trigon.hardware.misc.leds; import com.ctre.phoenix.led.LarsonAnimation; -import com.ctre.phoenix.led.TwinkleAnimation; import edu.wpi.first.wpilibj.AddressableLED; import edu.wpi.first.wpilibj.AddressableLEDBuffer; import edu.wpi.first.wpilibj.Timer; @@ -13,13 +12,10 @@ public class AddressableLEDStrip extends LEDStrip { private static AddressableLED LED; private static AddressableLEDBuffer LED_BUFFER; private int lastBreatheLED; - private double lastBreatheMovementTime = 0; + private double lastLEDMovementTime = 0; private double rainbowFirstPixelHue = 0; private boolean areLEDsOnForBlinking = false; - private double lastBlinkTime = 0; private boolean alternateColor = true; - private double lastAlternateColorTime = 0; - private double lastColorFlowTime = 0; private int amountOfColorFlowLEDs = 0; public static void setLED(AddressableLED led) { @@ -48,8 +44,8 @@ void clearLEDColors() { @Override void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { double currentTime = Timer.getFPGATimestamp(); - if (currentTime - lastBlinkTime > blinkingIntervalSeconds) { - lastBlinkTime = currentTime; + if (currentTime - lastLEDMovementTime > blinkingIntervalSeconds) { + lastLEDMovementTime = currentTime; areLEDsOnForBlinking = !areLEDsOnForBlinking; } if (areLEDsOnForBlinking) @@ -69,8 +65,8 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean sh inverted = this.inverted != inverted; double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; double currentTime = Timer.getFPGATimestamp(); - if (currentTime - lastBreatheMovementTime > moveLEDTimeSeconds) { - lastBreatheMovementTime = currentTime; + if (currentTime - lastLEDMovementTime > moveLEDTimeSeconds) { + lastLEDMovementTime = currentTime; if (inverted) lastBreatheLED--; else @@ -96,8 +92,9 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean clearLEDColors(); inverted = this.inverted != inverted; double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; - if (Timer.getFPGATimestamp() - lastColorFlowTime > moveLEDTimeSeconds) { - lastColorFlowTime = Timer.getFPGATimestamp(); + double currentTime = Timer.getFPGATimestamp(); + if (currentTime - lastLEDMovementTime > moveLEDTimeSeconds) { + lastLEDMovementTime = currentTime; if (inverted) amountOfColorFlowLEDs--; else @@ -118,10 +115,11 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean } @Override - void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { - if (Timer.getFPGATimestamp() - lastAlternateColorTime > intervalSeconds) { + void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) { + double currentTime = Timer.getFPGATimestamp(); + if (currentTime - lastLEDMovementTime > intervalSeconds) { alternateColor = !alternateColor; - lastAlternateColorTime = Timer.getFPGATimestamp(); + lastLEDMovementTime = currentTime; } if (alternateColor) { for (int i = 0; i < numberOfLEDs; i++) @@ -160,13 +158,10 @@ void sectionColor(int amountOfSections, Supplier[] colors) { @Override void resetLEDSettings() { lastBreatheLED = indexOffset; - lastBreatheMovementTime = Timer.getFPGATimestamp(); + lastLEDMovementTime = Timer.getFPGATimestamp(); rainbowFirstPixelHue = 0; areLEDsOnForBlinking = false; - lastBlinkTime = 0; alternateColor = true; - lastAlternateColorTime = 0; - lastColorFlowTime = 0; amountOfColorFlowLEDs = 0; } 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 885a6b52..71a949b5 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -68,7 +68,7 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean sh } @Override - void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { + void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) { if (Timer.getFPGATimestamp() - lastAlternateColorTime > intervalSeconds) { alternateColor = !alternateColor; lastAlternateColorTime = Timer.getFPGATimestamp(); 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 f6e384b4..ec520d8b 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -1,7 +1,6 @@ package org.trigon.hardware.misc.leds; import com.ctre.phoenix.led.LarsonAnimation; -import com.ctre.phoenix.led.TwinkleAnimation; import edu.wpi.first.wpilibj.util.Color; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.FunctionalCommand; @@ -51,9 +50,9 @@ public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, ).ignoringDisable(true); } - public static Command getAlternateColorCommand(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider) { + public static Command getAlternateColorCommand(Color firstColor, Color secondColor, double intervalSeconds, LEDStrip... ledStrips) { return new ExecuteEndCommand( - () -> runForLEDs(LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds, divider), LEDStrip.LED_STRIPS), + () -> runForLEDs(LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds), ledStrips), () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrip.LED_STRIPS) ).ignoringDisable(true); } 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 de5bf11f..8a033db1 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -1,7 +1,6 @@ package org.trigon.hardware.misc.leds; import com.ctre.phoenix.led.LarsonAnimation; -import com.ctre.phoenix.led.TwinkleAnimation; import edu.wpi.first.wpilibj.util.Color; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.SubsystemBase; @@ -42,7 +41,7 @@ void resetLEDSettings() { abstract void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted); - abstract void alternateColor(Color firstColor, Color secondColor, double intervalSeconds, TwinkleAnimation.TwinklePercent divider); + abstract void alternateColor(Color firstColor, Color secondColor, double intervalSeconds); abstract void sectionColor(int amountOfSections, Supplier[] colors); From 8b007061a082aa1e6d074fcad0459314524ad4da Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Mon, 11 Nov 2024 11:08:18 +0200 Subject: [PATCH 06/31] added jDocs for the patterns, and fixed colorFlow inverted --- .../hardware/misc/leds/CANdleLEDStrip.java | 1 + .../trigon/hardware/misc/leds/LEDStrip.java | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) 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 71a949b5..278b0fbe 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -98,6 +98,7 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) @Override void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted) { + inverted = this.inverted != inverted; CANDLE.animate( new ColorFlowAnimation( (int) color.red, 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 8a033db1..b28a2324 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -33,18 +33,67 @@ public int getNumberOfLEDS() { 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); + /** + * Blinks the LED strip between two colors. + * + * @param firstColor the first color to blink + * @param secondColor the second color to blink, only for AddressableLEDStrip + * @param blinkingIntervalSeconds the interval in seconds to blink between the two colors + */ abstract void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds); + /** + * "Breathes" a bunch of LEDs with a given color. + * + * @param color the color of the breathing LEDs + * @param breathingLEDs the amount of breathing LEDs + * @param cycleTimeSeconds the time it takes for a full cycle of the breathing + * @param shouldLoop whether the breathing should loop, only for AddressableLEDStrip + * @param inverted whether the breathing should be inverted, only for AddressableLEDStrip + * @param bounceMode the bounce mode of the breathing, only for CANdleLEDStrip + */ abstract void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode); + /** + * Flows a color through the LED strip. + * + * @param color the color to flow through the LED strip + * @param cycleTimeSeconds the time it takes for the color to flow through the LED strip + * @param shouldLoop whether the color should loop, only for AddressableLEDStrip + * @param inverted whether the color should be inverted + */ abstract void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted); + /** + * Displays two colors in an alternating pattern on the LED strip. + * + * @param firstColor the first color + * @param secondColor the second color + * @param intervalSeconds the interval in seconds to alternate colors + */ abstract void alternateColor(Color firstColor, Color secondColor, double intervalSeconds); + /** + * Colors the LED strip in sections. + * + * @param amountOfSections the amount of sections to color + * @param colors an array of the colors to color the sections with. The length of the array must be equal to the amount of sections + */ abstract void sectionColor(int amountOfSections, Supplier[] colors); + /** + * Displays a rainbow pattern on the LED strip. + * + * @param brightness the brightness of the rainbow, only for CANdleLEDStrip + * @param speed the speed of the rainbow, only for CANdleLEDStrip + */ abstract void rainbow(double brightness, double speed); abstract void clearLEDColors(); From 61cac26e4f7afb98c8fabe9803d5ec355b7aed78 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:25:41 +0200 Subject: [PATCH 07/31] added requirements --- .../trigon/hardware/misc/leds/LEDCommands.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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 ec520d8b..ccfb270e 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -13,14 +13,16 @@ public class LEDCommands { public static Command getStaticColorCommand(Color color, LEDStrip... ledStrips) { return new ExecuteEndCommand( () -> runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } public static Command getBlinkingCommand(Color firstColor, Color secondColor, double blinkingIntervalSeconds, LEDStrip... ledStrips) { return new ExecuteEndCommand( () -> runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } @@ -53,21 +55,24 @@ public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, public static Command getAlternateColorCommand(Color firstColor, Color secondColor, double intervalSeconds, LEDStrip... ledStrips) { return new ExecuteEndCommand( () -> runForLEDs(LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrip.LED_STRIPS) + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrip.LED_STRIPS), + ledStrips ).ignoringDisable(true); } public static Command getSectionColorCommand(int amountOfSections, Supplier[] colors, LEDStrip... ledStrips) { return new ExecuteEndCommand( () -> runForLEDs((LEDStrip) -> LEDStrip.sectionColor(amountOfSections, colors), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } public static Command getRainbowCommand(double brightness, double speed, LEDStrip... ledStrips) { return new ExecuteEndCommand( () -> runForLEDs(LEDStrip -> LEDStrip.rainbow(brightness, speed), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips) + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } From 6abcf665aea2add315047cb3165f11bfefffa220 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Mon, 11 Nov 2024 18:12:52 +0200 Subject: [PATCH 08/31] made CANdleLEDStrip use AddressableLEDStrip in sim --- .../hardware/misc/leds/CANdleLEDStrip.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) 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 278b0fbe..e10c9704 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -1,8 +1,11 @@ package org.trigon.hardware.misc.leds; import com.ctre.phoenix.led.*; +import edu.wpi.first.wpilibj.AddressableLED; +import edu.wpi.first.wpilibj.AddressableLEDBuffer; import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj.util.Color; +import org.trigon.hardware.RobotHardwareStats; import java.util.function.Supplier; @@ -10,6 +13,7 @@ public class CANdleLEDStrip extends LEDStrip { private static CANdle CANDLE; private static int LAST_CREATED_LED_STRIP_ANIMATION_SLOT = 0; private final int animationSlot; + private AddressableLEDStrip simulationLEDStrip; private boolean alternateColor = true; private double lastAlternateColorTime = 0; @@ -21,15 +25,29 @@ public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { super(inverted, numberOfLEDs, indexOffset); LAST_CREATED_LED_STRIP_ANIMATION_SLOT++; animationSlot = LAST_CREATED_LED_STRIP_ANIMATION_SLOT; + + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip = new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); + AddressableLEDStrip.setLED(new AddressableLED(0)); + AddressableLEDStrip.setLEDBuffer(new AddressableLEDBuffer(numberOfLEDs)); + } } @Override void clearLEDColors() { + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip.clearLEDColors(); + return; + } CANDLE.clearAnimation(animationSlot); } @Override void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds); + return; + } CANDLE.animate( new SingleFadeAnimation( (int) firstColor.red, @@ -46,11 +64,19 @@ void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) @Override void staticColor(Color color) { + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip.staticColor(color); + return; + } CANDLE.setLEDs((int) color.red, (int) color.green, (int) color.blue, 0, indexOffset, numberOfLEDs); } @Override void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip.breathe(color, breathingLEDs, cycleTimeSeconds, shouldLoop, inverted, bounceMode); + return; + } CANDLE.animate( new LarsonAnimation( (int) color.red, @@ -69,6 +95,10 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean sh @Override void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) { + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip.alternateColor(firstColor, secondColor, intervalSeconds); + return; + } if (Timer.getFPGATimestamp() - lastAlternateColorTime > intervalSeconds) { alternateColor = !alternateColor; lastAlternateColorTime = Timer.getFPGATimestamp(); @@ -98,6 +128,10 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) @Override void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted) { + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip.colorFlow(color, cycleTimeSeconds, shouldLoop, inverted); + return; + } inverted = this.inverted != inverted; CANDLE.animate( new ColorFlowAnimation( @@ -116,6 +150,10 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean @Override void rainbow(double brightness, double speed) { + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip.rainbow(brightness, speed); + return; + } CANDLE.animate( new RainbowAnimation( brightness, @@ -130,12 +168,22 @@ void rainbow(double brightness, double speed) { @Override void sectionColor(int amountOfSections, Supplier[] colors) { + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip.sectionColor(amountOfSections, colors); + return; + } if (amountOfSections != colors.length) throw new IllegalArgumentException("Amount of sections must be equal to the amount of colors"); final int LEDSPerSection = (int) Math.floor(numberOfLEDs / amountOfSections); setSectionColor(amountOfSections, LEDSPerSection, colors); } + @Override + void resetLEDSettings() { + if (RobotHardwareStats.isSimulation()) + simulationLEDStrip.resetLEDSettings(); + } + private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier[] colors) { if (inverted) { for (int i = 0; i < amountOfSections; i++) { From b97318a3f7ad74b951299931ef4165243c796ee7 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:43:07 +0200 Subject: [PATCH 09/31] cleaned a bunch --- .../misc/leds/AddressableLEDStrip.java | 25 +++---- .../hardware/misc/leds/CANdleLEDStrip.java | 66 +++++-------------- .../hardware/misc/leds/LEDCommands.java | 4 +- .../trigon/hardware/misc/leds/LEDStrip.java | 15 ++--- 4 files changed, 36 insertions(+), 74 deletions(-) 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 9bf8c7d8..455528b8 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -148,11 +148,16 @@ void rainbow(double brightness, double speed) { } @Override - void sectionColor(int amountOfSections, Supplier[] colors) { - if (amountOfSections != colors.length) - throw new IllegalArgumentException("Amount of sections must be equal to the amount of colors"); - final int LEDSPerSection = (int) Math.floor(numberOfLEDs / amountOfSections); - setSectionColor(amountOfSections, LEDSPerSection, colors); + void sectionColor(Supplier[] colors) { + final int amountOfSections = colors.length; + final int LEDsPerSection = (int) Math.floor(numberOfLEDs / amountOfSections); + + for (int i = 0; i < amountOfSections; i++) + setLEDColors( + inverted ? colors[amountOfSections - i - 1].get() : colors[i].get(), + LEDsPerSection * i, + i == amountOfSections - 1 ? numberOfLEDs - 1 : LEDsPerSection * (i + 1) - 1 + ); } @Override @@ -165,16 +170,6 @@ void resetLEDSettings() { amountOfColorFlowLEDs = 0; } - private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier[] colors) { - if (inverted) { - for (int i = 0; i < amountOfSections; i++) - setLEDColors(colors[amountOfSections - i - 1].get(), LEDSPerSection * i, i == amountOfSections - 1 ? numberOfLEDs - 1 : LEDSPerSection * (i + 1) - 1); - return; - } - for (int i = 0; i < amountOfSections; i++) - setLEDColors(colors[i].get(), LEDSPerSection * i, i == amountOfSections - 1 ? numberOfLEDs - 1 : LEDSPerSection * (i + 1) - 1); - } - private void setLEDColors(Color color, int startIndex, int endIndex) { for (int i = 0; i <= endIndex - startIndex; i++) LED_BUFFER.setLED(startIndex + indexOffset + i, color); 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 e10c9704..c17f50cc 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -1,8 +1,6 @@ package org.trigon.hardware.misc.leds; import com.ctre.phoenix.led.*; -import edu.wpi.first.wpilibj.AddressableLED; -import edu.wpi.first.wpilibj.AddressableLEDBuffer; import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj.util.Color; import org.trigon.hardware.RobotHardwareStats; @@ -13,24 +11,19 @@ public class CANdleLEDStrip extends LEDStrip { private static CANdle CANDLE; private static int LAST_CREATED_LED_STRIP_ANIMATION_SLOT = 0; private final int animationSlot; - private AddressableLEDStrip simulationLEDStrip; - private boolean alternateColor = true; + private final AddressableLEDStrip simulationLEDStrip; + private boolean isAlternateColorAlternated = true; private double lastAlternateColorTime = 0; public static void setCandle(CANdle candle) { CANDLE = candle; } - public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset, AddressableLEDStrip simulationLEDStrip) { super(inverted, numberOfLEDs, indexOffset); - LAST_CREATED_LED_STRIP_ANIMATION_SLOT++; animationSlot = LAST_CREATED_LED_STRIP_ANIMATION_SLOT; - - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip = new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); - AddressableLEDStrip.setLED(new AddressableLED(0)); - AddressableLEDStrip.setLEDBuffer(new AddressableLEDBuffer(numberOfLEDs)); - } + LAST_CREATED_LED_STRIP_ANIMATION_SLOT++; + this.simulationLEDStrip = simulationLEDStrip; } @Override @@ -100,30 +93,20 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) return; } if (Timer.getFPGATimestamp() - lastAlternateColorTime > intervalSeconds) { - alternateColor = !alternateColor; + isAlternateColorAlternated = !isAlternateColorAlternated; lastAlternateColorTime = Timer.getFPGATimestamp(); } - if (alternateColor) { + if (isAlternateColorAlternated) { for (int i = 0; i < numberOfLEDs; i++) CANDLE.setLEDs( - (int) firstColor.red, - (int) firstColor.green, - (int) firstColor.blue, + (int) (i % 2 == 0 ? firstColor.red : secondColor.red), + (int) (i % 2 == 0 ? firstColor.green : secondColor.green), + (int) (i % 2 == 0 ? firstColor.blue : secondColor.blue), 0, i + indexOffset, 1 ); - return; } - for (int i = 0; i < numberOfLEDs; i++) - CANDLE.setLEDs( - (int) firstColor.red, - (int) firstColor.green, - (int) firstColor.blue, - 0, - i + indexOffset, - 1 - ); } @Override @@ -167,15 +150,13 @@ void rainbow(double brightness, double speed) { } @Override - void sectionColor(int amountOfSections, Supplier[] colors) { + void sectionColor(Supplier[] colors) { if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.sectionColor(amountOfSections, colors); + simulationLEDStrip.sectionColor(colors); return; } - if (amountOfSections != colors.length) - throw new IllegalArgumentException("Amount of sections must be equal to the amount of colors"); - final int LEDSPerSection = (int) Math.floor(numberOfLEDs / amountOfSections); - setSectionColor(amountOfSections, LEDSPerSection, colors); + final int LEDSPerSection = (int) Math.floor(numberOfLEDs / colors.length); + setSectionColor(colors.length, LEDSPerSection, colors); } @Override @@ -185,24 +166,11 @@ void resetLEDSettings() { } private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier[] colors) { - if (inverted) { - for (int i = 0; i < amountOfSections; i++) { - CANDLE.setLEDs( - (int) colors[amountOfSections - i - 1].get().red, - (int) colors[amountOfSections - i - 1].get().green, - (int) colors[amountOfSections - i - 1].get().blue, - 0, - LEDSPerSection * i + indexOffset, - i == amountOfSections - 1 ? numberOfLEDs - 1 : LEDSPerSection * (i + 1) - 1 - ); - } - return; - } for (int i = 0; i < amountOfSections; i++) { CANDLE.setLEDs( - (int) colors[i].get().red, - (int) colors[i].get().green, - (int) colors[i].get().blue, + (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), 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 ccfb270e..aa9835c1 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -60,9 +60,9 @@ public static Command getAlternateColorCommand(Color firstColor, Color secondCol ).ignoringDisable(true); } - public static Command getSectionColorCommand(int amountOfSections, Supplier[] colors, LEDStrip... ledStrips) { + public static Command getSectionColorCommand(Supplier[] colors, LEDStrip... ledStrips) { return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip) -> LEDStrip.sectionColor(amountOfSections, colors), ledStrips), + () -> runForLEDs((LEDStrip) -> LEDStrip.sectionColor(colors), ledStrips), () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), ledStrips ).ignoringDisable(true); 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 b28a2324..77b3f204 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -14,9 +14,9 @@ public abstract class LEDStrip extends SubsystemBase { final int numberOfLEDs; public LEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { - this.indexOffset = indexOffset; this.inverted = inverted; this.numberOfLEDs = numberOfLEDs; + this.indexOffset = indexOffset; addLEDStripToLEDStripsArray(this); } @@ -44,7 +44,7 @@ void resetLEDSettings() { * Blinks the LED strip between two colors. * * @param firstColor the first color to blink - * @param secondColor the second color to blink, only for AddressableLEDStrip + * @param secondColor the second color to blink * @param blinkingIntervalSeconds the interval in seconds to blink between the two colors */ abstract void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds); @@ -55,8 +55,8 @@ void resetLEDSettings() { * @param color the color of the breathing LEDs * @param breathingLEDs the amount of breathing LEDs * @param cycleTimeSeconds the time it takes for a full cycle of the breathing - * @param shouldLoop whether the breathing should loop, only for AddressableLEDStrip - * @param inverted whether the breathing should be inverted, only for AddressableLEDStrip + * @param shouldLoop whether the breathing should loop + * @param inverted whether the breathing should be inverted * @param bounceMode the bounce mode of the breathing, only for CANdleLEDStrip */ abstract void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode); @@ -66,7 +66,7 @@ void resetLEDSettings() { * * @param color the color to flow through the LED strip * @param cycleTimeSeconds the time it takes for the color to flow through the LED strip - * @param shouldLoop whether the color should loop, only for AddressableLEDStrip + * @param shouldLoop whether the color should loop * @param inverted whether the color should be inverted */ abstract void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted); @@ -83,10 +83,9 @@ void resetLEDSettings() { /** * Colors the LED strip in sections. * - * @param amountOfSections the amount of sections to color - * @param colors an array of the colors to color the sections with. The length of the array must be equal to the amount of sections + * @param colors an array of the colors to color the sections with. The length of the array is the amount of sections. */ - abstract void sectionColor(int amountOfSections, Supplier[] colors); + abstract void sectionColor(Supplier[] colors); /** * Displays a rainbow pattern on the LED strip. From 63766488936a038ddd77fdb90f486c4113f73ecf Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:59:27 +0200 Subject: [PATCH 10/31] cleaned stuff --- .../misc/leds/AddressableLEDStrip.java | 66 ++++++++++--------- 1 file changed, 35 insertions(+), 31 deletions(-) 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 455528b8..74632aa5 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -12,10 +12,9 @@ public class AddressableLEDStrip extends LEDStrip { private static AddressableLED LED; private static AddressableLEDBuffer LED_BUFFER; private int lastBreatheLED; - private double lastLEDMovementTime = 0; + private double lastLEDAnimationChangeTime = 0; private double rainbowFirstPixelHue = 0; - private boolean areLEDsOnForBlinking = false; - private boolean alternateColor = true; + private boolean isLEDAnimationChanged = false; private int amountOfColorFlowLEDs = 0; public static void setLED(AddressableLED led) { @@ -44,11 +43,11 @@ void clearLEDColors() { @Override void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { double currentTime = Timer.getFPGATimestamp(); - if (currentTime - lastLEDMovementTime > blinkingIntervalSeconds) { - lastLEDMovementTime = currentTime; - areLEDsOnForBlinking = !areLEDsOnForBlinking; + if (currentTime - lastLEDAnimationChangeTime > blinkingIntervalSeconds) { + lastLEDAnimationChangeTime = currentTime; + isLEDAnimationChanged = !isLEDAnimationChanged; } - if (areLEDsOnForBlinking) + if (isLEDAnimationChanged) staticColor(firstColor); else staticColor(secondColor); @@ -65,26 +64,15 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean sh inverted = this.inverted != inverted; double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; double currentTime = Timer.getFPGATimestamp(); - if (currentTime - lastLEDMovementTime > moveLEDTimeSeconds) { - lastLEDMovementTime = currentTime; + + if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { + lastLEDAnimationChangeTime = currentTime; if (inverted) lastBreatheLED--; else lastBreatheLED++; } - if (inverted ? (lastBreatheLED < indexOffset) : (lastBreatheLED >= numberOfLEDs + indexOffset)) { - if (!shouldLoop) { - getDefaultCommand().schedule(); - return; - } - lastBreatheLED = inverted ? indexOffset + numberOfLEDs : indexOffset; - } - for (int i = 0; i < breathingLEDs; i++) { - if (lastBreatheLED - i >= indexOffset && lastBreatheLED - i < indexOffset + numberOfLEDs) - LED_BUFFER.setLED(lastBreatheLED - i, color); - else if (lastBreatheLED - i < indexOffset + numberOfLEDs) - LED_BUFFER.setLED(lastBreatheLED - i + numberOfLEDs, color); - } + setBreathingLEDs(color, breathingLEDs, shouldLoop); } @Override @@ -93,8 +81,8 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted = this.inverted != inverted; double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; double currentTime = Timer.getFPGATimestamp(); - if (currentTime - lastLEDMovementTime > moveLEDTimeSeconds) { - lastLEDMovementTime = currentTime; + if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { + lastLEDAnimationChangeTime = currentTime; if (inverted) amountOfColorFlowLEDs--; else @@ -117,11 +105,11 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean @Override void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) { double currentTime = Timer.getFPGATimestamp(); - if (currentTime - lastLEDMovementTime > intervalSeconds) { - alternateColor = !alternateColor; - lastLEDMovementTime = currentTime; + if (currentTime - lastLEDAnimationChangeTime > intervalSeconds) { + isLEDAnimationChanged = !isLEDAnimationChanged; + lastLEDAnimationChangeTime = currentTime; } - if (alternateColor) { + if (isLEDAnimationChanged) { for (int i = 0; i < numberOfLEDs; i++) LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? firstColor : secondColor); return; @@ -163,13 +151,29 @@ void sectionColor(Supplier[] colors) { @Override void resetLEDSettings() { lastBreatheLED = indexOffset; - lastLEDMovementTime = Timer.getFPGATimestamp(); + lastLEDAnimationChangeTime = Timer.getFPGATimestamp(); rainbowFirstPixelHue = 0; - areLEDsOnForBlinking = false; - alternateColor = true; + isLEDAnimationChanged = false; amountOfColorFlowLEDs = 0; } + private void setBreathingLEDs(Color color, int breathingLEDs, boolean shouldLoop) { + if (inverted ? (lastBreatheLED < indexOffset) : (lastBreatheLED >= numberOfLEDs + indexOffset)) { + if (!shouldLoop) { + getDefaultCommand().schedule(); + return; + } + lastBreatheLED = inverted ? indexOffset + numberOfLEDs : indexOffset; + } + + for (int i = 0; i < breathingLEDs; i++) { + if (lastBreatheLED - i >= indexOffset && lastBreatheLED - i < indexOffset + numberOfLEDs) + LED_BUFFER.setLED(lastBreatheLED - i, color); + else if (lastBreatheLED - i < indexOffset + numberOfLEDs) + LED_BUFFER.setLED(lastBreatheLED - i + numberOfLEDs, color); + } + } + private void setLEDColors(Color color, int startIndex, int endIndex) { for (int i = 0; i <= endIndex - startIndex; i++) LED_BUFFER.setLED(startIndex + indexOffset + i, color); From ac14f6e4258e97bb0712f72acd11e2fde1ddd5df Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:11:09 +0200 Subject: [PATCH 11/31] added jDocs and improved simulation --- .../misc/leds/AddressableLEDStrip.java | 17 +++++++++ .../hardware/misc/leds/CANdleLEDStrip.java | 36 +++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) 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 74632aa5..75bbccf1 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -17,14 +17,31 @@ public class AddressableLEDStrip extends LEDStrip { private boolean isLEDAnimationChanged = false; private int amountOfColorFlowLEDs = 0; + /** + * Sets the AddressableLED instance to be used for controlling the LED strip. Must be set before using any LED strips. + * + * @param led the LED instance to be used + */ public static void setLED(AddressableLED led) { LED = led; } + /** + * Sets the AddressableLEDBuffer instance to be used for controlling the LED strip. Must be set before using any LED strips. + * + * @param ledBuffer the LED buffer instance to be used + */ public static void setLEDBuffer(AddressableLEDBuffer ledBuffer) { LED_BUFFER = ledBuffer; } + /** + * Constructs a new AddressableLEDStrip. + * + * @param inverted whether the LED strip is inverted + * @param numberOfLEDs the amount of LEDs in the strip + * @param indexOffset the offset of the first LED in the strip + */ public AddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { super(inverted, numberOfLEDs, indexOffset); resetLEDSettings(); 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 c17f50cc..37024ad0 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -1,6 +1,8 @@ package org.trigon.hardware.misc.leds; import com.ctre.phoenix.led.*; +import edu.wpi.first.wpilibj.AddressableLED; +import edu.wpi.first.wpilibj.AddressableLEDBuffer; import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj.util.Color; import org.trigon.hardware.RobotHardwareStats; @@ -15,15 +17,45 @@ public class CANdleLEDStrip extends LEDStrip { private boolean isAlternateColorAlternated = true; private double lastAlternateColorTime = 0; + /** + * Sets the CANdle instance to be used for controlling the LED strips. Must be set before using any LED strips. + * + * @param candle the CANdle instance to be used + */ public static void setCandle(CANdle candle) { CANDLE = candle; } - public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset, AddressableLEDStrip simulationLEDStrip) { + /** + * Sets the simulation AddressableLED instance to be used for testing in simulation. Must be set before using any LED strips in simulation. + * + * @param simulationLEDStrip the AddressableLED instance to be used in simulation + */ + public static void setSimulationLED(AddressableLED simulationLEDStrip) { + AddressableLEDStrip.setLED(simulationLEDStrip); + } + + /** + * Sets the simulation AddressableLEDBuffer instance to be used for testing in simulation. Must be set before using any LED strips in simulation. + * + * @param simulationLEDBuffer the AddressableLED buffer instance to be used in simulation + */ + public static void setSimulationLEDBuffer(AddressableLEDBuffer simulationLEDBuffer) { + AddressableLEDStrip.setLEDBuffer(simulationLEDBuffer); + } + + /** + * Constructs a new CANdleLEDStrip. + * + * @param inverted whether the LED strip is inverted + * @param numberOfLEDs the amount of LEDs in the strip + * @param indexOffset the offset of the first LED in the strip + */ + public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { super(inverted, numberOfLEDs, indexOffset); animationSlot = LAST_CREATED_LED_STRIP_ANIMATION_SLOT; LAST_CREATED_LED_STRIP_ANIMATION_SLOT++; - this.simulationLEDStrip = simulationLEDStrip; + this.simulationLEDStrip = new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); } @Override From 9586d5ca1048a12d7e088af2404bce0d81a053c8 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:22:34 +0200 Subject: [PATCH 12/31] improved setLED method --- .../org/trigon/hardware/misc/leds/AddressableLEDStrip.java | 3 +++ 1 file changed, 3 insertions(+) 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 75bbccf1..1ea1599d 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -19,15 +19,18 @@ public class AddressableLEDStrip extends LEDStrip { /** * Sets the AddressableLED instance to be used for controlling the LED strip. Must be set before using any LED strips. + * The LED instance be configured before being set, however it does not need to be started. * * @param led the LED instance to be used */ public static void setLED(AddressableLED led) { LED = led; + LED.start(); } /** * Sets the AddressableLEDBuffer instance to be used for controlling the LED strip. Must be set before using any LED strips. + * The LED buffer instance must be configured before being set. * * @param ledBuffer the LED buffer instance to be used */ From 40ba58dd4a5eabb75e663f501faad3c5badee309 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:07:13 +0200 Subject: [PATCH 13/31] cleaned up colorFlow --- .../misc/leds/AddressableLEDStrip.java | 29 ++++++++++--------- .../hardware/misc/leds/CANdleLEDStrip.java | 2 +- 2 files changed, 16 insertions(+), 15 deletions(-) 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 1ea1599d..53872e0c 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -39,7 +39,7 @@ public static void setLEDBuffer(AddressableLEDBuffer ledBuffer) { } /** - * Constructs a new AddressableLEDStrip. + * Constructs a new AddressableLEDStrip. Before any commands are sent to the LED strip, the setLED method must be called. * * @param inverted whether the LED strip is inverted * @param numberOfLEDs the amount of LEDs in the strip @@ -103,23 +103,13 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean double currentTime = Timer.getFPGATimestamp(); if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { lastLEDAnimationChangeTime = currentTime; - if (inverted) + if (isLEDAnimationChanged) amountOfColorFlowLEDs--; else amountOfColorFlowLEDs++; } - if (inverted ? amountOfColorFlowLEDs < 0 : amountOfColorFlowLEDs >= numberOfLEDs) { - if (!shouldLoop) { - getDefaultCommand().schedule(); - return; - } - amountOfColorFlowLEDs = inverted ? numberOfLEDs : 0; - } - if (inverted) { - setLEDColors(color, numberOfLEDs - amountOfColorFlowLEDs, numberOfLEDs - 1); - return; - } - setLEDColors(color, 0, amountOfColorFlowLEDs); + checkIfColorFlowHasHitEnd(shouldLoop); + setLEDColors(color, inverted ? numberOfLEDs - amountOfColorFlowLEDs - 1 : 0, inverted ? numberOfLEDs - 1 : amountOfColorFlowLEDs); } @Override @@ -194,6 +184,17 @@ else if (lastBreatheLED - i < indexOffset + numberOfLEDs) } } + private void checkIfColorFlowHasHitEnd(boolean shouldLoop) { + if (amountOfColorFlowLEDs >= numberOfLEDs || amountOfColorFlowLEDs < 0) { + if (!shouldLoop) { + getDefaultCommand().schedule(); + return; + } + amountOfColorFlowLEDs = amountOfColorFlowLEDs < 0 ? amountOfColorFlowLEDs + 1 : amountOfColorFlowLEDs - 1; + isLEDAnimationChanged = !isLEDAnimationChanged; + } + } + private void setLEDColors(Color color, int startIndex, int endIndex) { for (int i = 0; i <= endIndex - startIndex; i++) LED_BUFFER.setLED(startIndex + indexOffset + i, color); 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 37024ad0..4558d5ec 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -45,7 +45,7 @@ public static void setSimulationLEDBuffer(AddressableLEDBuffer simulationLEDBuff } /** - * Constructs a new CANdleLEDStrip. + * Constructs a new CANdleLEDStrip. Before any commands are sent to the LED strip, the setLED method must be called. * * @param inverted whether the LED strip is inverted * @param numberOfLEDs the amount of LEDs in the strip From f10e39c0946a22aca693cf091609938bcdeb1c0b Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 13 Nov 2024 18:50:20 +0200 Subject: [PATCH 14/31] implementing this was soooooooooooooooooo annoying --- .../misc/leds/AddressableLEDStrip.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) 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 53872e0c..677233b4 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -80,11 +80,10 @@ void staticColor(Color color) { @Override void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { - clearLEDColors(); inverted = this.inverted != inverted; + clearLEDColors(); double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; double currentTime = Timer.getFPGATimestamp(); - if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { lastLEDAnimationChangeTime = currentTime; if (inverted) @@ -92,7 +91,8 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean sh else lastBreatheLED++; } - setBreathingLEDs(color, breathingLEDs, shouldLoop); + checkIfBreathingHasHitEnd(breathingLEDs, shouldLoop, inverted, bounceMode); + setBreathingLEDs(color, breathingLEDs, bounceMode); } @Override @@ -167,20 +167,30 @@ void resetLEDSettings() { amountOfColorFlowLEDs = 0; } - private void setBreathingLEDs(Color color, int breathingLEDs, boolean shouldLoop) { - if (inverted ? (lastBreatheLED < indexOffset) : (lastBreatheLED >= numberOfLEDs + indexOffset)) { + private void checkIfBreathingHasHitEnd(int amountOfBreathingLEDs, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + int bounceModeThing = switch (bounceMode) { + case Back -> amountOfBreathingLEDs; + case Center -> amountOfBreathingLEDs / 2; + default -> 0; + }; + if (inverted ? (lastBreatheLED < indexOffset + bounceModeThing) : (lastBreatheLED >= numberOfLEDs + indexOffset + bounceModeThing)) { if (!shouldLoop) { getDefaultCommand().schedule(); return; } lastBreatheLED = inverted ? indexOffset + numberOfLEDs : indexOffset; } + } + private void setBreathingLEDs(Color color, int breathingLEDs, LarsonAnimation.BounceMode bounceMode) { for (int i = 0; i < breathingLEDs; i++) { if (lastBreatheLED - i >= indexOffset && lastBreatheLED - i < indexOffset + numberOfLEDs) LED_BUFFER.setLED(lastBreatheLED - i, color); - else if (lastBreatheLED - i < indexOffset + numberOfLEDs) + else if (lastBreatheLED - i < indexOffset + numberOfLEDs) { + if (bounceMode.equals(LarsonAnimation.BounceMode.Back) || bounceMode.equals(LarsonAnimation.BounceMode.Center) && i > breathingLEDs / 2) + return; LED_BUFFER.setLED(lastBreatheLED - i + numberOfLEDs, color); + } } } From f6964e91012aed53c480c6aafaeb1a3dd290e28a Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 13 Nov 2024 18:59:55 +0200 Subject: [PATCH 15/31] made rainbow command work with params, and fixed var name --- .../misc/leds/AddressableLEDStrip.java | 18 +++++++++++------- .../trigon/hardware/misc/leds/LEDStrip.java | 6 +++--- 2 files changed, 14 insertions(+), 10 deletions(-) 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 677233b4..e52aa935 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -131,18 +131,22 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) @Override void rainbow(double brightness, double speed) { + int adjustedBrightness = (int) (brightness * 255); + int hueIncrement = (int) (speed * 3); + for (int led = 0; led < numberOfLEDs; led++) { final int hue = (int) (rainbowFirstPixelHue + (led * 180 / numberOfLEDs) % 180); - LED_BUFFER.setHSV(led + indexOffset, hue, 255, 128); + LED_BUFFER.setHSV(led + indexOffset, hue, 255, adjustedBrightness); } + if (inverted) { - rainbowFirstPixelHue -= 3; + rainbowFirstPixelHue -= hueIncrement; if (rainbowFirstPixelHue < 0) rainbowFirstPixelHue += 180; - return; + } else { + rainbowFirstPixelHue += hueIncrement; + rainbowFirstPixelHue %= 180; } - rainbowFirstPixelHue += 3; - rainbowFirstPixelHue %= 180; } @Override @@ -168,12 +172,12 @@ void resetLEDSettings() { } private void checkIfBreathingHasHitEnd(int amountOfBreathingLEDs, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { - int bounceModeThing = switch (bounceMode) { + int bounceModeAddition = switch (bounceMode) { case Back -> amountOfBreathingLEDs; case Center -> amountOfBreathingLEDs / 2; default -> 0; }; - if (inverted ? (lastBreatheLED < indexOffset + bounceModeThing) : (lastBreatheLED >= numberOfLEDs + indexOffset + bounceModeThing)) { + if (inverted ? (lastBreatheLED < indexOffset + bounceModeAddition) : (lastBreatheLED >= numberOfLEDs + indexOffset + bounceModeAddition)) { if (!shouldLoop) { getDefaultCommand().schedule(); return; 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 77b3f204..7d6b61b5 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -57,7 +57,7 @@ void resetLEDSettings() { * @param cycleTimeSeconds the time it takes for a full cycle of the breathing * @param shouldLoop whether the breathing should loop * @param inverted whether the breathing should be inverted - * @param bounceMode the bounce mode of the breathing, only for CANdleLEDStrip + * @param bounceMode the bounce mode of the breathing */ abstract void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode); @@ -90,8 +90,8 @@ void resetLEDSettings() { /** * Displays a rainbow pattern on the LED strip. * - * @param brightness the brightness of the rainbow, only for CANdleLEDStrip - * @param speed the speed of the rainbow, only for CANdleLEDStrip + * @param brightness the brightness of the rainbow, must be a number between 0 and 1 + * @param speed the speed of the rainbow */ abstract void rainbow(double brightness, double speed); From f5955aaf218c5d18271d1a1932c1abbd8079a68f Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 13 Nov 2024 19:02:45 +0200 Subject: [PATCH 16/31] removed extra new line --- .../java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java | 1 - src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) 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 e52aa935..11bb5154 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -128,7 +128,6 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? secondColor : firstColor); } - @Override void rainbow(double brightness, double speed) { int adjustedBrightness = (int) (brightness * 255); 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 7d6b61b5..842c7aad 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -103,4 +103,4 @@ private void addLEDStripToLEDStripsArray(LEDStrip ledStrip) { newLEDStrips[LED_STRIPS.length] = ledStrip; LED_STRIPS = newLEDStrips; } -} +} \ No newline at end of file From 087e07f55abc2250f21e83aa9898f317fc397404 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 13 Nov 2024 19:40:48 +0200 Subject: [PATCH 17/31] fixed commands, and removed shouldLoop (We don't need it Ezra) --- .../misc/leds/AddressableLEDStrip.java | 23 +-- .../hardware/misc/leds/CANdleLEDStrip.java | 8 +- .../hardware/misc/leds/LEDCommands.java | 132 +++++++++++++----- .../trigon/hardware/misc/leds/LEDStrip.java | 9 +- 4 files changed, 109 insertions(+), 63 deletions(-) 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 11bb5154..587701a8 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -79,7 +79,7 @@ void staticColor(Color color) { } @Override - void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode) { inverted = this.inverted != inverted; clearLEDColors(); double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; @@ -91,12 +91,12 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean sh else lastBreatheLED++; } - checkIfBreathingHasHitEnd(breathingLEDs, shouldLoop, inverted, bounceMode); + checkIfBreathingHasHitEnd(breathingLEDs, inverted, bounceMode); setBreathingLEDs(color, breathingLEDs, bounceMode); } @Override - void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted) { + void colorFlow(Color color, double cycleTimeSeconds, boolean inverted) { clearLEDColors(); inverted = this.inverted != inverted; double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; @@ -108,7 +108,7 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean else amountOfColorFlowLEDs++; } - checkIfColorFlowHasHitEnd(shouldLoop); + checkIfColorFlowHasHitEnd(); setLEDColors(color, inverted ? numberOfLEDs - amountOfColorFlowLEDs - 1 : 0, inverted ? numberOfLEDs - 1 : amountOfColorFlowLEDs); } @@ -170,19 +170,14 @@ void resetLEDSettings() { amountOfColorFlowLEDs = 0; } - private void checkIfBreathingHasHitEnd(int amountOfBreathingLEDs, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + private void checkIfBreathingHasHitEnd(int amountOfBreathingLEDs, boolean inverted, LarsonAnimation.BounceMode bounceMode) { int bounceModeAddition = switch (bounceMode) { case Back -> amountOfBreathingLEDs; case Center -> amountOfBreathingLEDs / 2; default -> 0; }; - if (inverted ? (lastBreatheLED < indexOffset + bounceModeAddition) : (lastBreatheLED >= numberOfLEDs + indexOffset + bounceModeAddition)) { - if (!shouldLoop) { - getDefaultCommand().schedule(); - return; - } + if (inverted ? (lastBreatheLED < indexOffset + bounceModeAddition) : (lastBreatheLED >= numberOfLEDs + indexOffset + bounceModeAddition)) lastBreatheLED = inverted ? indexOffset + numberOfLEDs : indexOffset; - } } private void setBreathingLEDs(Color color, int breathingLEDs, LarsonAnimation.BounceMode bounceMode) { @@ -197,12 +192,8 @@ else if (lastBreatheLED - i < indexOffset + numberOfLEDs) { } } - private void checkIfColorFlowHasHitEnd(boolean shouldLoop) { + private void checkIfColorFlowHasHitEnd() { if (amountOfColorFlowLEDs >= numberOfLEDs || amountOfColorFlowLEDs < 0) { - if (!shouldLoop) { - getDefaultCommand().schedule(); - return; - } amountOfColorFlowLEDs = amountOfColorFlowLEDs < 0 ? amountOfColorFlowLEDs + 1 : amountOfColorFlowLEDs - 1; isLEDAnimationChanged = !isLEDAnimationChanged; } 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 4558d5ec..f6cc6f45 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -97,9 +97,9 @@ void staticColor(Color color) { } @Override - void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode) { if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.breathe(color, breathingLEDs, cycleTimeSeconds, shouldLoop, inverted, bounceMode); + simulationLEDStrip.breathe(color, breathingLEDs, cycleTimeSeconds, inverted, bounceMode); return; } CANDLE.animate( @@ -142,9 +142,9 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) } @Override - void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted) { + void colorFlow(Color color, double cycleTimeSeconds, boolean inverted) { if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.colorFlow(color, cycleTimeSeconds, shouldLoop, inverted); + simulationLEDStrip.colorFlow(color, cycleTimeSeconds, inverted); return; } inverted = this.inverted != inverted; 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 aa9835c1..99ebae53 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -3,76 +3,130 @@ 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.FunctionalCommand; +import edu.wpi.first.wpilibj2.command.StartEndCommand; import org.trigon.commands.ExecuteEndCommand; +import org.trigon.hardware.RobotHardwareStats; import java.util.function.Consumer; import java.util.function.Supplier; public class LEDCommands { public static Command getStaticColorCommand(Color color, LEDStrip... ledStrips) { - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + if (isAddressableLED(ledStrips[0])) + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips + ).ignoringDisable(true); + return new StartEndCommand( + () -> { + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), ledStrips); + }, + () -> { + } ).ignoringDisable(true); } public static Command getBlinkingCommand(Color firstColor, Color secondColor, double blinkingIntervalSeconds, LEDStrip... ledStrips) { - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + if (isAddressableLED(ledStrips[0])) + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips + ).ignoringDisable(true); + return new StartEndCommand( + () -> { + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), ledStrips); + }, + () -> { + } ).ignoringDisable(true); } - public static Command getBreatheCommand(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... ledStrips) { - return new FunctionalCommand( + public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... ledStrips) { + if (isAddressableLED(ledStrips[0])) + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips + ).ignoringDisable(true); + return new StartEndCommand( () -> { - if (!shouldLoop) - runForLEDs(LEDStrip::resetLEDSettings, ledStrips); + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), ledStrips); }, - () -> runForLEDs((LEDStrip) -> LEDStrip.breathe(color, breathingLEDs, cycleTimeSeconds, shouldLoop, inverted, bounceMode), ledStrips), - (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - () -> false, - ledStrips + () -> { + } ).ignoringDisable(true); } - public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LEDStrip... ledStrips) { - return new FunctionalCommand( + public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean inverted, LEDStrip... ledStrips) { + if (isAddressableLED(ledStrips[0])) + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips + ).ignoringDisable(true); + return new StartEndCommand( () -> { - if (!shouldLoop) - runForLEDs(LEDStrip::resetLEDSettings, ledStrips); + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), ledStrips); }, - () -> runForLEDs((LEDStrip) -> LEDStrip.colorFlow(color, cycleTimeSeconds, shouldLoop, inverted), ledStrips), - (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - () -> false, - ledStrips + () -> { + } ).ignoringDisable(true); } public static Command getAlternateColorCommand(Color firstColor, Color secondColor, double intervalSeconds, LEDStrip... ledStrips) { - return new ExecuteEndCommand( - () -> runForLEDs(LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrip.LED_STRIPS), - ledStrips + if (isAddressableLED(ledStrips[0])) + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips + ).ignoringDisable(true); + return new StartEndCommand( + () -> { + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds)), ledStrips); + }, + () -> { + } ).ignoringDisable(true); } public static Command getSectionColorCommand(Supplier[] colors, LEDStrip... ledStrips) { - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip) -> LEDStrip.sectionColor(colors), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + if (isAddressableLED(ledStrips[0])) + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips + ).ignoringDisable(true); + return new StartEndCommand( + () -> { + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), ledStrips); + }, + () -> { + } ).ignoringDisable(true); } public static Command getRainbowCommand(double brightness, double speed, LEDStrip... ledStrips) { - return new ExecuteEndCommand( - () -> runForLEDs(LEDStrip -> LEDStrip.rainbow(brightness, speed), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + if (isAddressableLED(ledStrips[0])) + return new ExecuteEndCommand( + () -> runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), ledStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips + ).ignoringDisable(true); + return new StartEndCommand( + () -> { + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), ledStrips); + }, + () -> { + } ).ignoringDisable(true); } @@ -80,4 +134,8 @@ private static void runForLEDs(Consumer action, LEDStrip... ledStrips) for (LEDStrip LEDStrip : ledStrips) action.accept(LEDStrip); } + + public static boolean isAddressableLED(LEDStrip ledStrip) { + return ledStrip instanceof AddressableLEDStrip || RobotHardwareStats.isSimulation(); + } } \ No newline at end of file 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 842c7aad..9d5de762 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -30,8 +30,7 @@ public int getNumberOfLEDS() { return numberOfLEDs; } - void resetLEDSettings() { - } + abstract void resetLEDSettings(); /** * Sets the color of the LED strip to the given color. @@ -55,21 +54,19 @@ void resetLEDSettings() { * @param color the color of the breathing LEDs * @param breathingLEDs the amount of breathing LEDs * @param cycleTimeSeconds the time it takes for a full cycle of the breathing - * @param shouldLoop whether the breathing should loop * @param inverted whether the breathing should be inverted * @param bounceMode the bounce mode of the breathing */ - abstract void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean shouldLoop, boolean inverted, LarsonAnimation.BounceMode bounceMode); + abstract void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode); /** * Flows a color through the LED strip. * * @param color the color to flow through the LED strip * @param cycleTimeSeconds the time it takes for the color to flow through the LED strip - * @param shouldLoop whether the color should loop * @param inverted whether the color should be inverted */ - abstract void colorFlow(Color color, double cycleTimeSeconds, boolean shouldLoop, boolean inverted); + abstract void colorFlow(Color color, double cycleTimeSeconds, boolean inverted); /** * Displays two colors in an alternating pattern on the LED strip. From 30ec6a6428eb5e1319151a11190af919beaa5056 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 13 Nov 2024 21:32:07 +0200 Subject: [PATCH 18/31] cleaned code and jDocs --- .../hardware/misc/leds/CANdleLEDStrip.java | 6 +- .../hardware/misc/leds/LEDCommands.java | 106 +++++++++--------- .../trigon/hardware/misc/leds/LEDStrip.java | 14 +-- 3 files changed, 63 insertions(+), 63 deletions(-) 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 f6cc6f45..a42d6e5f 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -14,7 +14,7 @@ public class CANdleLEDStrip extends LEDStrip { private static int LAST_CREATED_LED_STRIP_ANIMATION_SLOT = 0; private final int animationSlot; private final AddressableLEDStrip simulationLEDStrip; - private boolean isAlternateColorAlternated = true; + private boolean areAlternateColorLEDsAlternated = true; private double lastAlternateColorTime = 0; /** @@ -125,10 +125,10 @@ void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) return; } if (Timer.getFPGATimestamp() - lastAlternateColorTime > intervalSeconds) { - isAlternateColorAlternated = !isAlternateColorAlternated; + areAlternateColorLEDsAlternated = !areAlternateColorLEDsAlternated; lastAlternateColorTime = Timer.getFPGATimestamp(); } - if (isAlternateColorAlternated) { + if (areAlternateColorLEDsAlternated) { for (int i = 0; i < numberOfLEDs; i++) CANDLE.setLEDs( (int) (i % 2 == 0 ? firstColor.red : secondColor.red), 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 99ebae53..0569a118 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -11,131 +11,131 @@ import java.util.function.Supplier; public class LEDCommands { - public static Command getStaticColorCommand(Color color, LEDStrip... ledStrips) { - if (isAddressableLED(ledStrips[0])) + public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) { + if (isAddressableLEDStrip(LEDStrips[0])) return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + () -> runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), LEDStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + LEDStrips ).ignoringDisable(true); return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); - runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), ledStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), LEDStrips); }, () -> { } ).ignoringDisable(true); } - public static Command getBlinkingCommand(Color firstColor, Color secondColor, double blinkingIntervalSeconds, LEDStrip... ledStrips) { - if (isAddressableLED(ledStrips[0])) + public static Command getBlinkingCommand(Color firstColor, Color secondColor, double blinkingIntervalSeconds, LEDStrip... LEDStrips) { + if (isAddressableLEDStrip(LEDStrips[0])) return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + () -> runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + LEDStrips ).ignoringDisable(true); return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); - runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), ledStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips); }, () -> { } ).ignoringDisable(true); } - public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... ledStrips) { - if (isAddressableLED(ledStrips[0])) + public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { + if (isAddressableLEDStrip(LEDStrips[0])) return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + () -> runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + LEDStrips ).ignoringDisable(true); return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); - runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), ledStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); }, () -> { } ).ignoringDisable(true); } - public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean inverted, LEDStrip... ledStrips) { - if (isAddressableLED(ledStrips[0])) + public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean inverted, LEDStrip... LEDStrips) { + if (isAddressableLEDStrip(LEDStrips[0])) return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + () -> runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + LEDStrips ).ignoringDisable(true); return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); - runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), ledStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); }, () -> { } ).ignoringDisable(true); } - public static Command getAlternateColorCommand(Color firstColor, Color secondColor, double intervalSeconds, LEDStrip... ledStrips) { - if (isAddressableLED(ledStrips[0])) + public static Command getAlternateColorCommand(Color firstColor, Color secondColor, double intervalSeconds, LEDStrip... LEDStrips) { + if (isAddressableLEDStrip(LEDStrips[0])) return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + () -> runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds)), LEDStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + LEDStrips ).ignoringDisable(true); return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); - runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds)), ledStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds)), LEDStrips); }, () -> { } ).ignoringDisable(true); } - public static Command getSectionColorCommand(Supplier[] colors, LEDStrip... ledStrips) { - if (isAddressableLED(ledStrips[0])) + public static Command getSectionColorCommand(Supplier[] colors, LEDStrip... LEDStrips) { + if (isAddressableLEDStrip(LEDStrips[0])) return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + () -> runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), LEDStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + LEDStrips ).ignoringDisable(true); return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); - runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), ledStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), LEDStrips); }, () -> { } ).ignoringDisable(true); } - public static Command getRainbowCommand(double brightness, double speed, LEDStrip... ledStrips) { - if (isAddressableLED(ledStrips[0])) + public static Command getRainbowCommand(double brightness, double speed, LEDStrip... LEDStrips) { + if (isAddressableLEDStrip(LEDStrips[0])) return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), ledStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), - ledStrips + () -> runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), LEDStrips), + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + LEDStrips ).ignoringDisable(true); return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), ledStrips); - runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), ledStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), LEDStrips); }, () -> { } ).ignoringDisable(true); } - private static void runForLEDs(Consumer action, LEDStrip... ledStrips) { - for (LEDStrip LEDStrip : ledStrips) + private static void runForLEDs(Consumer action, LEDStrip... LEDStrips) { + for (LEDStrip LEDStrip : LEDStrips) action.accept(LEDStrip); } - public static boolean isAddressableLED(LEDStrip ledStrip) { - return ledStrip instanceof AddressableLEDStrip || RobotHardwareStats.isSimulation(); + public static boolean isAddressableLEDStrip(LEDStrip LEDStrip) { + return LEDStrip instanceof AddressableLEDStrip || RobotHardwareStats.isSimulation(); } } \ No newline at end of file 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 9d5de762..ff739905 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -44,7 +44,7 @@ public int getNumberOfLEDS() { * * @param firstColor the first color to blink * @param secondColor the second color to blink - * @param blinkingIntervalSeconds the interval in seconds to blink between the two colors + * @param blinkingIntervalSeconds the interval in seconds to blink between color changes */ abstract void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds); @@ -53,9 +53,9 @@ public int getNumberOfLEDS() { * * @param color the color of the breathing LEDs * @param breathingLEDs the amount of breathing LEDs - * @param cycleTimeSeconds the time it takes for a full cycle of the breathing + * @param cycleTimeSeconds the time it takes for a full breathing cycle * @param inverted whether the breathing should be inverted - * @param bounceMode the bounce mode of the breathing + * @param bounceMode when the breathing LEDs should bounce back to the start of the strip */ abstract void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode); @@ -64,7 +64,7 @@ public int getNumberOfLEDS() { * * @param color the color to flow through the LED strip * @param cycleTimeSeconds the time it takes for the color to flow through the LED strip - * @param inverted whether the color should be inverted + * @param inverted whether the color flow should be inverted */ abstract void colorFlow(Color color, double cycleTimeSeconds, boolean inverted); @@ -80,15 +80,15 @@ public int getNumberOfLEDS() { /** * Colors the LED strip in sections. * - * @param colors an array of the colors to color the sections with. The length of the array is 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. */ abstract void sectionColor(Supplier[] colors); /** * Displays a rainbow pattern on the LED strip. * - * @param brightness the brightness of the rainbow, must be a number between 0 and 1 - * @param speed the speed of the rainbow + * @param brightness the brightness of the rainbow on a scale from 0 to 1 + * @param speed the speed of the rainbow's movement */ abstract void rainbow(double brightness, double speed); From ac7ac557ca42a896f22cd0ec8b72eb86ef63ac73 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 13 Nov 2024 22:37:16 +0200 Subject: [PATCH 19/31] reverted Command stuff, will figure out a solution tommorow --- .../misc/leds/AddressableLEDStrip.java | 14 +- .../hardware/misc/leds/CANdleLEDStrip.java | 31 ++-- .../hardware/misc/leds/LEDCommands.java | 143 ++++++------------ .../trigon/hardware/misc/leds/LEDStrip.java | 8 +- 4 files changed, 61 insertions(+), 135 deletions(-) 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 587701a8..396ecb48 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -113,19 +113,9 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean inverted) { } @Override - void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) { - double currentTime = Timer.getFPGATimestamp(); - if (currentTime - lastLEDAnimationChangeTime > intervalSeconds) { - isLEDAnimationChanged = !isLEDAnimationChanged; - lastLEDAnimationChangeTime = currentTime; - } - if (isLEDAnimationChanged) { - for (int i = 0; i < numberOfLEDs; i++) - LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? firstColor : secondColor); - return; - } + void alternateColor(Color firstColor, Color secondColor) { for (int i = 0; i < numberOfLEDs; i++) - LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? secondColor : firstColor); + LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? firstColor : secondColor); } @Override 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 a42d6e5f..6912679a 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -3,7 +3,6 @@ import com.ctre.phoenix.led.*; import edu.wpi.first.wpilibj.AddressableLED; import edu.wpi.first.wpilibj.AddressableLEDBuffer; -import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj.util.Color; import org.trigon.hardware.RobotHardwareStats; @@ -14,8 +13,6 @@ public class CANdleLEDStrip extends LEDStrip { private static int LAST_CREATED_LED_STRIP_ANIMATION_SLOT = 0; private final int animationSlot; private final AddressableLEDStrip simulationLEDStrip; - private boolean areAlternateColorLEDsAlternated = true; - private double lastAlternateColorTime = 0; /** * Sets the CANdle instance to be used for controlling the LED strips. Must be set before using any LED strips. @@ -119,26 +116,20 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean in } @Override - void alternateColor(Color firstColor, Color secondColor, double intervalSeconds) { + void alternateColor(Color firstColor, Color secondColor) { if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.alternateColor(firstColor, secondColor, intervalSeconds); + simulationLEDStrip.alternateColor(firstColor, secondColor); return; } - if (Timer.getFPGATimestamp() - lastAlternateColorTime > intervalSeconds) { - areAlternateColorLEDsAlternated = !areAlternateColorLEDsAlternated; - lastAlternateColorTime = Timer.getFPGATimestamp(); - } - if (areAlternateColorLEDsAlternated) { - for (int i = 0; i < numberOfLEDs; i++) - CANDLE.setLEDs( - (int) (i % 2 == 0 ? firstColor.red : secondColor.red), - (int) (i % 2 == 0 ? firstColor.green : secondColor.green), - (int) (i % 2 == 0 ? firstColor.blue : secondColor.blue), - 0, - i + indexOffset, - 1 - ); - } + for (int i = 0; i < numberOfLEDs; i++) + CANDLE.setLEDs( + (int) (i % 2 == 0 ? firstColor.red : secondColor.red), + (int) (i % 2 == 0 ? firstColor.green : secondColor.green), + (int) (i % 2 == 0 ? firstColor.blue : secondColor.blue), + 0, + i + indexOffset, + 1 + ); } @Override 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 0569a118..538f3d3c 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -3,130 +3,79 @@ 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.StartEndCommand; -import org.trigon.commands.ExecuteEndCommand; -import org.trigon.hardware.RobotHardwareStats; +import edu.wpi.first.wpilibj2.command.FunctionalCommand; import java.util.function.Consumer; import java.util.function.Supplier; public class LEDCommands { public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) { - if (isAddressableLEDStrip(LEDStrips[0])) - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), LEDStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips - ).ignoringDisable(true); - return new StartEndCommand( - () -> { - runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), LEDStrips); - }, - () -> { - } + return new FunctionalCommand( + () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + () -> runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), LEDStrips), + (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + () -> false, + LEDStrips ).ignoringDisable(true); } public static Command getBlinkingCommand(Color firstColor, Color secondColor, double blinkingIntervalSeconds, LEDStrip... LEDStrips) { - if (isAddressableLEDStrip(LEDStrips[0])) - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips - ).ignoringDisable(true); - return new StartEndCommand( - () -> { - runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips); - }, - () -> { - } + return new FunctionalCommand( + () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + () -> runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips), + (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + () -> false, + LEDStrips ).ignoringDisable(true); } public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { - if (isAddressableLEDStrip(LEDStrips[0])) - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips - ).ignoringDisable(true); - return new StartEndCommand( - () -> { - runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); - }, - () -> { - } + return new FunctionalCommand( + () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + () -> runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips), + (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + () -> false, + LEDStrips ).ignoringDisable(true); } public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean inverted, LEDStrip... LEDStrips) { - if (isAddressableLEDStrip(LEDStrips[0])) - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips - ).ignoringDisable(true); - return new StartEndCommand( - () -> { - runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); - }, - () -> { - } + return new FunctionalCommand( + () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + () -> runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips), + (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + () -> false, + LEDStrips ).ignoringDisable(true); } - public static Command getAlternateColorCommand(Color firstColor, Color secondColor, double intervalSeconds, LEDStrip... LEDStrips) { - if (isAddressableLEDStrip(LEDStrips[0])) - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds)), LEDStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips - ).ignoringDisable(true); - return new StartEndCommand( - () -> { - runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor, intervalSeconds)), LEDStrips); - }, - () -> { - } + public static Command getAlternateColorCommand(Color firstColor, Color secondColor, LEDStrip... LEDStrips) { + return new FunctionalCommand( + () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + () -> runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor)), LEDStrips), + (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + () -> false, + LEDStrips ).ignoringDisable(true); } public static Command getSectionColorCommand(Supplier[] colors, LEDStrip... LEDStrips) { - if (isAddressableLEDStrip(LEDStrips[0])) - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), LEDStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips - ).ignoringDisable(true); - return new StartEndCommand( - () -> { - runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), LEDStrips); - }, - () -> { - } + return new FunctionalCommand( + () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + () -> runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), LEDStrips), + (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + () -> false, + LEDStrips ).ignoringDisable(true); } public static Command getRainbowCommand(double brightness, double speed, LEDStrip... LEDStrips) { - if (isAddressableLEDStrip(LEDStrips[0])) - return new ExecuteEndCommand( - () -> runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), LEDStrips), - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips - ).ignoringDisable(true); - return new StartEndCommand( - () -> { - runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), LEDStrips); - }, - () -> { - } + return new FunctionalCommand( + () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + () -> runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), LEDStrips), + (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), + () -> false, + LEDStrips ).ignoringDisable(true); } @@ -134,8 +83,4 @@ private static void runForLEDs(Consumer action, LEDStrip... LEDStrips) for (LEDStrip LEDStrip : LEDStrips) action.accept(LEDStrip); } - - public static boolean isAddressableLEDStrip(LEDStrip LEDStrip) { - return LEDStrip instanceof AddressableLEDStrip || RobotHardwareStats.isSimulation(); - } } \ No newline at end of file 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 ff739905..ee12255f 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -71,11 +71,11 @@ public int getNumberOfLEDS() { /** * Displays two colors in an alternating pattern on the LED strip. * - * @param firstColor the first color - * @param secondColor the second color - * @param intervalSeconds the interval in seconds to alternate colors + * @param firstColor the first color + * @param secondColor the second color + * = */ - abstract void alternateColor(Color firstColor, Color secondColor, double intervalSeconds); + abstract void alternateColor(Color firstColor, Color secondColor); /** * Colors the LED strip in sections. From 77f1f147428df068344626c239d8b7298222daf4 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 13 Nov 2024 22:56:08 +0200 Subject: [PATCH 20/31] finally fixed critical error!!!!!!!!!!!!!!!!!!!!!!!! :8ball: --- .../misc/leds/AddressableLEDStrip.java | 2 ++ .../hardware/misc/leds/CANdleLEDStrip.java | 6 ++++ .../hardware/misc/leds/LEDCommands.java | 35 +++++++++++++++---- .../trigon/hardware/misc/leds/LEDStrip.java | 6 ++++ 4 files changed, 42 insertions(+), 7 deletions(-) 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 396ecb48..d09a6c7e 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -52,6 +52,7 @@ public AddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) @Override public void periodic() { + currentAnimation.run(); LED.setData(LED_BUFFER); } @@ -75,6 +76,7 @@ void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) @Override void staticColor(Color color) { + currentAnimation = () -> staticColor(color); setLEDColors(color, 0, numberOfLEDs - 1); } 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 6912679a..62657434 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -55,6 +55,12 @@ public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { this.simulationLEDStrip = new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); } + @Override + public void periodic() { + if (RobotHardwareStats.isSimulation()) + simulationLEDStrip.periodic(); + } + @Override void clearLEDColors() { if (RobotHardwareStats.isSimulation()) { 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 538f3d3c..035600c6 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -12,7 +12,10 @@ public class LEDCommands { public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), - () -> runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), LEDStrips), + () -> { + runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.staticColor(color)), LEDStrips); + }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, LEDStrips @@ -22,7 +25,10 @@ public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) public static Command getBlinkingCommand(Color firstColor, Color secondColor, double blinkingIntervalSeconds, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), - () -> runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips), + () -> { + runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips); + }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, LEDStrips @@ -32,7 +38,10 @@ public static Command getBlinkingCommand(Color firstColor, Color secondColor, do public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), - () -> runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips), + () -> { + runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); + }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, LEDStrips @@ -42,7 +51,10 @@ public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean inverted, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), - () -> runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips), + () -> { + runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); + }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, LEDStrips @@ -52,7 +64,10 @@ public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, public static Command getAlternateColorCommand(Color firstColor, Color secondColor, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), - () -> runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor)), LEDStrips), + () -> { + runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.alternateColor(firstColor, secondColor)), LEDStrips); + }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, LEDStrips @@ -62,7 +77,10 @@ public static Command getAlternateColorCommand(Color firstColor, Color secondCol public static Command getSectionColorCommand(Supplier[] colors, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), - () -> runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), LEDStrips), + () -> { + runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.sectionColor(colors)), LEDStrips); + }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, LEDStrips @@ -72,7 +90,10 @@ public static Command getSectionColorCommand(Supplier[] colors, LEDStrip. public static Command getRainbowCommand(double brightness, double speed, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), - () -> runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), LEDStrips), + () -> { + runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.rainbow(brightness, speed)), LEDStrips); + }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, LEDStrips 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 ee12255f..ea4f705d 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -12,6 +12,8 @@ public abstract class LEDStrip extends SubsystemBase { final int indexOffset; final boolean inverted; final int numberOfLEDs; + Runnable currentAnimation = () -> { + }; public LEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { this.inverted = inverted; @@ -30,6 +32,10 @@ public int getNumberOfLEDS() { return numberOfLEDs; } + void setCurrentAnimation(Runnable currentAnimation) { + this.currentAnimation = currentAnimation; + } + abstract void resetLEDSettings(); /** From d78a89be96c87c288aaf357bac8b6f7e43627563 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:16:44 +0200 Subject: [PATCH 21/31] fixed CANdle LED sim bug and added more jdocs --- .../hardware/misc/leds/AddressableLEDStrip.java | 3 +++ .../hardware/misc/leds/CANdleLEDStrip.java | 16 +++++++++++++--- .../trigon/hardware/misc/leds/LEDCommands.java | 3 +++ .../org/trigon/hardware/misc/leds/LEDStrip.java | 3 +++ 4 files changed, 22 insertions(+), 3 deletions(-) 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 d09a6c7e..71893f30 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -8,6 +8,9 @@ import java.util.function.Supplier; +/** + * A LED strip that is controlled by an AddressableLED. + */ public class AddressableLEDStrip extends LEDStrip { private static AddressableLED LED; private static AddressableLEDBuffer LED_BUFFER; 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 62657434..b25d5eba 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -8,6 +8,9 @@ import java.util.function.Supplier; +/** + * A LED strip that is controlled by a CANdle, and uses AddressableLED for simulation. + */ public class CANdleLEDStrip extends LEDStrip { private static CANdle CANDLE; private static int LAST_CREATED_LED_STRIP_ANIMATION_SLOT = 0; @@ -74,6 +77,7 @@ void clearLEDColors() { void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { if (RobotHardwareStats.isSimulation()) { simulationLEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)); return; } CANDLE.animate( @@ -94,15 +98,17 @@ void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) void staticColor(Color color) { if (RobotHardwareStats.isSimulation()) { simulationLEDStrip.staticColor(color); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.staticColor(color)); return; } CANDLE.setLEDs((int) color.red, (int) color.green, (int) color.blue, 0, indexOffset, numberOfLEDs); } @Override - void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + void breathe(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode) { if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.breathe(color, breathingLEDs, cycleTimeSeconds, inverted, bounceMode); + simulationLEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)); return; } CANDLE.animate( @@ -114,7 +120,7 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean in cycleTimeSeconds, this.numberOfLEDs, bounceMode, - breathingLEDs, + amountOfBreathingLEDs, indexOffset ), animationSlot @@ -125,6 +131,7 @@ void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean in void alternateColor(Color firstColor, Color secondColor) { if (RobotHardwareStats.isSimulation()) { simulationLEDStrip.alternateColor(firstColor, secondColor); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.alternateColor(firstColor, secondColor)); return; } for (int i = 0; i < numberOfLEDs; i++) @@ -142,6 +149,7 @@ void alternateColor(Color firstColor, Color secondColor) { void colorFlow(Color color, double cycleTimeSeconds, boolean inverted) { if (RobotHardwareStats.isSimulation()) { simulationLEDStrip.colorFlow(color, cycleTimeSeconds, inverted); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.colorFlow(color, cycleTimeSeconds, inverted)); return; } inverted = this.inverted != inverted; @@ -164,6 +172,7 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean inverted) { void rainbow(double brightness, double speed) { if (RobotHardwareStats.isSimulation()) { simulationLEDStrip.rainbow(brightness, speed); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.rainbow(brightness, speed)); return; } CANDLE.animate( @@ -182,6 +191,7 @@ void rainbow(double brightness, double speed) { void sectionColor(Supplier[] colors) { if (RobotHardwareStats.isSimulation()) { simulationLEDStrip.sectionColor(colors); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.sectionColor(colors)); return; } final int LEDSPerSection = (int) Math.floor(numberOfLEDs / colors.length); 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 035600c6..9781b285 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -8,6 +8,9 @@ import java.util.function.Consumer; import java.util.function.Supplier; +/** + * A class with static functions for getting LED commands. These commands work with both types of LEDStrips. + */ public class LEDCommands { public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) { return new FunctionalCommand( 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 ea4f705d..aa84a43c 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -7,6 +7,9 @@ import java.util.function.Supplier; +/** + * A wrapper class for LED strips. This class provides a set of methods for controlling LED strips. + */ public abstract class LEDStrip extends SubsystemBase { public static LEDStrip[] LED_STRIPS = new LEDStrip[0]; final int indexOffset; From 6e7065a9566205927801affc10d0126182ef8487 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Thu, 14 Nov 2024 21:45:32 +0200 Subject: [PATCH 22/31] jDoc changes, and replace intervalSeconds with speed. (CANdle animations are so annoying! --- .../misc/leds/AddressableLEDStrip.java | 37 ++++++++++--------- .../hardware/misc/leds/CANdleLEDStrip.java | 32 ++++++++-------- .../hardware/misc/leds/LEDCommands.java | 20 +++++----- .../trigon/hardware/misc/leds/LEDStrip.java | 34 ++++++++--------- 4 files changed, 61 insertions(+), 62 deletions(-) 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 71893f30..1ab96eae 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -26,7 +26,7 @@ public class AddressableLEDStrip extends LEDStrip { * * @param led the LED instance to be used */ - public static void setLED(AddressableLED led) { + public static void setAddressableLED(AddressableLED led) { LED = led; LED.start(); } @@ -37,7 +37,7 @@ public static void setLED(AddressableLED led) { * * @param ledBuffer the LED buffer instance to be used */ - public static void setLEDBuffer(AddressableLEDBuffer ledBuffer) { + public static void setAddressableLEDBuffer(AddressableLEDBuffer ledBuffer) { LED_BUFFER = ledBuffer; } @@ -65,46 +65,47 @@ void clearLEDColors() { } @Override - void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { + void blink(Color firstColor, double speed) { + double correctedSpeed = 1 - speed; double currentTime = Timer.getFPGATimestamp(); - if (currentTime - lastLEDAnimationChangeTime > blinkingIntervalSeconds) { + if (currentTime - lastLEDAnimationChangeTime > correctedSpeed) { lastLEDAnimationChangeTime = currentTime; isLEDAnimationChanged = !isLEDAnimationChanged; } - if (isLEDAnimationChanged) + if (isLEDAnimationChanged) { staticColor(firstColor); - else - staticColor(secondColor); + return; + } + clearLEDColors(); } @Override void staticColor(Color color) { - currentAnimation = () -> staticColor(color); setLEDColors(color, 0, numberOfLEDs - 1); } @Override - void breathe(Color color, int breathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode) { - inverted = this.inverted != inverted; + void breathe(Color color, int breathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + boolean correctedInverted = this.inverted != inverted; clearLEDColors(); - double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; + double moveLEDTimeSeconds = 1 - speed; double currentTime = Timer.getFPGATimestamp(); if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { lastLEDAnimationChangeTime = currentTime; - if (inverted) + if (correctedInverted) lastBreatheLED--; else lastBreatheLED++; } - checkIfBreathingHasHitEnd(breathingLEDs, inverted, bounceMode); + checkIfBreathingHasHitEnd(breathingLEDs, correctedInverted, bounceMode); setBreathingLEDs(color, breathingLEDs, bounceMode); } @Override - void colorFlow(Color color, double cycleTimeSeconds, boolean inverted) { + void colorFlow(Color color, double speed, boolean inverted) { clearLEDColors(); - inverted = this.inverted != inverted; - double moveLEDTimeSeconds = cycleTimeSeconds / numberOfLEDs; + boolean correctedInverted = this.inverted != inverted; + double moveLEDTimeSeconds = 1 - speed; double currentTime = Timer.getFPGATimestamp(); if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { lastLEDAnimationChangeTime = currentTime; @@ -114,7 +115,7 @@ void colorFlow(Color color, double cycleTimeSeconds, boolean inverted) { amountOfColorFlowLEDs++; } checkIfColorFlowHasHitEnd(); - setLEDColors(color, inverted ? numberOfLEDs - amountOfColorFlowLEDs - 1 : 0, inverted ? numberOfLEDs - 1 : amountOfColorFlowLEDs); + setLEDColors(color, correctedInverted ? numberOfLEDs - amountOfColorFlowLEDs - 1 : 0, correctedInverted ? numberOfLEDs - 1 : amountOfColorFlowLEDs); } @Override @@ -126,7 +127,7 @@ void alternateColor(Color firstColor, Color secondColor) { @Override void rainbow(double brightness, double speed) { int adjustedBrightness = (int) (brightness * 255); - int hueIncrement = (int) (speed * 3); + int hueIncrement = (int) (speed * 8); for (int led = 0; led < numberOfLEDs; led++) { final int hue = (int) (rainbowFirstPixelHue + (led * 180 / numberOfLEDs) % 180); 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 b25d5eba..1704bdc5 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -32,7 +32,7 @@ public static void setCandle(CANdle candle) { * @param simulationLEDStrip the AddressableLED instance to be used in simulation */ public static void setSimulationLED(AddressableLED simulationLEDStrip) { - AddressableLEDStrip.setLED(simulationLEDStrip); + AddressableLEDStrip.setAddressableLED(simulationLEDStrip); } /** @@ -41,7 +41,7 @@ public static void setSimulationLED(AddressableLED simulationLEDStrip) { * @param simulationLEDBuffer the AddressableLED buffer instance to be used in simulation */ public static void setSimulationLEDBuffer(AddressableLEDBuffer simulationLEDBuffer) { - AddressableLEDStrip.setLEDBuffer(simulationLEDBuffer); + AddressableLEDStrip.setAddressableLEDBuffer(simulationLEDBuffer); } /** @@ -74,10 +74,10 @@ void clearLEDColors() { } @Override - void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) { + void blink(Color firstColor, double speed) { if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)); + simulationLEDStrip.blink(firstColor, speed); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.blink(firstColor, speed)); return; } CANDLE.animate( @@ -86,7 +86,7 @@ void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds) (int) firstColor.green, (int) firstColor.blue, 0, - blinkingIntervalSeconds, + speed, this.numberOfLEDs, indexOffset ), @@ -105,10 +105,10 @@ void staticColor(Color color) { } @Override - void breathe(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode) { + void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)); + simulationLEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode)); return; } CANDLE.animate( @@ -117,7 +117,7 @@ void breathe(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, bo (int) color.green, (int) color.blue, 0, - cycleTimeSeconds, + speed, this.numberOfLEDs, bounceMode, amountOfBreathingLEDs, @@ -146,22 +146,22 @@ void alternateColor(Color firstColor, Color secondColor) { } @Override - void colorFlow(Color color, double cycleTimeSeconds, boolean inverted) { + void colorFlow(Color color, double speed, boolean inverted) { if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.colorFlow(color, cycleTimeSeconds, inverted); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.colorFlow(color, cycleTimeSeconds, inverted)); + simulationLEDStrip.colorFlow(color, speed, inverted); + simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.colorFlow(color, speed, inverted)); return; } - inverted = this.inverted != inverted; + boolean correctedInverted = this.inverted != inverted; CANDLE.animate( new ColorFlowAnimation( (int) color.red, (int) color.green, (int) color.blue, 0, - cycleTimeSeconds, + speed, this.numberOfLEDs, - inverted ? ColorFlowAnimation.Direction.Backward : ColorFlowAnimation.Direction.Forward, + correctedInverted ? ColorFlowAnimation.Direction.Backward : ColorFlowAnimation.Direction.Forward, indexOffset ), animationSlot 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 9781b285..66e84687 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -9,7 +9,7 @@ import java.util.function.Supplier; /** - * A class with static functions for getting LED commands. These commands work with both types of LEDStrips. + * A class that contains static functions for getting LED commands. These commands work with both types of LEDStrips. */ public class LEDCommands { public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) { @@ -25,12 +25,12 @@ public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) ).ignoringDisable(true); } - public static Command getBlinkingCommand(Color firstColor, Color secondColor, double blinkingIntervalSeconds, LEDStrip... LEDStrips) { + public static Command getBlinkingCommand(Color firstColor, double speed, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), () -> { - runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.blink(firstColor, secondColor, blinkingIntervalSeconds)), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, speed)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.blink(firstColor, speed)), LEDStrips); }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, @@ -38,12 +38,12 @@ public static Command getBlinkingCommand(Color firstColor, Color secondColor, do ).ignoringDisable(true); } - public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { + public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), () -> { - runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode)), LEDStrips); }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, @@ -51,12 +51,12 @@ public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, ).ignoringDisable(true); } - public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean inverted, LEDStrip... LEDStrips) { + public static Command getColorFlowCommand(Color color, double speed, boolean inverted, LEDStrip... LEDStrips) { return new FunctionalCommand( () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), () -> { - runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, speed, inverted)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, speed, inverted)), LEDStrips); }, (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), () -> false, 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 aa84a43c..d77fb91e 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -51,45 +51,43 @@ void setCurrentAnimation(Runnable currentAnimation) { /** * Blinks the LED strip between two colors. * - * @param firstColor the first color to blink - * @param secondColor the second color to blink - * @param blinkingIntervalSeconds the interval in seconds to blink between color changes + * @param firstColor the first color to blink + * @param speed how fast the LED strip should blink on a scale between 0 and 1 */ - abstract void blink(Color firstColor, Color secondColor, double blinkingIntervalSeconds); + abstract void blink(Color firstColor, double speed); /** - * "Breathes" a bunch of LEDs with a given 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 cycleTimeSeconds the time it takes for a full breathing cycle - * @param inverted whether the breathing should be inverted - * @param bounceMode when the breathing LEDs should bounce back to the start of the strip + * @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 cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode); + abstract void breathe(Color color, int breathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode); /** * Flows a color through the LED strip. * - * @param color the color to flow through the LED strip - * @param cycleTimeSeconds the time it takes for the color to flow through the LED strip - * @param inverted whether the color flow should be inverted + * @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 cycleTimeSeconds, boolean inverted); + abstract void colorFlow(Color color, double speed, boolean inverted); /** * 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); /** * Colors the LED strip in sections. * - * @param colors an array of the colors to color the sections with. 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 */ abstract void sectionColor(Supplier[] colors); @@ -97,7 +95,7 @@ void setCurrentAnimation(Runnable currentAnimation) { * 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 + * @param speed the speed of the rainbow's movement on a scale from 0 to 1 */ abstract void rainbow(double brightness, double speed); From ae3d972577ee07e1172b663f8b27c2c60b0f8743 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:03:11 +0200 Subject: [PATCH 23/31] Forgot to do this earlier --- .../hardware/misc/leds/LEDCommands.java | 69 +++++++++---------- 1 file changed, 31 insertions(+), 38 deletions(-) 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 66e84687..0614b185 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -3,7 +3,7 @@ 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.FunctionalCommand; +import edu.wpi.first.wpilibj2.command.StartEndCommand; import java.util.function.Consumer; import java.util.function.Supplier; @@ -13,92 +13,85 @@ */ public class LEDCommands { public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) { - return new FunctionalCommand( - () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + return new StartEndCommand( () -> { + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.staticColor(color)), LEDStrips); }, - (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - () -> false, + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips ).ignoringDisable(true); } - public static Command getBlinkingCommand(Color firstColor, double speed, LEDStrip... LEDStrips) { - return new FunctionalCommand( - () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + public static Command getBlinkingCommand(Color firstColor, double blinkingIntervalSeconds, LEDStrip... LEDStrips) { + return new StartEndCommand( () -> { - runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, speed)), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.blink(firstColor, speed)), LEDStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, blinkingIntervalSeconds)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.blink(firstColor, blinkingIntervalSeconds)), LEDStrips); }, - (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - () -> false, + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips ).ignoringDisable(true); } - public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { - return new FunctionalCommand( - () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { + return new StartEndCommand( () -> { - runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode)), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode)), LEDStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); }, - (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - () -> false, + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips ).ignoringDisable(true); } - public static Command getColorFlowCommand(Color color, double speed, boolean inverted, LEDStrip... LEDStrips) { - return new FunctionalCommand( - () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean inverted, LEDStrip... LEDStrips) { + return new StartEndCommand( () -> { - runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, speed, inverted)), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, speed, inverted)), LEDStrips); + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); + runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); }, - (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - () -> false, + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips ).ignoringDisable(true); } public static Command getAlternateColorCommand(Color firstColor, Color secondColor, LEDStrip... LEDStrips) { - return new FunctionalCommand( - () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + return new StartEndCommand( () -> { + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.alternateColor(firstColor, secondColor)), LEDStrips); }, - (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - () -> false, + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips ).ignoringDisable(true); } public static Command getSectionColorCommand(Supplier[] colors, LEDStrip... LEDStrips) { - return new FunctionalCommand( - () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + return new StartEndCommand( () -> { + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.sectionColor(colors)), LEDStrips); }, - (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - () -> false, + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips ).ignoringDisable(true); } public static Command getRainbowCommand(double brightness, double speed, LEDStrip... LEDStrips) { - return new FunctionalCommand( - () -> runForLEDs((LEDStrip::clearLEDColors), LEDStrips), + return new StartEndCommand( () -> { + runForLEDs((LEDStrip::clearLEDColors), LEDStrips); runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.rainbow(brightness, speed)), LEDStrips); }, - (interrupted) -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - () -> false, + () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips ).ignoringDisable(true); } From b2dce8e55814167be3614314979712ac236d509d Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Mon, 18 Nov 2024 10:22:58 +0200 Subject: [PATCH 24/31] cleaned a bit, optimized, and stuff --- .../misc/leds/AddressableLEDStrip.java | 14 +++-- .../hardware/misc/leds/CANdleLEDStrip.java | 62 +++++++++---------- .../hardware/misc/leds/LEDCommands.java | 7 --- .../trigon/hardware/misc/leds/LEDStrip.java | 1 + 4 files changed, 38 insertions(+), 46 deletions(-) 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 1ab96eae..3d454493 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -14,6 +14,7 @@ public class AddressableLEDStrip extends LEDStrip { private static AddressableLED LED; private static AddressableLEDBuffer LED_BUFFER; + private int lastBreatheLED; private double lastLEDAnimationChangeTime = 0; private double rainbowFirstPixelHue = 0; @@ -22,13 +23,15 @@ public class AddressableLEDStrip extends LEDStrip { /** * Sets the AddressableLED instance to be used for controlling the LED strip. Must be set before using any LED strips. - * The LED instance be configured before being set, however it does not need to be started. + * The LED instance should be configured before being set, however it does not need to be started. * * @param led the LED instance to be used */ public static void setAddressableLED(AddressableLED led) { - LED = led; - LED.start(); + if (LED == null) { + LED = led; + LED.start(); + } } /** @@ -38,11 +41,12 @@ public static void setAddressableLED(AddressableLED led) { * @param ledBuffer the LED buffer instance to be used */ public static void setAddressableLEDBuffer(AddressableLEDBuffer ledBuffer) { - LED_BUFFER = ledBuffer; + if (LED_BUFFER == null) + LED_BUFFER = ledBuffer; } /** - * Constructs a new AddressableLEDStrip. Before any commands are sent to the LED strip, the setLED method must be called. + * Constructs a new AddressableLEDStrip. Before any commands are sent to the LED strip, the setAddressableLED and setAddressableLEDBuffer methods must be called. * * @param inverted whether the LED strip is inverted * @param numberOfLEDs the amount of LEDs in the strip 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 1704bdc5..f3aa75e4 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -22,8 +22,9 @@ public class CANdleLEDStrip extends LEDStrip { * * @param candle the CANdle instance to be used */ - public static void setCandle(CANdle candle) { - CANDLE = candle; + public static void setCANdle(CANdle candle) { + if (CANDLE == null) + CANDLE = candle; } /** @@ -45,7 +46,7 @@ public static void setSimulationLEDBuffer(AddressableLEDBuffer simulationLEDBuff } /** - * Constructs a new CANdleLEDStrip. Before any commands are sent to the LED strip, the setLED method must be called. + * Constructs a new CANdleLEDStrip. Before any commands are sent to the LED strip, the setCANdle method must be called. * * @param inverted whether the LED strip is inverted * @param numberOfLEDs the amount of LEDs in the strip @@ -59,9 +60,8 @@ public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { } @Override - public void periodic() { - if (RobotHardwareStats.isSimulation()) - simulationLEDStrip.periodic(); + public void simulationPeriodic() { + simulationLEDStrip.periodic(); } @Override @@ -75,11 +75,9 @@ void clearLEDColors() { @Override void blink(Color firstColor, double speed) { - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.blink(firstColor, speed); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.blink(firstColor, speed)); + if (isSimulation(() -> simulationLEDStrip.blink(firstColor, speed))) return; - } + CANDLE.animate( new SingleFadeAnimation( (int) firstColor.red, @@ -96,21 +94,17 @@ void blink(Color firstColor, double speed) { @Override void staticColor(Color color) { - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.staticColor(color); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.staticColor(color)); + if (isSimulation(() -> simulationLEDStrip.staticColor(color))) return; - } + CANDLE.setLEDs((int) color.red, (int) color.green, (int) color.blue, 0, indexOffset, numberOfLEDs); } @Override void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode)); + if (isSimulation(() -> simulationLEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode))) return; - } + CANDLE.animate( new LarsonAnimation( (int) color.red, @@ -129,11 +123,9 @@ void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inver @Override void alternateColor(Color firstColor, Color secondColor) { - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.alternateColor(firstColor, secondColor); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.alternateColor(firstColor, secondColor)); + if (isSimulation(() -> simulationLEDStrip.alternateColor(firstColor, secondColor))) return; - } + for (int i = 0; i < numberOfLEDs; i++) CANDLE.setLEDs( (int) (i % 2 == 0 ? firstColor.red : secondColor.red), @@ -147,11 +139,9 @@ void alternateColor(Color firstColor, Color secondColor) { @Override void colorFlow(Color color, double speed, boolean inverted) { - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.colorFlow(color, speed, inverted); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.colorFlow(color, speed, inverted)); + if (isSimulation(() -> simulationLEDStrip.colorFlow(color, speed, inverted))) return; - } + boolean correctedInverted = this.inverted != inverted; CANDLE.animate( new ColorFlowAnimation( @@ -170,11 +160,9 @@ void colorFlow(Color color, double speed, boolean inverted) { @Override void rainbow(double brightness, double speed) { - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.rainbow(brightness, speed); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.rainbow(brightness, speed)); + if (isSimulation(() -> simulationLEDStrip.rainbow(brightness, speed))) return; - } + CANDLE.animate( new RainbowAnimation( brightness, @@ -189,11 +177,9 @@ void rainbow(double brightness, double speed) { @Override void sectionColor(Supplier[] colors) { - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.sectionColor(colors); - simulationLEDStrip.setCurrentAnimation(() -> simulationLEDStrip.sectionColor(colors)); + if (isSimulation(() -> simulationLEDStrip.sectionColor(colors))) return; - } + final int LEDSPerSection = (int) Math.floor(numberOfLEDs / colors.length); setSectionColor(colors.length, LEDSPerSection, colors); } @@ -216,4 +202,12 @@ private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier< ); } } + + private boolean isSimulation(Runnable simulationAction) { + if (RobotHardwareStats.isSimulation()) { + setCurrentAnimation(simulationAction); + return true; + } + return false; + } } \ No newline at end of file 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 0614b185..72a1af71 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -16,7 +16,6 @@ public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.staticColor(color)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.staticColor(color)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), @@ -28,7 +27,6 @@ public static Command getBlinkingCommand(Color firstColor, double blinkingInterv return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.blink(firstColor, blinkingIntervalSeconds)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.blink(firstColor, blinkingIntervalSeconds)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), @@ -40,7 +38,6 @@ public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), @@ -52,7 +49,6 @@ public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), @@ -64,7 +60,6 @@ public static Command getAlternateColorCommand(Color firstColor, Color secondCol return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.alternateColor(firstColor, secondColor)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.alternateColor(firstColor, secondColor)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), @@ -76,7 +71,6 @@ public static Command getSectionColorCommand(Supplier[] colors, LEDStrip. return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.sectionColor(colors)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.sectionColor(colors)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), @@ -88,7 +82,6 @@ public static Command getRainbowCommand(double brightness, double speed, LEDStri return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs((LEDStrip -> LEDStrip.rainbow(brightness, speed)), LEDStrips); runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.rainbow(brightness, speed)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), 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 d77fb91e..8b427cb0 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -37,6 +37,7 @@ public int getNumberOfLEDS() { void setCurrentAnimation(Runnable currentAnimation) { this.currentAnimation = currentAnimation; + currentAnimation.run(); } abstract void resetLEDSettings(); From e29f91f64d9deced86425e7d91f6f39f23a26359 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Tue, 19 Nov 2024 12:28:28 +0200 Subject: [PATCH 25/31] fixed method name and cleaned a bit --- .../hardware/misc/leds/CANdleLEDStrip.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) 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 f3aa75e4..3b4c6f4f 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -66,16 +66,15 @@ public void simulationPeriodic() { @Override void clearLEDColors() { - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.clearLEDColors(); + if (runIfInSimulation(simulationLEDStrip::clearLEDColors)) return; - } + CANDLE.clearAnimation(animationSlot); } @Override void blink(Color firstColor, double speed) { - if (isSimulation(() -> simulationLEDStrip.blink(firstColor, speed))) + if (runIfInSimulation(() -> simulationLEDStrip.blink(firstColor, speed))) return; CANDLE.animate( @@ -94,7 +93,7 @@ void blink(Color firstColor, double speed) { @Override void staticColor(Color color) { - if (isSimulation(() -> simulationLEDStrip.staticColor(color))) + if (runIfInSimulation(() -> simulationLEDStrip.staticColor(color))) return; CANDLE.setLEDs((int) color.red, (int) color.green, (int) color.blue, 0, indexOffset, numberOfLEDs); @@ -102,7 +101,7 @@ void staticColor(Color color) { @Override void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { - if (isSimulation(() -> simulationLEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode))) + if (runIfInSimulation(() -> simulationLEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode))) return; CANDLE.animate( @@ -123,7 +122,7 @@ void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inver @Override void alternateColor(Color firstColor, Color secondColor) { - if (isSimulation(() -> simulationLEDStrip.alternateColor(firstColor, secondColor))) + if (runIfInSimulation(() -> simulationLEDStrip.alternateColor(firstColor, secondColor))) return; for (int i = 0; i < numberOfLEDs; i++) @@ -139,7 +138,7 @@ void alternateColor(Color firstColor, Color secondColor) { @Override void colorFlow(Color color, double speed, boolean inverted) { - if (isSimulation(() -> simulationLEDStrip.colorFlow(color, speed, inverted))) + if (runIfInSimulation(() -> simulationLEDStrip.colorFlow(color, speed, inverted))) return; boolean correctedInverted = this.inverted != inverted; @@ -160,7 +159,7 @@ void colorFlow(Color color, double speed, boolean inverted) { @Override void rainbow(double brightness, double speed) { - if (isSimulation(() -> simulationLEDStrip.rainbow(brightness, speed))) + if (runIfInSimulation(() -> simulationLEDStrip.rainbow(brightness, speed))) return; CANDLE.animate( @@ -177,7 +176,7 @@ void rainbow(double brightness, double speed) { @Override void sectionColor(Supplier[] colors) { - if (isSimulation(() -> simulationLEDStrip.sectionColor(colors))) + if (runIfInSimulation(() -> simulationLEDStrip.sectionColor(colors))) return; final int LEDSPerSection = (int) Math.floor(numberOfLEDs / colors.length); @@ -203,7 +202,7 @@ private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier< } } - private boolean isSimulation(Runnable simulationAction) { + private boolean runIfInSimulation(Runnable simulationAction) { if (RobotHardwareStats.isSimulation()) { setCurrentAnimation(simulationAction); return true; From 82a022bbb27fe8351e11d8aff13be6ad48c00f1b Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Sun, 24 Nov 2024 15:07:29 +0200 Subject: [PATCH 26/31] simple is good --- .../hardware/misc/leds/CANdleLEDStrip.java | 33 ++----------------- .../hardware/misc/leds/LEDCommands.java | 12 +++---- 2 files changed, 9 insertions(+), 36 deletions(-) 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 3b4c6f4f..67b1bcdb 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -66,17 +66,16 @@ public void simulationPeriodic() { @Override void clearLEDColors() { - if (runIfInSimulation(simulationLEDStrip::clearLEDColors)) + if (RobotHardwareStats.isSimulation()) { + simulationLEDStrip.clearLEDColors(); return; + } CANDLE.clearAnimation(animationSlot); } @Override void blink(Color firstColor, double speed) { - if (runIfInSimulation(() -> simulationLEDStrip.blink(firstColor, speed))) - return; - CANDLE.animate( new SingleFadeAnimation( (int) firstColor.red, @@ -93,17 +92,11 @@ void blink(Color firstColor, double speed) { @Override void staticColor(Color color) { - if (runIfInSimulation(() -> simulationLEDStrip.staticColor(color))) - return; - CANDLE.setLEDs((int) color.red, (int) color.green, (int) color.blue, 0, indexOffset, numberOfLEDs); } @Override void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { - if (runIfInSimulation(() -> simulationLEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode))) - return; - CANDLE.animate( new LarsonAnimation( (int) color.red, @@ -122,9 +115,6 @@ void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inver @Override void alternateColor(Color firstColor, Color secondColor) { - if (runIfInSimulation(() -> simulationLEDStrip.alternateColor(firstColor, secondColor))) - return; - for (int i = 0; i < numberOfLEDs; i++) CANDLE.setLEDs( (int) (i % 2 == 0 ? firstColor.red : secondColor.red), @@ -138,9 +128,6 @@ void alternateColor(Color firstColor, Color secondColor) { @Override void colorFlow(Color color, double speed, boolean inverted) { - if (runIfInSimulation(() -> simulationLEDStrip.colorFlow(color, speed, inverted))) - return; - boolean correctedInverted = this.inverted != inverted; CANDLE.animate( new ColorFlowAnimation( @@ -159,9 +146,6 @@ void colorFlow(Color color, double speed, boolean inverted) { @Override void rainbow(double brightness, double speed) { - if (runIfInSimulation(() -> simulationLEDStrip.rainbow(brightness, speed))) - return; - CANDLE.animate( new RainbowAnimation( brightness, @@ -176,9 +160,6 @@ void rainbow(double brightness, double speed) { @Override void sectionColor(Supplier[] colors) { - if (runIfInSimulation(() -> simulationLEDStrip.sectionColor(colors))) - return; - final int LEDSPerSection = (int) Math.floor(numberOfLEDs / colors.length); setSectionColor(colors.length, LEDSPerSection, colors); } @@ -201,12 +182,4 @@ private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier< ); } } - - private boolean runIfInSimulation(Runnable simulationAction) { - if (RobotHardwareStats.isSimulation()) { - setCurrentAnimation(simulationAction); - return true; - } - return false; - } } \ No newline at end of file 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 72a1af71..e236e046 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -23,33 +23,33 @@ public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) ).ignoringDisable(true); } - public static Command getBlinkingCommand(Color firstColor, double blinkingIntervalSeconds, LEDStrip... LEDStrips) { + public static Command getBlinkingCommand(Color firstColor, double speed, LEDStrip... LEDStrips) { return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.blink(firstColor, blinkingIntervalSeconds)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.blink(firstColor, speed)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips ).ignoringDisable(true); } - public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double cycleTimeSeconds, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { + public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.breathe(color, amountOfBreathingLEDs, cycleTimeSeconds, inverted, bounceMode)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.breathe(color, amountOfBreathingLEDs, speed, inverted, bounceMode)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips ).ignoringDisable(true); } - public static Command getColorFlowCommand(Color color, double cycleTimeSeconds, boolean inverted, LEDStrip... LEDStrips) { + public static Command getColorFlowCommand(Color color, double speed, boolean inverted, LEDStrip... LEDStrips) { return new StartEndCommand( () -> { runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, cycleTimeSeconds, inverted)), LEDStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, speed, inverted)), LEDStrips); }, () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), LEDStrips From 4ee8a80a8e2c6e507e0d37c3116df9f79f4e6e96 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Mon, 25 Nov 2024 12:51:27 +0200 Subject: [PATCH 27/31] minor optimization thing --- .../trigon/hardware/misc/leds/AddressableLEDStrip.java | 10 ++++++++++ .../org/trigon/hardware/misc/leds/CANdleLEDStrip.java | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) 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 3d454493..f4d524da 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -5,6 +5,7 @@ import edu.wpi.first.wpilibj.AddressableLEDBuffer; import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj.util.Color; +import org.trigon.hardware.RobotHardwareStats; import java.util.function.Supplier; @@ -15,6 +16,7 @@ public class AddressableLEDStrip extends LEDStrip { private static AddressableLED LED; private static AddressableLEDBuffer LED_BUFFER; + private final boolean shouldOnlyRunInSimulation; private int lastBreatheLED; private double lastLEDAnimationChangeTime = 0; private double rainbowFirstPixelHue = 0; @@ -53,12 +55,20 @@ public static void setAddressableLEDBuffer(AddressableLEDBuffer ledBuffer) { * @param indexOffset the offset of the first LED in the strip */ public AddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + this(inverted, numberOfLEDs, indexOffset, false); + } + + AddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset, boolean shouldOnlyRunInSimulation) { super(inverted, numberOfLEDs, indexOffset); + this.shouldOnlyRunInSimulation = shouldOnlyRunInSimulation; resetLEDSettings(); } @Override public void periodic() { + if (shouldOnlyRunInSimulation && !RobotHardwareStats.isSimulation()) + return; + currentAnimation.run(); LED.setData(LED_BUFFER); } 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 67b1bcdb..79a57bd7 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -56,7 +56,7 @@ public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { super(inverted, numberOfLEDs, indexOffset); animationSlot = LAST_CREATED_LED_STRIP_ANIMATION_SLOT; LAST_CREATED_LED_STRIP_ANIMATION_SLOT++; - this.simulationLEDStrip = new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); + this.simulationLEDStrip = new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset, true); } @Override From e3c9f6d2e90fb71b3697594da8cb5e0d9ec32f58 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Mon, 25 Nov 2024 18:59:02 +0200 Subject: [PATCH 28/31] cleaning. jdocs and rainbow inverted --- .../misc/leds/AddressableLEDStrip.java | 36 ++--- .../hardware/misc/leds/CANdleLEDStrip.java | 14 +- .../hardware/misc/leds/LEDCommands.java | 133 +++++++++++++----- .../trigon/hardware/misc/leds/LEDStrip.java | 7 +- 4 files changed, 126 insertions(+), 64 deletions(-) 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 f4d524da..8fa79f36 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -24,7 +24,7 @@ public class AddressableLEDStrip extends LEDStrip { private int amountOfColorFlowLEDs = 0; /** - * Sets the AddressableLED instance to be used for controlling the LED strip. Must be set before using any LED strips. + * Sets the AddressableLED instance to be used for controlling the LED strip. Must be set before using any LED strips. Should only be called once. * The LED instance should be configured before being set, however it does not need to be started. * * @param led the LED instance to be used @@ -37,8 +37,7 @@ public static void setAddressableLED(AddressableLED led) { } /** - * Sets the AddressableLEDBuffer instance to be used for controlling the LED strip. Must be set before using any LED strips. - * The LED buffer instance must be configured before being set. + * Sets the AddressableLEDBuffer instance to be used for controlling the LED strip. Must be set before using any LED strips. Should only be called once. * * @param ledBuffer the LED buffer instance to be used */ @@ -80,8 +79,8 @@ void clearLEDColors() { @Override void blink(Color firstColor, double speed) { - double correctedSpeed = 1 - speed; - double currentTime = Timer.getFPGATimestamp(); + final double correctedSpeed = 1 - speed; + final double currentTime = Timer.getFPGATimestamp(); if (currentTime - lastLEDAnimationChangeTime > correctedSpeed) { lastLEDAnimationChangeTime = currentTime; isLEDAnimationChanged = !isLEDAnimationChanged; @@ -100,10 +99,10 @@ void staticColor(Color color) { @Override void breathe(Color color, int breathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { - boolean correctedInverted = this.inverted != inverted; + final boolean correctedInverted = this.inverted != inverted; clearLEDColors(); - double moveLEDTimeSeconds = 1 - speed; - double currentTime = Timer.getFPGATimestamp(); + final double moveLEDTimeSeconds = 1 - speed; + final double currentTime = Timer.getFPGATimestamp(); if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { lastLEDAnimationChangeTime = currentTime; if (correctedInverted) @@ -118,9 +117,9 @@ void breathe(Color color, int breathingLEDs, double speed, boolean inverted, Lar @Override void colorFlow(Color color, double speed, boolean inverted) { clearLEDColors(); - boolean correctedInverted = this.inverted != inverted; - double moveLEDTimeSeconds = 1 - speed; - double currentTime = Timer.getFPGATimestamp(); + final boolean correctedInverted = this.inverted != inverted; + final double moveLEDTimeSeconds = 1 - speed; + final double currentTime = Timer.getFPGATimestamp(); if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { lastLEDAnimationChangeTime = currentTime; if (isLEDAnimationChanged) @@ -139,23 +138,24 @@ void alternateColor(Color firstColor, Color secondColor) { } @Override - void rainbow(double brightness, double speed) { - int adjustedBrightness = (int) (brightness * 255); - int hueIncrement = (int) (speed * 8); + 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); for (int led = 0; led < numberOfLEDs; led++) { final int hue = (int) (rainbowFirstPixelHue + (led * 180 / numberOfLEDs) % 180); LED_BUFFER.setHSV(led + indexOffset, hue, 255, adjustedBrightness); } - if (inverted) { + if (correctedInverted) { rainbowFirstPixelHue -= hueIncrement; if (rainbowFirstPixelHue < 0) rainbowFirstPixelHue += 180; - } else { - rainbowFirstPixelHue += hueIncrement; - rainbowFirstPixelHue %= 180; + return; } + rainbowFirstPixelHue += hueIncrement; + rainbowFirstPixelHue %= 180; } @Override 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 79a57bd7..42390289 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -18,7 +18,7 @@ public class CANdleLEDStrip extends LEDStrip { private final AddressableLEDStrip simulationLEDStrip; /** - * Sets the CANdle instance to be used for controlling the LED strips. Must be set before using any LED strips. + * Sets the CANdle instance to be used for controlling the LED strips. Must be set before using any LED strips. Should only be called once * * @param candle the CANdle instance to be used */ @@ -28,7 +28,8 @@ public static void setCANdle(CANdle candle) { } /** - * Sets the simulation AddressableLED instance to be used for testing in simulation. Must be set before using any LED strips in simulation. + * Sets the simulation AddressableLED instance to be used for testing in simulation. Must be set before using any LED strips in simulation. Should only be called once. + * The LED instance should be configured before being set, however it does not need to be started. * * @param simulationLEDStrip the AddressableLED instance to be used in simulation */ @@ -37,7 +38,7 @@ public static void setSimulationLED(AddressableLED simulationLEDStrip) { } /** - * Sets the simulation AddressableLEDBuffer instance to be used for testing in simulation. Must be set before using any LED strips in simulation. + * Sets the simulation AddressableLEDBuffer instance to be used for testing in simulation. Must be set before using any LED strips in simulation. Should only be called once. * * @param simulationLEDBuffer the AddressableLED buffer instance to be used in simulation */ @@ -128,7 +129,7 @@ void alternateColor(Color firstColor, Color secondColor) { @Override void colorFlow(Color color, double speed, boolean inverted) { - boolean correctedInverted = this.inverted != inverted; + final boolean correctedInverted = this.inverted != inverted; CANDLE.animate( new ColorFlowAnimation( (int) color.red, @@ -145,13 +146,14 @@ void colorFlow(Color color, double speed, boolean inverted) { } @Override - void rainbow(double brightness, double speed) { + void rainbow(double brightness, double speed, boolean inverted) { + final boolean correctedInverted = this.inverted != inverted; CANDLE.animate( new RainbowAnimation( brightness, speed, this.numberOfLEDs, - inverted, + correctedInverted, indexOffset ), animationSlot 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 e236e046..5d8d9367 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -12,85 +12,144 @@ * A class that contains static functions for getting LED commands. These commands work with both types of LEDStrips. */ public class LEDCommands { - public static Command getStaticColorCommand(Color color, LEDStrip... LEDStrips) { + /** + * Gets a command that sets the color of the LED strip to the given color. + * + * @param color the color to set the LED strip 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 -> LEDStrip.setCurrentAnimation(() -> LEDStrip.staticColor(color)), LEDStrips); + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.staticColor(color)), ledStrips); }, - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } - public static Command getBlinkingCommand(Color firstColor, double speed, LEDStrip... LEDStrips) { + /** + * Gets a command that 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 + * @param ledStrips the LED strips to be used + * @return the command + */ + public static Command getBlinkingCommand(Color firstColor, 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(firstColor, speed)), ledStrips); }, - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } - public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode, LEDStrip... LEDStrips) { + /** + * Gets a command that "breathes" a pocket of light across the LED strip. + * + * @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 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 + * @return the command + */ + public static Command getBreatheCommand(Color color, int amountOfBreathingLEDs, 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, amountOfBreathingLEDs, speed, inverted, bounceMode)), ledStrips); }, - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } - public static Command getColorFlowCommand(Color color, double speed, boolean inverted, LEDStrip... LEDStrips) { + /** + * Gets a command that 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 flow 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 -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, speed, inverted)), LEDStrips); + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.colorFlow(color, speed, inverted)), ledStrips); }, - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } - public static Command getAlternateColorCommand(Color firstColor, Color secondColor, LEDStrip... LEDStrips) { + /** + * Gets a command that displays two colors in an alternating pattern on the LED strips. + * + * @param firstColor the first color + * @param secondColor the second color + * @param ledStrips the LED strips to be used + * @return the command + */ + public static Command getAlternateColorCommand(Color firstColor, Color secondColor, LEDStrip... ledStrips) { return new StartEndCommand( () -> { - runForLEDs((LEDStrip::clearLEDColors), LEDStrips); - runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.alternateColor(firstColor, secondColor)), LEDStrips); + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.alternateColor(firstColor, secondColor)), ledStrips); }, - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } - public static Command getSectionColorCommand(Supplier[] colors, LEDStrip... LEDStrips) { + /** + * Gets a command that colors the LED strips in sections. + * + * @param colors an array of colors to set the sections to. 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 -> LEDStrip.setCurrentAnimation(() -> LEDStrip.sectionColor(colors)), LEDStrips); + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.sectionColor(colors)), ledStrips); }, - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } - public static Command getRainbowCommand(double brightness, double speed, LEDStrip... LEDStrips) { + /** + * Gets a command that displays a rainbow pattern on the LED strips. + * + * @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 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 -> LEDStrip.setCurrentAnimation(() -> LEDStrip.rainbow(brightness, speed)), LEDStrips); + runForLEDs((LEDStrip::clearLEDColors), ledStrips); + runForLEDs(LEDStrip -> LEDStrip.setCurrentAnimation(() -> LEDStrip.rainbow(brightness, speed, inverted)), ledStrips); }, - () -> runForLEDs(LEDStrip::clearLEDColors, LEDStrips), - LEDStrips + () -> runForLEDs(LEDStrip::clearLEDColors, ledStrips), + ledStrips ).ignoringDisable(true); } - private static void runForLEDs(Consumer action, LEDStrip... LEDStrips) { - for (LEDStrip LEDStrip : LEDStrips) + private static void runForLEDs(Consumer action, LEDStrip... ledStrips) { + for (LEDStrip LEDStrip : ledStrips) action.accept(LEDStrip); } } \ No newline at end of file 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 8b427cb0..cda0a8af 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -50,9 +50,9 @@ void setCurrentAnimation(Runnable currentAnimation) { abstract void staticColor(Color color); /** - * Blinks the LED strip between two colors. + * Blinks the LED strip with a specific color. * - * @param firstColor the first color to blink + * @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); @@ -97,8 +97,9 @@ void setCurrentAnimation(Runnable currentAnimation) { * * @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); + abstract void rainbow(double brightness, double speed, boolean inverted); abstract void clearLEDColors(); From 3903b82e77dc0ea43c6a1472c1fcb4db83df3eec Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Tue, 26 Nov 2024 22:39:41 +0200 Subject: [PATCH 29/31] sim cleaning thing. Still working on annoying LED_STRIPS array bug --- .../hardware/misc/leds/AddressableLEDStrip.java | 14 ++------------ .../trigon/hardware/misc/leds/CANdleLEDStrip.java | 4 ++-- .../org/trigon/hardware/misc/leds/LEDStrip.java | 11 +++++++++++ 3 files changed, 15 insertions(+), 14 deletions(-) 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 8fa79f36..f6a238cc 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -5,7 +5,6 @@ import edu.wpi.first.wpilibj.AddressableLEDBuffer; import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj.util.Color; -import org.trigon.hardware.RobotHardwareStats; import java.util.function.Supplier; @@ -16,7 +15,6 @@ public class AddressableLEDStrip extends LEDStrip { private static AddressableLED LED; private static AddressableLEDBuffer LED_BUFFER; - private final boolean shouldOnlyRunInSimulation; private int lastBreatheLED; private double lastLEDAnimationChangeTime = 0; private double rainbowFirstPixelHue = 0; @@ -53,21 +51,13 @@ public static void setAddressableLEDBuffer(AddressableLEDBuffer ledBuffer) { * @param numberOfLEDs the amount of LEDs in the strip * @param indexOffset the offset of the first LED in the strip */ - public AddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { - this(inverted, numberOfLEDs, indexOffset, false); - } - - AddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset, boolean shouldOnlyRunInSimulation) { + AddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { super(inverted, numberOfLEDs, indexOffset); - this.shouldOnlyRunInSimulation = shouldOnlyRunInSimulation; resetLEDSettings(); } @Override public void periodic() { - if (shouldOnlyRunInSimulation && !RobotHardwareStats.isSimulation()) - return; - currentAnimation.run(); LED.setData(LED_BUFFER); } @@ -99,8 +89,8 @@ void staticColor(Color color) { @Override void breathe(Color color, int breathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) { - final boolean correctedInverted = this.inverted != inverted; clearLEDColors(); + final boolean correctedInverted = this.inverted != inverted; final double moveLEDTimeSeconds = 1 - speed; final double currentTime = Timer.getFPGATimestamp(); if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { 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 42390289..48e33e66 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -53,11 +53,11 @@ public static void setSimulationLEDBuffer(AddressableLEDBuffer simulationLEDBuff * @param numberOfLEDs the amount of LEDs in the strip * @param indexOffset the offset of the first LED in the strip */ - public CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + CANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { super(inverted, numberOfLEDs, indexOffset); animationSlot = LAST_CREATED_LED_STRIP_ANIMATION_SLOT; LAST_CREATED_LED_STRIP_ANIMATION_SLOT++; - this.simulationLEDStrip = new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset, true); + this.simulationLEDStrip = new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); } @Override 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 cda0a8af..6d2b8ee3 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -4,6 +4,7 @@ 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; import java.util.function.Supplier; @@ -26,6 +27,16 @@ public LEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { addLEDStripToLEDStripsArray(this); } + public static AddressableLEDStrip createAddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + return new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); + } + + public static LEDStrip createCANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + if (RobotHardwareStats.isReplay() || RobotHardwareStats.isSimulation()) + return new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); + return new CANdleLEDStrip(inverted, numberOfLEDs, indexOffset); + } + public static void setDefaultCommandForAllLEDS(Command command) { for (LEDStrip ledStrip : LED_STRIPS) ledStrip.setDefaultCommand(command); From 7b34c7cfde74c4c57742221c56e5d7617940230a Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:56:13 +0200 Subject: [PATCH 30/31] cleaned a bit, finished sim logic and added initiateAddressableLED --- .../misc/leds/AddressableLEDStrip.java | 24 +++++------ .../hardware/misc/leds/CANdleLEDStrip.java | 40 ++----------------- .../trigon/hardware/misc/leds/LEDStrip.java | 35 +++++++++++----- 3 files changed, 38 insertions(+), 61 deletions(-) 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 f6a238cc..a7912674 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -22,28 +22,22 @@ public class AddressableLEDStrip extends LEDStrip { private int amountOfColorFlowLEDs = 0; /** - * Sets the AddressableLED instance to be used for controlling the LED strip. Must be set before using any LED strips. Should only be called once. - * The LED instance should be configured before being set, however it does not need to be started. + * Sets and configures the AddressableLED and AddressableLEDBuffer instances to be used for controlling the LED strip. + * Must be set before using any LED strips. Should only be called once. * - * @param led the LED instance to be used + * @param port the port of the LED strip + * @param totalAmountOfLEDs the total amount of LEDs in all LED strips */ - public static void setAddressableLED(AddressableLED led) { + public static void initiateAddressableLED(int port, int totalAmountOfLEDs) { + if (LED_BUFFER == null) + LED_BUFFER = new AddressableLEDBuffer(totalAmountOfLEDs); if (LED == null) { - LED = led; + LED = new AddressableLED(port); + LED.setLength(totalAmountOfLEDs); LED.start(); } } - /** - * Sets the AddressableLEDBuffer instance to be used for controlling the LED strip. Must be set before using any LED strips. Should only be called once. - * - * @param ledBuffer the LED buffer instance to be used - */ - public static void setAddressableLEDBuffer(AddressableLEDBuffer ledBuffer) { - if (LED_BUFFER == null) - LED_BUFFER = ledBuffer; - } - /** * Constructs a new AddressableLEDStrip. Before any commands are sent to the LED strip, the setAddressableLED and setAddressableLEDBuffer methods must be called. * 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 48e33e66..97ed7ccc 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -1,8 +1,6 @@ package org.trigon.hardware.misc.leds; import com.ctre.phoenix.led.*; -import edu.wpi.first.wpilibj.AddressableLED; -import edu.wpi.first.wpilibj.AddressableLEDBuffer; import edu.wpi.first.wpilibj.util.Color; import org.trigon.hardware.RobotHardwareStats; @@ -15,7 +13,6 @@ public class CANdleLEDStrip extends LEDStrip { private static CANdle CANDLE; private static int LAST_CREATED_LED_STRIP_ANIMATION_SLOT = 0; private final int animationSlot; - private final AddressableLEDStrip simulationLEDStrip; /** * Sets the CANdle instance to be used for controlling the LED strips. Must be set before using any LED strips. Should only be called once @@ -27,23 +24,9 @@ public static void setCANdle(CANdle candle) { CANDLE = candle; } - /** - * Sets the simulation AddressableLED instance to be used for testing in simulation. Must be set before using any LED strips in simulation. Should only be called once. - * The LED instance should be configured before being set, however it does not need to be started. - * - * @param simulationLEDStrip the AddressableLED instance to be used in simulation - */ - public static void setSimulationLED(AddressableLED simulationLEDStrip) { - AddressableLEDStrip.setAddressableLED(simulationLEDStrip); - } - - /** - * Sets the simulation AddressableLEDBuffer instance to be used for testing in simulation. Must be set before using any LED strips in simulation. Should only be called once. - * - * @param simulationLEDBuffer the AddressableLED buffer instance to be used in simulation - */ - public static void setSimulationLEDBuffer(AddressableLEDBuffer simulationLEDBuffer) { - AddressableLEDStrip.setAddressableLEDBuffer(simulationLEDBuffer); + public static void setTotalAmountOfLEDs(int totalAmountOfLEDs) { + if (RobotHardwareStats.isSimulation() || RobotHardwareStats.isReplay()) + AddressableLEDStrip.initiateAddressableLED(0, totalAmountOfLEDs); } /** @@ -57,21 +40,10 @@ public static void setSimulationLEDBuffer(AddressableLEDBuffer simulationLEDBuff super(inverted, numberOfLEDs, indexOffset); animationSlot = LAST_CREATED_LED_STRIP_ANIMATION_SLOT; LAST_CREATED_LED_STRIP_ANIMATION_SLOT++; - this.simulationLEDStrip = new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); - } - - @Override - public void simulationPeriodic() { - simulationLEDStrip.periodic(); } @Override void clearLEDColors() { - if (RobotHardwareStats.isSimulation()) { - simulationLEDStrip.clearLEDColors(); - return; - } - CANDLE.clearAnimation(animationSlot); } @@ -166,12 +138,6 @@ void sectionColor(Supplier[] colors) { setSectionColor(colors.length, LEDSPerSection, colors); } - @Override - void resetLEDSettings() { - if (RobotHardwareStats.isSimulation()) - simulationLEDStrip.resetLEDSettings(); - } - private void setSectionColor(int amountOfSections, int LEDSPerSection, Supplier[] colors) { for (int i = 0; i < amountOfSections; i++) { CANDLE.setLEDs( 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 6d2b8ee3..6899ab53 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -19,18 +19,26 @@ public abstract class LEDStrip extends SubsystemBase { Runnable currentAnimation = () -> { }; - public LEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { - this.inverted = inverted; - this.numberOfLEDs = numberOfLEDs; - this.indexOffset = indexOffset; - - addLEDStripToLEDStripsArray(this); - } - + /** + * Creates a new AddressableLEDStrip. + * + * @param inverted whether the LED strip is inverted + * @param numberOfLEDs the amount of LEDs in the strip + * @param indexOffset the offset of the first LED in the strip + * @return the created AddressableLEDStrip + */ public static AddressableLEDStrip createAddressableLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { return new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); } + /** + * Creates a new CANdleLEDStrip. In simulation or replay mode, an AddressableLEDStrip is created instead. + * + * @param inverted whether the LED strip is inverted + * @param numberOfLEDs the amount of LEDs in the strip + * @param indexOffset the offset of the first LED in the strip + * @return the created LEDStrip + */ public static LEDStrip createCANdleLEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { if (RobotHardwareStats.isReplay() || RobotHardwareStats.isSimulation()) return new AddressableLEDStrip(inverted, numberOfLEDs, indexOffset); @@ -42,6 +50,14 @@ public static void setDefaultCommandForAllLEDS(Command command) { ledStrip.setDefaultCommand(command); } + public LEDStrip(boolean inverted, int numberOfLEDs, int indexOffset) { + this.inverted = inverted; + this.numberOfLEDs = numberOfLEDs; + this.indexOffset = indexOffset; + + addLEDStripToLEDStripsArray(this); + } + public int getNumberOfLEDS() { return numberOfLEDs; } @@ -51,7 +67,8 @@ void setCurrentAnimation(Runnable currentAnimation) { currentAnimation.run(); } - abstract void resetLEDSettings(); + void resetLEDSettings() { + } /** * Sets the color of the LED strip to the given color. From 927817b8660048d5b6d07510326e77faae51a182 Mon Sep 17 00:00:00 2001 From: Nummun14 <142012009+Nummun14@users.noreply.github.com> Date: Wed, 27 Nov 2024 16:47:38 +0200 Subject: [PATCH 31/31] cleaning --- .../hardware/misc/leds/AddressableLEDStrip.java | 11 ++++++++++- .../trigon/hardware/misc/leds/CANdleLEDStrip.java | 6 ++++++ .../trigon/hardware/misc/leds/LEDCommands.java | 2 +- .../org/trigon/hardware/misc/leds/LEDStrip.java | 15 ++++++++++----- 4 files changed, 27 insertions(+), 7 deletions(-) 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 a7912674..0a0533f3 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java @@ -31,6 +31,7 @@ public class AddressableLEDStrip extends LEDStrip { public static void initiateAddressableLED(int port, int totalAmountOfLEDs) { if (LED_BUFFER == null) LED_BUFFER = new AddressableLEDBuffer(totalAmountOfLEDs); + if (LED == null) { LED = new AddressableLED(port); LED.setLength(totalAmountOfLEDs); @@ -65,10 +66,12 @@ void clearLEDColors() { void blink(Color firstColor, double speed) { final double correctedSpeed = 1 - speed; final double currentTime = Timer.getFPGATimestamp(); + if (currentTime - lastLEDAnimationChangeTime > correctedSpeed) { lastLEDAnimationChangeTime = currentTime; isLEDAnimationChanged = !isLEDAnimationChanged; } + if (isLEDAnimationChanged) { staticColor(firstColor); return; @@ -87,6 +90,7 @@ void breathe(Color color, int breathingLEDs, double speed, boolean inverted, Lar final boolean correctedInverted = this.inverted != inverted; final double moveLEDTimeSeconds = 1 - speed; final double currentTime = Timer.getFPGATimestamp(); + if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { lastLEDAnimationChangeTime = currentTime; if (correctedInverted) @@ -94,6 +98,7 @@ void breathe(Color color, int breathingLEDs, double speed, boolean inverted, Lar else lastBreatheLED++; } + checkIfBreathingHasHitEnd(breathingLEDs, correctedInverted, bounceMode); setBreathingLEDs(color, breathingLEDs, bounceMode); } @@ -104,6 +109,7 @@ void colorFlow(Color color, double speed, boolean inverted) { final boolean correctedInverted = this.inverted != inverted; final double moveLEDTimeSeconds = 1 - speed; final double currentTime = Timer.getFPGATimestamp(); + if (currentTime - lastLEDAnimationChangeTime > moveLEDTimeSeconds) { lastLEDAnimationChangeTime = currentTime; if (isLEDAnimationChanged) @@ -111,6 +117,7 @@ void colorFlow(Color color, double speed, boolean inverted) { else amountOfColorFlowLEDs++; } + checkIfColorFlowHasHitEnd(); setLEDColors(color, correctedInverted ? numberOfLEDs - amountOfColorFlowLEDs - 1 : 0, correctedInverted ? numberOfLEDs - 1 : amountOfColorFlowLEDs); } @@ -165,11 +172,12 @@ void resetLEDSettings() { } private void checkIfBreathingHasHitEnd(int amountOfBreathingLEDs, boolean inverted, LarsonAnimation.BounceMode bounceMode) { - int bounceModeAddition = switch (bounceMode) { + final int bounceModeAddition = switch (bounceMode) { case Back -> amountOfBreathingLEDs; case Center -> amountOfBreathingLEDs / 2; default -> 0; }; + if (inverted ? (lastBreatheLED < indexOffset + bounceModeAddition) : (lastBreatheLED >= numberOfLEDs + indexOffset + bounceModeAddition)) lastBreatheLED = inverted ? indexOffset + numberOfLEDs : indexOffset; } @@ -178,6 +186,7 @@ private void setBreathingLEDs(Color color, int breathingLEDs, LarsonAnimation.Bo for (int i = 0; i < breathingLEDs; i++) { if (lastBreatheLED - i >= indexOffset && lastBreatheLED - i < indexOffset + numberOfLEDs) LED_BUFFER.setLED(lastBreatheLED - i, color); + else if (lastBreatheLED - i < indexOffset + numberOfLEDs) { if (bounceMode.equals(LarsonAnimation.BounceMode.Back) || bounceMode.equals(LarsonAnimation.BounceMode.Center) && i > breathingLEDs / 2) return; 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 97ed7ccc..1f3a6017 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java @@ -24,6 +24,12 @@ public static void setCANdle(CANdle candle) { CANDLE = candle; } + /** + * Sets the total amount of LEDs in all LED strips for simulation. + * Must be set before using any LED strips in simulation. Should only be called once. + * + * @param totalAmountOfLEDs the total amount of LEDs in all LED strips + */ public static void setTotalAmountOfLEDs(int totalAmountOfLEDs) { if (RobotHardwareStats.isSimulation() || RobotHardwareStats.isReplay()) AddressableLEDStrip.initiateAddressableLED(0, totalAmountOfLEDs); 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 5d8d9367..a798d404 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDCommands.java @@ -111,7 +111,7 @@ public static Command getAlternateColorCommand(Color firstColor, Color secondCol } /** - * Gets a command that colors the LED strips in sections. + * Gets a command that splits the LED strip into different sections. * * @param colors an array of colors to set the sections to. The length of the array dictates the amount of sections * @param ledStrips the LED strips to be used 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 6899ab53..c2802a0a 100644 --- a/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java +++ b/src/main/java/org/trigon/hardware/misc/leds/LEDStrip.java @@ -13,10 +13,10 @@ */ public abstract class LEDStrip extends SubsystemBase { public static LEDStrip[] LED_STRIPS = new LEDStrip[0]; - final int indexOffset; - final boolean inverted; - final int numberOfLEDs; - Runnable currentAnimation = () -> { + protected final int indexOffset; + protected final boolean inverted; + protected final int numberOfLEDs; + protected Runnable currentAnimation = () -> { }; /** @@ -45,6 +45,11 @@ public static LEDStrip createCANdleLEDStrip(boolean inverted, int numberOfLEDs, return new CANdleLEDStrip(inverted, numberOfLEDs, indexOffset); } + /** + * Sets the default command for all LED strips. + * + * @param command the default command to be set + */ public static void setDefaultCommandForAllLEDS(Command command) { for (LEDStrip ledStrip : LED_STRIPS) ledStrip.setDefaultCommand(command); @@ -114,7 +119,7 @@ void resetLEDSettings() { abstract void alternateColor(Color firstColor, Color secondColor); /** - * Colors the LED strip in sections. + * 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 */