Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

87 righe
2.0 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(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. powerOn();
  30. RTC.control(DS3231_12H, DS3231_OFF); //24 hours clock
  31. RTC.control(DS3231_INT_ENABLE, DS3231_OFF); //INTCN OFF
  32. powerOff();
  33. }
  34. void getTime(tmElements_t &time) {
  35. powerOn();
  36. RTC.readTime();
  37. powerOff();
  38. details::readTimeFromRegisters(time);
  39. VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  40. }
  41. void setTime(tmElements_t &time) {
  42. VERBOSE_FORMAT("setTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  43. details::writeTimeToRegisters(time);
  44. powerOn();
  45. RTC.writeTime();
  46. powerOff();
  47. }
  48. void setAlarm(uint16_t seconds) {
  49. tmElements_t currentTime;
  50. tmElements_t alarmTime;
  51. getTime(currentTime);
  52. breakTime(makeTimestamp(currentTime) + seconds, alarmTime);
  53. setAlarm(alarmTime);
  54. }
  55. void setAlarm(tmElements_t &time) {
  56. details::writeTimeToRegisters(time);
  57. hardware::i2c::rtcPowerOn();
  58. RTC.writeAlarm1(DS3231_ALM_DTHMS);
  59. RTC.control(DS3231_A1_FLAG, DS3231_OFF); //reset Alarm 1 flag
  60. RTC.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON
  61. RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON
  62. hardware::i2c::rtcPowerOff();
  63. Log.notice(F("Set alarm to : %d/%d/%d %d:%d:%d\n"), tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  64. }
  65. }