4
* Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
6
* This file is part of propeller-clock (hereafter referred to as "this
7
* program"). See http://ed.am/dev/software/arduino/propeller-clock for more
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU Lesser General Public License as published
12
* by the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU Lesser General Public License for more details.
20
* You should have received a copy of the GNU Lesser General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
50
// milliseconds at last update
51
static unsigned long _last_millis = millis();
53
// milliseconds carries over from last update
54
static unsigned long _carry = 0;
70
const char *Time::get_month_name()
72
static const char *months[] = {
87
return months[ _month - 1 ];
97
const char *Time::get_day_suffix()
117
int Time::get_hours()
123
int Time::get_minutes()
129
int Time::get_seconds()
135
static int days_in_month( int month, int year )
138
return !( year % 4 ) && ( ( year % 100 ) || !( year % 400 ) )? 29 : 28;
139
else if( month == 9 || month == 6 || month == 4 || month == 11 )
146
static void load_time()
148
// get the time from the real-time clock
150
RTC.get( rtc_data, true );
151
_year = rtc_data[ DS1307_YR ];
152
_month = rtc_data[ DS1307_MTH ];
153
_day = rtc_data[ DS1307_DAY ];
154
_hours = rtc_data[ DS1307_HR ];
155
_minutes = rtc_data[ DS1307_MIN ];
156
_seconds = rtc_data[ DS1307_SEC ];
158
// make sure some numbers are in range
159
if( _year < YEAR_MIN || _year > YEAR_MAX ) _year = 2010;
160
if( _month < 1 || _month > 12 ) _month = 1;
161
if( _day < 1 || _day > days_in_month( _year, _month ) ) _day = 1;
167
// how many milliseconds have elapsed since we last checked?
168
unsigned long millis = ::millis();
169
unsigned long delta = millis - _last_millis + _carry;
171
// update the previous time and carried-over milliseconds
172
_last_millis = millis;
173
_carry = delta % 1000;
175
// add the seconds that have passed to the time
176
_seconds += delta / 1000;
177
while( _seconds >= 60 ) {
180
if( _minutes >= 60 ) {
186
// We *could* manually work out the day here... but we might as
187
// well ask the RTC hardware. We can't do this *all* the time
188
// though, because it isn't fast enough and will actually cause
189
// a minor display glitch.
203
static void save_time()
205
// set the time on the real-time clock
207
RTC.set( DS1307_YR, _year );
208
RTC.set( DS1307_MTH, _month );
209
RTC.set( DS1307_DAY, _day );
210
RTC.set( DS1307_HR, _hours );
211
RTC.set( DS1307_MIN, _minutes );
212
RTC.set( DS1307_SEC, _seconds );
217
void Time::inc_hours()
225
void Time::inc_minutes()
227
if( ++_minutes >= 60 )
233
void Time::reset_seconds()
240
void Time::inc_year()
242
if( ++_year >= YEAR_MAX )
248
void Time::inc_month()
257
if( ++_day > days_in_month( _month, _year ) )