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.

LongFileName.ino 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Example use of lfnOpenNext and open by index.
  2. // You can use test files located in
  3. // SdFat/examples/LongFileName/testFiles.
  4. #include<SPI.h>
  5. #include "SdFat.h"
  6. #include "FreeStack.h"
  7. // SD card chip select pin.
  8. const uint8_t SD_CS_PIN = SS;
  9. SdFat sd;
  10. SdFile file;
  11. SdFile dirFile;
  12. // Number of files found.
  13. uint16_t n = 0;
  14. // Max of ten files since files are selected with a single digit.
  15. const uint16_t nMax = 10;
  16. // Position of file's directory entry.
  17. uint16_t dirIndex[nMax];
  18. //------------------------------------------------------------------------------
  19. void setup() {
  20. Serial.begin(9600);
  21. while (!Serial) {}
  22. delay(1000);
  23. // Print the location of some test files.
  24. Serial.println(F("\r\n"
  25. "You can use test files located in\r\n"
  26. "SdFat/examples/LongFileName/testFiles"));
  27. // Initialize at the highest speed supported by the board that is
  28. // not over 50 MHz. Try a lower speed if SPI errors occur.
  29. if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) {
  30. sd.initErrorHalt();
  31. }
  32. Serial.print(F("FreeStack: "));
  33. Serial.println(FreeStack());
  34. Serial.println();
  35. // List files in root directory.
  36. if (!dirFile.open("/", O_RDONLY)) {
  37. sd.errorHalt("open root failed");
  38. }
  39. while (n < nMax && file.openNext(&dirFile, O_RDONLY)) {
  40. // Skip directories and hidden files.
  41. if (!file.isSubDir() && !file.isHidden()) {
  42. // Save dirIndex of file in directory.
  43. dirIndex[n] = file.dirIndex();
  44. // Print the file number and name.
  45. Serial.print(n++);
  46. Serial.write(' ');
  47. file.printName(&Serial);
  48. Serial.println();
  49. }
  50. file.close();
  51. }
  52. }
  53. //------------------------------------------------------------------------------
  54. void loop() {
  55. int c;
  56. // Read any existing Serial data.
  57. do {
  58. delay(10);
  59. } while (Serial.available() && Serial.read() >= 0);
  60. Serial.print(F("\r\nEnter File Number: "));
  61. while (!Serial.available()) {
  62. SysCall::yield();
  63. }
  64. c = Serial.read();
  65. uint8_t i = c - '0';
  66. if (!isdigit(c) || i >= n) {
  67. Serial.println(F("Invald number"));
  68. return;
  69. }
  70. Serial.println(i);
  71. if (!file.open(&dirFile, dirIndex[i], O_RDONLY)) {
  72. sd.errorHalt(F("open"));
  73. }
  74. Serial.println();
  75. char last = 0;
  76. // Copy up to 500 characters to Serial.
  77. for (int k = 0; k < 500 && (c = file.read()) > 0; k++) {
  78. Serial.write(last = (char)c);
  79. }
  80. // Add new line if missing from last line.
  81. if (last != '\n') {
  82. Serial.println();
  83. }
  84. file.close();
  85. Serial.flush();
  86. delay(100);
  87. }