TimestampTest.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * This program tests the dateTimeCallback() function
  3. * and the timestamp() function.
  4. */
  5. #include <SPI.h>
  6. #include "SdFat.h"
  7. #include "sdios.h"
  8. SdFs sd;
  9. FsFile file;
  10. // Default SD chip select is SS pin
  11. const uint8_t chipSelect = SS;
  12. // create Serial stream
  13. ArduinoOutStream cout(Serial);
  14. //------------------------------------------------------------------------------
  15. // store error strings in flash to save RAM
  16. #define error(s) sd.errorHalt(F(s))
  17. //------------------------------------------------------------------------------
  18. /*
  19. * date/time values for debug
  20. * normally supplied by a real-time clock or GPS
  21. */
  22. // date 1-Oct-21
  23. uint16_t year = 2021;
  24. uint8_t month = 10;
  25. uint8_t day = 1;
  26. // time 20:30:40
  27. uint8_t hour = 20;
  28. uint8_t minute = 30;
  29. uint8_t second = 40;
  30. //------------------------------------------------------------------------------
  31. /*
  32. * User provided date time callback function.
  33. * See SdFile::dateTimeCallback() for usage.
  34. */
  35. void dateTime(uint16_t* date, uint16_t* time) {
  36. // User gets date and time from GPS or real-time
  37. // clock in real callback function
  38. // return date using FAT_DATE macro to format fields
  39. *date = FAT_DATE(year, month, day);
  40. // return time using FAT_TIME macro to format fields
  41. *time = FAT_TIME(hour, minute, second);
  42. }
  43. //------------------------------------------------------------------------------
  44. /*
  45. * Function to print all timestamps.
  46. */
  47. void printTimestamps(FsFile& f) {
  48. cout << F("Creation: ");
  49. f.printCreateDateTime(&Serial);
  50. cout << endl << F("Modify: ");
  51. f.printModifyDateTime(&Serial);
  52. cout << endl << F("Access: ");
  53. f.printAccessDateTime(&Serial);
  54. cout << endl;
  55. }
  56. //------------------------------------------------------------------------------
  57. void setup(void) {
  58. Serial.begin(9600);
  59. // Wait for USB Serial
  60. while (!Serial) {
  61. yield();
  62. }
  63. cout << F("Type any character to start\n");
  64. while (!Serial.available()) {
  65. yield();
  66. }
  67. // Initialize at the highest speed supported by the board that is
  68. // not over 50 MHz. Try a lower speed if SPI errors occur.
  69. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  70. sd.initErrorHalt();
  71. }
  72. // remove files if they exist
  73. sd.remove("callback.txt");
  74. sd.remove("default.txt");
  75. sd.remove("stamp.txt");
  76. // create a new file with default timestamps
  77. if (!file.open("default.txt", O_WRONLY | O_CREAT)) {
  78. error("open default.txt failed");
  79. }
  80. cout << F("\nOpen with default times\n");
  81. printTimestamps(file);
  82. // close file
  83. file.close();
  84. /*
  85. * Test the date time callback function.
  86. *
  87. * dateTimeCallback() sets the function
  88. * that is called when a file is created
  89. * or when a file's directory entry is
  90. * modified by sync().
  91. *
  92. * The callback can be disabled by the call
  93. * SdFile::dateTimeCallbackCancel()
  94. */
  95. // set date time callback function
  96. SdFile::dateTimeCallback(dateTime);
  97. // create a new file with callback timestamps
  98. if (!file.open("callback.txt", O_WRONLY | O_CREAT)) {
  99. error("open callback.txt failed");
  100. }
  101. cout << ("\nOpen with callback times\n");
  102. printTimestamps(file);
  103. // change call back date
  104. day += 1;
  105. // must add two to see change since FAT second field is 5-bits
  106. second += 2;
  107. // modify file by writing a byte
  108. file.write('t');
  109. // force dir update
  110. file.sync();
  111. cout << F("\nTimes after write\n");
  112. printTimestamps(file);
  113. // close file
  114. file.close();
  115. /*
  116. * Test timestamp() function
  117. *
  118. * Cancel callback so sync will not
  119. * change access/modify timestamp
  120. */
  121. SdFile::dateTimeCallbackCancel();
  122. // create a new file with default timestamps
  123. if (!file.open("stamp.txt", O_WRONLY | O_CREAT)) {
  124. error("open stamp.txt failed");
  125. }
  126. // set creation date time
  127. if (!file.timestamp(T_CREATE, 2021, 11, 10, 1, 2, 3)) {
  128. error("set create time failed");
  129. }
  130. // set write/modification date time
  131. if (!file.timestamp(T_WRITE, 2021, 11, 11, 4, 5, 6)) {
  132. error("set write time failed");
  133. }
  134. // set access date
  135. if (!file.timestamp(T_ACCESS, 2021, 11, 12, 7, 8, 9)) {
  136. error("set access time failed");
  137. }
  138. cout << F("\nTimes after timestamp() calls\n");
  139. printTimestamps(file);
  140. file.close();
  141. cout << F("\nDone\n");
  142. }
  143. void loop() {}