bzr branch
http://bzr.ed.am/elec/propeller-clock
56
by edam
updated software to include drawing abstraction infrastructure |
1 |
#include "time.h" |
2 |
#include <DS1307.h> |
|
3 |
#include <Wire.h> |
|
4 |
||
5 |
||
6 |
Time &Time::get_instance() |
|
7 |
{ |
|
8 |
static Time time; |
|
9 |
return time; |
|
10 |
} |
|
11 |
||
12 |
||
13 |
int Time::get_hours() |
|
14 |
{ |
|
15 |
return _hours; |
|
16 |
} |
|
17 |
||
18 |
||
19 |
int Time::get_minutes() |
|
20 |
{ |
|
21 |
return _minutes; |
|
22 |
} |
|
23 |
||
24 |
||
25 |
int Time::get_seconds() |
|
26 |
{ |
|
27 |
return _seconds; |
|
28 |
} |
|
29 |
||
30 |
||
31 |
void Time::update() |
|
32 |
{ |
|
33 |
// how many milliseonds have elapsed since we last checked? |
|
34 |
unsigned long millis = ::millis(); |
|
35 |
unsigned long delta = millis - _last_millis + _carry; |
|
36 |
||
37 |
// update the previous time and carried-over milliseconds |
|
38 |
_last_millis = millis; |
|
39 |
_carry = delta % 1000; |
|
40 |
||
41 |
// add the seconds that have passed to the time |
|
42 |
_seconds += delta / 1000; |
|
43 |
while( _seconds >= 60 ) { |
|
44 |
_seconds -= 60; |
|
45 |
_minutes++; |
|
46 |
if( _minutes >= 60 ) { |
|
47 |
_minutes -= 60; |
|
48 |
_hours++; |
|
49 |
if( _hours >= 24 ) |
|
50 |
_hours -= 24; |
|
51 |
} |
|
52 |
} |
|
53 |
} |
|
54 |
||
55 |
||
56 |
Time::Time() |
|
57 |
: |
|
58 |
_last_millis( millis() ), |
|
59 |
_carry( 0 ) |
|
60 |
{ |
|
61 |
// get the time from the real-time clock |
|
62 |
int rtc_data[ 7 ]; |
|
63 |
RTC.get( rtc_data, true ); |
|
64 |
_hours = rtc_data[ DS1307_HR ]; |
|
65 |
_minutes = rtc_data[ DS1307_MIN ]; |
|
66 |
_seconds = rtc_data[ DS1307_SEC ]; |
|
67 |
} |
|
68 |