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
|
/* -*- mode: c++; compile-command: "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 (tachometer) 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 and 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 analogue 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 tachometer 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 discover 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 "modes/switcher_major_mode.h"
#include "modes/settings_major_mode.h"
#include "modes/analogue_clock_mode.h"
#include "modes/digital_clock_mode.h"
#include "modes/info_mode.h"
#include "modes/test_pattern_mode.h"
#include "text.h"
#include "text_renderer.h"
#include "common.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 modes
static MajorMode *_modes[ 3 ];
// current major mode
static int _mode = 0;
//_____________________________________________________________________________
// code
// perform button events
void do_button_events()
{
// loop through pending events
while( int event = _button.get_event() )
{
switch( event )
{
case 1:
// short press
_modes[ _mode ]->press();
break;
case 2:
// long press
_modes[ _mode ]->long_press();
break;
case 3:
// looooong press (change major mode)
_modes[ _mode ]->deactivate();
if( !_modes[ ++_mode ] ) _mode = 0;
_modes[ _mode ]->activate();
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
// reset the text renderer's buffer
TextRenderer::reset_buffer();
if( reset )
{
_modes[ _mode ]->draw_reset();
// tell the text services we're starting a new frame
Text::draw_reset();
}
// draw
_modes[ _mode ]->draw( segment );
// TODO: remove this hack
Text::post_draw();
// draw text rednerer's buffer
TextRenderer::output_buffer();
#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 tachometer
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 notice 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 );
// initialise RTC
// Time::load_time();
// init text renderer
TextRenderer::init();
// reset text
Text::reset();
leds_off();
static SwitcherMajorMode switcher;
static SettingsMajorMode settings( _button );
// add major modes
int mode = 0;
_modes[ mode++ ] = &switcher;
_modes[ mode++ ] = &settings;
_modes[ mode ] = 0;
// activate the current major mode
_modes[ _mode ]->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
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 );
}
|