diff --git a/src/main/java/frc/robot/Constants.java b/src/main/java/frc/robot/Constants.java index 7ee63a9..31e363c 100644 --- a/src/main/java/frc/robot/Constants.java +++ b/src/main/java/frc/robot/Constants.java @@ -56,4 +56,12 @@ public static class DrivetrainConstants { public static final double ROTATION_KI = 0.0; public static final double ROTATION_KD = 0.0; } + + //placeholder constants + public static class LEDConstants { + public static final int KPORT = 0; + public static final int KLENGTH = 60; + + public static final double BLINK_TIME = 1; // in seconds for after intake/outtake + } } diff --git a/src/main/java/frc/robot/subsystems/LEDSubsystem.java b/src/main/java/frc/robot/subsystems/LEDSubsystem.java new file mode 100644 index 0000000..d4d8750 --- /dev/null +++ b/src/main/java/frc/robot/subsystems/LEDSubsystem.java @@ -0,0 +1,114 @@ +package frc.robot.subsystems; + +import edu.wpi.first.units.Units; +import static edu.wpi.first.units.Units.Percent; +import static edu.wpi.first.units.Units.Second; + +import edu.wpi.first.units.measure.Time; +import edu.wpi.first.wpilibj.AddressableLED; +import edu.wpi.first.wpilibj.AddressableLEDBuffer; +import edu.wpi.first.wpilibj.LEDPattern; +import edu.wpi.first.wpilibj.util.Color; +import edu.wpi.first.wpilibj2.command.Command; +import edu.wpi.first.wpilibj2.command.SubsystemBase; +import frc.robot.Constants.LEDConstants; + +public class LEDSubsystem extends SubsystemBase { + private final AddressableLED m_led; + private final AddressableLEDBuffer m_ledBuffer; + + /** Called once at the beginning of the robot program. */ + public LEDSubsystem() { + // PWM port 9 + // Must be a PWM header, not MXP or DIO + m_led = new AddressableLED(LEDConstants.KPORT); + + // Reuse buffer + // Default to a length of 60, start empty output + // Length is expensive to set, so only set it once, then just update data + m_ledBuffer = new AddressableLEDBuffer(LEDConstants.KLENGTH); + m_led.setLength(m_ledBuffer.getLength()); + + // Set the data + m_led.setData(m_ledBuffer); + + m_led.start(); + + // Set the default command to turn the strip off, otherwise the last colors written by + // the last command to run will continue to be displayed. + // Note: Other default patterns could be used instead! + setDefaultCommand((runScrollingYellowBlueCommand()).withName("LED/default colors")); + } + + + @Override + public void periodic() { + // Periodically send the latest LED color data to the LED strip for it to display + m_led.setData(m_ledBuffer); + } + + /** + * Creates a command that runs a pattern on the entire LED strip. + * + * @param pattern the LED pattern to run + */ + public Command runPattern(LEDPattern pattern) { + return run(() -> pattern.applyTo(m_ledBuffer)).ignoringDisable(true); + } + + public Command runBlinkGreen() { + return runPattern(LEDPattern.solid(Color.kGreen).blink(Time.ofBaseUnits(0.25, Units.Seconds))) + .withName("LED/blink green"); + } + + public Command runSolidYellow() { + return runPattern(LEDPattern.solid(Color.kYellow)) + .withName("LED/solid yellow"); + } + + public Command runSolidBlue() { + return runPattern(LEDPattern.solid(Color.kDarkBlue)) + .withName("LED/solid blue"); + } + + public Command runSolidOrange() { + return runPattern(LEDPattern.solid(Color.kOrange)) + .withName("LED/solid orange"); + } + + public Command runSolidGreen() { + return runPattern(LEDPattern.solid(Color.kGreen)) + .withName("LED/solid green"); + } + + public Command runSolidRed() { + return runPattern(LEDPattern.solid(Color.kRed)) + .withName("LED/solid red"); + } + + public Command runSolidWhite() { + return runPattern(LEDPattern.solid(Color.kWhite)) + .withName("LED/solid white"); + } + + public Command runSolidPink() { + return runPattern(LEDPattern.solid(Color.kPink)) + .withName("LED/solid pink"); + } + + public Command runSolidPurple() { + return runPattern(LEDPattern.solid(Color.kPurple)) + .withName("LED/solid purple"); + } + + public Command runGradientBlueYellow() { + LEDPattern gradient = LEDPattern.gradient(LEDPattern.GradientType.kDiscontinuous, Color.kYellow, Color.kBlue); + return runPattern(gradient).withName("LED/gradient blue-yellow"); + } + + public Command runScrollingYellowBlueCommand() { + LEDPattern base = LEDPattern.gradient(LEDPattern.GradientType.kDiscontinuous, Color.kYellow, Color.kBlue); + LEDPattern pattern = base.scrollAtRelativeSpeed(Percent.per(Second).of(100)); + return runPattern(pattern).withName("LED/scrolling yellow-blue"); + } +}