NeoPixelBitmap.ino 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // NeoPixelBuffer
  2. // This example will animate pixels using a bitmap stored on a SD card
  3. //
  4. //
  5. // This will demonstrate the use of the NeoBitmapFile object
  6. // NOTE: The images provided in the example directory should be copied to
  7. // the root of the SD card so the below code will find it.
  8. // NOTE: This sample and the included images were built for a 144 pixel strip so
  9. // running this with a smaller string may not look as interesting. Try providing
  10. // your own 24 bit bitmap for better results.
  11. #include <NeoPixelBus.h>
  12. #include <NeoPixelAnimator.h>
  13. #include <SPI.h>
  14. #include <SD.h>
  15. const int chipSelect = D8; // make sure to set this to your SD carder reader CS
  16. //typedef NeoGrbFeature MyPixelColorFeature;
  17. typedef NeoGrbwFeature MyPixelColorFeature;
  18. const uint16_t PixelCount = 144; // the sample images are meant for 144 pixels
  19. const uint16_t PixelPin = 2;
  20. const uint16_t AnimCount = 1; // we only need one
  21. NeoPixelBus<MyPixelColorFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  22. // for esp8266 omit the pin
  23. //NeoPixelBus<MyPixelColorFeature, Neo800KbpsMethod> strip(PixelCount);
  24. NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object
  25. // our NeoBitmapFile will use the same color feature as NeoPixelBus and
  26. // we want it to use the SD File object
  27. NeoBitmapFile<MyPixelColorFeature, File> image;
  28. uint16_t animState;
  29. void LoopAnimUpdate(const AnimationParam& param)
  30. {
  31. // wait for this animation to complete,
  32. // we are using it as a timer of sorts
  33. if (param.state == AnimationState_Completed)
  34. {
  35. // done, time to restart this position tracking animation/timer
  36. animations.RestartAnimation(param.index);
  37. // draw the complete row at animState to the complete strip
  38. image.Blt(strip, 0, 0, animState, image.Width());
  39. animState = (animState + 1) % image.Height(); // increment and wrap
  40. }
  41. }
  42. void setup() {
  43. Serial.begin(115200);
  44. while (!Serial); // wait for serial attach
  45. strip.Begin();
  46. strip.Show();
  47. Serial.print("Initializing SD card...");
  48. // see if the card is present and can be initialized:
  49. if (!SD.begin(chipSelect))
  50. {
  51. Serial.println("Card failed, or not present");
  52. // don't do anything more:
  53. return;
  54. }
  55. Serial.println("card initialized.");
  56. // open the file
  57. File bitmapFile = SD.open("strings.bmp");
  58. if (!bitmapFile)
  59. {
  60. Serial.println("File open fail, or not present");
  61. // don't do anything more:
  62. return;
  63. }
  64. // initialize the image with the file
  65. if (!image.Begin(bitmapFile))
  66. {
  67. Serial.println("File format fail, not a supported bitmap");
  68. // don't do anything more:
  69. return;
  70. }
  71. animState = 0;
  72. // we use the index 0 animation to time how often we rotate all the pixels
  73. animations.StartAnimation(0, 30, LoopAnimUpdate);
  74. }
  75. void loop() {
  76. // this is all that is needed to keep it running
  77. // and avoiding using delay() is always a good thing for
  78. // any timing related routines
  79. animations.UpdateAnimations();
  80. strip.Show();
  81. }