25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

88 satır
2.1 KiB

  1. #include "Debug.h"
  2. #include "Rtc.h"
  3. #include "Pins.h"
  4. #include <Wire.h>
  5. #include <uDS3231.h>
  6. #define LOGGER_NAME "Rtc"
  7. using namespace utils;
  8. namespace rtc {
  9. void setup() {
  10. VERBOSE("setup");
  11. hardware::i2c::powerOn();
  12. RTC.control(DS3231_12H, DS3231_OFF); //24 hours clock
  13. RTC.control(DS3231_A1_INT_ENABLE, DS3231_OFF); //Alarm 1 OFF
  14. RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON
  15. hardware::i2c::powerOff();
  16. //TODO : check wether the osc has been halted (meaning the battery could be dead)
  17. }
  18. float getTemperature() {
  19. hardware::i2c::powerOn();
  20. float temperature = RTC.readTempRegister();
  21. hardware::i2c::powerOff();
  22. return temperature;
  23. }
  24. bool isAccurate() {
  25. hardware::i2c::powerOn();
  26. bool accurate = RTC.status(DS3231_HALTED_FLAG) == DS3231_OFF;
  27. hardware::i2c::powerOff();
  28. return accurate;
  29. }
  30. timestamp_t getTime() {
  31. tmElements_t time;
  32. getTime(time);
  33. return time::makeTimestamp(time);
  34. }
  35. void getTime(tmElements_t &time) {
  36. hardware::i2c::powerOn();
  37. RTC.readTime(time);
  38. hardware::i2c::powerOff();
  39. VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  40. }
  41. void setTime(const tmElements_t &time) {
  42. VERBOSE_FORMAT("setTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  43. hardware::i2c::powerOn();
  44. RTC.writeTime(time);
  45. RTC.control(DS3231_HALTED_FLAG, DS3231_OFF);
  46. hardware::i2c::powerOff();
  47. }
  48. void setAlarm(uint16_t seconds) {
  49. tmElements_t currentTime;
  50. tmElements_t alarmTime;
  51. getTime(currentTime);
  52. time::breakTime(time::makeTimestamp(currentTime) + seconds, alarmTime);
  53. setAlarm(alarmTime);
  54. }
  55. void setAlarm(const tmElements_t &time) {
  56. hardware::i2c::powerOn();
  57. RTC.writeAlarm1(DS3231_ALM_HMS, time);
  58. RTC.control(DS3231_A1_FLAG, DS3231_OFF); //reset Alarm 1 flag
  59. RTC.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON
  60. RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON
  61. NOTICE_FORMAT("setAlarm", "Next alarm : %d:%d:%d", time.Hour, time.Minute, time.Second);
  62. hardware::i2c::powerOff();
  63. }
  64. }