-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
animated_gif_sdcard_spiffs.ino
136 lines (117 loc) · 3.5 KB
/
animated_gif_sdcard_spiffs.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
#include <SD.h>
#include <SPI.h>
#include <FS.h>
#include <SPIFFS.h>
#include <TFT_eSPI.h> // Install this library with the Arduino Library Manager
// Don't forget to configure the driver for the display!
#include <AnimatedGIF.h> // Install this library with the Arduino Library Manager
#define SD_CS_PIN 12 // Chip Select Pin (CS) for SD card Reader
AnimatedGIF gif;
File gifFile; // Global File object for the GIF file
TFT_eSPI tft = TFT_eSPI();
const char *filename = "/x_wing.gif"; // Change to load other gif files in images/GIF
void setup()
{
Serial.begin(115200);
tft.begin();
tft.setRotation(2); // Adjust Rotation of your screen (0-3)
tft.fillScreen(TFT_BLACK);
// Initialize SD card
Serial.println("SD card initialization...");
if (!SD.begin(SD_CS_PIN))
{
Serial.println("SD card initialization failed!");
return;
}
// Initialize SPIFFS
Serial.println("Initialize SPIFFS...");
if (!SPIFFS.begin(true))
{
Serial.println("SPIFFS initialization failed!");
}
// Reformmating the SPIFFS to have space if a large GIF is loaded
// You could run out of SPIFFS storage space if you load more than one image or a large GIF
// You can also increase the SPIFFS storage space by changing the partition of the ESP32
//
Serial.println("Formatting SPIFFS...");
SPIFFS.format(); // This will erase all the files, change as needed, SPIFFs is non-volatile memory
Serial.println("SPIFFS formatted successfully.");
// Open GIF file from SD card
Serial.println("Openning GIF file from SD card...");
File sdFile = SD.open(filename);
if (!sdFile)
{
Serial.println("Failed to open GIF file from SD card!");
return;
}
// Create a file in SPIFFS to store the GIF
File spiffsFile = SPIFFS.open(filename, FILE_WRITE, true);
if (!spiffsFile)
{
Serial.println("Failed to copy GIF in SPIFFS!");
return;
}
// Read the GIF from SD card and write to SPIFFS
Serial.println("Copy GIF in SPIFFS...");
byte buffer[512];
while (sdFile.available())
{
int bytesRead = sdFile.read(buffer, sizeof(buffer));
spiffsFile.write(buffer, bytesRead);
}
spiffsFile.close();
sdFile.close();
// Initialize the GIF
Serial.println("Starting animation...");
gif.begin(BIG_ENDIAN_PIXELS);
}
void loop()
{
if (gif.open(filename, fileOpen, fileClose, fileRead, fileSeek, GIFDraw))
{
tft.startWrite(); // The TFT chip slect is locked low
while (gif.playFrame(true, NULL))
{
}
gif.close();
tft.endWrite(); // Release TFT chip select for the SD Card Reader
}
}
// Callbacks for file operations for the Animated GIF Lobrary
void *fileOpen(const char *filename, int32_t *pFileSize)
{
gifFile = SPIFFS.open(filename, FILE_READ);
*pFileSize = gifFile.size();
if (!gifFile)
{
Serial.println("Failed to open GIF file from SPIFFS!");
}
return &gifFile;
}
void fileClose(void *pHandle)
{
gifFile.close();
}
int32_t fileRead(GIFFILE *pFile, uint8_t *pBuf, int32_t iLen)
{
int32_t iBytesRead;
iBytesRead = iLen;
if ((pFile->iSize - pFile->iPos) < iLen)
iBytesRead = pFile->iSize - pFile->iPos;
if (iBytesRead <= 0)
return 0;
gifFile.seek(pFile->iPos);
int32_t bytesRead = gifFile.read(pBuf, iLen);
pFile->iPos += iBytesRead;
return bytesRead;
}
int32_t fileSeek(GIFFILE *pFile, int32_t iPosition)
{
if (iPosition < 0)
iPosition = 0;
else if (iPosition >= pFile->iSize)
iPosition = pFile->iSize - 1;
pFile->iPos = iPosition;
gifFile.seek(pFile->iPos);
return iPosition;
}