You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.3KB

  1. /*
  2. * This sketch will remove the files and directories
  3. * created by the SdFatMakeDir.pde sketch.
  4. *
  5. * Performance is erratic due to the large number
  6. * of flash erase operations caused by many random
  7. * writes to file structures.
  8. */
  9. #include <SPI.h>
  10. #include <SdFat.h>
  11. #include <SdFatUtil.h>
  12. const uint8_t SD_CHIP_SELECT = SS;
  13. SdFat sd;
  14. // store error strings in flash to save RAM
  15. #define error(s) sd.errorHalt(F(s))
  16. /*
  17. * remove all files in dir.
  18. */
  19. void deleteFiles(FatFile* dir) {
  20. char name[32];
  21. SdFile file;
  22. // open and delete files
  23. for (uint16_t n = 0; ; n++){
  24. sprintf(name, "%u.TXT", n);
  25. // open start time
  26. uint32_t t0 = millis();
  27. // assume done if open fails
  28. if (!file.open(dir, name, O_WRITE)) return;
  29. // open end time and remove start time
  30. uint32_t t1 = millis();
  31. if (!file.remove()) error("file.remove failed");
  32. // remove end time
  33. uint32_t t2 = millis();
  34. Serial.print(F("RM "));
  35. Serial.print(n);
  36. Serial.write(' ');
  37. // open time
  38. Serial.print(t1 - t0);
  39. Serial.write(' ');
  40. // remove time
  41. Serial.println(t2 - t1);
  42. }
  43. }
  44. void setup() {
  45. Serial.begin(9600);
  46. while (!Serial) {} // wait for Leonardo
  47. Serial.println(F("Type any character to start"));
  48. while (Serial.read() <= 0) {}
  49. delay(200); // Catch Due reset problem
  50. // initialize the SD card at SPI_FULL_SPEED for best performance.
  51. // try SPI_HALF_SPEED if bus errors occur.
  52. if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) sd.initErrorHalt();
  53. // delete files in root if FAT32
  54. if (sd.vol()->fatType() == 32) {
  55. Serial.println(F("Remove files in root"));
  56. deleteFiles(sd.vwd());
  57. }
  58. // open SUB1 and delete files
  59. SdFile sub1;
  60. if (!sub1.open("SUB1", O_READ)) error("open SUB1 failed");
  61. Serial.println(F("Remove files in SUB1"));
  62. deleteFiles(&sub1);
  63. // open SUB2 and delete files
  64. SdFile sub2;
  65. if (!sub2.open(&sub1, "SUB2", O_READ)) error("open SUB2 failed");
  66. Serial.println(F("Remove files in SUB2"));
  67. deleteFiles(&sub2);
  68. // remove SUB2
  69. if (!sub2.rmdir()) error("sub2.rmdir failed");
  70. Serial.println(F("SUB2 removed"));
  71. // remove SUB1
  72. if (!sub1.rmdir()) error("sub1.rmdir failed");
  73. Serial.println(F("SUB1 removed"));
  74. Serial.println(F("Done"));
  75. }
  76. void loop() { }