/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
/*
 * 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/>.
 */

//_____________________________________________________________________________
//                                                                         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;

// display mode
static


//_____________________________________________________________________________
//                                                                         code


// 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();
	}
}


// draw a particular segment
void drawNextSegment( bool reset )
{
	static unsigned int segment = 0;
	if( reset ) segment = 0;
	segment++;

	for( int a = 0; a < 10; a++ )
		digitalWrite( a + 4, ( ( segment >> a ) & 1 )? HIGH : LOW );
}


// 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;
	semgment_step_sub += semgment_step_sub_step;
	if( semgment_step_sub >= NUM_SEGMENTS ) {
		semgment_step_sub -= NUM_SEGMENTS;
		end_time++;
	}

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


// 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 );

	// 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;

	// 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 );
}