Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

176 rindas
4.3KB

  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. // Wait for USB Serial
  69. while (!Serial) {
  70. SysCall::yield();
  71. }
  72. cout << F("Type any character to start\n");
  73. while (!Serial.available()) {
  74. SysCall::yield();
  75. }
  76. // Initialize at the highest speed supported by the board that is
  77. // not over 50 MHz. Try a lower speed if SPI errors occur.
  78. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  79. sd.initErrorHalt();
  80. }
  81. // remove files if they exist
  82. sd.remove("callback.txt");
  83. sd.remove("default.txt");
  84. sd.remove("stamp.txt");
  85. // create a new file with default timestamps
  86. if (!file.open("default.txt", O_CREAT | O_WRITE)) {
  87. error("open default.txt failed");
  88. }
  89. cout << F("\nOpen with default times\n");
  90. printTimestamps(file);
  91. // close file
  92. file.close();
  93. /*
  94. * Test the date time callback function.
  95. *
  96. * dateTimeCallback() sets the function
  97. * that is called when a file is created
  98. * or when a file's directory entry is
  99. * modified by sync().
  100. *
  101. * The callback can be disabled by the call
  102. * SdFile::dateTimeCallbackCancel()
  103. */
  104. // set date time callback function
  105. SdFile::dateTimeCallback(dateTime);
  106. // create a new file with callback timestamps
  107. if (!file.open("callback.txt", O_CREAT | O_WRITE)) {
  108. error("open callback.txt failed");
  109. }
  110. cout << ("\nOpen with callback times\n");
  111. printTimestamps(file);
  112. // change call back date
  113. day += 1;
  114. // must add two to see change since FAT second field is 5-bits
  115. second += 2;
  116. // modify file by writing a byte
  117. file.write('t');
  118. // force dir update
  119. file.sync();
  120. cout << F("\nTimes after write\n");
  121. printTimestamps(file);
  122. // close file
  123. file.close();
  124. /*
  125. * Test timestamp() function
  126. *
  127. * Cancel callback so sync will not
  128. * change access/modify timestamp
  129. */
  130. SdFile::dateTimeCallbackCancel();
  131. // create a new file with default timestamps
  132. if (!file.open("stamp.txt", O_CREAT | O_WRITE)) {
  133. error("open stamp.txt failed");
  134. }
  135. // set creation date time
  136. if (!file.timestamp(T_CREATE, 2014, 11, 10, 1, 2, 3)) {
  137. error("set create time failed");
  138. }
  139. // set write/modification date time
  140. if (!file.timestamp(T_WRITE, 2014, 11, 11, 4, 5, 6)) {
  141. error("set write time failed");
  142. }
  143. // set access date
  144. if (!file.timestamp(T_ACCESS, 2014, 11, 12, 7, 8, 9)) {
  145. error("set access time failed");
  146. }
  147. cout << F("\nTimes after timestamp() calls\n");
  148. printTimestamps(file);
  149. file.close();
  150. cout << F("\nDone\n");
  151. }
  152. void loop() {}