From 6edd0bf9729348cefc4828cd3f2f19c7a697965a Mon Sep 17 00:00:00 2001 From: Merridew1 Date: Mon, 2 Dec 2024 00:21:24 -0600 Subject: [PATCH] LEDs --- src/main/java/frc/robot/RobotContainer.java | 3 +- src/main/java/frc/robot/subsystems/.keep | 0 src/main/java/frc/robot/subsystems/LEDs.java | 128 ++++++++++++++++++ .../ballMechanism/ballMechanism.java | 8 +- 4 files changed, 136 insertions(+), 3 deletions(-) delete mode 100644 src/main/java/frc/robot/subsystems/.keep create mode 100644 src/main/java/frc/robot/subsystems/LEDs.java diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 221544b..626bdaf 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -4,6 +4,7 @@ import edu.wpi.first.wpilibj.XboxController; import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; +import edu.wpi.first.wpilibj.util.Color; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.InstantCommand; import edu.wpi.first.wpilibj2.command.WaitCommand; @@ -72,7 +73,7 @@ private void configureButtonBindings() { drivetrain.setDefaultCommand(drivetrain.driveCommand(driver)); driver.a().whileTrue(hatch.hatchUpCommand()); driver.a().whileTrue(hatch.hatchNeutralCommand()); - driver.leftTrigger().whileTrue(intake.intakeCommand()); + driver.leftTrigger().whileTrue(intake.intakeCommand(Color.kPurple, Color.kWhiteSmoke)); driver.rightTrigger().whileTrue(intake.outtakeCommand()); } diff --git a/src/main/java/frc/robot/subsystems/.keep b/src/main/java/frc/robot/subsystems/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/frc/robot/subsystems/LEDs.java b/src/main/java/frc/robot/subsystems/LEDs.java new file mode 100644 index 0000000..a69e592 --- /dev/null +++ b/src/main/java/frc/robot/subsystems/LEDs.java @@ -0,0 +1,128 @@ +package frc.robot.subsystems; + +import edu.wpi.first.wpilibj.AddressableLED; +import edu.wpi.first.wpilibj.AddressableLEDBuffer; +import edu.wpi.first.wpilibj.util.Color; +import edu.wpi.first.wpilibj2.command.SubsystemBase; + +/** + * This is the class header for the LEDs Subsystem + */ +public class LEDs extends SubsystemBase { + private AddressableLEDBuffer controLedBuffer; + @SuppressWarnings("IOCheck") + private AddressableLED addressableLED; + + /** + * constructs a LED Subsystem + * + * @param length length of the addressable LEDS + * @param port port ID for PWM + */ + public LEDs(int length, int port) { + controLedBuffer = new AddressableLEDBuffer(length); + addressableLED = new AddressableLED(port); + + addressableLED.setLength(controLedBuffer.getLength()); + addressableLED.setData(controLedBuffer); + addressableLED.start(); + } + + /** + * Get LED strip length + * + * @return number of LEDs + */ + public int getLength() { + return controLedBuffer.getLength(); + } + + /** + * Set individual LED color via HSV + * + * @param index the LED index to set + * @param h the h value [0-180) + * @param s the s value [0-255] + * @param v the v value [0-255] + */ + public void setHSV(int index, int h, int s, int v) { + controLedBuffer.setHSV(index, h, s, v); + } + + /** + * Sets the LED output data. + */ + public void setData() { + addressableLED.setData(controLedBuffer); + } + + /** + * Set individual LED color via RGB + * + * @param index the LED index to set + * @param r the r value [0-255] + * @param g the g value [0-255] + * @param b the b value [0-255] + */ + public void setRGB(int index, int r, int g, int b) { + controLedBuffer.setRGB(index, r, g, b); + } + + /** + * Sets RGB Color of the entire LED strip + * + * @param r - [0 - 255] + * @param g - [0 - 255] + * @param b - [0 - 255] + */ + public void setRGB(int r, int g, int b) { + for (var i = 0; i < getLength(); i++) { + setRGB(i, r, g, b); + } + setData(); + } + + /** + * Set individual LED color via Color + * + * @param index the LED index to set + * @param color The color of the LED + */ + public void setColor(int index, Color color) { + controLedBuffer.setLED(index, color); + } + + /** + * Sets the Color of the entire LED strip + * + * @param color color set for the LEDs + */ + public void setColor(Color color) { + for (var i = 0; i < getLength(); i++) { + setColor(i, color); + } + setData(); + } + + public Color getColor(int index) { + return controLedBuffer.getLED(index); + } + + private int flashingDelay = 0; + + + public void flash(Color color, Color altColor) { + if (flashingDelay < 10) { + for (var i = 0; i < getLength(); i++) { + setColor(i, color); + } + } else { + for (var i = 0; i < getLength(); i++) { + setColor(i, altColor); + } + } + setData(); + flashingDelay++; + flashingDelay %= 20; + } +} diff --git a/src/main/java/frc/robot/subsystems/ballMechanism/ballMechanism.java b/src/main/java/frc/robot/subsystems/ballMechanism/ballMechanism.java index 9c4f9ad..a7969e9 100644 --- a/src/main/java/frc/robot/subsystems/ballMechanism/ballMechanism.java +++ b/src/main/java/frc/robot/subsystems/ballMechanism/ballMechanism.java @@ -1,12 +1,15 @@ package frc.robot.subsystems.ballMechanism; +import edu.wpi.first.wpilibj.util.Color; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.Commands; import edu.wpi.first.wpilibj2.command.SubsystemBase; +import frc.robot.subsystems.LEDs; public class ballMechanism extends SubsystemBase { ballMechanismIO io; ballMechanismInputsAutoLogged intakeAutoLogged = new ballMechanismInputsAutoLogged(); + LEDs lights = new LEDs(9, 100); public ballMechanism(ballMechanismIO io) { this.io = io; @@ -31,12 +34,13 @@ public boolean getOuttakeBeamBrakeStatus() { return intakeAutoLogged.outtakeBeamBrake; } - public Command intakeCommand() { + public Command intakeCommand(Color color, Color altColor) { return Commands.startEnd(() -> { setBallMotor(1); }, () -> { setBallMotor(0); - }, this).until(() -> getIntakeBeamBreakStatus()).unless(() -> getIntakeBeamBreakStatus()); + }, this).until(() -> getIntakeBeamBreakStatus()).unless(() -> getIntakeBeamBreakStatus()) + .andThen(() -> lights.flash(color, altColor)); }