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
|
/* -*- 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 "button.h"
#include "time.h"
#include "Arduino.h"
#include "analogue_clock.h"
#include "digital_clock.h"
#include "test_pattern.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 );
// modes
static int _major_mode = 0;
static int _minor_mode = 0;
#define MAIN_MODE_IDX 0
#define ANALOGUE_CLOCK_IDX 0
#define DIGITAL_CLOCK_IDX 1
#define TEST_PATTERN_IDX 2
//_____________________________________________________________________________
// code
// activate the current minor mode
void activate_minor_mode()
{
switch( _minor_mode ) {
case DIGITAL_CLOCK_IDX: digital_clock_activate(); break;
}
}
// perform button events
void do_button_events()
{
// loop through pending events
while( int event = _button.get_event() )
{
switch( event )
{
case 1:
// short press
switch( _major_mode ) {
case MAIN_MODE_IDX:
switch( _minor_mode ) {
case ANALOGUE_CLOCK_IDX: analogue_clock_press(); break;
case DIGITAL_CLOCK_IDX: digital_clock_press(); break;
}
break;
}
break;
case 2:
// long press
switch( _major_mode ) {
case MAIN_MODE_IDX:
if( ++_minor_mode >= 3 )
_minor_mode = 0;
switch( _minor_mode ) {
case DIGITAL_CLOCK_IDX: digital_clock_activate(); break;
}
break;
}
break;
case 3:
// looooong press (change major mode)
if( ++_major_mode > 0 )
_major_mode = 0;
switch( _major_mode ) {
case MAIN_MODE_IDX: _minor_mode = 0; break;
}
activate_minor_mode();
break;
}
}
}
// draw a display segment
void draw_next_segment( 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
switch( _major_mode ) {
case MAIN_MODE_IDX:
switch( _minor_mode ) {
case ANALOGUE_CLOCK_IDX: analogue_clock_draw( segment ); break;
case DIGITAL_CLOCK_IDX: digital_clock_draw( segment ); break;
case TEST_PATTERN_IDX: test_pattern_draw( segment ); break;
}
break;
}
#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 calculate_segment_times()
{
// 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 wait_till_end_of_segment( 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 fan_pulse_handler()
{
// 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, fan_pulse_handler, 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 );
// get time from RTC
Time::init();
// activate the minor mode
switch( _major_mode ) {
case MAIN_MODE_IDX: activate_minor_mode(); break;
}
}
// 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
calculate_segment_times();
// keep track of time
Time::update();
// perform button events
do_button_events();
}
// draw this segment
draw_next_segment( reset );
// wait till it's time to draw the next segment
wait_till_end_of_segment( reset );
}
|