forked from techy101/LED-Tophat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilenameFunctions.cpp
98 lines (73 loc) · 2.29 KB
/
FilenameFunctions.cpp
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
/*
* Animated GIFs Display Code for SmartMatrix and 32x32 RGB LED Panels
*
* This file contains code to enumerate and select animated GIF files by name
*
* Written by: Craig A. Lindley
*/
#include <SD.h>
int numberOfFiles;
bool isAnimationFile(const char filename []) {
if (filename[0] == '_')
return false;
if (filename[0] == '~')
return false;
if (filename[0] == '.')
return false;
String filenameString = String(filename).toUpperCase();
if (filenameString.endsWith(".GIF") != 1)
return false;
return true;
}
// Enumerate and possibly display the animated GIF filenames in GIFS directory
int enumerateGIFFiles(const char *directoryName, boolean displayFilenames) {
numberOfFiles = 0;
File directory = SD.open(directoryName);
if (!directory) {
return -1;
}
File file = directory.openNextFile();
while (file) {
if (isAnimationFile(file.name())) {
numberOfFiles++;
if (displayFilenames) {
Serial.println(file.name());
}
}
file.close();
file = directory.openNextFile();
}
file.close();
directory.close();
return numberOfFiles;
}
// Get the full path/filename of the GIF file with specified index
void getGIFFilenameByIndex(const char *directoryName, int index, char *pnBuffer) {
char* filename;
// Make sure index is in range
if ((index < 0) || (index >= numberOfFiles))
return;
File directory = SD.open(directoryName);
if (!directory)
return;
File file = directory.openNextFile();
while (file && (index >= 0)) {
filename = file.name();
if (isAnimationFile(file.name())) {
index--;
// Copy the directory name into the pathname buffer
strcpy(pnBuffer, directoryName);
// Append the filename to the pathname
strcat(pnBuffer, filename);
}
file.close();
file = directory.openNextFile();
}
file.close();
directory.close();
}
// 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);
}