From fdeff7bc4cee0fd8d5b69b29e850a91b2b69578e Mon Sep 17 00:00:00 2001 From: CrumblzTheEgg Date: Tue, 20 May 2025 18:56:53 -0500 Subject: [PATCH 1/2] feat: reworked the LED manager to run multiple LED strips Able to run several LED strips with different patterns utilizing the LEDSegment class. --- src/main/java/frc/robot/Constants.java | 13 ++++ src/main/java/frc/robot/RobotContainer.java | 5 ++ .../java/frc/robot/subsystems/LEDManager.java | 62 +++++++++++++++---- .../java/frc/robot/subsystems/LEDSegment.java | 31 ++++++++++ 4 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 src/main/java/frc/robot/subsystems/LEDSegment.java diff --git a/src/main/java/frc/robot/Constants.java b/src/main/java/frc/robot/Constants.java index 7ec11ad..6ca5d15 100644 --- a/src/main/java/frc/robot/Constants.java +++ b/src/main/java/frc/robot/Constants.java @@ -49,6 +49,19 @@ public static class Defaults { } + public static class LEDConstants { + + /** + * The PWM ID of the LED strip. + */ + public static final int LED_ID = 1; + + /** + * The total length of the LED strip, in # of LEDs. + */ + public static final int STRIP_TOTAL_LENGTH = 200; + } + /** *

OperatorConstants

* The {@code OperatorConstants} class is a subclass contained within the {@code Constants} class. diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 1af11e1..c2fe1ba 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -6,6 +6,7 @@ import frc.robot.Constants.DriveConstants; import frc.robot.Constants.FieldConstants; +import frc.robot.Constants.LEDConstants; import frc.robot.Constants.NarwhalConstants; import frc.robot.Constants.OceanViewConstants; import frc.robot.Constants.OperatorConstants; @@ -14,11 +15,13 @@ import frc.robot.routines.CenterScoreRoutine; import frc.robot.routines.MultiScoreRoutine; import frc.robot.subsystems.DriveSubsystem; +import frc.robot.subsystems.LEDManager; import frc.robot.subsystems.narwhal.NarwhalUpperAssembly; import frc.robot.subsystems.upper_assembly.UpperAssemblyBase; import frc.robot.subsystems.vision.OceanViewManager; import frc.robot.subsystems.vision.odometry.VisionOdometry; import frc.robot.util.EnumPrettifier; +import frc.robot.util.LEDs.LEDConfiguration; import frc.robot.util.autonomous.Alliance; import frc.robot.util.autonomous.AutonomousRoutine; import frc.robot.util.autonomous.StartingPosition; @@ -86,6 +89,8 @@ public RobotContainer() { // Setup Dashboard setupSmartDashboard(); + + LEDManager.initialize(LEDConstants.LED_ID, LEDConstants.STRIP_TOTAL_LENGTH); } /** diff --git a/src/main/java/frc/robot/subsystems/LEDManager.java b/src/main/java/frc/robot/subsystems/LEDManager.java index d3bb79a..7b3ed67 100644 --- a/src/main/java/frc/robot/subsystems/LEDManager.java +++ b/src/main/java/frc/robot/subsystems/LEDManager.java @@ -1,7 +1,10 @@ package frc.robot.subsystems; +import java.util.ArrayList; + import edu.wpi.first.wpilibj.AddressableLED; import edu.wpi.first.wpilibj.AddressableLEDBuffer; +import edu.wpi.first.wpilibj.AddressableLEDBufferView; import frc.robot.util.LEDs.LEDConfiguration; /** @@ -26,34 +29,69 @@ */ public class LEDManager { + private static LEDManager instance; + // Hardware private AddressableLED ledStrip; private AddressableLEDBuffer ledBuffer; /** - * Creates a new LEDManager object using the provided PWM port number. + * returns the singleton object of the LED Manager + * @return the singleton object + */ + public static LEDManager getInstance() { + if (instance == null) { + throw new IllegalStateException("LEDManager has not been initialized. Call initialize() first."); + } else { + return instance; + } + } + + /** + * initialized the LEDManager object using the provided PWM port number and length. + * + * @param port The port number the LED strip is plugged into on the PWM. + * @param length The number of LEDs in the strip + */ + public static void initialize(int port, int length) { + if (instance != null) { + throw new IllegalStateException("LEDManager has already been initialized."); + } + instance = new LEDManager(port, length); + } + + /** + * Creates a new LEDManager object using the provided PWM port number and length. * - * @param portNum The port number the LED strip is plugged into on the PWM. + * @param port The port number the LED strip is plugged into on the PWM. + * @param length The number of LEDs in the strip */ - public LEDManager(int portNum) { + private LEDManager(int port, int length) { // Create new AddressableLED and AddressableLEDBuffer objects. - this.ledStrip = new AddressableLED(portNum); - this.ledBuffer = new AddressableLEDBuffer(60); // TODO: Figure out actual number. 60 is just a placeholder. + this.ledStrip = new AddressableLED(port); + this.ledBuffer = new AddressableLEDBuffer(length); // Setup the LED strip hardware this.ledStrip.setLength(this.ledBuffer.getLength()); this.ledStrip.setData(ledBuffer); this.ledStrip.start(); - } + } /** - * To set ledPattern, method taking in a LEDConfiguration value. - * - * @param ledConfiguration The LEDConfiguration this LEDManager's LED strip will display. + * creates a view to control a segment of the led strip + * @param ledSegment the segment to create the view out of + * @return the view to control the segment */ - public void LEDPatternSet(LEDConfiguration ledConfiguration) { - ledConfiguration.getLEDPattern().applyTo(this.ledBuffer); - this.ledStrip.setData(this.ledBuffer); + public static AddressableLEDBufferView getViewForSegment(LEDSegment ledSegment) { + return instance.ledBuffer.createView(ledSegment.getStartingIndex(),ledSegment.getEndingIndex()); } + + /** + * updates the LED strip. + */ + public static void update() { + instance.ledStrip.setData(instance.ledBuffer); + } + } diff --git a/src/main/java/frc/robot/subsystems/LEDSegment.java b/src/main/java/frc/robot/subsystems/LEDSegment.java new file mode 100644 index 0000000..05b775d --- /dev/null +++ b/src/main/java/frc/robot/subsystems/LEDSegment.java @@ -0,0 +1,31 @@ +package frc.robot.subsystems; + +import edu.wpi.first.wpilibj.AddressableLEDBufferView; +import frc.robot.util.LEDs.LEDConfiguration; + +public class LEDSegment { + + private int startingIndex; + private int endingIndex; + + private AddressableLEDBufferView view; + + public LEDSegment(int startingIndex, int endingIndex) { + this.startingIndex = startingIndex; + this.endingIndex = endingIndex; + view = LEDManager.getViewForSegment(this); + } + + public void setPattern(LEDConfiguration configuration) { + configuration.getLEDPattern().applyTo(view); + } + + public int getStartingIndex() { + return startingIndex; + } + + public int getEndingIndex() { + return endingIndex; + } + +} From bac9fedcbc4cfffaf7a9700f76b451a9433be7d7 Mon Sep 17 00:00:00 2001 From: CrumblzTheEgg <115754191+CrumblzTheEgg@users.noreply.github.com> Date: Thu, 22 May 2025 11:02:46 -0500 Subject: [PATCH 2/2] Update src/main/java/frc/robot/subsystems/LEDManager.java Co-authored-by: Graham Mueller --- src/main/java/frc/robot/subsystems/LEDManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/frc/robot/subsystems/LEDManager.java b/src/main/java/frc/robot/subsystems/LEDManager.java index 7b3ed67..62a4755 100644 --- a/src/main/java/frc/robot/subsystems/LEDManager.java +++ b/src/main/java/frc/robot/subsystems/LEDManager.java @@ -42,9 +42,9 @@ public class LEDManager { public static LEDManager getInstance() { if (instance == null) { throw new IllegalStateException("LEDManager has not been initialized. Call initialize() first."); - } else { - return instance; } + + return instance; } /**