-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilenameFunctions.cpp
More file actions
153 lines (115 loc) · 3.55 KB
/
FilenameFunctions.cpp
File metadata and controls
153 lines (115 loc) · 3.55 KB
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
/*
* 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"
// #include <SD.h>
int numberOfFiles;
File file;
// Chip select for SD card
#define SD_CS BUILTIN_SDCARD
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);
}
int fileSizeCallback(void) {
return file.size();
}
int initFileSystem(int chipSelectPin) {
// initialize the SD card at full speed
if (chipSelectPin >= 0) {
pinMode(chipSelectPin, OUTPUT);
}
if (!SD.begin(chipSelectPin))
return -1;
return 0;
}
bool isFileType(const char filename[], const char extension[]) {
String filenameString(filename);
if ((filenameString[0] == '_') || (filenameString[0] == '~') || (filenameString[0] == '.')) {
return false;
}
filenameString.toUpperCase();
if (filenameString.endsWith(extension) != 1)
return false;
return true;
}
// Get the full path/filename of the GIF file with specified index
void getFilenameByIndex(const char *directoryName, int index, char *pnBuffer, const char *extension) {
// Serial.printf("(get) Dir: %s\tExt: %s\tIdx: %d\n", directoryName, extension, index);
// Make sure index is in range
if ((index < 0) || (index >= numberOfFiles))
return;
File directory = SD.open(directoryName);
if (!directory)
return;
// Serial.println("Dir opened");
while ((index >= 0)) {
// Serial.printf("Opening %d\n", index);
file = directory.openNextFile();
if (!file) break;
if (isFileType(file.name(), extension)) {
// Serial.printf("%s Is a %s\n", file.name(), extension);
index--;
// Copy the directory name into the pathname buffer
strcpy(pnBuffer, directoryName);
int len = strlen(pnBuffer);
if (len == 0 || pnBuffer[len - 1] != '/') strcat(pnBuffer, "/");
// Append the filename to the pathname
strcat(pnBuffer, file.name());
}
file.close();
}
file.close();
directory.close();
}
int openFilenameByIndex(const char *directoryName, int index, const char *extension) {
char pathname[255];
// Serial.printf("(open) Dir: %s\tExt: %s\tIdx: %d\n", directoryName, extension, index);
getFilenameByIndex(directoryName, index, pathname, extension);
// Serial.print("Pathname: ");
// Serial.println(pathname);
if (file)
file.close();
// Attempt to open the file for reading
file = SD.open(pathname);
if (!file) {
// Serial.println("Error opening file");
return -1;
}
return 0;
}
// Callbacks for PNGDec and JPEGDec (Larry Bank's libs)
void * bankOpen(const char *filename, int32_t *size){
// Serial.printf("Attempting to open %s\n", filename);
file = SD.open(filename);
*size = file.size();
return &file;
}
void bankClose(void *handle){
if (file) file.close();
}
int32_t bankRead(void *handle, uint8_t *buf, int32_t length){
if (!file) return 0;
return file.read(buf, length);
}
int32_t bankSeek(void *handle, int32_t pos){
if (!file) return 0;
return file.seek(pos);
}
// 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);
// }