DirectoryFunctions.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Example use of chdir(), ls(), mkdir(), and rmdir().
  3. */
  4. #include "SdFat.h"
  5. #include "sdios.h"
  6. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  7. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  8. #define SD_FAT_TYPE 0
  9. /*
  10. Change the value of SD_CS_PIN if you are using SPI and
  11. your hardware does not use the default value, SS.
  12. Common values are:
  13. Arduino Ethernet shield: pin 4
  14. Sparkfun SD shield: pin 8
  15. Adafruit SD shields and modules: pin 10
  16. */
  17. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  18. #ifndef SDCARD_SS_PIN
  19. const uint8_t SD_CS_PIN = SS;
  20. #else // SDCARD_SS_PIN
  21. // Assume built-in SD is used.
  22. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  23. #endif // SDCARD_SS_PIN
  24. // Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur.
  25. #define SPI_CLOCK SD_SCK_MHZ(50)
  26. // Try to select the best SD card configuration.
  27. #if HAS_SDIO_CLASS
  28. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  29. #elif ENABLE_DEDICATED_SPI
  30. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
  31. #else // HAS_SDIO_CLASS
  32. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
  33. #endif // HAS_SDIO_CLASS
  34. //------------------------------------------------------------------------------
  35. #if SD_FAT_TYPE == 0
  36. SdFat sd;
  37. File file;
  38. File root;
  39. #elif SD_FAT_TYPE == 1
  40. SdFat32 sd;
  41. File32 file;
  42. File32 root;
  43. #elif SD_FAT_TYPE == 2
  44. SdExFat sd;
  45. ExFile file;
  46. ExFile root;
  47. #elif SD_FAT_TYPE == 3
  48. SdFs sd;
  49. FsFile file;
  50. FsFile root;
  51. #endif // SD_FAT_TYPE
  52. // Create a Serial output stream.
  53. ArduinoOutStream cout(Serial);
  54. //------------------------------------------------------------------------------
  55. // Store error strings in flash to save RAM.
  56. #define error(s) sd.errorHalt(&Serial, F(s))
  57. //------------------------------------------------------------------------------
  58. void setup() {
  59. Serial.begin(9600);
  60. // Wait for USB Serial
  61. while (!Serial) {
  62. yield();
  63. }
  64. delay(1000);
  65. cout << F("Type any character to start\n");
  66. while (!Serial.available()) {
  67. yield();
  68. }
  69. // Initialize the SD card.
  70. if (!sd.begin(SD_CONFIG)) {
  71. sd.initErrorHalt(&Serial);
  72. }
  73. if (sd.exists("Folder1") || sd.exists("Folder1/file1.txt") ||
  74. sd.exists("Folder1/File2.txt")) {
  75. error("Please remove existing Folder1, file1.txt, and File2.txt");
  76. }
  77. int rootFileCount = 0;
  78. if (!root.open("/")) {
  79. error("open root");
  80. }
  81. while (file.openNext(&root, O_RDONLY)) {
  82. if (!file.isHidden()) {
  83. rootFileCount++;
  84. }
  85. file.close();
  86. if (rootFileCount > 10) {
  87. error("Too many files in root. Please use an empty SD.");
  88. }
  89. }
  90. if (rootFileCount) {
  91. cout << F("\nPlease use an empty SD for best results.\n\n");
  92. delay(1000);
  93. }
  94. // Create a new folder.
  95. if (!sd.mkdir("Folder1")) {
  96. error("Create Folder1 failed");
  97. }
  98. cout << F("Created Folder1\n");
  99. // Create a file in Folder1 using a path.
  100. if (!file.open("Folder1/file1.txt", O_WRONLY | O_CREAT)) {
  101. error("create Folder1/file1.txt failed");
  102. }
  103. file.close();
  104. cout << F("Created Folder1/file1.txt\n");
  105. // Change volume working directory to Folder1.
  106. if (!sd.chdir("Folder1")) {
  107. error("chdir failed for Folder1.\n");
  108. }
  109. cout << F("chdir to Folder1\n");
  110. // Create File2.txt in current directory.
  111. if (!file.open("File2.txt", O_WRONLY | O_CREAT)) {
  112. error("create File2.txt failed");
  113. }
  114. file.close();
  115. cout << F("Created File2.txt in current directory\n");
  116. cout << F("\nList of files on the SD.\n");
  117. sd.ls("/", LS_R);
  118. // Remove files from current directory.
  119. if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) {
  120. error("remove failed");
  121. }
  122. cout << F("\nfile1.txt and File2.txt removed.\n");
  123. // Change current directory to root.
  124. if (!sd.chdir()) {
  125. error("chdir to root failed.\n");
  126. }
  127. cout << F("\nList of files on the SD.\n");
  128. sd.ls(LS_R);
  129. // Remove Folder1.
  130. if (!sd.rmdir("Folder1")) {
  131. error("rmdir for Folder1 failed\n");
  132. }
  133. cout << F("\nFolder1 removed.\n");
  134. cout << F("\nList of files on the SD.\n");
  135. sd.ls(LS_R);
  136. cout << F("Done!\n");
  137. }
  138. //------------------------------------------------------------------------------
  139. // Nothing happens in loop.
  140. void loop() {}