Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * This program 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)) {
  49. error("f.dirEntry failed");
  50. }
  51. cout << F("Creation: ");
  52. f.printFatDate(d.creationDate);
  53. cout << ' ';
  54. f.printFatTime(d.creationTime);
  55. cout << endl;
  56. cout << F("Modify: ");
  57. f.printFatDate(d.lastWriteDate);
  58. cout <<' ';
  59. f.printFatTime(d.lastWriteTime);
  60. cout << endl;
  61. cout << F("Access: ");
  62. f.printFatDate(d.lastAccessDate);
  63. cout << endl;
  64. }
  65. //------------------------------------------------------------------------------
  66. void setup(void) {
  67. Serial.begin(9600);
  68. while (!Serial) {} // wait for Leonardo
  69. cout << F("Type any character to start\n");
  70. while (!Serial.available());
  71. delay(400); // catch Due reset problem
  72. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  73. // breadboards. use SPI_FULL_SPEED for better performance.
  74. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  75. sd.initErrorHalt();
  76. }
  77. // remove files if they exist
  78. sd.remove("callback.txt");
  79. sd.remove("default.txt");
  80. sd.remove("stamp.txt");
  81. // create a new file with default timestamps
  82. if (!file.open("default.txt", O_CREAT | O_WRITE)) {
  83. error("open default.txt failed");
  84. }
  85. cout << F("\nOpen with default times\n");
  86. printTimestamps(file);
  87. // close file
  88. file.close();
  89. /*
  90. * Test the date time callback function.
  91. *
  92. * dateTimeCallback() sets the function
  93. * that is called when a file is created
  94. * or when a file's directory entry is
  95. * modified by sync().
  96. *
  97. * The callback can be disabled by the call
  98. * SdFile::dateTimeCallbackCancel()
  99. */
  100. // set date time callback function
  101. SdFile::dateTimeCallback(dateTime);
  102. // create a new file with callback timestamps
  103. if (!file.open("callback.txt", O_CREAT | O_WRITE)) {
  104. error("open callback.txt failed");
  105. }
  106. cout << ("\nOpen with callback times\n");
  107. printTimestamps(file);
  108. // change call back date
  109. day += 1;
  110. // must add two to see change since FAT second field is 5-bits
  111. second += 2;
  112. // modify file by writing a byte
  113. file.write('t');
  114. // force dir update
  115. file.sync();
  116. cout << F("\nTimes after write\n");
  117. printTimestamps(file);
  118. // close file
  119. file.close();
  120. /*
  121. * Test timestamp() function
  122. *
  123. * Cancel callback so sync will not
  124. * change access/modify timestamp
  125. */
  126. SdFile::dateTimeCallbackCancel();
  127. // create a new file with default timestamps
  128. if (!file.open("stamp.txt", O_CREAT | O_WRITE)) {
  129. error("open stamp.txt failed");
  130. }
  131. // set creation date time
  132. if (!file.timestamp(T_CREATE, 2014, 11, 10, 1, 2, 3)) {
  133. error("set create time failed");
  134. }
  135. // set write/modification date time
  136. if (!file.timestamp(T_WRITE, 2014, 11, 11, 4, 5, 6)) {
  137. error("set write time failed");
  138. }
  139. // set access date
  140. if (!file.timestamp(T_ACCESS, 2014, 11, 12, 7, 8, 9)) {
  141. error("set access time failed");
  142. }
  143. cout << F("\nTimes after timestamp() calls\n");
  144. printTimestamps(file);
  145. file.close();
  146. cout << F("\nDone\n");
  147. }
  148. void loop(void) {}