/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: 2012-04-29 15:27:19 UTC
  • Revision ID: tim@ed.am-20120429152719-4cu2t9lx7bxpbml1
added adjustable text scaling factor

Show diffs side-by-side

added added

removed removed

26
26
 
27
27
 
28
28
#define YEAR_MAX 2030
29
 
#define YEAR_MIN 2012
 
29
#define YEAR_MIN 2010
30
30
 
31
31
 
32
32
// year
33
 
static int _year = 2012;
 
33
static int _year;
34
34
 
35
35
// month
36
 
static int _month = 5;
 
36
static int _month;
37
37
 
38
38
// day
39
 
static int _day = 18;
 
39
static int _day;
40
40
 
41
41
// hours
42
 
static int _hours = 15;
 
42
static int _hours;
43
43
 
44
44
// minutes
45
 
static int _minutes = 36;
 
45
static int _minutes;
46
46
 
47
47
// seconds
48
 
static int _seconds = 0;
 
48
static int _seconds;
49
49
 
50
50
// milliseconds at last update
51
51
static unsigned long _last_millis = millis();
54
54
static unsigned long _carry = 0;
55
55
 
56
56
 
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()
69
 
{
70
 
        // get the time from the real-time clock
71
 
        int rtc_data[ 7 ];
72
 
        RTC.get( rtc_data, true );
73
 
        _year = rtc_data[ DS1307_YR ];
74
 
        _month = rtc_data[ DS1307_MTH ];
75
 
        _day = rtc_data[ DS1307_DAY ];
76
 
        _hours = rtc_data[ DS1307_HR ];
77
 
        _minutes = rtc_data[ DS1307_MIN ];
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();
98
 
}
99
 
 
100
57
 
101
58
int Time::get_year()
102
59
{
175
132
}
176
133
 
177
134
 
 
135
static int days_in_month( int month, int year )
 
136
{
 
137
        if( month == 2 )
 
138
                return !( year % 4 ) && ( ( year % 100 ) || !( year % 400 ) )? 29 : 28;
 
139
        else if( month == 9 || month == 6 || month == 4 || month == 11 )
 
140
                return 30;
 
141
        else
 
142
                return 31;
 
143
}
 
144
 
 
145
 
 
146
static void load_time()
 
147
{
 
148
        // get the time from the real-time clock
 
149
        int rtc_data[ 7 ];
 
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 ];
 
157
 
 
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;
 
162
}
 
163
 
 
164
 
178
165
void Time::update()
179
166
{
180
167
        // how many milliseconds have elapsed since we last checked?
207
194
}
208
195
 
209
196
 
 
197
void Time::init()
 
198
{
 
199
        load_time();
 
200
}
 
201
 
 
202
 
 
203
static void save_time()
 
204
{
 
205
        // set the time on the real-time clock
 
206
        RTC.stop();
 
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 );
 
213
        RTC.start();
 
214
}
 
215
 
 
216
 
210
217
void Time::inc_hours()
211
218
{
212
219
        if( ++_hours >= 24 )
213
220
                _hours = 0;
 
221
        save_time();
214
222
}
215
223
 
216
224
 
218
226
{
219
227
        if( ++_minutes >= 60 )
220
228
                _minutes = 0;
 
229
        save_time();
221
230
}
222
231
 
223
232
 
224
233
void Time::reset_seconds()
225
234
{
226
235
        _seconds = 0;
 
236
        save_time();
227
237
}
228
238
 
229
239
 
231
241
{
232
242
        if( ++_year >= YEAR_MAX )
233
243
                _year = YEAR_MIN;
 
244
        save_time();
234
245
}
235
246
 
236
247
 
245
256
{
246
257
        if( ++_day > days_in_month( _month, _year ) )
247
258
                _day = 1;
 
259
        save_time();
248
260
}
249
261
 
250
262