-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClockTime.cpp
138 lines (107 loc) · 2.93 KB
/
ClockTime.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
#include "ClockTime.h"
ClockTime::ClockTime(Tiny_SH1106 *lcd)
{
_lcd = lcd;
_rtc = new RtcDS3231<TwoWire>(Wire);
_rtc->Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!_rtc->IsDateTimeValid())
{
if (_rtc->LastError() != 0)
{
Serial.print(F("RTC communications error = "));
Serial.println(_rtc->LastError());
}
else
_rtc->SetDateTime(compiled);
}
if (!_rtc->GetIsRunning())
_rtc->SetIsRunning(true);
RtcDateTime now = _rtc->GetDateTime();
if (now < compiled)
_rtc->SetDateTime(compiled);
_rtc->Enable32kHzPin(false);
_rtc->SetSquareWavePin(DS3231SquareWavePin_ModeNone);
_timer = new Interval();
_timer->Start(1000, true);
}
ClockTime::~ClockTime()
{
free(_timer);
free(_rtc);
}
void ClockTime::SetMode(int mode)
{
_mode = mode;
_timer->Now();
Render(false);
}
void ClockTime::Render(bool display)
{
if (_mode > 1 || !_timer->Ready() || !_rtc->IsDateTimeValid())
return;
RtcDateTime now = _rtc->GetDateTime();
_lcd->fillRect(1, 0, 118,38, BLACK);
_lcd->setCursor(28,14);
_lcd->setTextSize(1);
_lcd->setTextColor(WHITE);
_lcd->setFont(&DSEG7_Classic_Regular_20);
char *buffer = new char[6];
if (_clockDots)
sprintf(buffer, "%02d:%02d", now.Hour(), now.Minute());
else
sprintf(buffer, "%02d %02d", now.Hour(), now.Minute());
_lcd->print(buffer);
delete[] buffer;
_clockDots = !_clockDots;
_lcd->setFont();
// date
char *dow;
switch (now.DayOfWeek())
{
case 0 : dow = "Sun"; break;
case 1 : dow = "Mon"; break;
case 2 : dow = "Tue"; break;
case 3 : dow = "Wed"; break;
case 4 : dow = "Thu"; break;
case 5 : dow = "Fri"; break;
case 6 : dow = "Sat"; break;
default: dow = "???";
}
char *mon;
switch (now.Month())
{
case 1 : mon = "Jan"; break;
case 2 : mon = "Feb"; break;
case 3 : mon = "Mar"; break;
case 4 : mon = "Apr"; break;
case 5 : mon = "May"; break;
case 6 : mon = "Jun"; break;
case 7 : mon = "Jul"; break;
case 8 : mon = "Aug"; break;
case 9 : mon = "Sep"; break;
case 10 : mon = "Oct"; break;
case 11 : mon = "Nov"; break;
case 12 : mon = "Dec"; break;
default: mon = "???";
}
char *buffer2 = new char[28];
sprintf(buffer2, "%s, %02d %s %04d\0", dow, now.Day(), mon, now.Year());
_lcd->setCursor(64 - ((strlen(buffer2) * 6) /2),25);
_lcd->print(buffer2);
delete[] buffer2;
if (display)
_lcd->display();
}
void ClockTime::GetIso8601(char* buffer)
{
RtcDateTime dateStamp = _rtc->GetDateTime();
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
dateStamp.Year(), dateStamp.Month(), dateStamp.Day(),
dateStamp.Hour(), dateStamp.Minute(), dateStamp.Second());
}
void ClockTime::SetTime(struct tm newTime)
{
RtcDateTime upd = RtcDateTime(newTime.tm_year, newTime.tm_mon, newTime.tm_mday, newTime.tm_hour, newTime.tm_min, newTime.tm_sec);
_rtc->SetDateTime(upd);
}