2011-04-18 23:58:54 +02:00
|
|
|
/*
|
2013-11-11 13:19:48 +01:00
|
|
|
Listfiles
|
2011-04-18 23:58:54 +02:00
|
|
|
|
2013-11-11 13:19:48 +01:00
|
|
|
This example shows how print out the files in a
|
|
|
|
directory on a SD card
|
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
The circuit:
|
|
|
|
* SD card attached to SPI bus as follows:
|
|
|
|
** MOSI - pin 11
|
|
|
|
** MISO - pin 12
|
|
|
|
** CLK - pin 13
|
|
|
|
** CS - pin 4
|
2014-10-15 11:53:19 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
created Nov 2010
|
|
|
|
by David A. Mellis
|
2012-04-09 15:51:35 +02:00
|
|
|
modified 9 Apr 2012
|
2011-04-18 23:58:54 +02:00
|
|
|
by Tom Igoe
|
2014-02-02 11:35:04 +01:00
|
|
|
modified 2 Feb 2014
|
|
|
|
by Scott Fitzgerald
|
2011-04-18 23:58:54 +02:00
|
|
|
|
|
|
|
This example code is in the public domain.
|
2014-10-15 11:53:19 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
*/
|
2014-10-15 11:53:19 +02:00
|
|
|
#include <SPI.h>
|
2011-04-18 23:58:54 +02:00
|
|
|
#include <SD.h>
|
|
|
|
|
|
|
|
File root;
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
2012-04-02 15:07:58 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2011-04-18 23:58:54 +02:00
|
|
|
Serial.begin(9600);
|
2014-10-15 11:53:19 +02:00
|
|
|
while (!Serial) {
|
2012-04-09 15:51:35 +02:00
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
Serial.print("Initializing SD card...");
|
|
|
|
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
|
2014-10-15 11:53:19 +02:00
|
|
|
// Note that even if it's not used as the CS pin, the hardware SS pin
|
|
|
|
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
|
|
|
|
// or the SD library functions will not work.
|
2011-04-18 23:58:54 +02:00
|
|
|
pinMode(10, OUTPUT);
|
|
|
|
|
2014-02-02 11:35:04 +01:00
|
|
|
if (!SD.begin(4)) {
|
2011-04-18 23:58:54 +02:00
|
|
|
Serial.println("initialization failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Serial.println("initialization done.");
|
|
|
|
|
|
|
|
root = SD.open("/");
|
2014-10-15 11:53:19 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
printDirectory(root, 0);
|
2014-10-15 11:53:19 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
Serial.println("done!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
// nothing happens after setup finishes.
|
|
|
|
}
|
|
|
|
|
|
|
|
void printDirectory(File dir, int numTabs) {
|
|
|
|
while(true) {
|
|
|
|
|
|
|
|
File entry = dir.openNextFile();
|
|
|
|
if (! entry) {
|
|
|
|
// no more files
|
|
|
|
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);
|
|
|
|
}
|
2012-12-20 16:26:46 +01:00
|
|
|
entry.close();
|
2011-04-18 23:58:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|