mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpilib] Add LED pattern API for easily animating addressable LEDs (#6344)
Add LEDReader and LEDWriter helper interfaces to facilitate composing simple patterns into more complex ones, e.g. LEDPattern.solid(Color.kBlue).breathe(Seconds.of(0.75)). Pattern composition relies on changing out the write behavior; for example, offsetBy increments the indexes to write to; while blink will switch between playing a base pattern and turning off all the LEDs. Add a view class for splitting a single large buffer into smaller distinct sections, which is useful for dealing with long chained LED strips mounted on different parts of a robot. Views cannot be written directly to an LED strip (in fact, trying to do so won't even compile). Adds some utility methods to the Color class for interpolating between two colors, and support color representations with 32-bit integers to avoid object allocations. Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
@@ -4,15 +4,31 @@
|
||||
|
||||
package edu.wpi.first.wpilibj.examples.addressableled;
|
||||
|
||||
import static edu.wpi.first.units.Units.Meters;
|
||||
import static edu.wpi.first.units.Units.MetersPerSecond;
|
||||
|
||||
import edu.wpi.first.units.Distance;
|
||||
import edu.wpi.first.units.Measure;
|
||||
import edu.wpi.first.wpilibj.AddressableLED;
|
||||
import edu.wpi.first.wpilibj.AddressableLEDBuffer;
|
||||
import edu.wpi.first.wpilibj.LEDPattern;
|
||||
import edu.wpi.first.wpilibj.TimedRobot;
|
||||
|
||||
public class Robot extends TimedRobot {
|
||||
private AddressableLED m_led;
|
||||
private AddressableLEDBuffer m_ledBuffer;
|
||||
// Store what the last hue of the first pixel is
|
||||
private int m_rainbowFirstPixelHue;
|
||||
|
||||
// Create an LED pattern that will display a rainbow across
|
||||
// all hues at maximum saturation and half brightness
|
||||
private final LEDPattern m_rainbow = LEDPattern.rainbow(255, 128);
|
||||
|
||||
// Our LED strip has a density of 120 LEDs per meter
|
||||
private static final Measure<Distance> kLedSpacing = Meters.of(1 / 120.0);
|
||||
|
||||
// Create a new pattern that scrolls the rainbow pattern across the LED strip, moving at a speed
|
||||
// of 1 meter per second.
|
||||
private final LEDPattern m_scrollingRainbow =
|
||||
m_rainbow.scrollAtAbsoluteSpeed(MetersPerSecond.of(1), kLedSpacing);
|
||||
|
||||
@Override
|
||||
public void robotInit() {
|
||||
@@ -33,24 +49,9 @@ public class Robot extends TimedRobot {
|
||||
|
||||
@Override
|
||||
public void robotPeriodic() {
|
||||
// Fill the buffer with a rainbow
|
||||
rainbow();
|
||||
// Update the buffer with the rainbow animation
|
||||
m_scrollingRainbow.applyTo(m_ledBuffer);
|
||||
// Set the LEDs
|
||||
m_led.setData(m_ledBuffer);
|
||||
}
|
||||
|
||||
private void rainbow() {
|
||||
// For every pixel
|
||||
for (var i = 0; i < m_ledBuffer.getLength(); i++) {
|
||||
// Calculate the hue - hue is easier for rainbows because the color
|
||||
// shape is a circle so only one value needs to precess
|
||||
final var hue = (m_rainbowFirstPixelHue + (i * 180 / m_ledBuffer.getLength())) % 180;
|
||||
// Set the value
|
||||
m_ledBuffer.setHSV(i, hue, 255, 128);
|
||||
}
|
||||
// Increase by to make the rainbow "move"
|
||||
m_rainbowFirstPixelHue += 3;
|
||||
// Check bounds
|
||||
m_rainbowFirstPixelHue %= 180;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user