/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-05-18 14:55:12 UTC
  • Revision ID: tim@ed.am-20120518145512-4dg4ble01d47a8z5
moved rount Time a bit, and passed _button to SettingsMajorMode so that it can
come in and out of send_interim mode

Show diffs side-by-side

added added

removed removed

30
30
 
31
31
 
32
32
// year
33
 
static int _year;
 
33
static int _year = 2012;
34
34
 
35
35
// month
36
 
static int _month;
 
36
static int _month = 5;
37
37
 
38
38
// day
39
 
static int _day;
 
39
static int _day = 18;
40
40
 
41
41
// hours
42
 
static int _hours;
 
42
static int _hours = 15;
43
43
 
44
44
// minutes
45
 
static int _minutes;
 
45
static int _minutes = 36;
46
46
 
47
47
// seconds
48
 
static int _seconds;
 
48
static int _seconds = 0;
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 = 2010;
 
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
 
57
100
 
58
101
int Time::get_year()
59
102
{
132
175
}
133
176
 
134
177
 
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
 
 
165
178
void Time::update()
166
179
{
167
180
        // how many milliseconds have elapsed since we last checked?
194
207
}
195
208
 
196
209
 
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
 
 
217
210
void Time::inc_hours()
218
211
{
219
212
        if( ++_hours >= 24 )
220
213
                _hours = 0;
221
 
        save_time();
222
214
}
223
215
 
224
216
 
226
218
{
227
219
        if( ++_minutes >= 60 )
228
220
                _minutes = 0;
229
 
        save_time();
230
221
}
231
222
 
232
223
 
233
224
void Time::reset_seconds()
234
225
{
235
226
        _seconds = 0;
236
 
        save_time();
237
227
}
238
228
 
239
229
 
241
231
{
242
232
        if( ++_year >= YEAR_MAX )
243
233
                _year = YEAR_MIN;
244
 
        save_time();
245
234
}
246
235
 
247
236
 
256
245
{
257
246
        if( ++_day > days_in_month( _month, _year ) )
258
247
                _day = 1;
259
 
        save_time();
260
248
}
261
249
 
262
250