-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclock.cpp
178 lines (147 loc) · 3.49 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <Arduino.h>
#include "clock.h"
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire* oneWire=NULL;
DallasTemperature* tempSensor=NULL;
const char *p2dig(uint8_t v, uint8_t mode)
// print 2 digits leading zero
{
uint8_t n = 0;
static char c[3] = { "00" };
switch(mode)
{
case HEX:
{
c[0] = htoa((v >> 4) & 0xf);
c[1] = htoa(v & 0xf);
}
break;
case DEC:
{
c[0] = ((v / 10) % 10) + '0';
c[1] = (v % 10) + '0';
}
break;
}
return(c);
}
char htoa(uint8_t i)
{
if (i >= 0 && i <= 9)
return(i + '0');
if (i >= 10 && i <= 15)
return(i - 10 + 'a');
return('?');
}
uint8_t htoi(char c)
{
c = toupper(c);
if (c >= '0' && c <= '9')
return(c - '0');
else if (c >= 'A' && c <= 'F')
return(c - 'A' + 10);
else
return(0);
}
void RTC_Init()
{
#if defined(CLOCK_DS1307)
RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
RTC.control(DS1307_SQW_RUN, DS1307_OFF);
RTC.control(DS1307_12H, DS1307_OFF);
if(DS18B20_PIN>=0) {
oneWire=new OneWire(DS18B20_PIN);
tempSensor=new DallasTemperature(oneWire);
tempSensor->begin();
tempSensor->setWaitForConversion(false);
tempSensor->requestTemperatures();
}
#elif defined(CLOCK_DS3231)
RTC.control(DS3231_CLOCK_HALT, DS3231_OFF);
RTC.control(DS3231_SQW_ENABLE, DS3231_OFF);
RTC.control(DS3231_12H, DS3231_OFF);
#endif
}
float Temp_Read()
{
static float lastTemp=0;
//needs to be async because it takes 94ms for default 9bit resolution
if(tempSensor!=NULL&&tempSensor->isConversionComplete())
{
lastTemp=tempSensor->getTempCByIndex(0);
tempSensor->requestTemperatures();
}
return lastTemp;
}
void RTC_WriteTime_UTC(int yyyy,int mm, int dd, int h, int m, int s)
{
RTC.yyyy = yyyy;
RTC.mm = mm;
RTC.dd = dd;
RTC.h = h;
RTC.m = m;
RTC.s = s;
RTC.dow = RTC.calcDoW(yyyy,mm,dd);
RTC.writeTime();
}
void RTC_ReadTime_UTC()
{
RTC.readTime();
}
void RTC_ReadTime_Local()
{
RTC.readTime();
// ---- Convert UTC to Local Time ------------------------------------
int theHour = RTC.h;
int theDay = RTC.dd;
int theMonth = RTC.mm;
int theYear = RTC.yyyy;
byte daysPerMonth [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// -------------------- check for leap year --------------------------
if ((theYear % 4) == 0) {
daysPerMonth[1] = 29; // leap year
}
int dst_b=pgm_read_word_near(DSTBegin+(theYear - DSTYearOfs));
int dst_e=pgm_read_word_near(DSTEnd+(theYear - DSTYearOfs));
// ------------------- dst time zone --------
if (theMonth * 100 + theDay >= dst_b && theMonth * 100 + theDay < dst_e) {
theHour += TimeZoneDST;
} else {
// --------------------normal time zone --------
theHour += TimeZoneSTD;
}
// ----------------------- correct day, mo & yr for roll backward ----
if (theHour < 0) {
theHour += 24;
theDay -= 1;
if (theDay < 1) {
if (theMonth == 1) { // Jan 1
theMonth = 12;
theYear -= 1;
} else {
theMonth -= 1;
}
theDay = daysPerMonth[theMonth - 1];
}
}
// ----------------------- roll forward if east of prime meridian ----
if (theHour >= 24) {
theHour -= 24;
theDay += 1;
if (theDay > daysPerMonth[theMonth - 1]) {
theDay = 1;
theMonth += 1;
if (theMonth > 12) { // Jan 1
theYear += 1;
theMonth = 1;
}
}
}
//write back
RTC.h=theHour;
RTC.dd=theDay;
RTC.mm=theMonth;
RTC.yyyy=theYear;
}