NeoPixelGamma.ino 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // NeoPixelGamma
  2. // This example will display a timed series of color gradiants with gamma correction
  3. // and then without.
  4. // If the last pixel is on, then the colors being shown are color corrected.
  5. // It will show Red grandiant, Green grandiant, Blue grandiant, a White grandiant, and
  6. // then repeat.
  7. //
  8. // This will demonstrate the use of the NeoGamma class
  9. //
  10. //
  11. #include <NeoPixelBus.h>
  12. #include <NeoPixelAnimator.h>
  13. const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip
  14. const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
  15. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  16. // for esp8266 omit the pin
  17. //NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount);
  18. // uncomment only one of these to compare memory use or speed
  19. //
  20. // NeoGamma<NeoGammaEquationMethod> colorGamma;
  21. NeoGamma<NeoGammaTableMethod> colorGamma;
  22. void DrawPixels(bool corrected, HslColor startColor, HslColor stopColor)
  23. {
  24. for (uint16_t index = 0; index < strip.PixelCount() - 1; index++)
  25. {
  26. float progress = index / static_cast<float>(strip.PixelCount() - 2);
  27. RgbColor color = HslColor::LinearBlend<NeoHueBlendShortestDistance>(startColor, stopColor, progress);
  28. if (corrected)
  29. {
  30. color = colorGamma.Correct(color);
  31. }
  32. strip.SetPixelColor(index, color);
  33. }
  34. // use the last pixel to indicate if we are showing corrected colors or not
  35. if (corrected)
  36. {
  37. strip.SetPixelColor(strip.PixelCount() - 1, RgbColor(64));
  38. }
  39. else
  40. {
  41. strip.SetPixelColor(strip.PixelCount() - 1, RgbColor(0));
  42. }
  43. strip.Show();
  44. }
  45. void setup()
  46. {
  47. strip.Begin();
  48. strip.Show();
  49. }
  50. void loop()
  51. {
  52. HslColor startColor;
  53. HslColor stopColor;
  54. // red color
  55. startColor = HslColor(0.0f, 1.0f, 0.0f);
  56. stopColor = HslColor(0.0f, 1.0f, 0.5f);
  57. DrawPixels(true, startColor, stopColor);
  58. delay(5000);
  59. DrawPixels(false, startColor, stopColor);
  60. delay(5000);
  61. // green color
  62. startColor = HslColor(0.33f, 1.0f, 0.0f);
  63. stopColor = HslColor(0.33f, 1.0f, 0.5f);
  64. DrawPixels(true, startColor, stopColor);
  65. delay(5000);
  66. DrawPixels(false, startColor, stopColor);
  67. delay(5000);
  68. // blue color
  69. startColor = HslColor(0.66f, 1.0f, 0.0f);
  70. stopColor = HslColor(0.66f, 1.0f, 0.5f);
  71. DrawPixels(true, startColor, stopColor);
  72. delay(5000);
  73. DrawPixels(false, startColor, stopColor);
  74. delay(5000);
  75. // white color
  76. startColor = HslColor(0.0f, 0.0f, 0.0f);
  77. stopColor = HslColor(0.0f, 0.0f, 0.5f);
  78. DrawPixels(true, startColor, stopColor);
  79. delay(5000);
  80. DrawPixels(false, startColor, stopColor);
  81. delay(5000);
  82. }