/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: edam
  • Date: 2012-02-28 17:03:09 UTC
  • Revision ID: edam@waxworlds.org-20120228170309-gaaj6k3prrgvwvp8
remove TextRenderer singleton and save space

Show diffs side-by-side

added added

removed removed

25
25
#include <Wire.h>
26
26
 
27
27
 
28
 
// year
29
 
static int _year;
30
 
 
31
 
// month
32
 
static int _month;
33
 
 
34
 
// day
35
 
static int _day;
36
 
 
37
 
// hours
38
 
static int _hours;
39
 
 
40
 
// minutes
41
 
static int _minutes;
42
 
 
43
 
// seconds
44
 
static int _seconds;
45
 
 
46
 
// milliseconds at last update
47
 
static unsigned long _last_millis = millis();
48
 
 
49
 
// milliseconds carries over from last update
50
 
static unsigned long _carry = 0;
51
 
 
52
 
 
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
 
 
67
 
int Time::get_year()
68
 
{
69
 
        return _year;
70
 
}
71
 
 
72
 
 
73
 
int Time::get_month()
74
 
{
75
 
        return _month;
76
 
}
77
 
 
78
 
 
79
 
const char *Time::get_month_name()
80
 
{
81
 
        static const char *months[] = {
82
 
                "January",
83
 
                "February",
84
 
                "March",
85
 
                "April",
86
 
                "May",
87
 
                "June",
88
 
                "July",
89
 
                "August",
90
 
                "September",
91
 
                "October",
92
 
                "November",
93
 
                "December",
94
 
        };
95
 
 
96
 
        return months[ _month - 1 ];
97
 
}
98
 
 
99
 
 
100
 
int Time::get_day()
101
 
{
102
 
        return _day;
103
 
}
104
 
 
105
 
 
106
 
const char *Time::get_day_suffix()
107
 
{
108
 
        switch( _day )
109
 
        {
110
 
        case 1:
111
 
        case 21:
112
 
        case 31:
113
 
                return "st";
114
 
        case 2:
115
 
        case 22:
116
 
                return "nd";
117
 
        case 3:
118
 
        case 23:
119
 
                return "rd";
120
 
        default:
121
 
                return "th";
122
 
        }
 
28
Time &Time::get_instance()
 
29
{
 
30
        static Time time;
 
31
        return time;
123
32
}
124
33
 
125
34
 
143
52
 
144
53
void Time::update()
145
54
{
146
 
        // how many milliseconds have elapsed since we last checked?
 
55
        // how many milliseonds have elapsed since we last checked?
147
56
        unsigned long millis = ::millis();
148
57
        unsigned long delta = millis - _last_millis + _carry;
149
58
 
159
68
                if( _minutes >= 60 ) {
160
69
                        _minutes -= 60;
161
70
                        _hours++;
162
 
                        if( _hours >= 24 ) {
 
71
                        if( _hours >= 24 )
163
72
                                _hours -= 24;
164
 
 
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();
168
 
                        }
169
73
                }
170
74
        }
171
75
}
172
76
 
173
77
 
174
 
void Time::init()
 
78
Time::Time()
 
79
        :
 
80
        _last_millis( millis() ),
 
81
        _carry( 0 )
175
82
{
176
 
        probe_rtc();
 
83
        // get the time from the real-time clock
 
84
        int rtc_data[ 7 ];
 
85
        RTC.get( rtc_data, true );
 
86
        _hours = rtc_data[ DS1307_HR ];
 
87
        _minutes = rtc_data[ DS1307_MIN ];
 
88
        _seconds = rtc_data[ DS1307_SEC ];
177
89
}
178
90
 
179