-
Notifications
You must be signed in to change notification settings - Fork 0
create LED template #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package frc.robot.subsystems; | ||
|
|
||
| import edu.wpi.first.units.Units; | ||
|
|
||
| import static edu.wpi.first.units.Units.Centimeters; | ||
| import static edu.wpi.first.units.Units.Meters; | ||
| import static edu.wpi.first.units.Units.Percent; | ||
| import static edu.wpi.first.units.Units.Second; | ||
|
|
||
| import edu.wpi.first.units.measure.Distance; | ||
| 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((runScrollingRedBlueCommand()).withName("Off")); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we change the name of this command to something like "Default colors" or something? |
||
| } | ||
|
|
||
|
|
||
| @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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add names to all of these commands with |
||
| return run(() -> pattern.applyTo(m_ledBuffer)).ignoringDisable(true); | ||
| } | ||
|
|
||
| public Command runBlinkGreen() { | ||
| return runPattern(LEDPattern.solid(Color.kGreen).blink(Time.ofBaseUnits(0.25, Units.Seconds))); | ||
| } | ||
|
|
||
| public Command runSolidYellow() { | ||
| return runPattern(LEDPattern.solid(Color.kYellow)); | ||
| } | ||
|
|
||
| public Command runSolidBlue() { | ||
| return runPattern(LEDPattern.solid(Color.kDarkBlue)); | ||
| } | ||
|
|
||
| public Command runSolidOrange() { | ||
| return runPattern(LEDPattern.solid(Color.kOrange)); | ||
| } | ||
|
|
||
| public Command runSolidGreen() { | ||
| return runPattern(LEDPattern.solid(Color.kGreen)); | ||
| } | ||
|
|
||
| public Command runSolidRed() { | ||
| return runPattern(LEDPattern.solid(Color.kRed)); | ||
| } | ||
|
|
||
| public Command runSolidWhite() { | ||
| return runPattern(LEDPattern.solid(Color.kWhite)); | ||
| } | ||
|
|
||
| public Command runSolidPink() { | ||
| return runPattern(LEDPattern.solid(Color.kPink)); | ||
| } | ||
|
|
||
| public Command runSolidPurple() { | ||
| return runPattern(LEDPattern.solid(Color.kPurple)); | ||
| } | ||
|
|
||
| public Command runGradientBlueYellow(){ | ||
| LEDPattern gradient = LEDPattern.gradient(LEDPattern.GradientType.kDiscontinuous, Color.kYellow, Color.kBlue); | ||
| return runPattern(gradient); | ||
| } | ||
|
|
||
| public Command runGradientGreenYellow(){ | ||
| LEDPattern gradient = LEDPattern.gradient(LEDPattern.GradientType.kDiscontinuous, Color.kYellow, Color.kGreen).scrollAtRelativeSpeed(Percent.per(Second).of(100)); | ||
| return runPattern(gradient); | ||
| } | ||
|
|
||
| public Command runScrollingRedBlueCommand(){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it red and blue? :( I liked blue + yellow better--they're our team colors! /hj |
||
| Distance ledSpacing = Meters.of(1 / 60.0); | ||
| LEDPattern base = LEDPattern.gradient(LEDPattern.GradientType.kDiscontinuous, Color.kRed, Color.kBlue); | ||
| LEDPattern pattern = base.scrollAtRelativeSpeed(Percent.per(Second).of(100)); | ||
| LEDPattern absolute = base.scrollAtAbsoluteSpeed(Centimeters.per(Second).of(6.25), ledSpacing); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to be unused code. Not sure if we want to keep it. |
||
| return runPattern(pattern); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable seems to be unused.