/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * propeller-clock.pde
 *
 * Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
 *
 * This file is part of propeller-clock (hereafter referred to as "this
 * program"). See http://ed.am/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/>.
 */

/******************************************************************************

Set up:

 * a PC fan is wired up to a 12V power supply

 * the fan's SENSE (tachiometer) pin connected to pin 2 on the
   arduino.

 * the pins 4 to 13 on the arduino should directly drive an LED (the
   LED on pin 4 is in the centre of the clock face and the LED on pin
   13 is at the outside.

 * if a longer hand (and a larger clock face) is desired, pin 4 can be
   used to indirectly drive (via a MOSFET) multiple LEDs which turn on
   and off in unison in the centre of the clock.

 * a button should be attached to pin 3 that grounds it when pressed.

Implementation details:

 * for a schematic, see project/propeller-clock.sch.

 * the timing of the drawing of the clock face is recalculated with
   every rotation of the propeller.
    
 * a PC fan actually sends 2 tachiometer pulses per revolution, so the
   software skips every other one. This means that the clock may
   appear upside-down if started with the propeller in the wrong
   position. You will need to experiment to dicsover the position that
   the propeller must be in when starting the clock.
    
Usage instructions:

 * pressing the button cycles between variations of the current
   display mode.
  
 * pressing and holding the button for a second cycles between display
   modes (e.g., analogue and digital).

 * pressing and holding the button for 5 seconds enters "time set"
   mode. In this mode, the following applies:
    - the field that is being set flashes
    - pressing the button increments the field currently being set
    - pressing and holding the button for a second cycles through the
      fields that can be set
    - press and holding the button for 5 seconds to finish

******************************************************************************/


#include <Bounce.h>

//_____________________________________________________________________________
//                                                                         data


// when non-zero, the time (in microseconds) of a new fan pulse that
// has just occurred, which means that segment drawing needs to be
// restarted
static unsigned long new_pulse_at = 0;

// the time (in microseconds) when the last fan pulse occurred
static unsigned long last_pulse_at = 0;

// duration (in microseconds) that a segment should be displayed
static unsigned long segment_step = 0;

// remainder after divisor and a tally of the remainders for each segment
static unsigned long segment_step_sub_step = 0;
static unsigned long segment_step_sub = 0;

// flag to indicate that the drawing mode should be cycled to the next one
static bool inc_draw_mode = false;

// a bounce-managed button
static Bounce button( 3, 5 );

// the time
static int time_hours = 0;
static int time_minutes = 0;
static int time_seconds = 0;

// number of segments in a full display (rotation) is 60 (one per
// second) times the desired number of sub-divisions of a second
#define NUM_SECOND_SEGMENTS 5
#define NUM_SEGMENTS ( 60 * NUM_SECOND_SEGMENTS )

//_____________________________________________________________________________
//                                                                         code


// check for button presses
void checkButtons()
{
	// update buttons
	button.update();

	// notice button presses
	if( button.risingEdge() )
		inc_draw_mode = true;
}


// keep track of time
void trackTime()
{
	// previous time and any carried-over milliseconds
	static unsigned long last_time = millis();
	static unsigned long carry = 0;

	// how many milliseonds have elapsed since we last checked?
	unsigned long next_time = millis();
	unsigned long delta = next_time - last_time + carry;

	// update the previous time and carried-over milliseconds
	last_time = next_time;
	carry = delta % 1000;

	// add the seconds that have passed to the time
	time_seconds += delta / 1000;
	while( time_seconds >= 60 ) {
		time_seconds -= 60;
		time_minutes++;
		if( time_minutes >= 60 ) {
			time_minutes -= 60;
			time_hours++;
			if( time_hours >= 24 )
				time_hours -= 24;
		}
	}
}


// draw a segment for the test display
void drawNextSegment_test( bool reset )
{
	// keep track of segment
	static unsigned int segment = 0;
	if( reset ) segment = 0;
	segment++;

	// turn on inside and outside LEDs
	digitalWrite( 4, HIGH );
	digitalWrite( 13, HIGH );

	// display segment number in binary across in the inside LEDs,
	// with the LED on pin 12 showing the least-significant bit
	for( int a = 0; a < 8; a++ )
		digitalWrite( 12 - a, ( ( segment >> a ) & 1 )? HIGH : LOW );
}


// draw a segment for the time display
void drawNextSegment_time( bool reset )
{
	static unsigned int second = 0;
	static unsigned int segment = 0;

	// handle display reset
	if( reset ) {
		second = 0;
		segment = 0;
	}

	// what needs to be drawn?
	bool draw_tick = !segment && second % 5 == 0;
	bool draw_second = !segment && second == time_seconds;
	bool draw_minute = !segment && second == time_minute;
	bool draw_hour = !segment && second == time_hour;

	// set the LEDs
	digitalWrite( 13, HIGH );
	digitalWrite( 12, draw_tick || draw_minute );
	for( int a = 10; a <= 11; a++ )
		digitalWrite( a, draw_minute || draw_second );
	for( int a = 4; a <= 9; a++ )
		digitalWrite( 10, draw_minute | draw_second || draw_hour );

	// inc position
	if( ++segment >= NUM_SECOND_SEGMENTS ) {
		segment = 0;
		second++;
	}
}


// draw a display segment
void drawNextSegment( bool reset )
{
	static int draw_mode = 0;

 	// handle mode switch requests
	if( reset && inc_draw_mode ) {
		inc_draw_mode = false;
		draw_mode++;
		if( draw_mode >= 2 )
			draw_mode = 0;
	}

	// draw the segment
	switch( draw_mode ) {
	case 0: drawNextSegment_test( reset ); break;
	case 1: drawNextSegment_time( reset ); break;
	}
}


// calculate time constants when a new pulse has occurred
void calculateSegmentTimes()
{
	// check for overflows, and only recalculate times if there isn't
	// one (if there is, we'll just go with the last pulse's times)
	if( new_pulse_at > last_pulse_at )
	{
		// new segment stepping times
		unsigned long delta = new_pulse_at - last_pulse_at;
		segment_step = delta / NUM_SEGMENTS;
		segment_step_sub = 0;
		segment_step_sub_step = delta % NUM_SEGMENTS;
	}

	// now we have dealt with this pulse, save the pulse time and
	// clear new_pulse_at, ready for the next pulse
	last_pulse_at = new_pulse_at;
	new_pulse_at = 0;
}


// wait until it is time to draw the next segment or a new pulse has
// occurred
void waitTillNextSegment( bool reset )
{
	static unsigned long end_time = 0;

	// handle reset
	if( reset )
		end_time = last_pulse_at;

	// work out the time that this segment should be displayed until
	end_time += segment_step;
	segment_step_sub += segment_step_sub_step;
	if( segment_step_sub >= NUM_SEGMENTS ) {
		segment_step_sub -= NUM_SEGMENTS;
		end_time++;
	}

	// wait
	while( micros() < end_time && !new_pulse_at );
}


// ISR to handle the pulses from the fan's tachiometer
void fanPulseHandler()
{
	// the fan actually sends two pulses per revolution. These pulses
	// may not be exactly evenly distributed around the rotation, so
	// we can't recalculate times on every pulse. Instead, we ignore
	// every other pulse so timings are based on a complete rotation.
	static bool ignore = true;
	ignore = !ignore;
	if( !ignore )
	{
		// set a new pulse time
		new_pulse_at = micros();
	}
}


// main setup
void setup()
{
	// set up an interrupt handler on pin 2 to nitice fan pulses
	attachInterrupt( 0, fanPulseHandler, RISING );
	digitalWrite( 2, HIGH );
  
	// set up output pins (4 to 13) for the led array
	for( int a = 4; a < 14; a++ )
		pinMode( a, OUTPUT );

	// set up mode-switch button on pin 3
	pinMode( 3, INPUT );

	// serial comms
	Serial.begin( 9600 );
}


// main loop
void loop()
{
	// if there has been a new pulse, we'll be resetting the display
	bool reset = new_pulse_at? true : false;

	// only do this stuff at the start of a display cycle, to ensure
	// that no state changes mid-display
	if( reset )
	{
		// check buttons
		checkButtons();

		// keep track of time
		trackTime();
	}

	// draw this segment
	drawNextSegment( reset );

	// do we need to recalculate segment times?
	if( reset )
		calculateSegmentTimes();

	// wait till it's time to draw the next segment
	waitTillNextSegment( reset );
}