|
| 1 | +// |
| 2 | +// LOLRGB GIF player example |
| 3 | +// |
| 4 | +// Written by Larry Bank |
| 5 | +// |
| 6 | +// This Arduino example sketch demonstrates how to play animated GIF images |
| 7 | +// on the UnexpectedMaker 5x14 RGB LED shield. This code depends on two libraries |
| 8 | +// (both are available from the Arduino library manager), my AnimatedGIF library and |
| 9 | +// Adafruit's NeoPixel. |
| 10 | +// |
| 11 | +#include <AnimatedGIF.h> |
| 12 | +#include "rainbow_5x14.h" |
| 13 | + |
| 14 | +#include <Adafruit_NeoPixel.h> |
| 15 | + |
| 16 | +// Which pin on the Arduino is connected to the NeoPixels? |
| 17 | +#define PIN 14 // TinyPico + LOLRGB Shield use this GPIO pin |
| 18 | + |
| 19 | +// 5x14 = 70 |
| 20 | +#define NUMPIXELS 70 |
| 21 | +Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); |
| 22 | +AnimatedGIF gif; // static instance of the class uses 22.5K of RAM |
| 23 | + |
| 24 | +// Normal color values are too bright on the LED array, so reduce the values |
| 25 | +#define BRIGHT_SHIFT 3 |
| 26 | + |
| 27 | +// |
| 28 | +// GIF decoder callback function |
| 29 | +// called once per line as the image is decoded |
| 30 | +// |
| 31 | +void GIFDraw(GIFDRAW *pDraw) |
| 32 | +{ |
| 33 | +uint8_t r, g, b, *s, *p, *pPal = (uint8_t *)pDraw->pPalette; |
| 34 | +int x, y = pDraw->iY + pDraw->y; |
| 35 | + |
| 36 | + s = pDraw->pPixels; |
| 37 | + if (pDraw->ucDisposalMethod == 2) // restore to background color |
| 38 | + { |
| 39 | + p = &pPal[pDraw->ucBackground * 3]; |
| 40 | + r = p[0] >> BRIGHT_SHIFT; g = p[1] >> BRIGHT_SHIFT; b = p[2] >> BRIGHT_SHIFT; |
| 41 | + for (x=0; x<pDraw->iWidth; x++) |
| 42 | + { |
| 43 | + if (s[x] == pDraw->ucTransparent) { |
| 44 | + pixels.setPixelColor(((4-x)*14)+y, pixels.Color(r, g, b)); |
| 45 | + } |
| 46 | + } |
| 47 | + pDraw->ucHasTransparency = 0; |
| 48 | + } |
| 49 | + // Apply the new pixels to the main image |
| 50 | + if (pDraw->ucHasTransparency) // if transparency used |
| 51 | + { |
| 52 | + const uint8_t ucTransparent = pDraw->ucTransparent; |
| 53 | + for (x=0; x<pDraw->iWidth; x++) |
| 54 | + { |
| 55 | + if (s[x] != ucTransparent) { |
| 56 | + p = &pPal[s[x] * 3]; |
| 57 | + pixels.setPixelColor(((4-x)*14)+y, pixels.Color(p[0]>>BRIGHT_SHIFT, p[1]>>BRIGHT_SHIFT, p[2]>>BRIGHT_SHIFT)); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + else // no transparency, just copy them all |
| 62 | + { |
| 63 | + for (x=0; x<pDraw->iWidth; x++) |
| 64 | + { |
| 65 | + p = &pPal[s[x] * 3]; |
| 66 | + pixels.setPixelColor(((4-x)*14)+y, pixels.Color(p[0]>>BRIGHT_SHIFT, p[1]>>BRIGHT_SHIFT, p[2]>>BRIGHT_SHIFT)); |
| 67 | + } |
| 68 | + } |
| 69 | + if (pDraw->y == pDraw->iHeight-1) // last line has been decoded, display the image |
| 70 | + pixels.show(); |
| 71 | +} /* GIFDraw() */ |
| 72 | + |
| 73 | +void setup() { |
| 74 | + pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) |
| 75 | + pixels.clear(); |
| 76 | + gif.begin(GIF_PALETTE_RGB888); // request 24-bit palette |
| 77 | +} /* setup() */ |
| 78 | + |
| 79 | +void loop() { |
| 80 | + int rc; |
| 81 | + // This 13K GIF image is compiled with the sketch and read from FLASH memory |
| 82 | + rc = gif.open((uint8_t *)rainbow_5x14, sizeof(rainbow_5x14), GIFDraw); |
| 83 | + if (rc) { |
| 84 | + while (rc) { |
| 85 | + rc = gif.playFrame(true, NULL); // play a frame and pause for the correct amount of time |
| 86 | + } |
| 87 | + gif.close(); |
| 88 | + } |
| 89 | +} /* loop() */ |
0 commit comments