您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

129 行
3.3KB

  1. /*
  2. * Example use of chdir(), ls(), mkdir(), and rmdir().
  3. */
  4. #include <SPI.h>
  5. #include "SdFat.h"
  6. #include "sdios.h"
  7. // SD card chip select pin.
  8. const uint8_t chipSelect = SS;
  9. //------------------------------------------------------------------------------
  10. // File system object.
  11. SdFat sd;
  12. // Directory file.
  13. SdFile root;
  14. // Use for file creation in folders.
  15. SdFile file;
  16. // Create a Serial output stream.
  17. ArduinoOutStream cout(Serial);
  18. // Buffer for Serial input.
  19. char cinBuf[40];
  20. // Create a serial input stream.
  21. ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
  22. //==============================================================================
  23. // Error messages stored in flash.
  24. #define error(msg) sd.errorHalt(F(msg))
  25. //------------------------------------------------------------------------------
  26. void setup() {
  27. Serial.begin(9600);
  28. // Wait for USB Serial
  29. while (!Serial) {
  30. SysCall::yield();
  31. }
  32. delay(1000);
  33. cout << F("Type any character to start\n");
  34. // Wait for input line and discard.
  35. cin.readline();
  36. cout << endl;
  37. // Initialize at the highest speed supported by the board that is
  38. // not over 50 MHz. Try a lower speed if SPI errors occur.
  39. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  40. sd.initErrorHalt();
  41. }
  42. if (sd.exists("Folder1")
  43. || sd.exists("Folder1/file1.txt")
  44. || sd.exists("Folder1/File2.txt")) {
  45. error("Please remove existing Folder1, file1.txt, and File2.txt");
  46. }
  47. int rootFileCount = 0;
  48. if (!root.open("/")) {
  49. error("open root failed");
  50. }
  51. while (file.openNext(&root, O_RDONLY)) {
  52. if (!file.isHidden()) {
  53. rootFileCount++;
  54. }
  55. file.close();
  56. if (rootFileCount > 10) {
  57. error("Too many files in root. Please use an empty SD.");
  58. }
  59. }
  60. if (rootFileCount) {
  61. cout << F("\nPlease use an empty SD for best results.\n\n");
  62. delay(1000);
  63. }
  64. // Create a new folder.
  65. if (!sd.mkdir("Folder1")) {
  66. error("Create Folder1 failed");
  67. }
  68. cout << F("Created Folder1\n");
  69. // Create a file in Folder1 using a path.
  70. if (!file.open("Folder1/file1.txt", O_WRONLY | O_CREAT)) {
  71. error("create Folder1/file1.txt failed");
  72. }
  73. file.close();
  74. cout << F("Created Folder1/file1.txt\n");
  75. // Change volume working directory to Folder1.
  76. if (!sd.chdir("Folder1")) {
  77. error("chdir failed for Folder1.\n");
  78. }
  79. cout << F("chdir to Folder1\n");
  80. // Create File2.txt in current directory.
  81. if (!file.open("File2.txt", O_WRONLY | O_CREAT)) {
  82. error("create File2.txt failed");
  83. }
  84. file.close();
  85. cout << F("Created File2.txt in current directory\n");
  86. cout << F("\nList of files on the SD.\n");
  87. sd.ls("/", LS_R);
  88. // Remove files from current directory.
  89. if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) {
  90. error("remove failed");
  91. }
  92. cout << F("\nfile1.txt and File2.txt removed.\n");
  93. // Change current directory to root.
  94. if (!sd.chdir()) {
  95. error("chdir to root failed.\n");
  96. }
  97. cout << F("\nList of files on the SD.\n");
  98. sd.ls(LS_R);
  99. // Remove Folder1.
  100. if (!sd.rmdir("Folder1")) {
  101. error("rmdir for Folder1 failed\n");
  102. }
  103. cout << F("\nFolder1 removed.\n");
  104. cout << F("\nList of files on the SD.\n");
  105. sd.ls(LS_R);
  106. cout << F("Done!\n");
  107. }
  108. //------------------------------------------------------------------------------
  109. // Nothing happens in loop.
  110. void loop() {}