Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

124 linhas
3.2KB

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