-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtc.cpp
More file actions
195 lines (161 loc) · 3.55 KB
/
Copy pathrtc.cpp
File metadata and controls
195 lines (161 loc) · 3.55 KB
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
185
186
187
188
189
190
191
192
193
194
195
/*
* This file is part of LynxGradProject.
*
* LynxGradProject is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LynxGradProject is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LynxGradProject. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rtc.h"
#include "Wire.h"
#include "I2Cdev.h"
#include "DS1307.h"
bool RTC::s_Initialized = false;
DS1307 RTC::s_RTC;
RTC::RTC()
{
}
RTC::~RTC()
{
}
void RTC::init()
{
if (s_Initialized == false) {
s_Initialized = true;
s_RTC.initialize();
// verify connection
if (s_RTC.testConnection() == false) {
Serial.println("DS1307 connection failed");
while (1);
}
s_RTC.setSquareWaveRate(1); // 4kHz
if (s_RTC.getClockRunning() != true) {
s_RTC.setClockRunning(true);
}
// Switch to 12 Hour mode
if (s_RTC.getMode() != 1) {
s_RTC.setMode(1);
}
// Hardware bug workaround.
byte seconds;
for (int i = 0; i < 4; i++) {
seconds = s_RTC.getSeconds();
delay(300);
if (seconds != s_RTC.getSeconds()) {
// Clock is running
return;
}
delay(400);
if (seconds != s_RTC.getSeconds()) {
// Clock is running
return;
}
delay(400);
if (seconds != s_RTC.getSeconds()) {
// Clock is running
return;
}
// Toggle clock
s_RTC.setClockRunning(false);
delay(10);
s_RTC.setClockRunning(true);
}
#ifdef DEBUG
Serial.println("Unable to start RTC clock.");
#endif
}
}
void RTC::setSQEnabled(bool state)
{
if (s_Initialized == false) {
return;
}
s_RTC.setSquareWaveRate(1); // 4kHz
s_RTC.setSquareWaveEnabled(state);
}
unsigned long RTC::getTime()
{
if (s_Initialized == false) {
return 0;
}
return s_RTC.getSeconds();
}
void RTC::setTime(unsigned long time)
{
if (s_Initialized == false) {
return;
}
s_RTC.setSeconds(time);
}
unsigned int RTC::getYear()
{
return s_RTC.getYear();
}
byte RTC::getMonth()
{
return s_RTC.getMonth();
}
byte RTC::getDay()
{
return s_RTC.getDay();
}
byte RTC::getDayOfWeek()
{
s_RTC.getDayOfWeek();
}
bool RTC::getPM()
{
return s_RTC.getAMPM();
}
byte RTC::getHours12()
{
return s_RTC.getHours12();
}
byte RTC::getMinutes()
{
return s_RTC.getMinutes();
}
byte RTC::getSeconds()
{
return s_RTC.getSeconds();
}
void RTC::setDayOfWeek(byte dayOfWeek)
{
s_RTC.setDayOfWeek(dayOfWeek);
}
void RTC::setSeconds(byte seconds)
{
s_RTC.setSeconds(seconds);
}
void RTC::setMinutes(byte minutes)
{
s_RTC.setMinutes(minutes);
}
void RTC::setHours12(byte hours12, bool pm)
{
s_RTC.setHours12(hours12, pm);
}
void RTC::setDay(byte day)
{
s_RTC.setDay(day);
}
void RTC::setMonth(byte month)
{
s_RTC.setMonth(month);
}
void RTC::setYear(unsigned int year)
{
s_RTC.setYear(year);
}
void RTC::setPM(bool pm)
{
s_RTC.setAMPM(pm);
}