選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

141 行
3.9KB

  1. // Test of time-stamp callback.
  2. // Set the callback with this statement.
  3. // FsDateTime::setCallback(dateTime);
  4. #include "SdFat.h"
  5. // https://github.com/adafruit/RTClib
  6. #include "RTClib.h"
  7. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  8. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  9. #define SD_FAT_TYPE 0
  10. /*
  11. Change the value of SD_CS_PIN if you are using SPI and
  12. your hardware does not use the default value, SS.
  13. Common values are:
  14. Arduino Ethernet shield: pin 4
  15. Sparkfun SD shield: pin 8
  16. Adafruit SD shields and modules: pin 10
  17. */
  18. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  19. #ifndef SDCARD_SS_PIN
  20. const uint8_t SD_CS_PIN = SS;
  21. #else // SDCARD_SS_PIN
  22. // Assume built-in SD is used.
  23. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  24. #endif // SDCARD_SS_PIN
  25. // Try to select the best SD card configuration.
  26. #if HAS_SDIO_CLASS
  27. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  28. #elif ENABLE_DEDICATED_SPI
  29. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  30. #else // HAS_SDIO_CLASS
  31. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  32. #endif // HAS_SDIO_CLASS
  33. #if SD_FAT_TYPE == 0
  34. SdFat sd;
  35. File file;
  36. #elif SD_FAT_TYPE == 1
  37. SdFat32 sd;
  38. File32 file;
  39. #elif SD_FAT_TYPE == 2
  40. SdExFat sd;
  41. ExFile file;
  42. #elif SD_FAT_TYPE == 3
  43. SdFs sd;
  44. FsFile file;
  45. #else // SD_FAT_TYPE
  46. #error Invalid SD_FAT_TYPE
  47. #endif // SD_FAT_TYPE
  48. RTC_DS1307 rtc;
  49. //------------------------------------------------------------------------------
  50. // Call back for file timestamps. Only called for file create and sync().
  51. void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
  52. DateTime now = rtc.now();
  53. // Return date using FS_DATE macro to format fields.
  54. *date = FS_DATE(now.year(), now.month(), now.day());
  55. // Return time using FS_TIME macro to format fields.
  56. *time = FS_TIME(now.hour(), now.minute(), now.second());
  57. // Return low time bits in units of 10 ms, 0 <= ms10 <= 199.
  58. *ms10 = now.second() & 1 ? 100 : 0;
  59. }
  60. //------------------------------------------------------------------------------
  61. void printField(Print* pr, char sep, uint8_t v) {
  62. if (sep) {
  63. pr->write(sep);
  64. }
  65. if (v < 10) {
  66. pr->write('0');
  67. }
  68. pr->print(v);
  69. }
  70. //------------------------------------------------------------------------------
  71. void printNow(Print* pr) {
  72. DateTime now = rtc.now();
  73. pr->print(now.year());
  74. printField(pr, '-',now.month());
  75. printField(pr, '-',now.day());
  76. printField(pr, ' ',now.hour());
  77. printField(pr, ':',now.minute());
  78. printField(pr, ':',now.second());
  79. }
  80. //------------------------------------------------------------------------------
  81. void setup() {
  82. Serial.begin(9600);
  83. while (!Serial) {
  84. yield();
  85. }
  86. Serial.println(F("Type any character to begin"));
  87. while (!Serial.available()) {
  88. yield();
  89. }
  90. if (!rtc.begin()) {
  91. Serial.println(F("rtc.begin failed"));
  92. return;
  93. }
  94. if (!rtc.isrunning()) {
  95. Serial.println("RTC is NOT running!");
  96. return;
  97. // following line sets the RTC to the date & time this sketch was compiled
  98. // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  99. // This line sets the RTC with an explicit date & time, for example to set
  100. // January 21, 2014 at 3am you would call:
  101. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  102. }
  103. Serial.print(F("DateTime::now "));
  104. printNow(&Serial);
  105. Serial.println();
  106. // Set callback
  107. FsDateTime::setCallback(dateTime);
  108. if (!sd.begin(SD_CONFIG)) {
  109. sd.initErrorHalt(&Serial);
  110. }
  111. // Remove old version to set create time.
  112. if (sd.exists("RtcTest.txt")) {
  113. sd.remove("RtcTest.txt");
  114. }
  115. if (!file.open("RtcTest.txt", FILE_WRITE)) {
  116. Serial.println(F("file.open failed"));
  117. return;
  118. }
  119. // Print current date time to file.
  120. file.print(F("Test file at: "));
  121. printNow(&file);
  122. file.println();
  123. file.close();
  124. // List files in SD root.
  125. sd.ls(LS_DATE | LS_SIZE);
  126. Serial.println(F("Done"));
  127. }
  128. //------------------------------------------------------------------------------
  129. void loop() {
  130. }