You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.2KB

  1. // Example use of openNextLFN 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 <SdFatUtil.h>
  7. // SD card chip select pin.
  8. const uint8_t SD_CS_PIN = SS;
  9. SdFat sd;
  10. SdFile file;
  11. // Number of files found.
  12. uint16_t n = 0;
  13. // Max of ten files since files are selected with a single digit.
  14. const uint16_t nMax = 10;
  15. // Position of file's directory entry.
  16. uint16_t dirIndex[nMax];
  17. //------------------------------------------------------------------------------
  18. void setup() {
  19. const size_t NAME_DIM = 50;
  20. char name[NAME_DIM];
  21. dir_t dir;
  22. Serial.begin(9600);
  23. while (!Serial) {}
  24. // Print the location of some test files.
  25. Serial.println(F("\r\n"
  26. "You can use test files located in\r\n"
  27. "SdFat/examples/LongFileName/testFiles"));
  28. if (!sd.begin(SD_CS_PIN)) sd.initErrorHalt();
  29. Serial.print(F("Free RAM: "));
  30. Serial.println(FreeRam());
  31. Serial.println();
  32. // List files in root directory. Volume working directory is initially root.
  33. sd.vwd()->rewind();
  34. while (n < nMax && file.openNextLFN(sd.vwd(), name, NAME_DIM, O_READ) > 0) {
  35. // Skip directories and hidden files.
  36. if (!file.isSubDir() && !file.isHidden()) {
  37. // Save dirIndex of file in directory.
  38. dirIndex[n] = file.dirIndex();
  39. // Print the file number and name.
  40. Serial.print(n++);
  41. Serial.write(' ');
  42. Serial.println(name);
  43. }
  44. file.close();
  45. }
  46. }
  47. //------------------------------------------------------------------------------
  48. void loop() {
  49. int c;
  50. // Discard any Serial input.
  51. while (Serial.read() > 0) {}
  52. Serial.print(F("\r\nEnter File Number: "));
  53. while ((c = Serial.read()) < 0) {};
  54. if (!isdigit(c) || (c -= '0') >= n) {
  55. Serial.println(F("Invald number"));
  56. return;
  57. }
  58. Serial.println(c);
  59. if (!file.open(sd.vwd(), dirIndex[c], O_READ)) {
  60. sd.errorHalt(F("open"));
  61. }
  62. Serial.println();
  63. char last;
  64. // Copy up to 500 characters to Serial.
  65. for (int i = 0; i < 500 && (c = file.read()) > 0; i++) {
  66. Serial.write(last = (char)c);
  67. }
  68. // Add new line if missing from last line.
  69. if (last != '\n') Serial.println();
  70. file.close();
  71. }