選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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