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.

5 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if (!file.rename("name2.txt")) {
  50. error("name2.txt");
  51. }
  52. file.println("A test line for name2.txt");
  53. // list files
  54. cout << F("------") << endl;
  55. sd.ls(LS_R);
  56. // make a new directory - "Dir1"
  57. if (!sd.mkdir("Dir1")) {
  58. error("Dir1");
  59. }
  60. // move file into Dir1, rename it NAME3.txt and add a line
  61. if (!file.rename("Dir1/NAME3.txt")) {
  62. error("NAME3.txt");
  63. }
  64. file.println("A line for Dir1/NAME3.txt");
  65. // list files
  66. cout << F("------") << endl;
  67. sd.ls(LS_R);
  68. // make directory "dir2"
  69. if (!sd.mkdir("dir2")) {
  70. error("dir2");
  71. }
  72. // close file before rename(oldPath, newPath)
  73. file.close();
  74. // move Dir1 into dir2 and rename it DIR3
  75. if (!sd.rename("Dir1", "dir2/DIR3")) {
  76. error("dir2/DIR3");
  77. }
  78. // open file for append in new location and add a line
  79. if (!file.open("dir2/DIR3/NAME3.txt", O_WRONLY | O_APPEND)) {
  80. error("dir2/DIR3/NAME3.txt");
  81. }
  82. file.println("A line for dir2/DIR3/NAME3.txt");
  83. file.close();
  84. // list files
  85. cout << F("------") << endl;
  86. sd.ls(LS_R);
  87. cout << F("Done") << endl;
  88. }
  89. void loop() {}