NeoPixelBufferShader.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // NeoPixelBufferShader
  2. // This example will provide a shader class to the NeoPixelBuffer that will dim and brighten
  3. // the pixels that are in the buffer (a device dependent 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 buffer object,
  11. // defined to use memory with the same feature as the strip
  12. // initialized with the same number of pixels as our strip
  13. NeoBuffer<NeoBufferMethod<NeoGrbFeature>> image(8,8,NULL);
  14. const RgbColor BrightRed(255, 0, 0);
  15. const RgbColor BrightGreen(0, 255, 0);
  16. const RgbColor BrightBlue(0, 0, 255);
  17. const RgbColor BrightYellow(255, 255, 0);
  18. const RgbColor BrightCyan(0, 255, 255);
  19. const RgbColor BrightPurple(255, 0, 255);
  20. const RgbColor DarkRed(32, 0, 0);
  21. const RgbColor DarkGreen(0, 32, 0);
  22. const RgbColor DarkBlue(0, 0, 32);
  23. const RgbColor DarkYellow(32, 32, 0);
  24. const RgbColor DarkCyan(0, 32, 32);
  25. const RgbColor DarkPurple(32, 0, 32);
  26. const RgbColor White(255);
  27. const RgbColor Black(0);
  28. // define a custom shader object that provides brightness support
  29. // based upon the NeoBitsBase
  30. template<typename T_COLOR_FEATURE> class BrightnessShader : public NeoBitsBase
  31. {
  32. public:
  33. BrightnessShader():
  34. NeoBitsBase(),
  35. _brightness(255) // default to full bright
  36. {}
  37. // required for a shader object, it will be called for
  38. // every pixel
  39. void Apply(uint16_t index, uint8_t* pDest, uint8_t* pSrc)
  40. {
  41. // we don't care what the index is so we ignore it
  42. //
  43. // to apply our brightness shader,
  44. // use the source color, modify, and apply to the destination
  45. // for every byte in the pixel,
  46. // scale the source value by the brightness and
  47. // store it in the destination byte
  48. const uint8_t* pSrcEnd = pSrc + T_COLOR_FEATURE::PixelSize;
  49. while (pSrc != pSrcEnd)
  50. {
  51. *pDest++ = (*pSrc++ * (uint16_t(_brightness) + 1)) >> 8;
  52. }
  53. }
  54. // provide an accessor to set brightness
  55. void setBrightness(uint8_t brightness)
  56. {
  57. _brightness = brightness;
  58. Dirty(); // must call dirty when a property changes
  59. }
  60. // provide an accessor to get brightness
  61. uint8_t getBrightness()
  62. {
  63. return _brightness;
  64. }
  65. private:
  66. uint8_t _brightness;
  67. };
  68. // create an instance of our shader object with the same feature as our buffer
  69. BrightnessShader<NeoGrbFeature> shader;
  70. // some dimming tracking variables and settings
  71. int8_t delta;
  72. void setup()
  73. {
  74. Serial.begin(115200);
  75. while (!Serial); // wait for serial attach
  76. Serial.println();
  77. Serial.println("Initializing...");
  78. Serial.flush();
  79. // this resets all the neopixels to an off state
  80. strip.Begin();
  81. strip.Show();
  82. // dibs do not default to any color,
  83. // so clear it to black if you aren't going to draw
  84. // into every pixel
  85. image.ClearTo(Black);
  86. // draw a pattern into the image
  87. uint8_t x = 0;
  88. uint8_t y = 0;
  89. image.SetPixelColor(x++, y, DarkRed);
  90. image.SetPixelColor(x++, y, DarkYellow);
  91. image.SetPixelColor(x++, y, DarkGreen);
  92. image.SetPixelColor(x++, y, DarkCyan);
  93. image.SetPixelColor(x++, y, DarkBlue);
  94. image.SetPixelColor(x++, y, DarkPurple);
  95. x = 0;
  96. y = 1;
  97. image.SetPixelColor(x++, y, BrightRed);
  98. image.SetPixelColor(x++, y, BrightYellow);
  99. image.SetPixelColor(x++, y, BrightGreen);
  100. image.SetPixelColor(x++, y, BrightCyan);
  101. image.SetPixelColor(x++, y, BrightBlue);
  102. image.SetPixelColor(x++, y, BrightPurple);
  103. Serial.println();
  104. Serial.println("Running...");
  105. delta = -1; // start by dimming downward
  106. }
  107. void loop()
  108. {
  109. // we increment by delta every 30ms
  110. delay(30);
  111. // update the brightness in shader
  112. //
  113. uint8_t brightness = shader.getBrightness();
  114. // check if we flip directions
  115. if (brightness == 0)
  116. {
  117. delta = 1; // increment
  118. }
  119. else if (brightness == 255)
  120. {
  121. delta = -1; // decrement
  122. }
  123. // modify and apply
  124. brightness += delta;
  125. shader.setBrightness(brightness);
  126. Serial.println(brightness);
  127. // render the image using the shader and then call Show()
  128. // these two should be called together in order
  129. //
  130. // need to provide the type of color feature for the strip and
  131. // the type of our custom shader
  132. image.Render<BrightnessShader<NeoGrbFeature>>(strip, shader);
  133. strip.Show();
  134. }