mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +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
@@ -92,3 +92,38 @@ void AddressableLED::Stop() {
|
||||
HAL_StopAddressableLEDOutput(m_handle, &status);
|
||||
wpi_setHALError(status);
|
||||
}
|
||||
|
||||
void AddressableLED::LEDData::SetHSV(int h, int s, int v) {
|
||||
if (s == 0) {
|
||||
SetRGB(v, v, v);
|
||||
return;
|
||||
}
|
||||
|
||||
int region = h / 30;
|
||||
int remainder = (h - (region * 30)) * 6;
|
||||
|
||||
int p = (v * (255 - s)) >> 8;
|
||||
int q = (v * (255 - ((s * remainder) >> 8))) >> 8;
|
||||
int t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
|
||||
|
||||
switch (region) {
|
||||
case 0:
|
||||
SetRGB(v, t, p);
|
||||
break;
|
||||
case 1:
|
||||
SetRGB(q, v, p);
|
||||
break;
|
||||
case 2:
|
||||
SetRGB(p, v, t);
|
||||
break;
|
||||
case 3:
|
||||
SetRGB(p, q, v);
|
||||
break;
|
||||
case 4:
|
||||
SetRGB(t, p, v);
|
||||
break;
|
||||
default:
|
||||
SetRGB(v, p, q);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,12 +35,25 @@ class AddressableLED : public ErrorBase {
|
||||
|
||||
/**
|
||||
* A helper method to set all values of the LED.
|
||||
*
|
||||
* @param r the r value [0-255]
|
||||
* @param g the g value [0-255]
|
||||
* @param b the b value [0-255]
|
||||
*/
|
||||
void SetLED(int r, int g, int b) {
|
||||
void SetRGB(int r, int g, int b) {
|
||||
this->r = r;
|
||||
this->g = g;
|
||||
this->b = b;
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper method to set all values of the LED.
|
||||
*
|
||||
* @param h the h value [0-180]
|
||||
* @param s the s value [0-255]
|
||||
* @param v the v value [0-255]
|
||||
*/
|
||||
void SetHSV(int h, int s, int v);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user