Files
NeoPixelBus/examples/gamma/NeoPixelGammaDynamic/NeoPixelGammaDynamic.ino
T

103 lines
3.0 KiB
Arduino
Raw Normal View History

2023-03-16 13:10:43 -07:00
// NeoPixelGammaDynamic
2023-04-06 16:46:46 -07:00
// This example will display a timed series of color gradients with gamma correction
2016-03-20 10:06:58 -07:00
// and then without.
// If the last pixel is on, then the colors being shown are color corrected.
// It will show Red grandiant, Green grandiant, Blue grandiant, a White grandiant, and
// then repeat.
//
2023-03-16 13:10:43 -07:00
// This will demonstrate the use of the NeoGamma class with the
// NeoGammaDynamicTableMethod being used for a custom gamma table
2016-03-20 10:06:58 -07:00
//
//
#include <NeoPixelBus.h>
#include <NeoPixelAnimator.h>
const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip
const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
2023-03-16 13:10:43 -07:00
// for esp8266 the pin is ignored unless it is the bitbang method
2023-04-06 10:19:28 -07:00
NeoPixelBus<NeoGrbFeature, NeoWs2812xMethod> strip(PixelCount, PixelPin);
2016-03-20 10:06:58 -07:00
2023-03-16 13:10:43 -07:00
NeoGamma<NeoGammaDynamicTableMethod> colorGamma;
2016-03-20 10:06:58 -07:00
void DrawPixels(bool corrected, HslColor startColor, HslColor stopColor)
{
for (uint16_t index = 0; index < strip.PixelCount() - 1; index++)
{
float progress = index / static_cast<float>(strip.PixelCount() - 2);
2016-03-28 12:21:11 -07:00
RgbColor color = HslColor::LinearBlend<NeoHueBlendShortestDistance>(startColor, stopColor, progress);
2016-03-20 10:06:58 -07:00
if (corrected)
{
2016-03-20 11:20:50 -07:00
color = colorGamma.Correct(color);
2016-03-20 10:06:58 -07:00
}
strip.SetPixelColor(index, color);
}
// use the last pixel to indicate if we are showing corrected colors or not
if (corrected)
{
strip.SetPixelColor(strip.PixelCount() - 1, RgbColor(64));
}
else
{
strip.SetPixelColor(strip.PixelCount() - 1, RgbColor(0));
}
strip.Show();
}
2023-03-16 13:10:43 -07:00
float GammaCalc(float unitValue)
{
// we will use CieLab gamma equation for our custom table
return NeoEase::GammaCieLab(unitValue);
}
2016-03-20 10:06:58 -07:00
void setup()
{
2023-03-16 13:10:43 -07:00
// initialize the internal gamma table using our GammaCalc function
// if you plan on using 16bit element colors, add a second optional arg as `true`
NeoGammaDynamicTableMethod::Initialize(GammaCalc); // , true);
2016-03-20 10:06:58 -07:00
strip.Begin();
strip.Show();
}
void loop()
{
HslColor startColor;
HslColor stopColor;
// red color
startColor = HslColor(0.0f, 1.0f, 0.0f);
stopColor = HslColor(0.0f, 1.0f, 0.5f);
DrawPixels(true, startColor, stopColor);
delay(5000);
DrawPixels(false, startColor, stopColor);
delay(5000);
// green color
startColor = HslColor(0.33f, 1.0f, 0.0f);
stopColor = HslColor(0.33f, 1.0f, 0.5f);
DrawPixels(true, startColor, stopColor);
delay(5000);
DrawPixels(false, startColor, stopColor);
delay(5000);
// blue color
startColor = HslColor(0.66f, 1.0f, 0.0f);
stopColor = HslColor(0.66f, 1.0f, 0.5f);
DrawPixels(true, startColor, stopColor);
delay(5000);
DrawPixels(false, startColor, stopColor);
delay(5000);
// white color
startColor = HslColor(0.0f, 0.0f, 0.0f);
stopColor = HslColor(0.0f, 0.0f, 0.5f);
DrawPixels(true, startColor, stopColor);
delay(5000);
DrawPixels(false, startColor, stopColor);
delay(5000);
}