mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-07 22:46:08 +01:00
72 lines
1.8 KiB
Plaintext
72 lines
1.8 KiB
Plaintext
/*
|
|
* This sketch reads and prints the file
|
|
* PRINT00.TXT created by SdFatPrint.pde or
|
|
* WRITE00.TXT created by SdFatWrite.pde
|
|
*/
|
|
#include <SdFat.h>
|
|
#include <SdFatUtil.h>
|
|
|
|
Sd2Card card;
|
|
SdVolume volume;
|
|
SdFile root;
|
|
SdFile file;
|
|
|
|
// store error strings in flash to save RAM
|
|
#define error(s) error_P(PSTR(s))
|
|
|
|
void error_P(const char* str) {
|
|
PgmPrint("error: ");
|
|
SerialPrintln_P(str);
|
|
if (card.errorCode()) {
|
|
PgmPrint("SD error: ");
|
|
Serial.print(card.errorCode(), HEX);
|
|
Serial.print(',');
|
|
Serial.println(card.errorData(), HEX);
|
|
}
|
|
while(1);
|
|
}
|
|
|
|
void setup(void) {
|
|
Serial.begin(9600);
|
|
Serial.println();
|
|
Serial.println("type any character to start");
|
|
while (!Serial.available());
|
|
Serial.println();
|
|
|
|
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
|
|
// breadboards. use SPI_FULL_SPEED for better performance.
|
|
if (!card.init(SPI_HALF_SPEED)) error("card.init failed");
|
|
|
|
// initialize a FAT volume
|
|
if (!volume.init(&card)) error("volume.init failed");
|
|
|
|
// open the root directory
|
|
if (!root.openRoot(&volume)) error("openRoot failed");
|
|
|
|
// open a file
|
|
if (file.open(&root, "PRINT00.TXT", O_READ)) {
|
|
Serial.println("Opened PRINT00.TXT");
|
|
}
|
|
else if (file.open(&root, "WRITE00.TXT", O_READ)) {
|
|
Serial.println("Opened WRITE00.TXT");
|
|
}
|
|
else{
|
|
error("file.open failed");
|
|
}
|
|
Serial.println();
|
|
|
|
// copy file to serial port
|
|
int16_t n;
|
|
uint8_t buf[7];// nothing special about 7, just a lucky number.
|
|
while ((n = file.read(buf, sizeof(buf))) > 0) {
|
|
for (uint8_t i = 0; i < n; i++) Serial.print(buf[i]);
|
|
}
|
|
/* easier way
|
|
int16_t c;
|
|
while ((c = file.read()) > 0) Serial.print((char)c);
|
|
*/
|
|
Serial.println("\nDone");
|
|
}
|
|
|
|
void loop(void) {}
|