forked from MarcFork/AnimatedGIFs
-
Notifications
You must be signed in to change notification settings - Fork 13
/
FilenameFunctions_Impl.h
185 lines (152 loc) · 4.97 KB
/
FilenameFunctions_Impl.h
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
/*
* Animated GIFs Display Code for SmartMatrix and 32x32 RGB LED Panels
*
* This file contains code to enumerate and select animated GIF files by name
* Originally written by: Craig A. Lindley
* Largely reworked by Marc MERLIN <[email protected]> to support SPIFFS and FatFS
*/
#include "animatedgif_config.h"
File file;
// This can be used by the caller to retrieve the name by index
char pathname[128];
int numberOfFiles;
bool fileSeekCallback(unsigned long position) {
return file.seek(position);
}
unsigned long filePositionCallback(void) {
return file.position();
}
int fileReadCallback(void) {
return file.read();
}
int fileReadBlockCallback(void * buffer, int numberOfBytes) {
return file.read((uint8_t*)buffer, numberOfBytes);
}
#ifdef FSOSD
int initSdCard(int chipSelectPin) {
// initialize the SD card at full speed
pinMode(chipSelectPin, OUTPUT);
if (!SD.begin(chipSelectPin)) die("SD Begin failed");
return 0;
}
bool isAnimationFile(const char filename []) {
String filenameString(filename);
#else
bool isAnimationFile(String filenameString) {
// ESP32 filename includes the full path, so need to remove the path before looking at the filename
int pathindex = filenameString.lastIndexOf("/");
if(pathindex >= 0)
filenameString.remove(0, pathindex + 1);
#endif
//Serial.print(filenameString);
if ((filenameString[0] == '_') || (filenameString[0] == '~') || (filenameString[0] == '.')) {
Serial.println(" ignoring: leading _/~/. character");
return false;
}
filenameString.toUpperCase();
if (filenameString.endsWith(".GIF") != 1) {
Serial.println(" ignoring: doesn't end of .GIF");
return false;
}
return true;
}
// Enumerate and possibly display the animated GIF filenames in GIFS directory
int enumerateGIFFiles(const char *directoryName, boolean displayFilenames) {
numberOfFiles = 0;
Serial.print("Enumerate files in dir ");
Serial.println(directoryName);
#ifdef ESP8266
// ESP8266 SPIFFS uses special directory objects
Dir dir = SPIFFS.openDir(directoryName);
while (dir.next()) {
String filename = dir.fileName();
if (isAnimationFile(filename)) {
numberOfFiles++;
if (displayFilenames) Serial.println(filename);
}
}
#else
File directory = FSO.open(directoryName);
if (!directory) die("Can't open directory");
while (File file = directory.openNextFile()) {
if (isAnimationFile(file.name())) {
numberOfFiles++;
if (displayFilenames) Serial.println(file.name());
}
file.close();
}
directory.close();
#endif
return numberOfFiles;
}
// Get the full path/filename of the GIF file with specified index
void getGIFFilenameByIndex(const char *directoryName, int index, char *pnBuffer) {
// Make sure index is in range
if ((index < 0) || (index >= numberOfFiles)) return;
#ifndef FSOSD
#ifdef ESP32
File dir = FSO.open(directoryName);
while (File file = dir.openNextFile()) {
String filename = file.name();
if (isAnimationFile(filename)) {
filename.toCharArray(pnBuffer, 127);
if (!index) break;
index--;
}
}
#else
Dir dir = FSO.openDir(directoryName);
while (dir.next() && index >= 0) {
String filename = dir.fileName();
if (isAnimationFile(filename)) {
index--;
filename.toCharArray(pnBuffer, 127);
}
}
#endif
#else
File directory = SD.open(directoryName);
if (!directory) return;
File file = directory.openNextFile();
while (file && (index >= 0)) {
char* filename = (char*)file.name();
if (isAnimationFile(file.name())) {
index--;
#if !defined(ESP32)
// Copy the directory name into the pathname buffer -
// ESP32 SD Library includes the full path name in the filename, so no need to add the directory name
strcpy(pnBuffer, directoryName);
// Append the filename to the pathname
strcat(pnBuffer, filename);
#else
strcpy(pnBuffer, filename);
#endif
}
file.close();
file = directory.openNextFile();
}
directory.close();
#endif
Serial.print("Selected file ");
Serial.println(pnBuffer);
file.close();
}
int openGifFilenameByIndex(const char *directoryName, int index) {
getGIFFilenameByIndex(directoryName, index, pathname);
// Pathname: /gifs/32anim_balls.gif
//Serial.print("Pathname: ");
//Serial.println(pathname);
if (file) file.close();
// Attempt to open the file for reading
#ifdef FSOSPIFFS
if (! (file = SPIFFS.open(pathname, "r")) ) die ("Error opening GIF file");
#else
if (! (file = FSO.open(pathname)) ) die ("Error opening GIF file");
#endif
return 0;
}
// Return a random animated gif path/filename from the specified directory
void chooseRandomGIFFilename(const char *directoryName, char *pnBuffer) {
int index = random(numberOfFiles);
getGIFFilenameByIndex(directoryName, index, pnBuffer);
}