RTClib.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Code by JeeLabs http://news.jeelabs.org/code/
  2. // Released to the public domain! Enjoy!
  3. #ifndef _RTCLIB_H_
  4. #define _RTCLIB_H_
  5. #include <Arduino.h>
  6. // onion.io: removed TimeSpan class to save space
  7. // class TimeSpan;
  8. #define PCF8523_ADDRESS 0x68
  9. #define PCF8523_CLKOUTCONTROL 0x0F
  10. #define PCF8523_CONTROL_3 0x02
  11. #define DS1307_ADDRESS 0x68
  12. #define DS1307_CONTROL 0x07
  13. #define DS1307_NVRAM 0x08
  14. #define DS3231_ADDRESS 0x68
  15. #define DS3231_CONTROL 0x0E
  16. #define DS3231_STATUSREG 0x0F
  17. #define SECONDS_PER_DAY 86400L
  18. #define SECONDS_FROM_1970_TO_2000 946684800
  19. // Simple general-purpose date/time class (no TZ / DST / leap second handling!)
  20. class DateTime {
  21. public:
  22. DateTime (uint32_t t =0);
  23. DateTime (uint16_t year, uint8_t month, uint8_t day,
  24. uint8_t hour =0, uint8_t min =0, uint8_t sec =0);
  25. DateTime (const DateTime& copy);
  26. // onion.io: removed to save space
  27. // DateTime (const char* date, const char* time);
  28. DateTime (const __FlashStringHelper* date, const __FlashStringHelper* time);
  29. uint16_t year() const { return 2000 + yOff; }
  30. uint8_t month() const { return m; }
  31. uint8_t day() const { return d; }
  32. uint8_t hour() const { return hh; }
  33. uint8_t minute() const { return mm; }
  34. uint8_t second() const { return ss; }
  35. uint8_t dayOfTheWeek() const;
  36. // 32-bit times as seconds since 1/1/2000
  37. long secondstime() const;
  38. // 32-bit times as seconds since 1/1/1970
  39. uint32_t unixtime(void) const;
  40. // onion.io: removed TimeSpan class to save space
  41. // DateTime operator+(const TimeSpan& span);
  42. // DateTime operator-(const TimeSpan& span);
  43. // TimeSpan operator-(const DateTime& right);
  44. protected:
  45. uint8_t yOff, m, d, hh, mm, ss;
  46. };
  47. // onion.io: removed TimeSpan class to save space
  48. // // Timespan which can represent changes in time with seconds accuracy.
  49. // class TimeSpan {
  50. // public:
  51. // TimeSpan (int32_t seconds = 0);
  52. // TimeSpan (int16_t days, int8_t hours, int8_t minutes, int8_t seconds);
  53. // TimeSpan (const TimeSpan& copy);
  54. // int16_t days() const { return _seconds / 86400L; }
  55. // int8_t hours() const { return _seconds / 3600 % 24; }
  56. // int8_t minutes() const { return _seconds / 60 % 60; }
  57. // int8_t seconds() const { return _seconds % 60; }
  58. // int32_t totalseconds() const { return _seconds; }
  59. //
  60. // TimeSpan operator+(const TimeSpan& right);
  61. // TimeSpan operator-(const TimeSpan& right);
  62. //
  63. // protected:
  64. // int32_t _seconds;
  65. // };
  66. // RTC based on the DS1307 chip connected via I2C and the Wire library
  67. enum Ds1307SqwPinMode {
  68. OFF = 0x00,
  69. ON = 0x80,
  70. SquareWave1HZ = 0x10,
  71. SquareWave4kHz = 0x11,
  72. SquareWave8kHz = 0x12,
  73. SquareWave32kHz = 0x13
  74. };
  75. class RTC_DS1307 {
  76. public:
  77. boolean begin(void);
  78. static void adjust(const DateTime& dt);
  79. uint8_t isrunning(void);
  80. static DateTime now();
  81. static Ds1307SqwPinMode readSqwPinMode();
  82. static void writeSqwPinMode(Ds1307SqwPinMode mode);
  83. uint8_t readnvram(uint8_t address);
  84. void readnvram(uint8_t* buf, uint8_t size, uint8_t address);
  85. void writenvram(uint8_t address, uint8_t data);
  86. void writenvram(uint8_t address, uint8_t* buf, uint8_t size);
  87. };
  88. // onion.io: removed DS3231 class to save space
  89. // RTC based on the DS3231 chip connected via I2C and the Wire library
  90. // enum Ds3231SqwPinMode { DS3231_OFF = 0x01, DS3231_SquareWave1Hz = 0x00, DS3231_SquareWave1kHz = 0x08, DS3231_SquareWave4kHz = 0x10, DS3231_SquareWave8kHz = 0x18 };
  91. //
  92. // class RTC_DS3231 {
  93. // public:
  94. // boolean begin(void);
  95. // static void adjust(const DateTime& dt);
  96. // bool lostPower(void);
  97. // static DateTime now();
  98. // static Ds3231SqwPinMode readSqwPinMode();
  99. // static void writeSqwPinMode(Ds3231SqwPinMode mode);
  100. // };
  101. // onion.io: removed PCF8523 class to save space
  102. // // RTC based on the PCF8523 chip connected via I2C and the Wire library
  103. // enum Pcf8523SqwPinMode { PCF8523_OFF = 7, PCF8523_SquareWave1HZ = 6, PCF8523_SquareWave32HZ = 5, PCF8523_SquareWave1kHz = 4, PCF8523_SquareWave4kHz = 3, PCF8523_SquareWave8kHz = 2, PCF8523_SquareWave16kHz = 1, PCF8523_SquareWave32kHz = 0 };
  104. //
  105. // class RTC_PCF8523 {
  106. // public:
  107. // boolean begin(void);
  108. // void adjust(const DateTime& dt);
  109. // boolean initialized(void);
  110. // static DateTime now();
  111. //
  112. // Pcf8523SqwPinMode readSqwPinMode();
  113. // void writeSqwPinMode(Pcf8523SqwPinMode mode);
  114. // };
  115. // RTC using the internal millis() clock, has to be initialized before use
  116. // NOTE: this clock won't be correct once the millis() timer rolls over (>49d?)
  117. class RTC_Millis {
  118. public:
  119. static void begin(const DateTime& dt) { adjust(dt); }
  120. static void adjust(const DateTime& dt);
  121. static DateTime now();
  122. protected:
  123. static long offset;
  124. };
  125. #endif // _RTCLIB_H_