-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSdFuncs.cpp
More file actions
224 lines (181 loc) · 5.26 KB
/
SdFuncs.cpp
File metadata and controls
224 lines (181 loc) · 5.26 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
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
222
223
224
#include "BowieLogger.h"
bool BowieLogger::initSd() {
// From the SD CardInfo example code
// created 28 Mar 2011
// by Limor Fried
// modified 9 Apr 2012
// by Tom Igoe
Serial.print("\nInitializing SD card...");
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return false;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return false;
} else {
Serial.println("card initialized.");
}
// print the type of card
Serial.print("\nCard type: ");
switch(card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
return true;
}
void BowieLogger::getAllFiles() {
// from the SD listfiles example code
// created Nov 2010
// by David A. Mellis
// modified 9 Apr 2012
// by Tom Igoe
File root;
root = SD.open("/");
printDirectory(root, 0);
}
void BowieLogger::printDirectory(File dir, int numTabs) {
// from the SD listfiles example code
// created Nov 2010
// by David A. Mellis
// modified 9 Apr 2012
// by Tom Igoe
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
uint32_t BowieLogger::getSizeUsed() {
// returns kb
// tends to over-calculate the size used
// (as compared with Finder)
File root;
root = SD.open("/");
uint32_t totalsize = enterNextDir(root);
uint32_t totalsize_kb = 0;
Serial.print("\n\n");
Serial.print("Total size (bytes): ");
Serial.println(totalsize);
totalsize = ceil(totalsize/1024);
totalsize_kb = totalsize;
Serial.print("Total size (Kbytes): ");
Serial.println(totalsize);
totalsize = ceil(totalsize/1024);
Serial.print("Total size (Mbytes): ");
Serial.println(totalsize);
return totalsize_kb;
}
uint32_t BowieLogger::enterNextDir(File dir) {
uint32_t totalsize = 0;
while(true) {
File entry = dir.openNextFile();
if (!entry) {
break;
}
if (entry.isDirectory()) {
totalsize += enterNextDir(entry);
} else {
totalsize += entry.size();
}
entry.close();
}
return totalsize;
}
uint32_t BowieLogger::getSizeTotal() {
// returns kb
// From the SD CardInfo example code
// created 28 Mar 2011
// by Limor Fried
// modified 9 Apr 2012
// by Tom Igoe
// 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");
return 0;
}
// print the type and size of the first FAT-type volume
uint32_t volumesize = 0;
uint32_t volumesize_kb = 0;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
if (volumesize < 8388608ul) {
Serial.print("Volume size (bytes): ");
Serial.println(volumesize * 512); // SD card blocks are always 512 bytes
}
Serial.print("Volume size (Kbytes): ");
volumesize /= 2;
volumesize_kb = volumesize;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
return volumesize_kb;
}
float BowieLogger::getPercentAvailable() {
float n = ( (float)getSizeUsed() / (float)getSizeTotal() ) * 100.0;
n = 100.0-n;
Serial.print("\n\n");
Serial.print("Percent available: ");
Serial.println(n);
return n;
}
void BowieLogger::sendEntireFile(String filename, Stream *s) {
// from the SD example code - DumpFile
// created 22 December 2010
// by Limor Fried
// modified 9 Apr 2012
// by Tom Igoe
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open(filename.c_str());
// if the file is available, write to it:
if (dataFile) {
while (dataFile.available()) {
s->write(dataFile.read());
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.print("error opening ");
Serial.println(filename);
}
}