[wpilib] Allow LED pattern gradients to be discontinuous (#7174)

This commit is contained in:
Sam Carlberg
2024-10-11 01:05:33 -04:00
committed by GitHub
parent 0cfff31439
commit 2085ab3d47
5 changed files with 147 additions and 35 deletions

View File

@@ -263,7 +263,8 @@ LEDPattern LEDPattern::Steps(
return Steps(std::span{steps.begin(), steps.end()});
}
LEDPattern LEDPattern::Gradient(std::span<const Color> colors) {
LEDPattern LEDPattern::Gradient(GradientType type,
std::span<const Color> colors) {
if (colors.size() == 0) {
// no colors specified
return LEDPattern::Off();
@@ -273,11 +274,19 @@ LEDPattern LEDPattern::Gradient(std::span<const Color> colors) {
return LEDPattern::Solid(colors[0]);
}
return LEDPattern{[colors = std::vector(colors.begin(), colors.end())](
return LEDPattern{[type, colors = std::vector(colors.begin(), colors.end())](
auto data, auto writer) {
size_t numSegments = colors.size();
auto bufLen = data.size();
int ledsPerSegment = bufLen / numSegments;
int ledsPerSegment = 0;
switch (type) {
case kContinuous:
ledsPerSegment = bufLen / numSegments;
break;
case kDiscontinuous:
ledsPerSegment = (bufLen - 1) / (numSegments - 1);
break;
}
for (size_t led = 0; led < bufLen; led++) {
int colorIndex = (led / ledsPerSegment) % numSegments;
@@ -295,8 +304,9 @@ LEDPattern LEDPattern::Gradient(std::span<const Color> colors) {
}};
}
LEDPattern LEDPattern::Gradient(std::initializer_list<Color> colors) {
return Gradient(std::span{colors.begin(), colors.end()});
LEDPattern LEDPattern::Gradient(GradientType type,
std::initializer_list<Color> colors) {
return Gradient(type, std::span{colors.begin(), colors.end()});
}
LEDPattern LEDPattern::Rainbow(int saturation, int value) {

View File

@@ -315,29 +315,52 @@ class LEDPattern {
static LEDPattern Steps(
std::initializer_list<std::pair<double, Color>> steps);
/**
* Creates a pattern that displays a non-animated gradient of colors across
* the entire length of the LED strip. The gradient wraps around so the start
* and end of the strip are the same color, which allows the gradient to be
* modified with a scrolling effect with no discontinuities. Colors are evenly
* distributed along the full length of the LED strip.
*
* @param colors the colors to display in the gradient
* @return a motionless gradient pattern
*/
static LEDPattern Gradient(std::span<const Color> colors);
/** Types of gradients. */
enum GradientType {
/**
* A continuous gradient, where the gradient wraps around to allow for
* seamless scrolling effects.
*/
kContinuous,
/**
* A discontinuous gradient, where the first pixel is set to the first color
* of the gradient and the final pixel is set to the last color of the
* gradient. There is no wrapping effect, so scrolling effects will display
* an obvious seam.
*/
kDiscontinuous
};
/**
* Creates a pattern that displays a non-animated gradient of colors across
* the entire length of the LED strip. The gradient wraps around so the start
* and end of the strip are the same color, which allows the gradient to be
* modified with a scrolling effect with no discontinuities. Colors are evenly
* distributed along the full length of the LED strip.
* the entire length of the LED strip. Colors are evenly distributed along the
* full length of the LED strip. The gradient type is configured with the
* {@code type} parameter, allowing the gradient to be either continuous (no
* seams, good for scrolling effects) or discontinuous (a clear seam is
* visible, but the gradient applies to the full length of the LED strip
* without needing to use some space for wrapping).
*
* @param type the type of gradient (continuous or discontinuous)
* @param colors the colors to display in the gradient
* @return a motionless gradient pattern
*/
static LEDPattern Gradient(std::initializer_list<Color> colors);
static LEDPattern Gradient(GradientType type, std::span<const Color> colors);
/**
* Creates a pattern that displays a non-animated gradient of colors across
* the entire length of the LED strip. Colors are evenly distributed along the
* full length of the LED strip. The gradient type is configured with the
* {@code type} parameter, allowing the gradient to be either continuous (no
* seams, good for scrolling effects) or discontinuous (a clear seam is
* visible, but the gradient applies to the full length of the LED strip
* without needing to use some space for wrapping).
*
* @param type the type of gradient (continuous or discontinuous)
* @param colors the colors to display in the gradient
* @return a motionless gradient pattern
*/
static LEDPattern Gradient(GradientType type,
std::initializer_list<Color> colors);
/**
* Creates an LED pattern that displays a rainbow across the color wheel. The