/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-09 23:42:20 UTC
  • Revision ID: tim@ed.am-20120309234220-xr1vxzve0o5n2oss
added support for eclipse project and converted to a manual Makefile

Show diffs side-by-side

added added

removed removed

 
1
/*
 
2
 * time.cc
 
3
 *
 
4
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
 
5
 *
 
6
 * This file is part of propeller-clock (hereafter referred to as "this
 
7
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
 
8
 * information.
 
9
 *
 
10
 * This program is free software: you can redistribute it and/or modify
 
11
 * it under the terms of the GNU Lesser General Public License as published
 
12
 * by the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU Lesser General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
 */
 
23
#include "time.h"
 
24
#include <DS1307.h>
 
25
#include <Wire.h>
 
26
 
 
27
 
 
28
Time &Time::get_instance()
 
29
{
 
30
        static Time time;
 
31
        return time;
 
32
}
 
33
 
 
34
 
 
35
int Time::get_year()
 
36
{
 
37
        return _year;
 
38
}
 
39
 
 
40
 
 
41
int Time::get_month()
 
42
{
 
43
        return _month;
 
44
}
 
45
 
 
46
 
 
47
const char *Time::get_month_name()
 
48
{
 
49
        static const char *months[] = {
 
50
                "January",
 
51
                "February",
 
52
                "March",
 
53
                "April",
 
54
                "May",
 
55
                "June",
 
56
                "July",
 
57
                "August",
 
58
                "September",
 
59
                "October",
 
60
                "November",
 
61
                "December",
 
62
        };
 
63
 
 
64
        return months[ _month - 1 ];
 
65
}
 
66
 
 
67
 
 
68
int Time::get_day()
 
69
{
 
70
        return _day;
 
71
}
 
72
 
 
73
 
 
74
const char *Time::get_day_suffix()
 
75
{
 
76
        switch( _day )
 
77
        {
 
78
        case 1:
 
79
        case 21:
 
80
        case 31:
 
81
                return "st";
 
82
        case 2:
 
83
        case 22:
 
84
                return "nd";
 
85
        case 3:
 
86
        case 23:
 
87
                return "rd";
 
88
        default:
 
89
                return "th";
 
90
        }
 
91
}
 
92
 
 
93
 
 
94
int Time::get_hours()
 
95
{
 
96
        return _hours;
 
97
}
 
98
 
 
99
 
 
100
int Time::get_minutes()
 
101
{
 
102
        return _minutes;
 
103
}
 
104
 
 
105
 
 
106
int Time::get_seconds()
 
107
{
 
108
        return _seconds;
 
109
}
 
110
 
 
111
 
 
112
void Time::update()
 
113
{
 
114
        // how many milliseonds have elapsed since we last checked?
 
115
        unsigned long millis = ::millis();
 
116
        unsigned long delta = millis - _last_millis + _carry;
 
117
 
 
118
        // update the previous time and carried-over milliseconds
 
119
        _last_millis = millis;
 
120
        _carry = delta % 1000;
 
121
 
 
122
        // add the seconds that have passed to the time
 
123
        _seconds += delta / 1000;
 
124
        while( _seconds >= 60 ) {
 
125
                _seconds -= 60;
 
126
                _minutes++;
 
127
                if( _minutes >= 60 ) {
 
128
                        _minutes -= 60;
 
129
                        _hours++;
 
130
                        if( _hours >= 24 ) {
 
131
                                _hours -= 24;
 
132
 
 
133
                                // I mean we *could* work out the day... but FUCK IT! We bought
 
134
                                // some god damn HARDWARE to do that shit!
 
135
                                probe_rtc();
 
136
                        }
 
137
                }
 
138
        }
 
139
}
 
140
 
 
141
 
 
142
Time::Time()
 
143
        :
 
144
        _last_millis( millis() ),
 
145
        _carry( 0 )
 
146
{
 
147
        probe_rtc();
 
148
}
 
149
 
 
150
 
 
151
void Time::probe_rtc()
 
152
{
 
153
        // get the time from the real-time clock
 
154
        int rtc_data[ 7 ];
 
155
        RTC.get( rtc_data, true );
 
156
        _year = rtc_data[ DS1307_YR ];
 
157
        _month = rtc_data[ DS1307_MTH ];
 
158
        _day = rtc_data[ DS1307_DAY ];
 
159
        _hours = rtc_data[ DS1307_HR ];
 
160
        _minutes = rtc_data[ DS1307_MIN ];
 
161
        _seconds = rtc_data[ DS1307_SEC ];
 
162
}
 
163