MinimumSizeSdReader.ino 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Create a text file on the SD with this path using short 8.3 names.
  2. #define SFN_PATH "/DIR/TEST.TXT"
  3. // Modify CS_PIN for your chip select pin.
  4. #define CS_PIN SS
  5. // Set USE_SD_H to one for SD.h or zero for SdFat.
  6. #define USE_SD_H 0
  7. #if USE_SD_H
  8. #include "SD.h"
  9. File file;
  10. #else
  11. #include "SdFat.h"
  12. // Setting ENABLE_DEDICATED_SPI to zero saves over 200 more bytes.
  13. #if ENABLE_DEDICATED_SPI
  14. #warning \
  15. "Set ENABLE_DEDICATED_SPI zero in SdFat/src/SdFatConfig.h for minimum size"
  16. #endif // ENABLE_DEDICATED_SPI
  17. // Insure FAT16/FAT32 only.
  18. SdFat32 SD;
  19. // FatFile does not support Stream functions, just simple read/write.
  20. FatFile file;
  21. #endif
  22. void error(const char* msg) {
  23. Serial.println(msg);
  24. while (true) {
  25. }
  26. }
  27. void setup() {
  28. int n;
  29. char buf[4];
  30. Serial.begin(9600);
  31. while (!Serial) {
  32. }
  33. Serial.println("Type any character to begin");
  34. while (!Serial.available()) {
  35. }
  36. if (!SD.begin(CS_PIN)) error("SD.begin");
  37. #if USE_SD_H
  38. file = SD.open(SFN_PATH);
  39. if (!file) error("open");
  40. #else
  41. // Open existing file with a path of 8.3 names.
  42. // Directories will be opened O_RDONLY files O_RDWR.
  43. if (!file.openExistingSFN(SFN_PATH)) error("open");
  44. #endif
  45. while ((n = file.read(buf, sizeof(buf)))) {
  46. Serial.write(buf, n);
  47. }
  48. // close() is only needed if you write to the file. For example, read
  49. // config data, modify the data, rewind the file and write the data.
  50. // file.close();
  51. }
  52. void loop() {}