/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
/* -*- mode: c++; compile-command: "BOARD=pro5v make"; -*- */
/*
 * propeller-clock.ino
 *
 * 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/>.
 */

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

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 a transistor which in turn drives several
   LEDs that turn on anf off in unison in the centre of the clock.

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

 * A DS1307 remote clock is connected via I2C on analog pins 4 and 5.

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
    - pressing and holding the button for 5 seconds sets the time and
      exits "time set" mode

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

#include "config.h"
#include "display.h"
#include "button.h"
#include "time.h"
#include "switcher_major_mode.h"
#include "drawer.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;

// the button
static Button button( 3 );

// major mode
static int major_mode = 0;

#define MAX_MAJOR_MODES 5

// major modes
static MajorMode *major_modes[ MAX_MAJOR_MODES ] = { 0 };

//_____________________________________________________________________________
//                                                                         code


// perform button events
void doButtonEvents()
{
	// loop through pending events
	while( int event = button.get_event() )
	{
		switch( event )
		{
		case 1:
			// short press
			major_modes[ major_mode ]->press();
			break;

		case 2:
			// long press
			major_modes[ major_mode ]->long_press();
			break;

		case 3:
			// looooong press (change major mode)
			do {
				if( ++major_mode >= MAX_MAJOR_MODES )
					major_mode = 0;
			} while( major_modes[ major_mode ] == NULL );
			major_modes[ major_mode ]->activate();
			break;

		}
	}
}


// draw a display segment
void drawNextSegment( bool reset )
{
	// keep track of segment
#if CLOCK_FORWARD
	static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
	if( reset ) segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
#else
	static int segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
	if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
#endif

	// draw
	Drawer &drawer = major_modes[ major_mode ]->get_drawer();
	if( reset ) drawer.draw_reset();
	drawer.draw( segment );

#if CLOCK_FORWARD
	if( ++segment >= NUM_SEGMENTS ) segment = 0;
#else
	if( --segment < 0 ) segment = NUM_SEGMENTS - 1;
#endif
}


// 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 waitTillEndOfSegment( 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 );
	digitalWrite( 3, HIGH );
	static int event_times[] = { 5, 500, 4000, 0 };
	button.set_event_times( event_times );

	// set up major modes
	static SwitcherMajorMode switcher_major_mode;
	int mode = 0;
	major_modes[ mode++ ] = &switcher_major_mode;
	major_modes[ 0 ]->activate();
}


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

	// update button
	button.update();

	// only do this stuff at the start of a display cycle, to ensure
	// that no state changes mid-display
	if( reset )
	{
		// calculate segment times
		calculateSegmentTimes();

		// keep track of time
		Time &time = Time::get_instance();
		time.update();

		// perform button events
		doButtonEvents();
	}

	// draw this segment
	drawNextSegment( reset );

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