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.

66 righe
1.3 KiB

  1. #include "Rtc.h"
  2. #include "Pins.h"
  3. #include <Wire.h>
  4. #include <MD_DS3231.h>
  5. #define LOGGER_NAME "Rtc"
  6. namespace rtc {
  7. namespace details {
  8. timestamp_t readTimeFromRegisters() {
  9. tmElements_t tmElements = {
  10. RTC.s,
  11. RTC.m,
  12. RTC.h,
  13. RTC.dow,
  14. RTC.dd,
  15. RTC.mm,
  16. CalendarYrToTm(RTC.yyyy)
  17. };
  18. return makeTime(tmElements);
  19. }
  20. void writeTimeToRegisters(timestamp_t &time) {
  21. tmElements_t tmElements;
  22. breakTime(time, tmElements);
  23. RTC.s = tmElements.Second;
  24. RTC.m = tmElements.Minute;
  25. RTC.h = tmElements.Hour;
  26. RTC.dow = tmElements.Wday;
  27. RTC.dd = tmElements.Day;
  28. RTC.mm = tmElements.Month;
  29. RTC.yyyy = tmYearToCalendar(tmElements.Year);
  30. }
  31. }
  32. void setup() {
  33. RTC.control(DS3231_12H, DS3231_OFF); //24 hours clock
  34. RTC.control(DS3231_INT_ENABLE, DS3231_OFF); //INTCN OFF
  35. }
  36. timestamp_t getTime() {
  37. RTC.readTime();
  38. return details::readTimeFromRegisters();
  39. }
  40. void setTime(timestamp_t &time) {
  41. details::writeTimeToRegisters(time);
  42. RTC.writeTime();
  43. }
  44. void setAlarm(timestamp_t &time) {
  45. details::writeTimeToRegisters(time);
  46. RTC.writeAlarm1(DS3231_ALM_S);
  47. RTC.control(DS3231_A1_FLAG, DS3231_OFF);
  48. RTC.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON
  49. RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON
  50. }
  51. }