[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:
Sam Carlberg
2024-06-05 22:41:10 -04:00
committed by GitHub
parent 5221069bcc
commit ad18fa62ee
19 changed files with 3610 additions and 311 deletions

View File

@@ -1,52 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include <array>
#include <fmt/format.h>
#include <frc/AddressableLED.h>
#include <frc/simulation/AddressableLEDSim.h>
#include <frc/util/Color.h>
#include <frc/util/Color8Bit.h>
#include <gtest/gtest.h>
#include <hal/AddressableLEDTypes.h>
#include "Robot.h"
void AssertIndexColor(std::array<frc::AddressableLED::LEDData, 60> data,
int index, int hue, int saturation, int value);
TEST(RainbowTest, RainbowPattern) {
Robot robot;
robot.RobotInit();
frc::sim::AddressableLEDSim ledSim =
frc::sim::AddressableLEDSim::CreateForChannel(9);
EXPECT_TRUE(ledSim.GetRunning());
EXPECT_EQ(60, ledSim.GetLength());
auto rainbowFirstPixelHue = 0;
std::array<frc::AddressableLED::LEDData, 60> data;
for (int iteration = 0; iteration < 100; iteration++) {
SCOPED_TRACE(fmt::format("Iteration {} of 100", iteration));
robot.RobotPeriodic();
ledSim.GetData(data.data());
for (int i = 0; i < 60; i++) {
SCOPED_TRACE(fmt::format("LED {} of 60", i));
auto hue = (rainbowFirstPixelHue + (i * 180 / 60)) % 180;
AssertIndexColor(data, i, hue, 255, 128);
}
rainbowFirstPixelHue += 3;
rainbowFirstPixelHue %= 180;
}
}
void AssertIndexColor(std::array<frc::AddressableLED::LEDData, 60> data,
int index, int hue, int saturation, int value) {
frc::Color8Bit color{frc::Color::FromHSV(hue, saturation, value)};
EXPECT_EQ(0, data[index].padding);
EXPECT_EQ(color.red, data[index].r & 0xFF);
EXPECT_EQ(color.green, data[index].g & 0xFF);
EXPECT_EQ(color.blue, data[index].b & 0xFF);
}

View File

@@ -1,16 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include <gtest/gtest.h>
#include <hal/HALBase.h>
/**
* Runs all unit tests.
*/
int main(int argc, char** argv) {
HAL_Initialize(500, 0);
::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
return ret;
}