DirectoryFunctions.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. SysCall::yield();
  63. }
  64. delay(1000);
  65. cout << F("Type any character to start\n");
  66. while (!Serial.available()) {
  67. SysCall::yield();
  68. }
  69. // Initialize the SD card.
  70. if (!sd.begin(SD_CONFIG)) {
  71. sd.initErrorHalt(&Serial);
  72. }
  73. if (sd.exists("Folder1")
  74. || sd.exists("Folder1/file1.txt")
  75. || sd.exists("Folder1/File2.txt")) {
  76. error("Please remove existing Folder1, file1.txt, and File2.txt");
  77. }
  78. int rootFileCount = 0;
  79. if (!root.open("/")) {
  80. error("open root");
  81. }
  82. while (file.openNext(&root, O_RDONLY)) {
  83. if (!file.isHidden()) {
  84. rootFileCount++;
  85. }
  86. file.close();
  87. if (rootFileCount > 10) {
  88. error("Too many files in root. Please use an empty SD.");
  89. }
  90. }
  91. if (rootFileCount) {
  92. cout << F("\nPlease use an empty SD for best results.\n\n");
  93. delay(1000);
  94. }
  95. // Create a new folder.
  96. if (!sd.mkdir("Folder1")) {
  97. error("Create Folder1 failed");
  98. }
  99. cout << F("Created Folder1\n");
  100. // Create a file in Folder1 using a path.
  101. if (!file.open("Folder1/file1.txt", O_WRONLY | O_CREAT)) {
  102. error("create Folder1/file1.txt failed");
  103. }
  104. file.close();
  105. cout << F("Created Folder1/file1.txt\n");
  106. // Change volume working directory to Folder1.
  107. if (!sd.chdir("Folder1")) {
  108. error("chdir failed for Folder1.\n");
  109. }
  110. cout << F("chdir to Folder1\n");
  111. // Create File2.txt in current directory.
  112. if (!file.open("File2.txt", O_WRONLY | O_CREAT)) {
  113. error("create File2.txt failed");
  114. }
  115. file.close();
  116. cout << F("Created File2.txt in current directory\n");
  117. cout << F("\nList of files on the SD.\n");
  118. sd.ls("/", LS_R);
  119. // Remove files from current directory.
  120. if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) {
  121. error("remove failed");
  122. }
  123. cout << F("\nfile1.txt and File2.txt removed.\n");
  124. // Change current directory to root.
  125. if (!sd.chdir()) {
  126. error("chdir to root failed.\n");
  127. }
  128. cout << F("\nList of files on the SD.\n");
  129. sd.ls(LS_R);
  130. // Remove Folder1.
  131. if (!sd.rmdir("Folder1")) {
  132. error("rmdir for Folder1 failed\n");
  133. }
  134. cout << F("\nFolder1 removed.\n");
  135. cout << F("\nList of files on the SD.\n");
  136. sd.ls(LS_R);
  137. cout << F("Done!\n");
  138. }
  139. //------------------------------------------------------------------------------
  140. // Nothing happens in loop.
  141. void loop() {}