NeoPixelDibTest.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // NeoPixelDibTest
  2. // This example will provide a shader class to the NeoPixelDib that will dim and brighten
  3. // the pixels that are in the Dib (Device Independant Bitmap)
  4. //
  5. #include <NeoPixelBus.h>
  6. const uint16_t PixelCount = 64; // set this to the size of your strip
  7. const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
  8. // three element GRB pixels, change to your needs
  9. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  10. // the DIB object, using RgbColor and initialized with the same number of pixels as our strip
  11. NeoDib<RgbColor> image(PixelCount);
  12. const RgbColor BrightRed(255, 0, 0);
  13. const RgbColor BrightGreen(0, 255, 0);
  14. const RgbColor BrightBlue(0, 0, 255);
  15. const RgbColor BrightYellow(255, 255, 0);
  16. const RgbColor BrightCyan(0, 255, 255);
  17. const RgbColor BrightPurple(255, 0, 255);
  18. const RgbColor DarkRed(32, 0, 0);
  19. const RgbColor DarkGreen(0, 32, 0);
  20. const RgbColor DarkBlue(0, 0, 32);
  21. const RgbColor DarkYellow(32, 32, 0);
  22. const RgbColor DarkCyan(0, 32, 32);
  23. const RgbColor DarkPurple(32, 0, 32);
  24. const RgbColor White(255);
  25. const RgbColor Black(0);
  26. // define a custom shader object that provides brightness support
  27. // based upon the NeoBitsBase
  28. class BrightnessShader : public NeoBitsBase
  29. {
  30. public:
  31. BrightnessShader():
  32. NeoBitsBase(),
  33. _brightness(255) // default to full bright
  34. {}
  35. // required for a shader object, it will be called for
  36. // every pixel
  37. RgbColor Apply(uint16_t index, RgbColor original)
  38. {
  39. // we don't care what the index is so we ignore it
  40. //
  41. // to apply our brightness shader, modify the original color and return the color we want
  42. // blend from black (_brightness == 0.0) to the original color (_brightness == 1.0)
  43. return RgbColor::LinearBlend(Black, original, (float)_brightness / 255.0f);
  44. }
  45. // provide an accessor to set brightness
  46. void setBrightness(uint8_t brightness)
  47. {
  48. _brightness = brightness;
  49. Dirty(); // must call dirty when a property changes
  50. }
  51. // provide an accessor to get brightness
  52. uint8_t getBrightness()
  53. {
  54. return _brightness;
  55. }
  56. private:
  57. uint8_t _brightness;
  58. };
  59. // create an instance of our shader object
  60. BrightnessShader shader;
  61. // some dimming tracking variables and settings
  62. int8_t delta;
  63. void setup()
  64. {
  65. Serial.begin(115200);
  66. while (!Serial); // wait for serial attach
  67. Serial.println();
  68. Serial.println("Initializing...");
  69. Serial.flush();
  70. // this resets all the neopixels to an off state
  71. strip.Begin();
  72. strip.Show();
  73. // dibs do not default to any color,
  74. // so clear it to black if you aren't going to draw
  75. // into every pixel
  76. image.ClearTo(Black);
  77. // draw a pattern into the image
  78. uint8_t index = 0;
  79. image.SetPixelColor(index++, DarkRed);
  80. image.SetPixelColor(index++, DarkYellow);
  81. image.SetPixelColor(index++, DarkGreen);
  82. image.SetPixelColor(index++, DarkCyan);
  83. image.SetPixelColor(index++, DarkBlue);
  84. image.SetPixelColor(index++, DarkPurple);
  85. image.SetPixelColor(index++, Black);
  86. image.SetPixelColor(index++, Black);
  87. image.SetPixelColor(index++, BrightRed);
  88. image.SetPixelColor(index++, BrightYellow);
  89. image.SetPixelColor(index++, BrightGreen);
  90. image.SetPixelColor(index++, BrightCyan);
  91. image.SetPixelColor(index++, BrightBlue);
  92. image.SetPixelColor(index++, BrightPurple);
  93. Serial.println();
  94. Serial.println("Running...");
  95. delta = -1; // start by dimming downward
  96. }
  97. void loop()
  98. {
  99. // we increment by delta every 30ms
  100. delay(30);
  101. // update the brightness in shader
  102. //
  103. uint8_t brightness = shader.getBrightness();
  104. // check if we flip directions
  105. if (brightness == 0)
  106. {
  107. delta = 1; // increment
  108. }
  109. else if (brightness == 255)
  110. {
  111. delta = -1; // decrement
  112. }
  113. // modify and apply
  114. brightness += delta;
  115. shader.setBrightness(brightness);
  116. Serial.println(brightness);
  117. // render the image using the shader and then call Show()
  118. // these two should be called together in order
  119. //
  120. // need to provide the type of color feature for the strip and
  121. // the type of our custom shader
  122. image.Render<NeoGrbFeature, BrightnessShader>(strip, shader);
  123. strip.Show();
  124. }