mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add HSV helpers to AddressableLED (#2135)
Also change the setLED() method to setRGB() for consistency and clarity. Add rainbow example to demonstrate HSV usage.
This commit is contained in:
committed by
Peter Johnson
parent
5e97c81d80
commit
f66ae59992
@@ -14,7 +14,8 @@ import edu.wpi.first.wpilibj.TimedRobot;
|
||||
public class Robot extends TimedRobot {
|
||||
private AddressableLED m_led;
|
||||
private AddressableLEDBuffer m_ledBuffer;
|
||||
private int m_count;
|
||||
// Store what the last hue of the first pixel is
|
||||
private int m_rainbowFirstPixelHue;
|
||||
|
||||
@Override
|
||||
public void robotInit() {
|
||||
@@ -22,36 +23,37 @@ public class Robot extends TimedRobot {
|
||||
// Must be a PWM header, not MXP or DIO
|
||||
m_led = new AddressableLED(0);
|
||||
|
||||
// Default to a length of 12, start empty output
|
||||
// Length is expensive to set, so only set it once, then just update data
|
||||
m_led.setLength(12);
|
||||
|
||||
// Reuse buffer
|
||||
m_ledBuffer = new AddressableLEDBuffer(12);
|
||||
// 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(60);
|
||||
m_led.setLength(m_ledBuffer.getLength());
|
||||
|
||||
// Set the data
|
||||
m_led.setData(m_ledBuffer);
|
||||
m_led.start();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void robotPeriodic() {
|
||||
// Zero out all LEDS
|
||||
for (int i = 0; i < m_ledBuffer.getLength(); i++) {
|
||||
m_ledBuffer.setLED(i, 0, 0, 0);
|
||||
}
|
||||
|
||||
// Set 1 single LED to red
|
||||
m_ledBuffer.setLED(m_count, 50, 0, 0);
|
||||
|
||||
// Continue moving LED
|
||||
m_count++;
|
||||
if (m_count >= 12) {
|
||||
m_count = 0;
|
||||
}
|
||||
|
||||
// Buffer must be written to update.
|
||||
// Fill the buffer with a rainbow
|
||||
rainbow();
|
||||
// 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