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.

163 satır
4.1KB

  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. SdFat sd;
  9. SdFile 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-14
  23. uint16_t year = 2014;
  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(SdFile& 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. SysCall::yield();
  62. }
  63. cout << F("Type any character to start\n");
  64. while (!Serial.available()) {
  65. SysCall::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, 2014, 11, 10, 1, 2, 3)) {
  128. error("set create time failed");
  129. }
  130. // set write/modification date time
  131. if (!file.timestamp(T_WRITE, 2014, 11, 11, 4, 5, 6)) {
  132. error("set write time failed");
  133. }
  134. // set access date
  135. if (!file.timestamp(T_ACCESS, 2014, 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() {}