Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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