mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-30 16:24:09 +03:00
Combining MemoryCard and SdFat libraries into SD library.
Defaulting to CS pin 4 (for Arduino Ethernet Shield). Holding the SS pin high (for disabling W5100 on the Ethernet Shield).
This commit is contained in:
71
libraries/SD/utility/examples/SdFatRead/SdFatRead.pde
Executable file
71
libraries/SD/utility/examples/SdFatRead/SdFatRead.pde
Executable file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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) {}
|
Reference in New Issue
Block a user