/*
SdFat Test
SdFatConfig.hの以下の部分を変更する
//#define USE_ARDUINO_SPI_LIBRARY 1
#define USE_ARDUINO_SPI_LIBRARY 0
//#define USE_SOFTWARE_SPI 0
#define USE_SOFTWARE_SPI 1
//uint8_t const SOFT_SPI_CS_PIN = 10;
uint8_t const SOFT_SPI_CS_PIN = 2;
//uint8_t const SOFT_SPI_MOSI_PIN = 11;
uint8_t const SOFT_SPI_MOSI_PIN = 3;
//uint8_t const SOFT_SPI_MISO_PIN = 12;
uint8_t const SOFT_SPI_MISO_PIN = 4;
//uint8_t const SOFT_SPI_SCK_PIN = 13;
uint8_t const SOFT_SPI_SCK_PIN = 5;
*/
#include <SdFat.h> //
https://github.com/jbeynon/sdfatlib
// SD chip select pin
const uint8_t chipSelect = 2;
SdFat sd;
SdFile file;
void setup() {
Serial.begin(9600);
while (!Serial) {} // wait for Leonardo
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
Serial.println("begin failed");
return;
}
sd.remove("FILE1.TXT");
sd.remove("FILE2.TXT");
}
//------------------------------------------------------------------------------
void loop() {
int incomingByte;
Serial.println("1:Create File");
Serial.println("2:Remove File");
Serial.println("3:Append File");
Serial.println("4:Write File");
Serial.println("5:Rename File");
Serial.println("6:Read File");
Serial.println("A:Make DIR");
Serial.println("B:Remove DIR");
Serial.println("C:Change DIR to SUBDIR");
Serial.println("D:Change DIR to ROOT");
while (!Serial.available()) {}
incomingByte = Serial.read();
Serial.print("I received: ");
Serial.println(incomingByte, HEX);
if (incomingByte == 0x31) {
if (!sd.exists("FILE1.TXT")) {
// file.open("FILE1.TXT",
O_WRITE | O_CREAT | O_AT_END);
file.open("FILE1.TXT",
O_WRITE | O_CREAT | O_APPEND);
Serial.print("file.isOpen=");
Serial.println(file.isOpen());
file.close();
} else {
Serial.println("**FILE1.TXT
IS EXIST**");
}
} else if (incomingByte == 0x32) {
if (sd.exists("FILE1.TXT")) {
sd.remove("FILE1.TXT");
} else {
Serial.println("**FILE1.TXT
NOT EXIST**");
}
} else if (incomingByte == 0x33) {
if (sd.exists("FILE1.TXT")) {
file.open("FILE1.TXT",
O_WRITE | O_APPEND);
Serial.print("file.isOpen=");
Serial.println(file.isOpen());
file.println("Hello");
Serial.print("getWriteError=");
Serial.println(file.getWriteError());
file.close();
} else {
Serial.println("**FILE1.TXT
NOT EXIST**");
}
} else if (incomingByte == 0x34) {
if (sd.exists("FILE1.TXT")) {
file.open("FILE1.TXT",
O_WRITE | O_APPEND);
file.write('H');
file.write('E');
file.write('L');
file.write('L');
file.write('O');
file.write(0x0d);
file.write(0x0a);
Serial.print("getWriteError=");
Serial.println(file.getWriteError());
file.close();
} else {
Serial.println("**FILE1.TXT
NOT EXIST**");
}
} else if (incomingByte == 0x35) {
if (sd.exists("FILE1.TXT")) {
file.open("FILE1.TXT",
O_READ);
file.rename(sd.vwd(),
"FILE2.TXT");
file.close();
} else {
Serial.println("**FILE1.TXT
NOT EXIST**");
}
} else if (incomingByte == 0x36) {
if (sd.exists("FILE1.TXT")) {
file.open("FILE1.TXT",
O_READ);
Serial.println("fileSize="
+ String(file.fileSize()) + "byte");
int line = 0;
int crlf = 1;
char c;
while(file.available())
{
if(crlf) {
line++;
Serial.print("Line#:" + String(line));
crlf = 0;
}
c=file.read();
Serial.write(c);
if (c == 0x0a)
crlf = 1;
}
file.close();
} else {
Serial.println("**FILE1.TXT
NOT EXIST**");
}
} else if (incomingByte == 0x41) {
if (!sd.exists("SUBDIR/")) {
sd.mkdir("SUBDIR",1);
} else {
Serial.println("**SUBDIR
EXIST**");
}
} else if (incomingByte == 0x42) {
if (sd.exists("SUBDIR/")) {
sd.rmdir("SUBDIR");
} else {
Serial.println("**SUBDIR
NOT EXIST**");
}
} else if (incomingByte == 0x43) {
if (sd.exists("SUBDIR/")) {
sd.chdir("SUBDIR",1);
} else {
Serial.println("**SUBDIR
NOT EXIST**");
}
} else if (incomingByte == 0x44) {
sd.chdir("/",1);
}
Serial.println("*****************************************");
sd.ls(LS_DATE | LS_SIZE | LS_R);
Serial.println("*****************************************");
Serial.println("Done");
Serial.read();
} |