Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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