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.

110 rivejä
2.6 KiB

  1. #include "Debug.h"
  2. #include "Rtc.h"
  3. #include "Pins.h"
  4. #include <Wire.h>
  5. #include <MD_DS3231.h>
  6. #define LOGGER_NAME "Rtc"
  7. namespace rtc {
  8. namespace details {
  9. void readTimeFromRegisters(tmElements_t &time) {
  10. time.Second = RTC.s;
  11. time.Minute = RTC.m;
  12. time.Hour = RTC.h;
  13. time.Wday = RTC.dow;
  14. time.Day = RTC.dd;
  15. time.Month = RTC.mm;
  16. time.Year = CalendarYrToTm(RTC.yyyy);
  17. }
  18. void writeTimeToRegisters(const tmElements_t &time) {
  19. RTC.s = time.Second;
  20. RTC.m = time.Minute;
  21. RTC.h = time.Hour;
  22. RTC.dow = time.Wday;
  23. RTC.dd = time.Day;
  24. RTC.mm = time.Month;
  25. RTC.yyyy = tmYearToCalendar(time.Year);
  26. }
  27. }
  28. void setup() {
  29. VERBOSE("setup");
  30. hardware::i2c::powerOn();
  31. RTC.control(DS3231_12H, DS3231_OFF); //24 hours clock
  32. RTC.control(DS3231_A1_INT_ENABLE, DS3231_OFF); //Alarm 1 OFF
  33. RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN OFF
  34. hardware::i2c::powerOff();
  35. //TODO : check wether the osc has been halted (meaning the battery could be dead)
  36. }
  37. float getTemperature() {
  38. hardware::i2c::powerOn();
  39. float temperature = RTC.readTempRegister();
  40. hardware::i2c::powerOff();
  41. return temperature;
  42. }
  43. timestamp_t getTime() {
  44. tmElements_t time;
  45. getTime(time);
  46. return makeTimestamp(time);
  47. }
  48. void getTime(tmElements_t &time) {
  49. hardware::i2c::powerOn();
  50. RTC.readTime();
  51. hardware::i2c::powerOff();
  52. details::readTimeFromRegisters(time);
  53. VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  54. }
  55. void setTime(const timestamp_t timestamp) {
  56. tmElements_t time;
  57. breakTime(timestamp, time);
  58. setTime(time);
  59. }
  60. void setTime(const tmElements_t &time) {
  61. VERBOSE_FORMAT("setTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  62. details::writeTimeToRegisters(time);
  63. hardware::i2c::powerOn();
  64. RTC.writeTime();
  65. hardware::i2c::powerOff();
  66. }
  67. void setAlarm(uint16_t seconds) {
  68. tmElements_t currentTime;
  69. tmElements_t alarmTime;
  70. getTime(currentTime);
  71. breakTime(makeTimestamp(currentTime) + seconds, alarmTime);
  72. setAlarm(alarmTime);
  73. }
  74. void setAlarm(const tmElements_t &time) {
  75. details::writeTimeToRegisters(time);
  76. hardware::i2c::powerOn();
  77. RTC.writeAlarm1(DS3231_ALM_DTHMS);
  78. RTC.control(DS3231_A1_FLAG, DS3231_OFF); //reset Alarm 1 flag
  79. RTC.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON
  80. RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON
  81. hardware::i2c::powerOff();
  82. NOTICE_FORMAT("setAlarm", "Next alarm : %d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  83. }
  84. }