Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Timestamp.ino 4.3KB

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