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