From c4b89ec126d247a4631081fb9c062cf9fb1be7d7 Mon Sep 17 00:00:00 2001 From: Zach R Date: Sun, 4 Feb 2024 21:38:53 -0800 Subject: [PATCH] feat: LED Framework --- .../org/team1540/robot2024/Constants.java | 3 +- .../java/org/team1540/robot2024/Robot.java | 10 +++ .../team1540/robot2024/RobotContainer.java | 2 + .../robot2024/subsystems/led/LedPattern.java | 34 ++++++++++ .../subsystems/led/LedPatternRainbow.java | 23 +++++++ .../robot2024/subsystems/led/Leds.java | 50 +++++++++++++++ .../led/ZonedAddressableLEDBuffer.java | 64 +++++++++++++++++++ 7 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/team1540/robot2024/subsystems/led/LedPattern.java create mode 100644 src/main/java/org/team1540/robot2024/subsystems/led/LedPatternRainbow.java create mode 100644 src/main/java/org/team1540/robot2024/subsystems/led/Leds.java create mode 100644 src/main/java/org/team1540/robot2024/subsystems/led/ZonedAddressableLEDBuffer.java diff --git a/src/main/java/org/team1540/robot2024/Constants.java b/src/main/java/org/team1540/robot2024/Constants.java index 4d632b8c..0af642f6 100644 --- a/src/main/java/org/team1540/robot2024/Constants.java +++ b/src/main/java/org/team1540/robot2024/Constants.java @@ -20,7 +20,8 @@ public final class Constants { private static final Mode simMode = Mode.SIM; // Can also be Mode.REPLAY public static final Mode currentMode = Robot.isReal() ? Mode.REAL : simMode; - + public static final int LED_STRIP_PORT_PWM = 0; + public static final int LED_STRIP_LENGTH= 80; public enum Mode { /** * Running on a real robot. diff --git a/src/main/java/org/team1540/robot2024/Robot.java b/src/main/java/org/team1540/robot2024/Robot.java index 0872c1b6..25a4e0fb 100644 --- a/src/main/java/org/team1540/robot2024/Robot.java +++ b/src/main/java/org/team1540/robot2024/Robot.java @@ -1,6 +1,7 @@ package org.team1540.robot2024; import edu.wpi.first.wpilibj.DriverStation; +import edu.wpi.first.wpilibj.util.Color; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.CommandScheduler; import org.littletonrobotics.junction.LogFileUtil; @@ -9,6 +10,9 @@ import org.littletonrobotics.junction.networktables.NT4Publisher; import org.littletonrobotics.junction.wpilog.WPILOGReader; import org.littletonrobotics.junction.wpilog.WPILOGWriter; +import org.team1540.robot2024.subsystems.led.LedPattern; +import org.team1540.robot2024.subsystems.led.LedPatternRainbow; +import org.team1540.robot2024.subsystems.led.Leds; import org.team1540.robot2024.util.MechanismVisualiser; /** @@ -101,6 +105,8 @@ public void robotPeriodic() { */ @Override public void disabledInit() { + robotContainer.leds.setPattern(Leds.Zone.ZONE1, LedPattern.solid(Color.kBlueViolet)); + robotContainer.leds.setPattern(Leds.Zone.ZONE2, LedPattern.solid(Color.kPaleVioletRed)); } /** @@ -115,6 +121,7 @@ public void disabledPeriodic() { */ @Override public void autonomousInit() { + robotContainer.leds.setPattern(Leds.Zone.ZONE1,LedPattern.alternating(Color.kBlueViolet, Color.kCrimson)); autonomousCommand = robotContainer.getAutonomousCommand(); // schedule the autonomous command (example) if (autonomousCommand != null) { @@ -138,6 +145,7 @@ public void teleopInit() { // teleop starts running. If you want the autonomous to // continue until interrupted by another command, remove // this line or comment it out. + robotContainer.leds.setPattern(Leds.Zone.ZONE1, LedPattern.alternating(Color.kBlueViolet, Color.kGreen)); if (autonomousCommand != null) { autonomousCommand.cancel(); } @@ -155,6 +163,8 @@ public void teleopPeriodic() { */ @Override public void testInit() { + robotContainer.leds.setPattern(Leds.Zone.ZONE1,new LedPatternRainbow(1)); + robotContainer.leds.setPattern(Leds.Zone.ZONE2,new LedPatternRainbow(1)); // Cancels all running commands at the start of test mode. CommandScheduler.getInstance().cancelAll(); } diff --git a/src/main/java/org/team1540/robot2024/RobotContainer.java b/src/main/java/org/team1540/robot2024/RobotContainer.java index 8c9d79d7..79158466 100644 --- a/src/main/java/org/team1540/robot2024/RobotContainer.java +++ b/src/main/java/org/team1540/robot2024/RobotContainer.java @@ -27,6 +27,7 @@ import org.team1540.robot2024.subsystems.indexer.IndexerIO; import org.team1540.robot2024.subsystems.indexer.IndexerIOSim; import org.team1540.robot2024.subsystems.indexer.IndexerIOSparkMax; +import org.team1540.robot2024.subsystems.led.Leds; import org.team1540.robot2024.subsystems.shooter.*; import org.team1540.robot2024.subsystems.tramp.Tramp; import org.team1540.robot2024.subsystems.tramp.TrampIO; @@ -56,6 +57,7 @@ public class RobotContainer { public final Elevator elevator; public final Indexer indexer; public final AprilTagVision aprilTagVision; + public final Leds leds = new Leds(); // Controller public final CommandXboxController driver = new CommandXboxController(0); diff --git a/src/main/java/org/team1540/robot2024/subsystems/led/LedPattern.java b/src/main/java/org/team1540/robot2024/subsystems/led/LedPattern.java new file mode 100644 index 00000000..8ee9c340 --- /dev/null +++ b/src/main/java/org/team1540/robot2024/subsystems/led/LedPattern.java @@ -0,0 +1,34 @@ +package org.team1540.robot2024.subsystems.led; + +import edu.wpi.first.wpilibj.AddressableLEDBuffer; +import edu.wpi.first.wpilibj.util.Color; + +import java.util.function.Consumer; + +public class LedPattern { + private final Consumer applier; + public final boolean isDynamic; + LedPattern(Consumer applier, boolean isDynamic) { + this.applier = applier; + this.isDynamic = isDynamic; + } + + void apply(ZonedAddressableLEDBuffer buffer) { + this.applier.accept(buffer); + } + + public static LedPattern solid(Color color) { + return new LedPattern((buffer) -> { + for (int i = 0; i { + for (int i = 0; i end) { + throw new IllegalArgumentException("start must be less than end"); + } + this.buffer = buffer; + this.isInverted = isInverted; + this.start = start; + this.length = end - start; + } + + public void setRGB(int index, int r, int g, int b) { + buffer.setRGB(this.getAbsoluteIndex(index), r, g, b); + } + + public void setHSV(int index, int h, int s, int v) { + buffer.setHSV(this.getAbsoluteIndex(index), h, s, v); + } + + public void setLED(int index, Color color) { + System.out.println(this.getAbsoluteIndex(index)); + buffer.setLED(this.getAbsoluteIndex(index), color); + } + + public void setLED(int index, Color8Bit color) { + buffer.setLED(this.getAbsoluteIndex(index), color); + } + + public Color getLED(int index) { + return buffer.getLED(this.getAbsoluteIndex(index)); + } + + public Color8Bit getLED8Bit(int index) { + return buffer.getLED8Bit(this.getAbsoluteIndex(index)); + } + + public int getLength() { + return this.length; + } + + private int getAbsoluteIndex(int index) { + if (index >= length) { + DriverStation.reportWarning("led index out of bounds", false); + return 0; + } + if (this.isInverted) { + return this.start + length -1 - index; + } else { + return this.start + index; + } + } +}