clock.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "display.h"
  2. // RTC
  3. //#include <Wire.h>
  4. #if RTC_ENABLED == 1
  5. #include "RTClib.h"
  6. #define DESIRED_SQ_WAVE SquareWave32kHz
  7. #endif // RTC_ENABLED
  8. // Real Time Clock
  9. #if RTC_ENABLED == 1
  10. RTC_DS1307 rtc;
  11. DateTime currentTime;
  12. #endif // RTC_ENABLED
  13. uint32_t prevSeparatorTime = 0;
  14. uint32_t separatorInterval = 500;
  15. /*
  16. Setup Functions
  17. */
  18. // setup the RTC driver
  19. void _setupTime () {
  20. #if RTC_ENABLED == 1
  21. rtc.begin();
  22. if (! rtc.isrunning()) {
  23. // Serial.println("RTC is NOT running! Setting time to program compile time");
  24. // following line sets the RTC to the date & time this program was compiled
  25. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  26. }
  27. // set the desired square wave (if not already set)
  28. // if (rtc.readSqwPinMode() != DESIRED_SQ_WAVE) {
  29. // rtc.writeSqwPinMode(DESIRED_SQ_WAVE);
  30. // }
  31. #endif // RTC_ENABLED
  32. }
  33. // setup the clock sub-system
  34. void setupClock () {
  35. // RTC Setup //
  36. _setupTime();
  37. // 7-segment setup //
  38. setupDisplay();
  39. }
  40. // handler functions //
  41. void handleTime() {
  42. boolean bDay = false;
  43. #if RTC_ENABLED == 1
  44. currentTime = rtc.now();
  45. if (currentTime.hour() > 24 || currentTime.minute() > 60) {
  46. // handle the case where RTC cannot be read
  47. setError();
  48. }
  49. else {
  50. // find the setting for the day-night indicator
  51. if (currentTime.hour() >= 6 && currentTime.hour() < 18) {
  52. bDay = true;
  53. }
  54. // onion.io: TODO: lc should only be updated if rtc minute has changed since last time
  55. setTime(currentTime.hour(), currentTime.minute(), mcuConfig.twelveHMode, mcuConfig.dayNightIndicator, bDay );
  56. }
  57. #else
  58. setTime(2, 14, mcuConfig.twelveHMode, mcuConfig.dayNightIndicator, bDay );
  59. #endif // RTC_ENABLED
  60. // interval for flashing time display separator colon
  61. if (millis() - prevSeparatorTime >= separatorInterval) {
  62. flipTimeDisplaySeparator();
  63. // prepare for next interval
  64. prevSeparatorTime = millis();
  65. }
  66. }
  67. // helper functions
  68. void setRtcTime(long timestamp) {
  69. #if RTC_ENABLED == 1
  70. rtc.adjust(DateTime(timestamp));
  71. #endif // RTC_ENABLED
  72. // syncronize separator blink
  73. prevSeparatorTime = millis();
  74. }
  75. uint32_t getEpoch() {
  76. #if RTC_ENABLED == 1
  77. return (currentTime.unixtime());
  78. #else
  79. return (1517497971);
  80. #endif // RTC_ENABLED
  81. }
  82. uint32_t getSqWvSetting() {
  83. #if RTC_ENABLED == 1
  84. return (rtc.readSqwPinMode());
  85. #else
  86. return (0xff);
  87. #endif // RTC_ENABLED
  88. }