/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/time.cc

  • Committer: Tim Marston
  • Date: 2013-03-31 17:07:36 UTC
  • Revision ID: tim@ed.am-20130331170736-hphm2hg0y6l7w6z1
made rtc-test's DS1307 library a symlink to the main one in src/util

Show diffs side-by-side

added added

removed removed

25
25
#include <Wire.h>
26
26
 
27
27
 
 
28
#define YEAR_MAX 2030
 
29
#define YEAR_MIN 2012
 
30
 
 
31
 
28
32
// year
29
 
static int _year;
 
33
static int _year = 2012;
30
34
 
31
35
// month
32
 
static int _month;
 
36
static int _month = 5;
33
37
 
34
38
// day
35
 
static int _day;
 
39
static int _day = 18;
36
40
 
37
41
// hours
38
 
static int _hours;
 
42
static int _hours = 15;
39
43
 
40
44
// minutes
41
 
static int _minutes;
 
45
static int _minutes = 36;
42
46
 
43
47
// seconds
44
 
static int _seconds;
 
48
static int _seconds = 0;
45
49
 
46
50
// milliseconds at last update
47
51
static unsigned long _last_millis = millis();
50
54
static unsigned long _carry = 0;
51
55
 
52
56
 
53
 
static void probe_rtc()
 
57
static int days_in_month( int month, int year )
 
58
{
 
59
        if( month == 2 )
 
60
                return !( year % 4 ) && ( ( year % 100 ) || !( year % 400 ) )? 29 : 28;
 
61
        else if( month == 9 || month == 6 || month == 4 || month == 11 )
 
62
                return 30;
 
63
        else
 
64
                return 31;
 
65
}
 
66
 
 
67
 
 
68
void Time::load_time()
54
69
{
55
70
        // get the time from the real-time clock
56
71
        int rtc_data[ 7 ];
61
76
        _hours = rtc_data[ DS1307_HR ];
62
77
        _minutes = rtc_data[ DS1307_MIN ];
63
78
        _seconds = rtc_data[ DS1307_SEC ];
 
79
 
 
80
        // make sure some numbers are in range
 
81
        if( _year < YEAR_MIN || _year > YEAR_MAX ) _year = YEAR_MIN;
 
82
        if( _month < 1 || _month > 12 ) _month = 1;
 
83
        if( _day < 1 || _day > days_in_month( _year, _month ) ) _day = 1;
 
84
}
 
85
 
 
86
 
 
87
void Time::save_time()
 
88
{
 
89
        // set the time on the real-time clock
 
90
        RTC.stop();
 
91
        RTC.set( DS1307_YR, _year );
 
92
        RTC.set( DS1307_MTH, _month );
 
93
        RTC.set( DS1307_DAY, _day );
 
94
        RTC.set( DS1307_HR, _hours );
 
95
        RTC.set( DS1307_MIN, _minutes );
 
96
        RTC.set( DS1307_SEC, _seconds );
 
97
        RTC.start();
64
98
}
65
99
 
66
100
 
162
196
                        if( _hours >= 24 ) {
163
197
                                _hours -= 24;
164
198
 
165
 
                                // I mean we *could* work out the day... but FUCK IT! We bought
166
 
                                // some god damn HARDWARE to do that shit!
167
 
                                probe_rtc();
 
199
                                // We *could* manually work out the day here... but we might as
 
200
                                // well ask the RTC hardware. We can't do this *all* the time
 
201
                                // though, because it isn't fast enough and will actually cause
 
202
                                // a minor display glitch.
 
203
                                load_time();
168
204
                        }
169
205
                }
170
206
        }
171
207
}
172
208
 
173
209
 
174
 
void Time::init()
175
 
{
176
 
        probe_rtc();
 
210
void Time::inc_hours()
 
211
{
 
212
        if( ++_hours >= 24 )
 
213
                _hours = 0;
 
214
}
 
215
 
 
216
 
 
217
void Time::inc_minutes()
 
218
{
 
219
        if( ++_minutes >= 60 )
 
220
                _minutes = 0;
 
221
}
 
222
 
 
223
 
 
224
void Time::reset_seconds()
 
225
{
 
226
        _seconds = 0;
 
227
}
 
228
 
 
229
 
 
230
void Time::inc_year()
 
231
{
 
232
        if( ++_year >= YEAR_MAX )
 
233
                _year = YEAR_MIN;
 
234
}
 
235
 
 
236
 
 
237
void Time::inc_month()
 
238
{
 
239
        if( ++_month > 12 )
 
240
                _month = 1;
 
241
}
 
242
 
 
243
 
 
244
void Time::inc_day()
 
245
{
 
246
        if( ++_day > days_in_month( _month, _year ) )
 
247
                _day = 1;
177
248
}
178
249
 
179
250