| 1 |
/* |
| 2 |
Listfiles |
| 3 |
|
| 4 |
This example shows how print out the files in a |
| 5 |
directory on a SD card |
| 6 |
|
| 7 |
The circuit: |
| 8 |
* SD card attached to SPI bus as follows: |
| 9 |
** MOSI - pin 11 |
| 10 |
** MISO - pin 12 |
| 11 |
** CLK - pin 13 |
| 12 |
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) |
| 13 |
|
| 14 |
created Nov 2010 |
| 15 |
by David A. Mellis |
| 16 |
modified 9 Apr 2012 |
| 17 |
by Tom Igoe |
| 18 |
modified 2 Feb 2014 |
| 19 |
by Scott Fitzgerald |
| 20 |
|
| 21 |
This example code is in the public domain. |
| 22 |
|
| 23 |
*/ |
| 24 |
#include <SPI.h> |
| 25 |
#include <SD.h> |
| 26 |
|
| 27 |
File root; |
| 28 |
|
| 29 |
void setup() { |
| 30 |
// Open serial communications and wait for port to open: |
| 31 |
Serial.begin(9600); |
| 32 |
while (!Serial) { |
| 33 |
; // wait for serial port to connect. Needed for native USB port only |
| 34 |
} |
| 35 |
|
| 36 |
Serial.print("Initializing SD card..."); |
| 37 |
|
| 38 |
if (!SD.begin(4)) { |
| 39 |
Serial.println("initialization failed!"); |
| 40 |
return; |
| 41 |
} |
| 42 |
Serial.println("initialization done."); |
| 43 |
|
| 44 |
root = SD.open("/"); |
| 45 |
|
| 46 |
printDirectory(root, 0); |
| 47 |
|
| 48 |
Serial.println("done!"); |
| 49 |
} |
| 50 |
|
| 51 |
void loop() { |
| 52 |
// nothing happens after setup finishes. |
| 53 |
} |
| 54 |
|
| 55 |
void printDirectory(File dir, int numTabs) { |
| 56 |
while (true) { |
| 57 |
|
| 58 |
File entry = dir.openNextFile(); |
| 59 |
if (! entry) { |
| 60 |
// no more files |
| 61 |
break; |
| 62 |
} |
| 63 |
for (uint8_t i = 0; i < numTabs; i++) { |
| 64 |
Serial.print('\t'); |
| 65 |
} |
| 66 |
Serial.print(entry.name()); |
| 67 |
if (entry.isDirectory()) { |
| 68 |
Serial.println("/"); |
| 69 |
printDirectory(entry, numTabs + 1); |
| 70 |
} else { |
| 71 |
// files have sizes, directories do not |
| 72 |
Serial.print("\t\t"); |
| 73 |
Serial.println(entry.size(), DEC); |
| 74 |
} |
| 75 |
entry.close(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|