-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSystem.cpp
167 lines (140 loc) · 4.59 KB
/
FileSystem.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
/*
* FileSystem.cpp
*
* Created on: Dec 2, 2014
* Author: kig
*/
#include "FlixCapacitor.h"
#ifdef ENABLE_AUDIO_SD
#include "FileSystem.h"
#include "print_helpers.h"
Sd2Card card;
SdVolume volume;
SdFile root;
FileSystem::FileSystem(uint8_t pin) {
pinCS = pin;
sdCardInitialized = false;
}
bool FileSystem::hasInitialized() {
return sdCardInitialized;
}
bool FileSystem::initSDCard() {
boolean status;
if (root.isOpen())
root.close(); // allows repeated calls
// First, detect the card
status = card.init(pinCS); // Audio shield has SD card SD on pin 10
if (status) {
Serial.println("SD card is connected :-)");
} else {
Serial.println("SD card is not connected or unusable :-(");
sdCardInitialized = false;
return sdCardInitialized;
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println(
"Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
sdCardInitialized = false;
return sdCardInitialized;
}
root.openRoot(volume);
// print the type and size of the first FAT-type volume
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
float size = volume.blocksPerCluster() * volume.clusterCount();
size = size * (512.0 / 1e6); // convert blocks to millions of bytes
Serial.print("File system space is ");
Serial.print(size);
Serial.println(" Mbytes.");
status = SD.begin(pinCS); // Audio shield has SD card CS on pin 10
if (status) {
Serial.println("SD library is able to access the filesystem");
} else {
Serial.println("SD library can not access the filesystem!");
Serial.println(
"Please report this problem, with the make & model of your SD card.");
Serial.println(
" http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports");
}
sdCardInitialized = true;
return sdCardInitialized;
}
bool FileSystem::nextFileInList(FileList *fileList, char* fullPath, direction dir) {
if (fileList->size > 0) {
fileList->currentIndex += (int) dir;
if (fileList->currentIndex < 0)
fileList->currentIndex = fileList->size - 1;
fileList->currentIndex = fileList->currentIndex % fileList->size;
sprintf(fullPath, "%s/%s", fileList->parentFolder,
fileList->files[fileList->currentIndex]);
return true;
} else
return false;
}
bool FileSystem::randomFileInList(FileList *fileList, char* fullPath) {
if (fileList->size > 0) {
fileList->currentIndex = random(fileList->size);
sprintf(fullPath, "%s/%s", fileList->parentFolder, fileList->files[fileList->currentIndex]);
return true;
} else
return false;
}
void FileSystem::saveDirectory(FileList *fileList, File dir, int level, char *match) {
printv("Opening directory ", dir.name());
int files = 0;
// count how many files are there
while (true) {
File entry = dir.openNextFile();
if (!entry)
break;
if (!entry.isDirectory())
files++;
entry.close();
}
printv("Files found: ", files);
dir.rewindDirectory();
delay(10);
if ((fileList->files = (char **) malloc(files * sizeof(char *))) == NULL) {
Serial.println("Can't allocate RAM");
return;
}
fileList->allocated = files;
for (int i = 0; i < fileList->allocated; i++) {
if ((fileList->files[i] = (char *) malloc(FAT32_FILENAME_LENGTH))
== NULL) {
Serial.println("Can't allocate RAM");
return;
}
}
while (true) {
File entry = dir.openNextFile();
if (!entry)
break;
if (!entry.isDirectory()) {
if (strstr(entry.name(), match) != NULL) {
strcpy(fileList->files[fileList->size++], entry.name());
}
}
entry.close();
}
sprintf(buf, "loaded %d files from directory %s", files, dir.name());
Serial.println(buf);
}
FileList *FileSystem::findFilesMatchingExtension(char *folder, char *extension) {
FileList *fileList = new FileList;
if (fileList == NULL) {
Serial.println("Can not allocate RAM");
return fileList;
}
fileList->parentFolder = folder;
fileList->size = 0;
fileList->currentIndex = 0;
File root = SD.open(folder);
saveDirectory(fileList, root, 0, extension);
return fileList;
}
FileSystem::~FileSystem() {
}
#endif