-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clock.ino
184 lines (143 loc) · 5.42 KB
/
Clock.ino
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
void displayTime()
{
String s;
int hrs = rtc.now().hour();
if (hrs != currentHour)
{
//We will display the days and year first. This will ensure time cycles are not wasted
displayDate();
//Initialise the DST settings. Once a year only
if (runningYear != currentYear)
{
runningYear = currentYear;
InitialiseDST();
}
currentHour = hrs;
hrs += getDST(currentHour);
String AM = hrs >= 12 ? " PM":" AM" ;
if (pref.bAMPM)
hrs = hrs > 12 ? hrs-12 : hrs;
//construct the first line of display
s = hrs < 10 ? "0" + String(hrs) : String(hrs);
s+= ":";
mins = rtc.now().minute();
s+= mins < 10 ? "0" + String(mins) : String(mins);
s+= ":";
secs = rtc.now().second();
s += secs < 10 ? "0" + String(secs) : String(secs);
s += AM;
lcd.setCursor(pref.bAMPM ? 2 : 4,0);
lcd.print(s);
//if (bConnect)
//Serial.printf("Current Time: %s\n", timeClient.getFormattedTime().c_str());
//else
//Serial.printf("Current Time: %s%d:%s%d:%s%d\n",hrs< 10 ? "0":"",hrs,mins< 10 ? "0":"",mins,secs< 10 ? "0":"",secs);
}
else
{
int minutes = rtc.now().minute();
if (minutes != mins)
{
mins = minutes;
s = mins < 10 ? "0" + String(mins) : String(mins);
s += ":";
secs = rtc.now().second();
s += secs < 10 ? "0" + String(secs) : String(secs);
lcd.setCursor(pref.bAMPM ? 5 : 7,0);
lcd.print(s);
//Serial.printf("Time Now: %s%d:%s%d:%s%d\n",hrs< 10 ? "0":"",hrs,mins< 10 ? "0":"",mins,secs< 10 ? "0":"",secs);
//if (mins % 5 == 0)
//Serial.printf("Current Date: %s%d %s %s%d", currentDate < 10 ? "0":"",currentDate,MONTHS[currentMonth-1],currentYear < 10 ? "0":"",currentYear);
}
else
{
lcd.setCursor(pref.bAMPM ? 8 :10,0);
secs = rtc.now().second();
lcd.print(secs < 10 ? "0" + String(secs) : String(secs));
}
}
}
//Check if the time falls under DST
int getDST(int Hour)
{
//Not yet reached DST month or past DST month
if (currentMonth > currentDST.startMonth || currentMonth < currentDST.endMonth)
return 1;
//Current month is DST start month
if (currentMonth == currentDST.startMonth)
{
if (currentDate < currentDST.startDate)
return 0;
if (currentDate > currentDST.startDate)
return 1;
//Current Date is DST start date
if (Hour < currentDST.startTime)
return 0;
else
return 1;
}
//Not within DST range
if (currentMonth != currentDST.endMonth)
return 0;
//Current month is DST end month
if (currentDate < currentDST.endDate)
return 1;
if (currentDate > currentDST.endDate)
return 0;
//Current Date is DST start date
if (Hour < currentDST.endTime)
return 1;
return 0;
}
//Given date, month, year calculates the day and returns. Sunday is 0, Monday is 1 ...
int getWeekDay(int d, int m, int y)
{
y-= m < 3;
return (y + y/4 - y/100 + y/400 + MonthTable[m-1] + d) % 7;
}
//Called at the start of the year, will fill the DST structure
void InitialiseDST()
{
//Get the weekday of the first date of DST Start month
int firstDayOfWeek = getWeekDay(1,pref.DSTStartMonth,currentYear);
//Now get the first DST Start day
int firstDSTday = firstDayOfWeek > pref.DSTStartDay ? 8 - firstDayOfWeek + pref.DSTStartDay : pref.DSTStartDay- firstDayOfWeek + 1;
currentDST.startMonth = pref.DSTStartMonth;
if ((pref.DSTStartWeek-1) > 5) //Day is used
currentDST.startDate = pref.DSTStartWeek;
else
{
currentDST.startDate = firstDSTday + (7*(pref.DSTStartWeek-1));
if (currentDST.startDate > (30 + LastWeek[currentDST.startMonth]))
currentDST.startDate = currentDST.startDate - 7;
}
currentDST.startTime = pref.DSTStartTime;
//Get the weekday of the first date of DST End month
firstDayOfWeek = getWeekDay(1,pref.DSTEndMonth,currentYear);
//Now get the first DST End day
firstDSTday = firstDayOfWeek > pref.DSTEndDay ? 8 - firstDayOfWeek + pref.DSTEndDay : pref.DSTEndDay - firstDayOfWeek + 1;
currentDST.endMonth = pref.DSTEndMonth;
if ((pref.DSTEndWeek-1) > 5) //Day is used
currentDST.endDate = pref.DSTEndWeek;
else
{
currentDST.endDate = firstDSTday + (7*(pref.DSTEndWeek-1));
if (currentDST.endDate >(30 + LastWeek[currentDST.endMonth]))
currentDST.endDate = currentDST.endDate - 7;
}
currentDST.endTime = pref.DSTEndTime;
/* Serial.printf("Year: %d startMonth:%s, startDate:%d, startTime:%d, EndMonth:%s, EndDate:%d, EndTime:%d\n", currentYear,
MONTHS[currentDST.startMonth-1],currentDST.startDate,currentDST.startTime,
MONTHS[currentDST.endMonth-1],currentDST.endDate,currentDST.endTime);*/
}
//Displays the date on the second line of LCD
void displayDate()
{
currentYear = rtc.now().year();
currentMonth = rtc.now().month();
currentDate = rtc.now().day();
currentDay = rtc.now().dayOfTheWeek();
lcd.setCursor(0, 1);
lcd.print(String(WEEK[currentDay]) + (currentDate < 10 ? "0" + String(currentDate) : String(currentDate)) + String(MONTHS[currentMonth - 1]) + String(currentYear));
//Serial.printf("Current Date: %s%d %s %s%d", currentDate < 10 ? "0":"",currentDate,MONTHS[currentMonth-1],currentYear < 10 ? "0":"",currentYear);
}