Class SpectrumLEDs

java.lang.Object
frc.spectrumLib.leds.SpectrumLEDs
All Implemented Interfaces:
Subsystem
Direct Known Subclasses:
Leds

public class SpectrumLEDs extends Object implements Subsystem
CANdle-based addressable LED subsystem, with pattern factories for solids, stripes, blinks, breathes, rainbows, chases, bounces, gradients, ombres, waves, and countdowns.

Patterns come in two flavors. Hardware animations (blink(edu.wpi.first.wpilibj.util.Color, double), breathe(edu.wpi.first.wpilibj.util.Color, double), rainbow(), scrollingRainbow(), chase(edu.wpi.first.wpilibj.util.Color, double), bounce(edu.wpi.first.wpilibj.util.Color, double), fire(), rgbCycle()) run in the CANdle's firmware and cost nothing per loop. Software patterns (solid(edu.wpi.first.wpilibj.util.Color), stripe(double, edu.wpi.first.wpilibj.util.Color, edu.wpi.first.wpilibj.util.Color), gradient(edu.wpi.first.wpilibj.util.Color, edu.wpi.first.wpilibj.util.Color), ombre(edu.wpi.first.wpilibj.util.Color, edu.wpi.first.wpilibj.util.Color), wave(edu.wpi.first.wpilibj.util.Color, edu.wpi.first.wpilibj.util.Color, double, double), countdown(java.util.function.DoubleSupplier, double), switchCountdown(edu.wpi.first.wpilibj.util.Color), edges(edu.wpi.first.wpilibj.util.Color, int)) send a one-shot SolidColor that is resent every loop, and switching to one of them clears the running animations.

Several instances can share one physical CANdle by passing the same device to their SpectrumLEDs.Config and taking non-overlapping startIdx and numLeds ranges, each with its own animation slot.

setPattern(CANdlePattern, int) returns a Command that applies the pattern for as long as it runs and holds the priority it was given (checkPriority(int)).

  • Field Details

    • candle

      protected final CANdle candle
      The CANdle this instance drives, either owned or shared.
    • defaultPattern

      protected final SpectrumLEDs.CANdlePattern defaultPattern
      Pattern shown when no other command wants this subsystem (an orange blink). Built in the constructor body, after config is set, so the factory can read the config.
    • defaultCommand

      protected final Command defaultCommand
      The default command, which displays defaultPattern at the lowest priority. A subclass can install its own default command to replace it; query the installed one with Subsystem.getDefaultCommand().
    • defaultTrigger

      public final Trigger defaultTrigger
      Active while the installed default command is the one running, which means no higher-priority pattern owns the subsystem.
    • purple

      public final Color purple
      Spectrum purple color constant (RGB 130, 103, 185).
    • white

      public final Color white
      Convenience alias for Color.kWhite.
  • Constructor Details

    • SpectrumLEDs

      public SpectrumLEDs(SpectrumLEDs.Config config)
      Configures the hardware (or reuses a shared device) and registers with the WPILib CommandScheduler.
      Parameters:
      config - device, segment range, and strip type for this instance
  • Method Details

    • isAttached

      public boolean isAttached()
    • isAnimating

      public boolean isAnimating()
      True when the most recently applied pattern was a hardware animation running in firmware, false when it was a software SolidColor pattern.
    • getCurrentCommandName

      public String getCurrentCommandName()
      Name of the command currently applying a pattern, or "None" when none is running.
    • checkPriority

      public Trigger checkPriority(int priority)
      Returns a trigger that is true while the running pattern command's priority is at or below priority, for gating a lower-priority pattern out of the way of a higher-priority one.
    • setPattern

      public Command setPattern(SpectrumLEDs.CANdlePattern pattern, int priority)
      Returns a command that applies pattern to the segment for as long as it runs and holds priority while it does. The command runs while the robot is disabled.

      Switching from a hardware animation to a software SolidColor pattern clears the active animation slots before the first software write.

      Parameters:
      pattern - pattern to apply each loop cycle
      priority - priority level to hold in commandPriority while this command runs
      Returns:
      a command that applies the pattern continuously
    • setPattern

      public Command setPattern(SpectrumLEDs.CANdlePattern pattern)
    • blink

      public SpectrumLEDs.CANdlePattern blink(Color color, double onTimeSecs)
      Blinks between color and off. Each half-cycle, on and off, lasts onTimeSecs seconds, so the frame rate is one over that.
      Parameters:
      color - the blink color
      onTimeSecs - seconds per on or off half-cycle
      Returns:
      a hardware animation SpectrumLEDs.CANdlePattern
    • breathe

      public SpectrumLEDs.CANdlePattern breathe(Color color, double periodSecs)
      Fades between color and off and back, over periodSecs. Each animation frame moves brightness by 1%, so a full cycle needs 200 frames and the frame rate is 200 over the period.
      Parameters:
      color - the peak color at full brightness
      periodSecs - seconds for one full breathe cycle
      Returns:
      a hardware animation SpectrumLEDs.CANdlePattern
    • rainbow

      public SpectrumLEDs.CANdlePattern rainbow()
      Rainbow advancing slowly across the strip.
    • rainbow

      public SpectrumLEDs.CANdlePattern rainbow(double brightness)
      Rainbow advancing slowly across the strip, at the given brightness.
      Parameters:
      brightness - brightness scalar, 0.0-1.0
    • scrollingRainbow

      public SpectrumLEDs.CANdlePattern scrollingRainbow()
      Rainbow advancing quickly across the strip.
    • chase

      public SpectrumLEDs.CANdlePattern chase(Color color, double speed)
      Lights one LED at a time across the strip and repeats. The frame rate is numLeds * speed, so speed full cycles happen per second.
      Parameters:
      color - the chase color
      speed - desired full strip cycles per second
      Returns:
      a hardware animation SpectrumLEDs.CANdlePattern
    • bounce

      public SpectrumLEDs.CANdlePattern bounce(Color color, double durationSecs)
      Bouncing dot that travels back and forth along the strip.
      Parameters:
      color - the dot color
      durationSecs - seconds for one complete back-and-forth cycle
      Returns:
      a hardware animation SpectrumLEDs.CANdlePattern
    • fire

      Fire animation from the CANdle's own animation engine.
    • rgbCycle

      public SpectrumLEDs.CANdlePattern rgbCycle()
      RGB color cycle from the CANdle's own animation engine.
    • solid

      public SpectrumLEDs.CANdlePattern solid(Color color)
      Solid color, from a single SolidColor control request resent each loop.
      Parameters:
      color - the color to display
      Returns:
      a software SpectrumLEDs.CANdlePattern showing a constant solid color
    • stripe

      public SpectrumLEDs.CANdlePattern stripe(double percent, Color color1, Color color2)
      Two-color stripe: the first percent fraction of LEDs shows color1 and the rest shows color2, sent as two SolidColor controls.
      Parameters:
      percent - fraction of the strip (0.0-1.0) assigned to color1
      color1 - color for the leading segment
      color2 - color for the trailing segment
      Returns:
      a software SpectrumLEDs.CANdlePattern showing the two-color stripe
    • gradient

      public SpectrumLEDs.CANdlePattern gradient(Color color1, Color color2)
      Linear gradient from color1 to color2 across the segment, one SolidColor per LED, computed at first use.
      Parameters:
      color1 - color at the start (index 0) of the segment
      color2 - color at the end of the segment
      Returns:
      a software SpectrumLEDs.CANdlePattern showing the two-color gradient
    • edges

      public SpectrumLEDs.CANdlePattern edges(Color color, int length)
      Lights the first and last length LEDs with color and turns the middle off, with two or three SolidColor controls.
      Parameters:
      color - the color to apply to the edge LEDs
      length - the number of LEDs to illuminate at each end of the strip
      Returns:
      a software SpectrumLEDs.CANdlePattern showing lit edges and a dark center
    • ombre

      public SpectrumLEDs.CANdlePattern ombre(Color startColor, Color endColor)
      Ombre that blends between the two colors and scrolls the blend point along the strip over time. The colors are rebuilt every loop because RGBWColor is immutable.
      Parameters:
      startColor - the leading color
      endColor - the trailing color
      Returns:
      a software SpectrumLEDs.CANdlePattern showing the animated ombre
    • wave

      public SpectrumLEDs.CANdlePattern wave(Color c1, Color c2, double cycleLength, double durationSecs)
      Sine wave blending between the two colors, one SolidColor per LED.
      Parameters:
      c1 - first wave color
      c2 - second wave color
      cycleLength - number of LEDs per wave period
      durationSecs - period of the time-based animation in seconds
      Returns:
      a software SpectrumLEDs.CANdlePattern showing the wave
    • countdown

      public SpectrumLEDs.CANdlePattern countdown(DoubleSupplier countStartTimeSec, double durationInSeconds)
      Counts down by turning LEDs off from the end of the segment toward the start, while the color fades from yellow to red.
      Parameters:
      countStartTimeSec - supplies the FPGA timestamp (seconds) when the countdown began
      durationInSeconds - total countdown duration in seconds
      Returns:
      a software SpectrumLEDs.CANdlePattern showing the countdown
    • switchCountdown

      public SpectrumLEDs.CANdlePattern switchCountdown(Color startingColor)
      Alliance switch countdown. Colors follow a hard-coded match-time schedule, and LEDs turn off within each segment as its time runs out.

      Seconds remaining, and the color shown:

        140-130  purple
        130-105  startingColor
        105-80   opponent color
         80-55   startingColor
         55-30   opponent color
         30-0    purple
       
      Parameters:
      startingColor - the alliance color shown during this robot's segments
      Returns:
      a software SpectrumLEDs.CANdlePattern reflecting the current switch-countdown state
    • getConfig

      public SpectrumLEDs.Config getConfig()
    • getCandle

      public CANdle getCandle()
      The CANdle this instance drives, either owned or shared.
    • getCommandPriority

      public int getCommandPriority()
      Priority of the pattern command currently running; setPattern(CANdlePattern, int) stores this while a command runs and resets it to -1 when the command ends.
    • setCommandPriority

      public void setCommandPriority(int commandPriority)
      Priority of the pattern command currently running; setPattern(CANdlePattern, int) stores this while a command runs and resets it to -1 when the command ends.