Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

108 rindas
3.3KB

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