|
-
- #include <SPI.h>
- #include <SdFat.h>
-
- const uint8_t SD_CHIP_SELECT = SS;
-
-
- const bool ALLOW_WIPE = false;
-
-
- SdFat sd;
-
-
- SdFile file;
-
-
- ArduinoOutStream cout(Serial);
-
-
- char cinBuf[40];
-
-
- ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
-
-
- #define error(msg) sd.errorHalt(F(msg))
-
- void setup() {
- Serial.begin(9600);
- while (!Serial) {}
- delay(1000);
-
- cout << F("Type any character to start\n");
-
- cin.readline();
-
-
-
- if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) {
- sd.initErrorHalt();
- }
-
-
- if (file.openNext(sd.vwd(), O_READ)) {
- cout << F("Found files/folders in the root directory.\n");
- if (!ALLOW_WIPE) {
- error("SD not empty, use a blank SD or set ALLOW_WIPE true.");
- } else {
- cout << F("Type: 'WIPE' to delete all SD files.\n");
- char buf[10];
- cin.readline();
- cin.get(buf, sizeof(buf));
- if (cin.fail() || strncmp(buf, "WIPE", 4) || buf[4] >= ' ') {
- error("Invalid WIPE input");
- }
- file.close();
- if (!sd.vwd()->rmRfStar()) {
- error("wipe failed");
- }
- cout << F("***SD wiped clean.***\n\n");
- }
- }
-
-
- if (!sd.mkdir("Folder1")) {
- error("Create Folder1 failed");
- }
- cout << F("Created Folder1\n");
-
-
- if (!file.open("Folder1/file1.txt", O_CREAT | O_WRITE)) {
- error("create Folder1/file1.txt failed");
- }
- file.close();
- cout << F("Created Folder1/file1.txt\n");
-
-
- if (!sd.chdir("Folder1")) {
- error("chdir failed for Folder1.\n");
- }
- cout << F("chdir to Folder1\n");
-
-
- if (!file.open("File2.txt", O_CREAT | O_WRITE)) {
- error("create File2.txt failed");
- }
- file.close();
- cout << F("Created File2.txt in current directory\n");
-
- cout << F("List of files on the SD.\n");
- sd.ls("/", LS_R);
-
-
- if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) {
- error("remove failed");
- }
- cout << F("\nfile1.txt and File2.txt removed.\n");
-
-
- if (!sd.chdir()) {
- error("chdir to root failed.\n");
- }
-
- cout << F("List of files on the SD.\n");
- sd.ls(LS_R);
-
-
- if (!sd.rmdir("Folder1")) {
- error("rmdir for Folder1 failed\n");
- }
-
- cout << F("\nFolder1 removed, SD empty.\n");
- cout << F("Done!\n");
- }
-
-
- void loop() {}
|