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.

119 satır
3.2KB

  1. /*
  2. * Example use of chdir(), ls(), mkdir(), and rmdir().
  3. */
  4. #include <SPI.h>
  5. #include <SdFat.h>
  6. // SD card chip select pin.
  7. const uint8_t SD_CHIP_SELECT = SS;
  8. //------------------------------------------------------------------------------
  9. // Permit SD to be wiped if ALLOW_WIPE is true.
  10. const bool ALLOW_WIPE = false;
  11. // File system object.
  12. SdFat sd;
  13. // Use for file creation in folders.
  14. SdFile file;
  15. // Create a Serial output stream.
  16. ArduinoOutStream cout(Serial);
  17. // Buffer for Serial input.
  18. char cinBuf[40];
  19. // Create a serial input stream.
  20. ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
  21. //==============================================================================
  22. // Error messages stored in flash.
  23. #define error(msg) sd.errorHalt(F(msg))
  24. //------------------------------------------------------------------------------
  25. void setup() {
  26. Serial.begin(9600);
  27. while (!Serial) {} // wait for Leonardo
  28. delay(1000);
  29. cout << F("Type any character to start\n");
  30. // Wait for input line and discard.
  31. cin.readline();
  32. // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  33. // breadboards. use SPI_FULL_SPEED for better performance.
  34. if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) {
  35. sd.initErrorHalt();
  36. }
  37. // Check for empty SD.
  38. if (file.openNext(sd.vwd(), O_READ)) {
  39. cout << F("Found files/folders in the root directory.\n");
  40. if (!ALLOW_WIPE) {
  41. error("SD not empty, use a blank SD or set ALLOW_WIPE true.");
  42. } else {
  43. cout << F("Type: 'WIPE' to delete all SD files.\n");
  44. char buf[10];
  45. cin.readline();
  46. cin.get(buf, sizeof(buf));
  47. if (cin.fail() || strncmp(buf, "WIPE", 4) || buf[4] >= ' ') {
  48. error("Invalid WIPE input");
  49. }
  50. file.close();
  51. if (!sd.vwd()->rmRfStar()) {
  52. error("wipe failed");
  53. }
  54. cout << F("***SD wiped clean.***\n\n");
  55. }
  56. }
  57. // Create a new folder.
  58. if (!sd.mkdir("Folder1")) {
  59. error("Create Folder1 failed");
  60. }
  61. cout << F("Created Folder1\n");
  62. // Create a file in Folder1 using a path.
  63. if (!file.open("Folder1/file1.txt", O_CREAT | O_WRITE)) {
  64. error("create Folder1/file1.txt failed");
  65. }
  66. file.close();
  67. cout << F("Created Folder1/file1.txt\n");
  68. // Change volume working directory to Folder1.
  69. if (!sd.chdir("Folder1")) {
  70. error("chdir failed for Folder1.\n");
  71. }
  72. cout << F("chdir to Folder1\n");
  73. // Create File2.txt in current directory.
  74. if (!file.open("File2.txt", O_CREAT | O_WRITE)) {
  75. error("create File2.txt failed");
  76. }
  77. file.close();
  78. cout << F("Created File2.txt in current directory\n");
  79. cout << F("List of files on the SD.\n");
  80. sd.ls("/", LS_R);
  81. // Remove files from current directory.
  82. if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) {
  83. error("remove failed");
  84. }
  85. cout << F("\nfile1.txt and File2.txt removed.\n");
  86. // Change current directory to root.
  87. if (!sd.chdir()) {
  88. error("chdir to root failed.\n");
  89. }
  90. cout << F("List of files on the SD.\n");
  91. sd.ls(LS_R);
  92. // Remove Folder1.
  93. if (!sd.rmdir("Folder1")) {
  94. error("rmdir for Folder1 failed\n");
  95. }
  96. cout << F("\nFolder1 removed, SD empty.\n");
  97. cout << F("Done!\n");
  98. }
  99. //------------------------------------------------------------------------------
  100. // Nothing happens in loop.
  101. void loop() {}