forked from pixelmatix/AnimatedGIFs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnimatedGIFs.ino
378 lines (322 loc) · 12.6 KB
/
AnimatedGIFs.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Animated GIFs Display Code for SmartMatrix and 32x32 RGB LED Panels
*
* Uses SmartMatrix Library for Teensy 3.1 written by Louis Beaudoin at pixelmatix.com
*
* Written by: Craig A. Lindley
*
* Copyright (c) 2014 Craig A. Lindley
* Refactoring by Louis Beaudoin (Pixelmatix)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* This example displays 32x32 GIF animations loaded from a SD Card connected to the Teensy 3.1
* The GIFs can be up to 32 pixels in width and height.
* This code has been tested with 32x32 pixel and 16x16 pixel GIFs, but is optimized for 32x32 pixel GIFs.
*
* Wiring is on the default Teensy 3.1 SPI pins, and chip select can be on any GPIO,
* set by defining SD_CS in the code below
* Function | Pin
* DOUT | 11
* DIN | 12
* CLK | 13
* CS (default) | 15
*
* Wiring for ESP32 follows the default for the ESP32 SD Library, see: https://github.com/espressif/arduino-esp32/tree/master/libraries/SDis is on the default Teensy 3.1 SPI pins, and chip select can be on any GPIO,
*
* This code first looks for .gif files in the /gifs/ directory
* (customize below with the GIF_DIRECTORY definition) then plays random GIFs in the directory,
* looping each GIF for DISPLAY_TIME_SECONDS
*
* This example is meant to give you an idea of how to add GIF playback to your own sketch.
* For a project that adds GIF playback with other features, take a look at
* Light Appliance and Aurora:
* https://github.com/CraigLindley/LightAppliance
* https://github.com/pixelmatix/aurora
*
* If you find any GIFs that won't play properly, please attach them to a new
* Issue post in the GitHub repo here:
* https://github.com/pixelmatix/AnimatedGIFs/issues
*/
/*
* CONFIGURATION:
* - If you're using SmartLED Shield V4 (or above), uncomment the line that includes <SmartMatrixShieldV4.h>
* - update the "SmartMatrix configuration and memory allocation" section to match the width and height and other configuration of your display
* - Note for 128x32 and 64x64 displays with Teensy 3.2 - need to reduce RAM:
* set kRefreshDepth=24 and kDmaBufferRows=2 or set USB Type: "None" in Arduino,
* decrease refreshRate in setup() to 90 or lower to get good an accurate GIF frame rate
* - Set the chip select pin for your board. On Teensy 3.5/3.6, the onboard microSD CS pin is "BUILTIN_SDCARD"
* - For ESP32 and large panels, you don't need to lower the refreshRate, but you can lower the frameRate (number of times the refresh buffer
* is updaed with new data per second), giving more time for the CPU to decode the GIF.
* Use matrix.setMaxCalculationCpuPercentage() or matrix.setCalcRefreshRateDivider()
*/
//#define NEOMATRIX
//#define DEBUGLINE 6
// if you want to display a file and display that one first
#define FIRSTINDEX 0
#ifdef NEOMATRIX
#include "neomatrix_config.h"
#define rgb24 CRGB
#else
#define ENABLE_SCROLLING 1
#if defined (ARDUINO)
//#include <SmartLEDShieldV4.h> // uncomment this line for SmartLED Shield V4 (needs to be before #include <SmartMatrix3.h>)
#include <SmartMatrix3.h>
#elif defined (SPARK)
#include "application.h"
#include "SmartMatrix3_Photon_Apa102/SmartMatrix3_Photon_Apa102.h"
#endif
#endif
#include <SD.h>
#include "GifDecoder.h"
#include "FilenameFunctions.h"
#define DISPLAY_TIME_SECONDS 10
// range 0-255
const int defaultBrightness = 255;
const rgb24 COLOR_BLACK = {
0, 0, 0 };
/* SmartMatrix configuration and memory allocation */
#define COLOR_DEPTH 24 // known working: 24, 48 - If the sketch uses type `rgb24` directly, COLOR_DEPTH must be 24
const uint8_t kMatrixWidth = 32; // known working: 32, 64, 96, 128
const uint8_t kMatrixHeight = 32; // known working: 16, 32, 48, 64
#ifndef NEOMATRIX
const uint8_t kRefreshDepth = 36; // known working: 24, 36, 48
const uint8_t kDmaBufferRows = 2; // known working: 2-4
const uint8_t kPanelType = SMARTMATRIX_HUB75_32ROW_MOD16SCAN; // use SMARTMATRIX_HUB75_16ROW_MOD8SCAN for common 16x32 panels
const uint8_t kMatrixOptions = (SMARTMATRIX_OPTIONS_NONE); // see http://docs.pixelmatix.com/SmartMatrix for options
const uint8_t kBackgroundLayerOptions = (SM_BACKGROUND_OPTIONS_NONE);
const uint8_t kScrollingLayerOptions = (SM_SCROLLING_OPTIONS_NONE);
SMARTMATRIX_ALLOCATE_BUFFERS(matrix, kMatrixWidth, kMatrixHeight, kRefreshDepth, kDmaBufferRows, kPanelType, kMatrixOptions);
SMARTMATRIX_ALLOCATE_BACKGROUND_LAYER(backgroundLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kBackgroundLayerOptions);
#if ENABLE_SCROLLING == 1
SMARTMATRIX_ALLOCATE_SCROLLING_LAYER(scrollingLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kScrollingLayerOptions);
#endif
#endif
/* template parameters are maxGifWidth, maxGifHeight, lzwMaxBits
*
* The lzwMaxBits value of 12 supports all GIFs, but uses 16kB RAM
* lzwMaxBits can be set to 10 or 11 for small displays, 12 for large displays
* All 32x32-pixel GIFs tested work with 11, most work with 10
*/
GifDecoder<kMatrixWidth, kMatrixHeight, 12> decoder;
// Chip select for SD card on the SmartMatrix Shield or Photon
// Teensy 3.5/3.6
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)
#define SD_CS BUILTIN_SDCARD
#elif defined(ESP32)
#define SD_CS 5
#elif defined (ARDUINO)
#define SD_CS 15
//#define SD_CS BUILTIN_SDCARD
#elif defined (SPARK)
#define SD_CS SS
#endif
#if defined(ESP32)
// ESP32 SD Library can't handle a trailing slash in the directory name
#define GIF_DIRECTORY "/gifs"
#else
// Teensy SD Library requires a trailing slash in the directory name
#define GIF_DIRECTORY "/gifs/"
#endif
int num_files;
void screenClearCallback(void) {
#ifdef NEOMATRIX
matrix_clear();
#else
backgroundLayer.fillScreen({0,0,0});
#endif
}
void updateScreenCallback(void) {
#ifdef NEOMATRIX
matrix_show();
#else
backgroundLayer.swapBuffers();
#endif
}
void drawPixelCallback(int16_t x, int16_t y, uint8_t red, uint8_t green, uint8_t blue) {
#ifdef NEOMATRIX
CRGB color = CRGB(matrix->gamma[red], matrix->gamma[green], matrix->gamma[blue]);
// This works but does not handle out of bounds pixels well (it writes to the last pixel)
// matrixleds[XY(x+OFFSETX,y+OFFSETY)] = color;
matrix->setPassThruColor(color.red*65536 + color.green*256 + color.blue);
// drawPixel ensures we don't write out of bounds
matrix->drawPixel(x+OFFSETX, y+OFFSETY, 0);
#else
backgroundLayer.drawPixel(x, y, {red, green, blue});
#endif
#if DEBUGLINE
if (y == DEBUGLINE) {
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(">");
Serial.print(red);
Serial.print(",");
Serial.print(green);
Serial.print(",");
Serial.print(blue);
Serial.println("");
}
#endif
}
// Setup method runs once, when the sketch starts
void setup() {
// Wait for teensy to be ready
delay(3000);
decoder.setScreenClearCallback(screenClearCallback);
decoder.setUpdateScreenCallback(updateScreenCallback);
decoder.setDrawPixelCallback(drawPixelCallback);
decoder.setFileSeekCallback(fileSeekCallback);
decoder.setFilePositionCallback(filePositionCallback);
decoder.setFileReadCallback(fileReadCallback);
decoder.setFileReadBlockCallback(fileReadBlockCallback);
// Seed the random number generator
randomSeed(analogRead(14));
Serial.begin(115200);
Serial.println("Starting AnimatedGIFs Sketch");
#ifdef NEOMATRIX
matrix_setup();
#else
// Initialize matrix
matrix.addLayer(&backgroundLayer);
#if ENABLE_SCROLLING == 1
matrix.addLayer(&scrollingLayer);
#endif
matrix.setBrightness(defaultBrightness);
// for large panels, may want to set the refresh rate lower to leave more CPU time to decoding GIFs (needed if GIFs are playing back slowly)
//matrix.setRefreshRate(90);
#if !defined(ESP32)
matrix.begin();
#endif
#if defined(ESP32)
// for large panels on ESP32, may want to set the max percentage time dedicated to updating the refresh frames lower, to leave more CPU time to decoding GIFs (needed if GIFs are playing back slowly)
//matrix.setMaxCalculationCpuPercentage(50);
// alternatively, for large panels on ESP32, may want to set the calculation refresh rate divider lower to leave more CPU time to decoding GIFs (needed if GIFs are playing back slowly) - this has the same effect as matrix.setMaxCalculationCpuPercentage() but is set with a different parameter
//matrix.setCalcRefreshRateDivider(4);
// The ESP32 SD Card library is going to want to malloc about 28000 bytes of DMA-capable RAM, make sure at least that much is left free
matrix.begin(28000);
#endif
// Clear screen
backgroundLayer.fillScreen(COLOR_BLACK);
backgroundLayer.swapBuffers(false);
#endif // NEOMATRIX
if(initSdCard(SD_CS) < 0) {
#if ENABLE_SCROLLING == 1
scrollingLayer.start("No SD card", -1);
#endif
Serial.println("No SD card");
while(1);
}
// for ESP32 we need to allocate SmartMatrix DMA buffers after initializing the SD card to avoid using up too much memory
// Determine how many animated GIF files exist
num_files = enumerateGIFFiles(GIF_DIRECTORY, true);
if(num_files < 0) {
#if ENABLE_SCROLLING == 1
scrollingLayer.start("No gifs directory", -1);
#endif
Serial.println("No gifs directory");
delay(100000000);
}
if(!num_files) {
#if ENABLE_SCROLLING == 1
scrollingLayer.start("Empty gifs directory", -1);
#endif
Serial.println("Empty gifs directory");
delay(100000000);
}
Serial.print("Index of files: 0 to ");
Serial.println(num_files);
}
void adjust_gamma(float change) {
matrix_gamma += change;
matrix->precal_gamma(matrix_gamma);
Serial.print("Change gamma to: ");
Serial.println(matrix_gamma);
}
void loop() {
static unsigned long lastTime = millis();
static int index = FIRSTINDEX;
static int8_t new_file = 1;
// allow stalling on a picture if requested
static uint32_t longer = 0;
char readchar;
if (Serial.available()) readchar = Serial.read(); else readchar = 0;
switch(readchar) {
case 'n':
Serial.println("Serial => next");
new_file = 1;
index++;
break;
case 'p':
Serial.println("Serial => previous");
new_file = 1;
index--;
break;
case '+': adjust_gamma(+0.2); break;
case '-': adjust_gamma(-0.2); break;
// = allows staying on a single picture for up to 1H instead of a few seconds
case '=':
longer = longer?0:3600;
Serial.print("Image display time: ");
Serial.println(longer + DISPLAY_TIME_SECONDS);
break;
default:
// BUG: this does not work for index '0', just type '1', and 'p'
if (readchar) {
while ((readchar >= '0') && (readchar <= '9')) {
new_file = 10 * new_file + (readchar - '0');
readchar = 0;
if (Serial.available()) readchar = Serial.read();
}
if (new_file) {
Serial.print("Got new file via serial ");
Serial.println(new_file);
index = new_file;
} else {
Serial.print("Got serial char ");
Serial.println(readchar);
}
}
}
if (millis() - lastTime > ((DISPLAY_TIME_SECONDS + longer) * 1000)) {
new_file = 1;
index++;
}
if (new_file) {
new_file = 0;
lastTime = millis();
if (index >= num_files) index = 0;
if (index <= -1) index = num_files - 1;
Serial.print("Fetching file index #");
Serial.println(index);
if (openGifFilenameByIndex(GIF_DIRECTORY, index) >= 0) {
// Can clear screen for new animation here, but this might cause flicker with short animations
// matrix_clear();
decoder.startDecoding();
} else {
Serial.println("FATAL: failed to open file");
delay(1000000);
}
}
decoder.decodeFrame();
#if DEBUGLINE
delay(1000000);
#endif
}
// vim:sts=4:sw=4