Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

108 lines
2.6KB

  1. /*
  2. * This program demonstrates use of SdFile::rename()
  3. * and SdFat::rename().
  4. */
  5. #include <SPI.h>
  6. #include "SdFat.h"
  7. #include "sdios.h"
  8. // SD chip select pin
  9. const uint8_t chipSelect = SS;
  10. // file system
  11. SdFat sd;
  12. // Serial print stream
  13. ArduinoOutStream cout(Serial);
  14. //------------------------------------------------------------------------------
  15. // store error strings in flash to save RAM
  16. #define error(s) sd.errorHalt(F(s))
  17. //------------------------------------------------------------------------------
  18. void setup() {
  19. Serial.begin(9600);
  20. // Wait for USB Serial
  21. while (!Serial) {
  22. SysCall::yield();
  23. }
  24. cout << F("Insert an empty SD. Type any character to start.") << endl;
  25. while (!Serial.available()) {
  26. SysCall::yield();
  27. }
  28. // Initialize at the highest speed supported by the board that is
  29. // not over 50 MHz. Try a lower speed if SPI errors occur.
  30. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  31. sd.initErrorHalt();
  32. }
  33. // Remove file/dirs from previous run.
  34. if (sd.exists("dir2/DIR3/NAME3.txt")) {
  35. cout << F("Removing /dir2/DIR3/NAME3.txt") << endl;
  36. if (!sd.remove("dir2/DIR3/NAME3.txt") ||
  37. !sd.rmdir("dir2/DIR3/") ||
  38. !sd.rmdir("dir2/")) {
  39. error("remove/rmdir failed");
  40. }
  41. }
  42. // create a file and write one line to the file
  43. SdFile file("Name1.txt", O_WRONLY | O_CREAT);
  44. if (!file.isOpen()) {
  45. error("Name1.txt");
  46. }
  47. file.println("A test line for Name1.txt");
  48. // rename the file name2.txt and add a line.
  49. // sd.vwd() is the volume working directory, root.
  50. if (!file.rename(sd.vwd(), "name2.txt")) {
  51. error("name2.txt");
  52. }
  53. file.println("A test line for name2.txt");
  54. // list files
  55. cout << F("------") << endl;
  56. sd.ls(LS_R);
  57. // make a new directory - "Dir1"
  58. if (!sd.mkdir("Dir1")) {
  59. error("Dir1");
  60. }
  61. // move file into Dir1, rename it NAME3.txt and add a line
  62. if (!file.rename(sd.vwd(), "Dir1/NAME3.txt")) {
  63. error("NAME3.txt");
  64. }
  65. file.println("A line for Dir1/NAME3.txt");
  66. // list files
  67. cout << F("------") << endl;
  68. sd.ls(LS_R);
  69. // make directory "dir2"
  70. if (!sd.mkdir("dir2")) {
  71. error("dir2");
  72. }
  73. // close file before rename(oldPath, newPath)
  74. file.close();
  75. // move Dir1 into dir2 and rename it DIR3
  76. if (!sd.rename("Dir1", "dir2/DIR3")) {
  77. error("dir2/DIR3");
  78. }
  79. // open file for append in new location and add a line
  80. if (!file.open("dir2/DIR3/NAME3.txt", O_WRONLY | O_APPEND)) {
  81. error("dir2/DIR3/NAME3.txt");
  82. }
  83. file.println("A line for dir2/DIR3/NAME3.txt");
  84. file.close();
  85. // list files
  86. cout << F("------") << endl;
  87. sd.ls(LS_R);
  88. cout << F("Done") << endl;
  89. }
  90. void loop() {}