Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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