-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRTClock.cpp
111 lines (100 loc) · 1.93 KB
/
RTClock.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
#include "RTClock.h"
RTClockClass::RTClockClass()
{
}
RTClockClass::~RTClockClass()
{
}
void RTClockClass::resetClock()
{
t.hour = 0;
t.min = 0;
t.sec = 0;
t.mday = 1;
t.mon = 1;
t.year = 2016;
t.year_s = 16;
DS3231_set(t);
return;
}
int RTClockClass::getUnit(int unit)
{
DS3231_get(&t); // Get updated time
switch (unit)
{
case UNIT_HOUR:
return t.hour;
break;
case UNIT_MINUTE:
return t.min;
break;
case UNIT_SECOND:
return t.sec;
break;
case UNIT_DAY:
return t.mday;
break;
case UNIT_MONTH:
return t.mon;
break;
case UNIT_YEAR:
return t.year;
break;
case UNIT_YEAR_SHORT:
return t.year_s;
break;
}
}
void RTClockClass::incrementUnit(int unit)
{
bool isLeap = false;
switch (unit)
{
case UNIT_HOUR:
if (++t.hour == 24)
t.hour = 0;
break;
case UNIT_MINUTE:
if (++t.min == 60)
t.min = 0;
t.sec = 0;
break;
case UNIT_SECOND:
++t.sec;
break;
case UNIT_DAY:
++t.mday;
switch (t.mon) // See which month you're incrementing, adjust accordingly
{
case 1: if (t.mday == 32) t.mday = 1; break;
case 2: // Leap year evil-ness
if ((t.year % 4 == 0) && !(t.year % 100 == 0) || (t.year % 400 == 0))
isLeap = true;
if ((t.mday == 29) && !isLeap) // Normal Year
t.mday = 1;
else if (t.mday == 30) // Leap year
t.mday = 1;
break;
case 3: if (t.mday == 32) t.mday = 1; break;
case 4: if (t.mday == 31) t.mday = 1; break;
case 5: if (t.mday == 32) t.mday = 1; break;
case 6: if (t.mday == 31) t.mday = 1; break;
case 7: if (t.mday == 32) t.mday = 1; break;
case 8: if (t.mday == 32) t.mday = 1; break;
case 9: if (t.mday == 31) t.mday = 1; break;
case 10: if (t.mday == 32) t.mday = 1; break;
case 11: if (t.mday == 31) t.mday = 1; break;
case 12: if (t.mday == 32) t.mday = 1; break;
}
break;
case UNIT_MONTH:
if (++t.mon == 13)
t.mon = 1;
break;
case UNIT_YEAR:
++t.year;
++t.year_s;
break;
}
DS3231_set(t);
}