OpenNext.ino 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Print size, modify date/time, and name for all files in root.
  3. */
  4. #include "SdFat.h"
  5. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  6. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  7. #define SD_FAT_TYPE 0
  8. /*
  9. Change the value of SD_CS_PIN if you are using SPI and
  10. your hardware does not use the default value, SS.
  11. Common values are:
  12. Arduino Ethernet shield: pin 4
  13. Sparkfun SD shield: pin 8
  14. Adafruit SD shields and modules: pin 10
  15. */
  16. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  17. #ifndef SDCARD_SS_PIN
  18. const uint8_t SD_CS_PIN = SS;
  19. #else // SDCARD_SS_PIN
  20. // Assume built-in SD is used.
  21. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  22. #endif // SDCARD_SS_PIN
  23. // Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur.
  24. #define SPI_CLOCK SD_SCK_MHZ(50)
  25. // Try to select the best SD card configuration.
  26. #if HAS_SDIO_CLASS
  27. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  28. #elif ENABLE_DEDICATED_SPI
  29. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
  30. #else // HAS_SDIO_CLASS
  31. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
  32. #endif // HAS_SDIO_CLASS
  33. #if SD_FAT_TYPE == 0
  34. SdFat sd;
  35. File dir;
  36. File file;
  37. #elif SD_FAT_TYPE == 1
  38. SdFat32 sd;
  39. File32 dir;
  40. File32 file;
  41. #elif SD_FAT_TYPE == 2
  42. SdExFat sd;
  43. ExFile dir;
  44. ExFile file;
  45. #elif SD_FAT_TYPE == 3
  46. SdFs sd;
  47. FsFile dir;
  48. FsFile file;
  49. #else // SD_FAT_TYPE
  50. #error invalid SD_FAT_TYPE
  51. #endif // SD_FAT_TYPE
  52. //------------------------------------------------------------------------------
  53. // Store error strings in flash to save RAM.
  54. #define error(s) sd.errorHalt(&Serial, F(s))
  55. //------------------------------------------------------------------------------
  56. void setup() {
  57. Serial.begin(9600);
  58. // Wait for USB Serial
  59. while (!Serial) {
  60. yield();
  61. }
  62. Serial.println("Type any character to start");
  63. while (!Serial.available()) {
  64. yield();
  65. }
  66. // Initialize the SD.
  67. if (!sd.begin(SD_CONFIG)) {
  68. sd.initErrorHalt(&Serial);
  69. }
  70. // Open root directory
  71. if (!dir.open("/")) {
  72. error("dir.open failed");
  73. }
  74. // Open next file in root.
  75. // Warning, openNext starts at the current position of dir so a
  76. // rewind may be necessary in your application.
  77. while (file.openNext(&dir, O_RDONLY)) {
  78. file.printFileSize(&Serial);
  79. Serial.write(' ');
  80. file.printModifyDateTime(&Serial);
  81. Serial.write(' ');
  82. file.printName(&Serial);
  83. if (file.isDir()) {
  84. // Indicate a directory.
  85. Serial.write('/');
  86. }
  87. Serial.println();
  88. file.close();
  89. }
  90. if (dir.getError()) {
  91. Serial.println("openNext failed");
  92. } else {
  93. Serial.println("Done!");
  94. }
  95. }
  96. //------------------------------------------------------------------------------
  97. void loop() {}