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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
/*
* time.cc
*
* Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
*
* This file is part of propeller-clock (hereafter referred to as "this
* program"). See http://ed.am/dev/software/arduino/propeller-clock for more
* information.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "time.h"
#include <DS1307.h>
#include <Wire.h>
#define YEAR_MAX 2030
#define YEAR_MIN 2012
// year
static int _year = 2012;
// month
static int _month = 5;
// day
static int _day = 18;
// hours
static int _hours = 15;
// minutes
static int _minutes = 36;
// seconds
static int _seconds = 0;
// milliseconds at last update
static unsigned long _last_millis = millis();
// milliseconds carries over from last update
static unsigned long _carry = 0;
static int days_in_month( int month, int year )
{
if( month == 2 )
return !( year % 4 ) && ( ( year % 100 ) || !( year % 400 ) )? 29 : 28;
else if( month == 9 || month == 6 || month == 4 || month == 11 )
return 30;
else
return 31;
}
void Time::load_time()
{
// get the time from the real-time clock
int rtc_data[ 7 ];
RTC.get( rtc_data, true );
_year = rtc_data[ DS1307_YR ];
_month = rtc_data[ DS1307_MTH ];
_day = rtc_data[ DS1307_DAY ];
_hours = rtc_data[ DS1307_HR ];
_minutes = rtc_data[ DS1307_MIN ];
_seconds = rtc_data[ DS1307_SEC ];
// make sure some numbers are in range
if( _year < YEAR_MIN || _year > YEAR_MAX ) _year = YEAR_MIN;
if( _month < 1 || _month > 12 ) _month = 1;
if( _day < 1 || _day > days_in_month( _year, _month ) ) _day = 1;
}
void Time::save_time()
{
// set the time on the real-time clock
RTC.stop();
RTC.set( DS1307_YR, _year );
RTC.set( DS1307_MTH, _month );
RTC.set( DS1307_DAY, _day );
RTC.set( DS1307_HR, _hours );
RTC.set( DS1307_MIN, _minutes );
RTC.set( DS1307_SEC, _seconds );
RTC.start();
}
int Time::get_year()
{
return _year;
}
int Time::get_month()
{
return _month;
}
const char *Time::get_month_name()
{
static const char *months[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
};
return months[ _month - 1 ];
}
int Time::get_day()
{
return _day;
}
const char *Time::get_day_suffix()
{
switch( _day )
{
case 1:
case 21:
case 31:
return "st";
case 2:
case 22:
return "nd";
case 3:
case 23:
return "rd";
default:
return "th";
}
}
int Time::get_hours()
{
return _hours;
}
int Time::get_minutes()
{
return _minutes;
}
int Time::get_seconds()
{
return _seconds;
}
void Time::update()
{
// how many milliseconds have elapsed since we last checked?
unsigned long millis = ::millis();
unsigned long delta = millis - _last_millis + _carry;
// update the previous time and carried-over milliseconds
_last_millis = millis;
_carry = delta % 1000;
// add the seconds that have passed to the time
_seconds += delta / 1000;
while( _seconds >= 60 ) {
_seconds -= 60;
_minutes++;
if( _minutes >= 60 ) {
_minutes -= 60;
_hours++;
if( _hours >= 24 ) {
_hours -= 24;
// We *could* manually work out the day here... but we might as
// well ask the RTC hardware. We can't do this *all* the time
// though, because it isn't fast enough and will actually cause
// a minor display glitch.
load_time();
}
}
}
}
void Time::inc_hours()
{
if( ++_hours >= 24 )
_hours = 0;
}
void Time::inc_minutes()
{
if( ++_minutes >= 60 )
_minutes = 0;
}
void Time::reset_seconds()
{
_seconds = 0;
}
void Time::inc_year()
{
if( ++_year >= YEAR_MAX )
_year = YEAR_MIN;
}
void Time::inc_month()
{
if( ++_month > 12 )
_month = 1;
}
void Time::inc_day()
{
if( ++_day > days_in_month( _month, _year ) )
_day = 1;
}
|