Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

168 lines
4.3KB

  1. /*
  2. * This sketch tests the dateTimeCallback() function
  3. * and the timestamp() function.
  4. */
  5. #include <SdFat.h>
  6. SdFat sd;
  7. SdFile file;
  8. // Default SD chip select is SS pin
  9. const uint8_t chipSelect = SS;
  10. // create Serial stream
  11. ArduinoOutStream cout(Serial);
  12. //------------------------------------------------------------------------------
  13. // store error strings in flash to save RAM
  14. #define error(s) sd.errorHalt_P(PSTR(s))
  15. //------------------------------------------------------------------------------
  16. /*
  17. * date/time values for debug
  18. * normally supplied by a real-time clock or GPS
  19. */
  20. // date 1-Oct-09
  21. uint16_t year = 2009;
  22. uint8_t month = 10;
  23. uint8_t day = 1;
  24. // time 20:30:40
  25. uint8_t hour = 20;
  26. uint8_t minute = 30;
  27. uint8_t second = 40;
  28. //------------------------------------------------------------------------------
  29. /*
  30. * User provided date time callback function.
  31. * See SdFile::dateTimeCallback() for usage.
  32. */
  33. void dateTime(uint16_t* date, uint16_t* time) {
  34. // User gets date and time from GPS or real-time
  35. // clock in real callback function
  36. // return date using FAT_DATE macro to format fields
  37. *date = FAT_DATE(year, month, day);
  38. // return time using FAT_TIME macro to format fields
  39. *time = FAT_TIME(hour, minute, second);
  40. }
  41. //------------------------------------------------------------------------------
  42. /*
  43. * Function to print all timestamps.
  44. */
  45. void printTimestamps(SdFile& f) {
  46. dir_t d;
  47. if (!f.dirEntry(&d)) error("f.dirEntry failed");
  48. cout << pstr("Creation: ");
  49. f.printFatDate(d.creationDate);
  50. cout << ' ';
  51. f.printFatTime(d.creationTime);
  52. cout << endl;
  53. cout << pstr("Modify: ");
  54. f.printFatDate(d.lastWriteDate);
  55. cout <<' ';
  56. f.printFatTime(d.lastWriteTime);
  57. cout << endl;
  58. cout << pstr("Access: ");
  59. f.printFatDate(d.lastAccessDate);
  60. cout << endl;
  61. }
  62. //------------------------------------------------------------------------------
  63. void setup(void) {
  64. Serial.begin(9600);
  65. while (!Serial) {} // wait for Leonardo
  66. cout << pstr("Type any character to start\n");
  67. while (!Serial.available());
  68. delay(400); // catch Due reset problem
  69. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  70. // breadboards. use SPI_FULL_SPEED for better performance.
  71. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  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_CREAT | O_WRITE)) {
  78. error("open DEFAULT.TXT failed");
  79. }
  80. cout << pstr("\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_CREAT | O_WRITE)) {
  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 << pstr("\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_CREAT | O_WRITE)) {
  124. error("open STAMP.TXT failed");
  125. }
  126. // set creation date time
  127. if (!file.timestamp(T_CREATE, 2009, 11, 10, 1, 2, 3)) {
  128. error("set create time failed");
  129. }
  130. // set write/modification date time
  131. if (!file.timestamp(T_WRITE, 2009, 11, 11, 4, 5, 6)) {
  132. error("set write time failed");
  133. }
  134. // set access date
  135. if (!file.timestamp(T_ACCESS, 2009, 11, 12, 7, 8, 9)) {
  136. error("set access time failed");
  137. }
  138. cout << pstr("\nTimes after timestamp() calls\n");
  139. printTimestamps(file);
  140. file.close();
  141. cout << pstr("\nDone\n");
  142. }
  143. void loop(void){}