10000 setHSBPixel by pcbreflux · Pull Request #15 · nkolban/esp32-snippets · GitHub
[go: up one dir, main page]

Skip to content

setHSBPixel #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions cpp_utils/WS2812.cpp
E424
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,73 @@ void WS2812::setPixel(uint16_t index, uint32_t pixel) {
this->pixels[index].blue = (pixel & 0xff0000) >> 16;
} // setPixel

/**
* @brief Set the given pixel to the specified HSB color.
*
* The LEDs are not actually updated until a call to show().
*
* @param [in] index The pixel that is to have its color set.
* @param [in] hue The amount of hue in the pixel (0-360).
* @param [in] green The amount of saturation in the pixel (0-255).
* @param [in] blue The amount of brightnes in the pixel (0-255).
*/
void WS2812::setHSBPixel(uint16_t index, uint16_t hue, uint8_t saturation, uint8_t brightnes) {
double sat_red;
double sat_green;
double sat_blue;
double ctmp_red;
double ctmp_green;
double ctmp_blue;
double new_red;
double new_green;
double new_blue;
double dSaturation=(double)saturation/255;
double dBrightnes=(double)brightnes/255;

assert(index < pixelCount);

if (hue < 120) {
sat_red = (120 - hue) / 60.0;
sat_green = hue / 60.0;
sat_blue = 0;
} else if (hue < 240) {
sat_red = 0;
sat_green = (240 - hue) / 60.0;
sat_blue = (hue - 120) / 60.0;
} else {
sat_red = (hue - 240) / 60.0;
sat_green = 0;
sat_blue = (360 - hue) / 60.0;
}

if (sat_red>1.0) {
sat_red=1.0;
}
if (sat_green>1.0) {
sat_green=1.0;
}
if (sat_blue>1.0) {
sat_blue=1.0;
}

ctmp_red = 2 * dSaturation * sat_red + (1 - dSaturation);
ctmp_green = 2 * dSaturation * sat_green + (1 - dSaturation);
ctmp_blue = 2 * dSaturation * sat_blue + (1 - dSaturation);

if (dBrightnes < 0.5) {
new_red = dBrightnes * ctmp_red;
new_green = dBrightnes * ctmp_green;
new_blue = dBrightnes * ctmp_blue;
} else {
new_red = (1 - dBrightnes) * ctmp_red + 2 * dBrightnes - 1;
new_green = (1 - dBrightnes) * ctmp_green + 2 * dBrightnes - 1;
new_blue = (1 - dBrightnes) * ctmp_blue + 2 * dBrightnes - 1;
}

this->pixels[index].red = (uint8_t)(new_red*255);
this->pixels[index].green = (uint8_t)(new_green*255);
this->pixels[index].blue = (uint8_t)(new_blue*255);
} // setHSBPixel

/**
* @brief Clear all the pixel colors.
Expand Down
1 change: 1 addition & 0 deletions cpp_utils/WS2812.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class WS2812 {
void setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue);
void setPixel(uint16_t index, pixel_t pixel);
void setPixel(uint16_t index, uint32_t pixel);
void setHSBPixel(uint16_t index, uint16_t hue, uint8_t saturation, uint8_t brightnes);
void clear();
virtual ~WS2812();
private:
Expand Down
0