Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

99 rindas
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 <SdFat.h>
  10. const uint8_t SD_CHIP_SELECT = SS;
  11. SdFat sd;
  12. typedef File file_t;
  13. // store error strings in flash to save RAM
  14. #define error(s) sd.errorHalt(&Serial, F(s))
  15. /*
  16. * remove all files in dir.
  17. */
  18. void deleteFiles(FatFile* dir) {
  19. char name[32];
  20. file_t file;
  21. // open and delete files
  22. for (uint16_t n = 0; ; n++){
  23. sprintf(name, "%u.TXT", n);
  24. // open start time
  25. uint32_t t0 = millis();
  26. // assume done if open fails
  27. if (!file.open(dir, name, O_WRONLY)) return;
  28. // open end time and remove start time
  29. uint32_t t1 = millis();
  30. if (!file.remove()) error("file.remove failed");
  31. // remove end time
  32. uint32_t t2 = millis();
  33. Serial.print(F("RM "));
  34. Serial.print(n);
  35. Serial.write(' ');
  36. // open time
  37. Serial.print(t1 - t0);
  38. Serial.write(' ');
  39. // remove time
  40. Serial.println(t2 - t1);
  41. }
  42. }
  43. void setup() {
  44. file_t root;
  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)) {
  53. sd.initErrorHalt(&Serial);
  54. }
  55. root.openRoot(&sd);
  56. // delete files in root if not FAT16.
  57. if (sd.fatType() != 16) {
  58. Serial.println(F("Remove files in root"));
  59. deleteFiles(&root);
  60. }
  61. // open SUB1 and delete files
  62. file_t sub1;
  63. if (!sub1.open("SUB1", O_RDONLY)) error("open SUB1 failed");
  64. Serial.println(F("Remove files in SUB1"));
  65. deleteFiles(&sub1);
  66. // open SUB2 and delete files
  67. file_t sub2;
  68. if (!sub2.open(&sub1, "SUB2", O_RDONLY)) error("open SUB2 failed");
  69. Serial.println(F("Remove files in SUB2"));
  70. deleteFiles(&sub2);
  71. // remove SUB2
  72. if (!sub2.rmdir()) error("sub2.rmdir failed");
  73. Serial.println(F("SUB2 removed"));
  74. // remove SUB1
  75. if (!sub1.rmdir()) error("sub1.rmdir failed");
  76. Serial.println(F("SUB1 removed"));
  77. Serial.println(F("Done"));
  78. }
  79. void loop() { }