forked from aly-fly/EleksTubeHAX
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathClock.cpp
155 lines (139 loc) · 4.65 KB
/
Clock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "Clock.h"
#include "WiFi_WPS.h"
#if defined(HARDWARE_SI_HAI_CLOCK) || defined(HARDWARE_IPSTUBE_H401_CLOCK) // for Clocks with DS1302 chip #SI HAI or H401 XXXXXXXXXXXXXXXXXX
// If it is a SI HAI Clock, use differnt RTC chip drivers
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(DS1302_IO, DS1302_SCLK, DS1302_CE); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
void RtcBegin() {
Rtc.Begin();
// check if chip is connected and alive
/* TCS default value is 0x00 instead of 0x5C, but RTC seems to be working. Let's skip this check.
Serial.print("Checking DS1302 RTC... ");
uint8_t TCS = Rtc.GetTrickleChargeSettings(); // 01011100 Initial power-on state
Serial.print("TCS = ");
Serial.println(TCS);
if (TCS != 0x5C) {
Serial.println("Error communicating with DS1302 !");
}
*/
if (!Rtc.IsDateTimeValid()) {
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
}
if (Rtc.GetIsWriteProtected()) {
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning()) {
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
}
uint32_t RtcGet() {
RtcDateTime temptime;
temptime = Rtc.GetDateTime();
uint32_t returnvalue = temptime.Unix32Time();
Serial.println(returnvalue);
return returnvalue;
}
void RtcSet(uint32_t tt) {
RtcDateTime temptime;
temptime.InitWithUnix32Time(tt);
Rtc.SetDateTime(temptime);
}
#else
// For the DS1307 RTC
#include <DS1307RTC.h>
void RtcBegin() {}
uint32_t RtcGet() {
return RTC.get();
}
void RtcSet(uint32_t tt) {
RTC.set(tt);
}
#endif
void Clock::begin(StoredConfig::Config::Clock *config_) {
config = config_;
if (config->is_valid != StoredConfig::valid) {
// Config is invalid, probably a new device never had its config written.
// Load some reasonable defaults.
Serial.println("Loaded Clock config is invalid, using default. This is normal on first boot.");
setTwelveHour(false);
setBlankHoursZero(false);
setTimeZoneOffset(2 * 3600); // defaulting CEST
setActiveGraphicIdx(1);
config->is_valid = StoredConfig::valid;
}
RtcBegin();
ntpTimeClient.begin();
ntpTimeClient.update();
Serial.print("NTP time = ");
Serial.println(ntpTimeClient.getFormattedTime());
setSyncProvider(&Clock::syncProvider);
}
void Clock::loop() {
if (timeStatus() == timeNotSet) {
time_valid = false;
}
else {
loop_time = now();
local_time = loop_time + config->time_zone_offset;
time_valid = true;
}
}
// Static methods used for sync provider to TimeLib library.
time_t Clock::syncProvider() {
Serial.println("syncProvider()");
time_t ntp_now, rtc_now;
rtc_now = RtcGet();
if (millis() - millis_last_ntp > refresh_ntp_every_ms || millis_last_ntp == 0) {
if (WifiState == connected) {
// It's time to get a new NTP sync
Serial.print("Getting NTP.");
// ntpTimeClient.forceUpdate(); // maybe this breaks the NTP requests as this should not be done more than every minute.
if (ntpTimeClient.update()) {
Serial.print(".");
ntp_now = ntpTimeClient.getEpochTime();
Serial.println("NTP query done.");
Serial.print("NTP time = ");
Serial.println(ntpTimeClient.getFormattedTime());
// if (ntp_now > 1644601505) { //is it valid - reasonable number?
// Sync the RTC to NTP if needed.
Serial.println("NTP, RTC, Diff: ");
Serial.println(ntp_now);
Serial.println(rtc_now);
Serial.println(ntp_now-rtc_now);
if (ntp_now != rtc_now) {
RtcSet(ntp_now);
Serial.println("Updating RTC");
}
millis_last_ntp = millis();
Serial.println("Using NTP time.");
return ntp_now;
} else { // NTP valid
Serial.println("Invalid NTP response, using RTC time.");
return rtc_now;
}
} // no wifi
Serial.println("No WiFi, using RTC time.");
return rtc_now;
}
Serial.println("Using RTC time.");
return rtc_now;
}
uint8_t Clock::getHoursTens() {
uint8_t hour_tens = getHour()/10;
if (config->blank_hours_zero && hour_tens == 0) {
return TFTs::blanked;
}
else {
return hour_tens;
}
}
uint32_t Clock::millis_last_ntp = 0;
WiFiUDP Clock::ntpUDP;
NTPClient Clock::ntpTimeClient(ntpUDP);