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.

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