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.

94 line
2.2KB

  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. if (!sd.begin(SD_CS_PIN)) {
  28. sd.initErrorHalt();
  29. }
  30. Serial.print(F("FreeStack: "));
  31. Serial.println(FreeStack());
  32. Serial.println();
  33. // List files in root directory.
  34. if (!dirFile.open("/", O_READ)) {
  35. sd.errorHalt("open root failed");
  36. }
  37. while (n < nMax && file.openNext(&dirFile, O_READ)) {
  38. // Skip directories and hidden files.
  39. if (!file.isSubDir() && !file.isHidden()) {
  40. // Save dirIndex of file in directory.
  41. dirIndex[n] = file.dirIndex();
  42. // Print the file number and name.
  43. Serial.print(n++);
  44. Serial.write(' ');
  45. file.printName(&Serial);
  46. Serial.println();
  47. }
  48. file.close();
  49. }
  50. }
  51. //------------------------------------------------------------------------------
  52. void loop() {
  53. int c;
  54. // Discard any Serial input.
  55. while (Serial.read() > 0) {}
  56. Serial.print(F("\r\nEnter File Number: "));
  57. while ((c = Serial.read()) < 0) {};
  58. if (!isdigit(c) || (c -= '0') >= n) {
  59. Serial.println(F("Invald number"));
  60. return;
  61. }
  62. Serial.println(c);
  63. if (!file.open(&dirFile, dirIndex[c], O_READ)) {
  64. sd.errorHalt(F("open"));
  65. }
  66. Serial.println();
  67. char last;
  68. // Copy up to 500 characters to Serial.
  69. for (int i = 0; i < 500 && (c = file.read()) > 0; i++) {
  70. Serial.write(last = (char)c);
  71. }
  72. // Add new line if missing from last line.
  73. if (last != '\n') {
  74. Serial.println();
  75. }
  76. file.close();
  77. Serial.flush();
  78. delay(100);
  79. }