/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-03-12 21:11:20 UTC
  • Revision ID: tim@ed.am-20120312211120-r86cs574yxztgqij
added time set mode, made text renderer's buffer auto reset/output

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 2010
 
30
 
 
31
 
28
32
// year
29
33
static int _year;
30
34
 
50
54
static unsigned long _carry = 0;
51
55
 
52
56
 
53
 
static void probe_rtc()
54
 
{
55
 
        // get the time from the real-time clock
56
 
        int rtc_data[ 7 ];
57
 
        RTC.get( rtc_data, true );
58
 
        _year = rtc_data[ DS1307_YR ];
59
 
        _month = rtc_data[ DS1307_MTH ];
60
 
        _day = rtc_data[ DS1307_DAY ];
61
 
        _hours = rtc_data[ DS1307_HR ];
62
 
        _minutes = rtc_data[ DS1307_MIN ];
63
 
        _seconds = rtc_data[ DS1307_SEC ];
64
 
}
65
 
 
66
57
 
67
58
int Time::get_year()
68
59
{
141
132
}
142
133
 
143
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
 
144
165
void Time::update()
145
166
{
146
167
        // how many milliseconds have elapsed since we last checked?
162
183
                        if( _hours >= 24 ) {
163
184
                                _hours -= 24;
164
185
 
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();
 
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.
 
190
                                load_time();
168
191
                        }
169
192
                }
170
193
        }
173
196
 
174
197
void Time::init()
175
198
{
176
 
        probe_rtc();
 
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
void Time::inc_hours()
 
218
{
 
219
        if( ++_hours >= 24 )
 
220
                _hours = 0;
 
221
        save_time();
 
222
}
 
223
 
 
224
 
 
225
void Time::inc_minutes()
 
226
{
 
227
        if( ++_minutes >= 60 )
 
228
                _minutes = 0;
 
229
        save_time();
 
230
}
 
231
 
 
232
 
 
233
void Time::reset_seconds()
 
234
{
 
235
        _seconds = 0;
 
236
        save_time();
 
237
}
 
238
 
 
239
 
 
240
void Time::inc_year()
 
241
{
 
242
        if( ++_year >= YEAR_MAX )
 
243
                _year = YEAR_MIN;
 
244
        save_time();
 
245
}
 
246
 
 
247
 
 
248
void Time::inc_month()
 
249
{
 
250
        if( ++_month > 12 )
 
251
                _month = 1;
 
252
}
 
253
 
 
254
 
 
255
void Time::inc_day()
 
256
{
 
257
        if( ++_day > days_in_month( _month, _year ) )
 
258
                _day = 1;
 
259
        save_time();
177
260
}
178
261
 
179
262