Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.7 KiB

  1. /*
  2. RTC support module
  3. Copyright (C) 2018 by Pavel Chauzov <poulch at mail dot ru>
  4. in debug terminal - RTC return current rtc time
  5. - RTC YYYY-MM-DD DoW HH:MM:SS setup rtc
  6. */
  7. #if RTC_SUPPORT
  8. #if RTC_PROVIDER == RTC_PROVIDER_DS3231
  9. #include "rtc/ds3231.h"
  10. #elif RTC_PROVIDER == RTC_PROVIDER_DS1307
  11. #include "rtc/ds1307.h"
  12. #endif
  13. static int _rtc_recovery = 0;
  14. //------------------------------------------------------------------------------
  15. // Settings
  16. //------------------------------------------------------------------------------
  17. #if TERMINAL_SUPPORT
  18. String _rtc_getValue(String data, char sep, int idx) {
  19. int found = 0;
  20. int si[] = {0, -1};
  21. int maxi = data.length()-1;
  22. for(int i=0; i<=maxi && found<=idx; i++) {
  23. if(data.charAt(i)==sep || i==maxi) {
  24. found++;
  25. si[0] = si[1]+1;
  26. si[1] = (i == maxi) ? i+1 : i;
  27. }
  28. }
  29. return found>idx ? data.substring(si[0], si[1]) : "0";
  30. }
  31. void _rtcInitCommands() {
  32. settingsRegisterCommand(F("RTC"), [](Embedis* e) {
  33. String rtc;
  34. time_t t;
  35. tmElements_t tm;
  36. if (e->argc == 1) {
  37. t = rtcGetTime();
  38. DEBUG_MSG_P(PSTR("[RTC] Local Time: %s\n"), (char *) rtcDateTime(t).c_str());
  39. }
  40. if (e->argc > 3) {
  41. String sdate = String(e->argv[1]);
  42. String sdow = String(e->argv[2]);
  43. String stime = String(e->argv[3]);
  44. tm.Second = _rtc_getValue(stime,':',2).toInt();
  45. tm.Minute = _rtc_getValue(stime,':',1).toInt();
  46. tm.Hour = _rtc_getValue(stime,':',0).toInt();
  47. tm.Wday = sdow.toInt();
  48. tm.Day = _rtc_getValue(sdate,'-',2).toInt();
  49. tm.Month = _rtc_getValue(sdate,'-',1).toInt();
  50. tm.Year = y2kYearToTm(_rtc_getValue(sdate,'-',0).toInt()-2000);
  51. t = makeTime(tm);
  52. rtcSetTime(t);
  53. setTime(t);
  54. DEBUG_MSG_P(PSTR("[RTC] Local Time: %s\n"), (char *) rtcDateTime(t).c_str());
  55. }
  56. DEBUG_MSG_P(PSTR("+OK\n"));
  57. });
  58. }
  59. #endif // TERMINAL_SUPPORT
  60. static time_t _rtcSync() {
  61. return rtcGetTime(); // defined in the driver
  62. }
  63. // -----------------------------------------------------------------------------
  64. // Setup
  65. // -----------------------------------------------------------------------------
  66. void rtcSetup() {
  67. #if TERMINAL_SUPPORT
  68. _rtcInitCommands();
  69. #endif // TERMINAL_SUPPORT
  70. // overwrite sync provider,
  71. // there might be two attempts for NTP synchro on boot
  72. setSyncProvider(_rtcSync);
  73. // Dump current time from local RTC
  74. DEBUG_MSG_P(PSTR("[RTC] Local Time: %s\n"), (char *) rtcDateTime(rtcGetTime()).c_str());
  75. }
  76. #endif // RTC_SUPPORT