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.

ATS_SD_File.ino 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // modified from ArduinoTestSuite 0022 by William Greiman
  2. // Tests writing to and reading from a file, in particular the
  3. // the Stream implementation (e.g. read() and peek()).
  4. #include <SdFat.h>
  5. #include <SdFatTestSuite.h>
  6. SdFat SD;
  7. #define FILE_WRITE O_RDWR | O_CREAT | O_AT_END
  8. #define ATS_PrintTestStatus(msg, b) testVerify_P(b, PSTR(msg))
  9. void setup() {
  10. boolean b;
  11. SdFile f;
  12. uint32_t fs;
  13. testBegin();
  14. ATS_PrintTestStatus("SD.begin()", b = SD.begin());
  15. if (!b) goto done;
  16. SD.remove("test.txt");
  17. f.open("test.txt", FILE_WRITE);
  18. ATS_PrintTestStatus("SD.open()", f.isOpen());
  19. if (!f.isOpen()) goto done;
  20. f.print("abc");
  21. f.print("de");
  22. f.close();
  23. f.open("test.txt", FILE_WRITE);
  24. ATS_PrintTestStatus("SD.open()", f.isOpen());
  25. if (!f.isOpen()) goto done;
  26. f.print("fgh");
  27. f.close();
  28. f.open("test.txt", O_READ);
  29. ATS_PrintTestStatus("SD.open()", f.isOpen());
  30. if (!f.isOpen()) goto done;
  31. fs =f.fileSize();
  32. ATS_PrintTestStatus("read()", f.read() == 'a');
  33. ATS_PrintTestStatus("peek()", f.peek() == 'b');
  34. ATS_PrintTestStatus("read()", f.read() == 'b');
  35. ATS_PrintTestStatus("read()", f.read() == 'c');
  36. ATS_PrintTestStatus("peek()", f.peek() == 'd');
  37. ATS_PrintTestStatus("peek()", f.peek() == 'd');
  38. ATS_PrintTestStatus("peek()", f.peek() == 'd');
  39. ATS_PrintTestStatus("peek()", f.peek() == 'd');
  40. ATS_PrintTestStatus("read()", f.read() == 'd');
  41. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  42. ATS_PrintTestStatus("read()", f.read() == 'e');
  43. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  44. ATS_PrintTestStatus("peek()", f.peek() == 'f');
  45. ATS_PrintTestStatus("read()", f.read() == 'f');
  46. ATS_PrintTestStatus("peek()", f.peek() == 'g');
  47. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  48. ATS_PrintTestStatus("peek()", f.peek() == 'g');
  49. ATS_PrintTestStatus("read()", f.read() == 'g');
  50. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  51. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  52. ATS_PrintTestStatus("available()", f.curPosition() != fs);
  53. ATS_PrintTestStatus("peek()", f.peek() == 'h');
  54. ATS_PrintTestStatus("read()", f.read() == 'h');
  55. ATS_PrintTestStatus("available()", f.curPosition() == fs);
  56. ATS_PrintTestStatus("peek()", f.peek() == -1);
  57. ATS_PrintTestStatus("read()", f.read() == -1);
  58. ATS_PrintTestStatus("peek()", f.peek() == -1);
  59. ATS_PrintTestStatus("read()", f.read() == -1);
  60. f.close();
  61. SD.remove("test2.txt");
  62. f.open("test2.txt", FILE_WRITE);
  63. ATS_PrintTestStatus("SD.open()", f.isOpen());
  64. if (!f.isOpen()) goto done;
  65. f.print("ABC");
  66. f.close();
  67. f.open("test.txt", O_READ);
  68. ATS_PrintTestStatus("SD.open()", f.isOpen());
  69. if (!f.isOpen()) goto done;
  70. ATS_PrintTestStatus("peek()", f.peek() == 'a');
  71. f.close();
  72. f.open("test2.txt", O_READ);
  73. ATS_PrintTestStatus("SD.open()", f.isOpen());
  74. if (!f.isOpen()) goto done;
  75. ATS_PrintTestStatus("peek()", f.peek() == 'A');
  76. ATS_PrintTestStatus("read()", f.read() == 'A');
  77. f.close();
  78. done:
  79. testEnd();
  80. }
  81. void loop() {}