-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathFilenameFunctions.cpp
221 lines (180 loc) · 5.16 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
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
/*
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 "FilenameFunctions.h" //defines USE_SPIFFS as reqd
#if defined (ARDUINO)
#ifdef USE_SPIFFS
#if defined(ESP8266)
#include "FS.h"
#define USE_SPIFFS_DIR
#elif defined(ESP32)
#include <SPIFFS.h>
#define SD SPIFFS
#else
#error USE_SPIFFS only valid on Expressif controllers
#endif
#else
#include <SD.h>
#endif
#elif defined (SPARK)
#include "sd-card-library-photon-compat/sd-card-library-photon-compat.h"
#endif
File file;
int numberOfFiles;
bool fileSeekCallback(unsigned long position) {
#ifdef USE_SPIFFS
return file.seek(position, SeekSet);
#else
return file.seek(position);
#endif
}
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); //.kbv
}
int initSdCard(int chipSelectPin) {
#ifdef USE_SPIFFS
// if you want to force PROGMEM instead of SPIFFS, 1k pulldown on SD_CS pin.
pinMode(chipSelectPin, INPUT_PULLUP);
delay(10);
int state = digitalRead(chipSelectPin);
pinMode(chipSelectPin, OUTPUT);
if (state == 0) return -1;
if (!SPIFFS.begin())
return -1;
#else
// if you want to force PROGMEM instead of SD, remove the SD from socket.
// initialize the SD card at full speed
pinMode(chipSelectPin, OUTPUT);
if (!SD.begin(chipSelectPin))
return -1;
#endif
return 0;
}
bool isAnimationFile(const char filename []) {
if (filename[0] == '_')
return false;
if (filename[0] == '~')
return false;
if (filename[0] == '.')
return false;
String filenameString = String(filename); //.kbv STM32 and ESP need separate statements
filenameString.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, bool displayFilenames) {
char *filename;
numberOfFiles = 0;
#ifdef USE_SPIFFS_DIR
File file;
Dir directory = SPIFFS.openDir(directoryName);
// if (!directory == 0) return -1;
while (directory.next()) {
file = directory.openFile("r");
if (!file) break;
#else
File file;
File directory = SD.open(directoryName);
if (!directory) {
return -1;
}
while (file = directory.openNextFile()) {
#endif
filename = (char*)file.name();
if (isAnimationFile(filename)) {
numberOfFiles++;
if (displayFilenames) {
Serial.print(numberOfFiles);
Serial.print(":");
Serial.print(filename);
Serial.print(" size:");
Serial.println(file.size());
}
}
else Serial.println(filename);
file.close();
}
// file.close();
#ifdef USE_SPIFFS
#else
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) {
char* filename;
// Make sure index is in range
if ((index < 0) || (index >= numberOfFiles))
return;
#ifdef USE_SPIFFS_DIR
Dir directory = SPIFFS.openDir(directoryName);
// if (!directory) return;
while (directory.next() && (index >= 0)) {
file = directory.openFile("r");
if (!file) break;
#else
File directory = SD.open(directoryName);
if (!directory) {
return;
}
while ((index >= 0)) {
file = directory.openNextFile();
if (!file) break;
#endif
filename = (char*)file.name(); //.kbv
if (isAnimationFile(filename)) {
index--;
// Copy the directory name into the pathname buffer
strcpy(pnBuffer, directoryName);
#if defined(ESP32) || defined(USE_SPIFFS)
pnBuffer[0] = 0;
#else
int len = strlen(pnBuffer);
if (len == 0 || pnBuffer[len - 1] != '/') strcat(pnBuffer, "/");
#endif
// Append the filename to the pathname
strcat(pnBuffer, filename);
}
file.close();
}
file.close();
#ifdef USE_SPIFFS
#else
directory.close();
#endif
}
int openGifFilenameByIndex(const char *directoryName, int index) {
char pathname[40];
getGIFFilenameByIndex(directoryName, index, pathname);
Serial.print("Pathname: ");
Serial.println(pathname);
if (file)
file.close();
// Attempt to open the file for reading
#ifdef USE_SPIFFS
file = SPIFFS.open(pathname, "r");
#else
file = SD.open(pathname);
#endif
if (!file) {
Serial.println("Error opening GIF file");
return -1;
}
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);
}