/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
58 by edam
removed Bounce library and updated/fixed new code
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
 */
56 by edam
updated software to include drawing abstraction infrastructure
23
#include "time.h"
24
#include <DS1307.h>
25
#include <Wire.h>
26
27
66 by Tim Marston
removed time singleton, not cause it saved much space, but cause i don't want singletons in this project!
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()
56 by edam
updated software to include drawing abstraction infrastructure
54
{
66 by Tim Marston
removed time singleton, not cause it saved much space, but cause i don't want singletons in this project!
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 ];
56 by edam
updated software to include drawing abstraction infrastructure
64
}
65
66
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
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
	}
123
}
124
125
56 by edam
updated software to include drawing abstraction infrastructure
126
int Time::get_hours()
127
{
128
	return _hours;
129
}
130
131
132
int Time::get_minutes()
133
{
134
	return _minutes;
135
}
136
137
138
int Time::get_seconds()
139
{
140
	return _seconds;
141
}
142
143
144
void Time::update()
145
{
146
	// how many milliseonds have elapsed since we last checked?
147
	unsigned long millis = ::millis();
148
	unsigned long delta = millis - _last_millis + _carry;
149
150
	// update the previous time and carried-over milliseconds
151
	_last_millis = millis;
152
	_carry = delta % 1000;
153
154
	// add the seconds that have passed to the time
155
	_seconds += delta / 1000;
156
	while( _seconds >= 60 ) {
157
		_seconds -= 60;
158
		_minutes++;
159
		if( _minutes >= 60 ) {
160
			_minutes -= 60;
161
			_hours++;
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
162
			if( _hours >= 24 ) {
56 by edam
updated software to include drawing abstraction infrastructure
163
				_hours -= 24;
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
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
			}
56 by edam
updated software to include drawing abstraction infrastructure
169
		}
170
	}
171
}
172
173
66 by Tim Marston
removed time singleton, not cause it saved much space, but cause i don't want singletons in this project!
174
void Time::init()
56 by edam
updated software to include drawing abstraction infrastructure
175
{
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
176
	probe_rtc();
177
}
178
179