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:
Austin Shalit
2019-11-29 15:16:57 -08:00
committed by Peter Johnson
parent 5e97c81d80
commit f66ae59992
6 changed files with 202 additions and 45 deletions

View File

@@ -12,35 +12,44 @@
#include <frc/smartdashboard/SmartDashboard.h>
class Robot : public frc::TimedRobot {
static constexpr int kLength = 60;
// PWM port 0
// Must be a PWM header, not MXP or DIO
frc::AddressableLED m_led{0};
std::array<frc::AddressableLED::LEDData, 12> m_ledBuffer; // Reuse the buffer
int m_count = 0;
frc::AddressableLED m_led{9};
std::array<frc::AddressableLED::LEDData, kLength>
m_ledBuffer; // Reuse the buffer
// Store what the last hue of the first pixel is
int firstPixelHue = 0;
public:
void Rainbow() {
// For every pixel
for (int i = 0; i < kLength; i++) {
// Calculate the hue - hue is easier for rainbows because the color
// shape is a circle so only one value needs to precess
const auto pixelHue = (firstPixelHue + (i * 180 / kLength)) % 180;
// Set the value
m_ledBuffer[i].SetHSV(pixelHue, 255, 128);
}
// Increase by to make the rainbow "move"
firstPixelHue += 3;
// Check bounds
firstPixelHue %= 180;
}
void RobotInit() override {
// Default to a length of 12, start empty output
// Default to a length of 60, start empty output
// Length is expensive to set, so only set it once, then just update data
m_led.SetLength(12);
m_led.SetLength(kLength);
m_led.SetData(m_ledBuffer);
m_led.Start();
}
void RobotPeriodic() override {
// Zero out all LEDs
for (auto& ledData : m_ledBuffer) {
ledData.SetLED(0, 0, 0);
}
// Set 1 single LED to red
m_ledBuffer[m_count].SetLED(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);
}
};