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.

124 line
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. // File system object.
  10. SdFat sd;
  11. // Use for file creation in folders.
  12. SdFile file;
  13. // Create a Serial output stream.
  14. ArduinoOutStream cout(Serial);
  15. // Buffer for Serial input.
  16. char cinBuf[40];
  17. // Create a serial input stream.
  18. ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
  19. //==============================================================================
  20. // Error messages stored in flash.
  21. #define error(msg) sd.errorHalt(F(msg))
  22. //------------------------------------------------------------------------------
  23. void setup() {
  24. Serial.begin(9600);
  25. // Wait for USB Serial
  26. while (!Serial) {
  27. SysCall::yield();
  28. }
  29. delay(1000);
  30. cout << F("Type any character to start\n");
  31. // Wait for input line and discard.
  32. cin.readline();
  33. cout << endl;
  34. // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  35. // breadboards. use SPI_FULL_SPEED for better performance.
  36. if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) {
  37. sd.initErrorHalt();
  38. }
  39. if (sd.exists("Folder1")
  40. || sd.exists("Folder1/file1.txt")
  41. || sd.exists("Folder1/File2.txt")) {
  42. error("Please remove existing Folder1, file1.txt, and File2.txt");
  43. }
  44. int rootFileCount = 0;
  45. sd.vwd()->rewind();
  46. while (file.openNext(sd.vwd(), O_READ)) {
  47. if (!file.isHidden()) {
  48. rootFileCount++;
  49. }
  50. file.close();
  51. if (rootFileCount > 10) {
  52. error("Too many files in root. Please use an empty SD.");
  53. }
  54. }
  55. if (rootFileCount) {
  56. cout << F("\nPlease use an empty SD for best results.\n\n");
  57. delay(1000);
  58. }
  59. // Create a new folder.
  60. if (!sd.mkdir("Folder1")) {
  61. error("Create Folder1 failed");
  62. }
  63. cout << F("Created Folder1\n");
  64. // Create a file in Folder1 using a path.
  65. if (!file.open("Folder1/file1.txt", O_CREAT | O_WRITE)) {
  66. error("create Folder1/file1.txt failed");
  67. }
  68. file.close();
  69. cout << F("Created Folder1/file1.txt\n");
  70. // Change volume working directory to Folder1.
  71. if (!sd.chdir("Folder1")) {
  72. error("chdir failed for Folder1.\n");
  73. }
  74. cout << F("chdir to Folder1\n");
  75. // Create File2.txt in current directory.
  76. if (!file.open("File2.txt", O_CREAT | O_WRITE)) {
  77. error("create File2.txt failed");
  78. }
  79. file.close();
  80. cout << F("Created File2.txt in current directory\n");
  81. cout << F("\nList of files on the SD.\n");
  82. sd.ls("/", LS_R);
  83. // Remove files from current directory.
  84. if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) {
  85. error("remove failed");
  86. }
  87. cout << F("\nfile1.txt and File2.txt removed.\n");
  88. // Change current directory to root.
  89. if (!sd.chdir()) {
  90. error("chdir to root failed.\n");
  91. }
  92. cout << F("\nList of files on the SD.\n");
  93. sd.ls(LS_R);
  94. // Remove Folder1.
  95. if (!sd.rmdir("Folder1")) {
  96. error("rmdir for Folder1 failed\n");
  97. }
  98. cout << F("\nFolder1 removed.\n");
  99. cout << F("\nList of files on the SD.\n");
  100. sd.ls(LS_R);
  101. cout << F("Done!\n");
  102. }
  103. //------------------------------------------------------------------------------
  104. // Nothing happens in loop.
  105. void loop() {}