No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

directoryFunctions.ino 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Permit SD to be wiped if ALLOW_WIPE is true.
  10. const bool ALLOW_WIPE = false;
  11. // File system object.
  12. SdFat sd;
  13. // Use for file creation in folders.
  14. SdFile file;
  15. // Create a Serial output stream.
  16. ArduinoOutStream cout(Serial);
  17. // Buffer for Serial input.
  18. char cinBuf[40];
  19. // Create a serial input stream.
  20. ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
  21. //==============================================================================
  22. // Error messages stored in flash.
  23. #define error(msg) sd.errorHalt(F(msg))
  24. //------------------------------------------------------------------------------
  25. void setup() {
  26. Serial.begin(9600);
  27. while (!Serial) {} // wait for Leonardo
  28. delay(1000);
  29. cout << pstr("Type any character to start\n");
  30. // Wait for input line and discard.
  31. cin.readline();
  32. // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  33. // breadboards. use SPI_FULL_SPEED for better performance.
  34. if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) sd.initErrorHalt();
  35. // Check for empty SD.
  36. if (file.openNext(sd.vwd(), O_READ)) {
  37. cout << pstr("Found files/folders in the root directory.\n");
  38. if (!ALLOW_WIPE) {
  39. error("SD not empty, use a blank SD or set ALLOW_WIPE true.");
  40. } else {
  41. cout << pstr("Type: 'WIPE' to delete all SD files.\n");
  42. char buf[10];
  43. cin.readline();
  44. cin.get(buf, sizeof(buf));
  45. if (cin.fail() || strncmp(buf, "WIPE", 4) || buf[4] >= ' ') {
  46. error("Invalid WIPE input");
  47. }
  48. file.close();
  49. sd.vwd()->rmRfStar();
  50. cout << pstr("***SD wiped clean.***\n\n");
  51. }
  52. }
  53. // Create a new folder.
  54. if (!sd.mkdir("FOLDER1")) error("Create FOLDER1 failed");
  55. cout << pstr("Created FOLDER1\n");
  56. // Create a file in FOLDER1 using a path.
  57. if (!file.open("FOLDER1/FILE1.TXT", O_CREAT | O_WRITE)) {
  58. error("create FOLDER1/FILE1.TXT failed");
  59. }
  60. file.close();
  61. cout << pstr("Created FOLDER1/FILE1.TXT\n");
  62. // Change volume working directory to FOLDER1.
  63. if (!sd.chdir("FOLDER1")) error("chdir failed for FOLDER1.\n");
  64. cout << pstr("chdir to FOLDER1\n");
  65. // Create FILE2.TXT in current directory.
  66. if (!file.open("FILE2.TXT", O_CREAT | O_WRITE)) {
  67. error("create FILE2.TXT failed");
  68. }
  69. file.close();
  70. cout << pstr("Created FILE2.TXT in current directory\n");
  71. cout << pstr("List of files on the SD.\n");
  72. sd.ls("/", LS_R);
  73. // Remove files from current directory.
  74. if (!sd.remove("FILE1.TXT") || !sd.remove("FILE2.TXT")) error("remove failed");
  75. cout << pstr("\nFILE1.TXT and FILE2.TXT removed.\n");
  76. // Change current directory to root.
  77. if (!sd.chdir()) error("chdir to root failed.\n");
  78. cout << pstr("List of files on the SD.\n");
  79. sd.ls(LS_R);
  80. // Remove FOLDER1.
  81. if (!sd.rmdir("FOLDER1")) error("rmdir for FOLDER1 failed\n");
  82. cout << pstr("\nFOLDER1 removed, SD empty.\n");
  83. cout << pstr("Done!\n");
  84. }
  85. //------------------------------------------------------------------------------
  86. // Nothing happens in loop.
  87. void loop() {}