/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/propeller-clock.cc

  • Committer: Tim Marston
  • Date: 2013-03-31 17:07:36 UTC
  • Revision ID: tim@ed.am-20130331170736-hphm2hg0y6l7w6z1
made rtc-test's DS1307 library a symlink to the main one in src/util

Show diffs side-by-side

added added

removed removed

1
 
/* -*- mode: c++; compile-command: "BOARD=pro5v make"; -*- */
 
1
/* -*- mode: c++; compile-command: "make"; -*- */
2
2
/*
3
3
 * propeller-clock.ino
4
4
 *
28
28
 
29
29
 * a PC fan is wired up to a 12V power supply
30
30
 
31
 
 * the fan's SENSE (tachiometer) pin connected to pin 2 on the
32
 
   arduino.
 
31
 * the fan's SENSE (tachometer) pin connected to pin 2 on the
 
32
   Arduino.
33
33
 
34
 
 * the pins 4 to 13 on the arduino should directly drive an LED (the
 
34
 * the pins 4 to 13 on the Arduino should directly drive an LED (the
35
35
   LED on pin 4 is in the centre of the clock face and the LED on pin
36
36
   13 is at the outside.
37
37
 
38
38
 * if a longer hand (and a larger clock face) is desired, pin 4 can be
39
39
   used to indirectly drive a transistor which in turn drives several
40
 
   LEDs that turn on anf off in unison in the centre of the clock.
 
40
   LEDs that turn on and off in unison in the centre of the clock.
41
41
 
42
42
 * a button should be attached to pin 3 that grounds it when pressed.
43
43
 
44
 
 * A DS1307 remote clock is connected via I2C on analog pins 4 and 5.
 
44
 * A DS1307 remote clock is connected via I2C on analogue pins 4 and 5.
45
45
 
46
46
Implementation details:
47
47
 
50
50
 * the timing of the drawing of the clock face is recalculated with
51
51
   every rotation of the propeller.
52
52
    
53
 
 * a PC fan actually sends 2 tachiometer pulses per revolution, so the
 
53
 * a PC fan actually sends 2 tachometer pulses per revolution, so the
54
54
   software skips every other one. This means that the clock may
55
55
   appear upside-down if started with the propeller in the wrong
56
 
   position. You will need to experiment to dicsover the position that
 
56
   position. You will need to experiment to discover the position that
57
57
   the propeller must be in when starting the clock.
58
58
    
59
59
Usage instructions:
79
79
#include "button.h"
80
80
#include "time.h"
81
81
#include "Arduino.h"
82
 
#include "analogue_clock.h"
83
 
#include "digital_clock.h"
84
 
#include "test_pattern.h"
 
82
#include "modes/switcher_major_mode.h"
 
83
#include "modes/settings_major_mode.h"
 
84
#include "modes/analogue_clock_mode.h"
 
85
#include "modes/digital_clock_mode.h"
 
86
#include "modes/info_mode.h"
 
87
#include "modes/test_pattern_mode.h"
 
88
#include "text.h"
 
89
#include "text_renderer.h"
 
90
#include "common.h"
85
91
 
86
92
//_____________________________________________________________________________
87
93
//                                                                         data
104
110
// the button
105
111
static Button _button( 3 );
106
112
 
107
 
// modes
108
 
static int _major_mode = 0;
109
 
static int _minor_mode = 0;
110
 
 
111
 
#define MAIN_MODE_IDX 0
112
 
 
113
 
#define ANALOGUE_CLOCK_IDX 0
114
 
#define DIGITAL_CLOCK_IDX 1
115
 
#define TEST_PATTERN_IDX 2
 
113
// major modes
 
114
static MajorMode *_modes[ 3 ];
 
115
 
 
116
// current major mode
 
117
static int _mode = 0;
 
118
 
 
119
// interupt handler's "ignore every other" flag
 
120
static bool _pulse_ignore = true;
116
121
 
117
122
//_____________________________________________________________________________
118
123
//                                                                         code
119
124
 
120
 
 
121
 
// activate the current minor mode
122
 
void activate_minor_mode()
123
 
{
124
 
        switch( _minor_mode ) {
125
 
        case DIGITAL_CLOCK_IDX: digital_clock_activate(); break;
126
 
        }
127
 
}
128
 
 
129
125
// perform button events
130
126
void do_button_events()
131
127
{
136
132
                {
137
133
                case 1:
138
134
                        // short press
139
 
                        switch( _major_mode ) {
140
 
                        case MAIN_MODE_IDX:
141
 
                                switch( _minor_mode ) {
142
 
                                case ANALOGUE_CLOCK_IDX: analogue_clock_press(); break;
143
 
                                case DIGITAL_CLOCK_IDX: digital_clock_press(); break;
144
 
                                }
145
 
                                break;
146
 
                        }
 
135
                        _modes[ _mode ]->press();
147
136
                        break;
148
 
 
149
137
                case 2:
150
138
                        // long press
151
 
                        switch( _major_mode ) {
152
 
                        case MAIN_MODE_IDX:
153
 
                                if( ++_minor_mode >= 3 )
154
 
                                        _minor_mode = 0;
155
 
                                switch( _minor_mode ) {
156
 
                                case DIGITAL_CLOCK_IDX: digital_clock_activate(); break;
157
 
                                }
158
 
                                break;
159
 
                        }
 
139
                        _modes[ _mode ]->long_press();
160
140
                        break;
161
 
 
162
141
                case 3:
163
142
                        // looooong press (change major mode)
164
 
                        if( ++_major_mode > 0 )
165
 
                                _major_mode = 0;
166
 
                        switch( _major_mode ) {
167
 
                        case MAIN_MODE_IDX: _minor_mode = 0; break;
168
 
                        }
169
 
                        activate_minor_mode();
 
143
                        _modes[ _mode ]->deactivate();
 
144
                        if( !_modes[ ++_mode ] ) _mode = 0;
 
145
                        _modes[ _mode ]->activate();
 
146
                        break;
 
147
                case 4:
 
148
                        // switch display upside-down
 
149
                        _pulse_ignore = !_pulse_ignore;
170
150
                        break;
171
151
                }
172
152
        }
177
157
void draw_next_segment( bool reset )
178
158
{
179
159
        // keep track of segment
 
160
        static int segment = 0;
180
161
#if CLOCK_FORWARD
181
 
        static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
182
162
        if( reset ) segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
183
163
#else
184
 
        static int segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
185
164
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
186
165
#endif
187
166
 
 
167
        // reset the text renderer's buffer
 
168
        TextRenderer::reset_buffer();
 
169
 
 
170
        if( reset )
 
171
        {
 
172
                _modes[ _mode ]->draw_reset();
 
173
 
 
174
                // tell the text services we're starting a new frame
 
175
                Text::draw_reset();
 
176
        }
 
177
 
188
178
        // draw
189
 
        switch( _major_mode ) {
190
 
        case MAIN_MODE_IDX:
191
 
                switch( _minor_mode ) {
192
 
                case ANALOGUE_CLOCK_IDX: analogue_clock_draw( segment ); break;
193
 
                case DIGITAL_CLOCK_IDX: digital_clock_draw( segment ); break;
194
 
                case TEST_PATTERN_IDX: test_pattern_draw( segment ); break;
195
 
                }
196
 
                break;
197
 
        }
 
179
        _modes[ _mode ]->draw( segment );
 
180
 
 
181
        // draw text
 
182
        Text::draw( segment );
 
183
 
 
184
        // draw text rednerer's buffer
 
185
        TextRenderer::output_buffer();
198
186
 
199
187
#if CLOCK_FORWARD
200
188
        if( ++segment >= NUM_SEGMENTS ) segment = 0;
248
236
}
249
237
 
250
238
 
251
 
// ISR to handle the pulses from the fan's tachiometer
 
239
// ISR to handle the pulses from the fan's tachometer
252
240
void fan_pulse_handler()
253
241
{
254
242
        // the fan actually sends two pulses per revolution. These pulses
255
243
        // may not be exactly evenly distributed around the rotation, so
256
244
        // we can't recalculate times on every pulse. Instead, we ignore
257
245
        // every other pulse so timings are based on a complete rotation.
258
 
        static bool ignore = true;
259
 
        ignore = !ignore;
260
 
        if( !ignore )
 
246
        _pulse_ignore = !_pulse_ignore;
 
247
        if( !_pulse_ignore )
261
248
        {
262
249
                // set a new pulse time
263
250
                _new_pulse_at = micros();
268
255
// main setup
269
256
void setup()
270
257
{
271
 
        // set up an interrupt handler on pin 2 to nitice fan pulses
 
258
        // set up an interrupt handler on pin 2 to notice fan pulses
272
259
        attachInterrupt( 0, fan_pulse_handler, RISING );
273
260
        digitalWrite( 2, HIGH );
274
261
  
279
266
        // set up mode-switch button on pin 3
280
267
        pinMode( 3, INPUT );
281
268
        digitalWrite( 3, HIGH );
282
 
        static int event_times[] = { 5, 500, 4000, 0 };
 
269
        static int event_times[] = { 10, 500, 2000, 4000, 0 };
283
270
        _button.set_event_times( event_times );
284
271
 
285
 
        // get time from RTC
286
 
        Time::init();
287
 
 
288
 
        // activate the minor mode
289
 
        switch( _major_mode ) {
290
 
        case MAIN_MODE_IDX: activate_minor_mode(); break;
291
 
        }
 
272
        // initialise RTC
 
273
        Time::load_time();
 
274
 
 
275
        // init text renderer
 
276
        TextRenderer::init();
 
277
 
 
278
        // reset text
 
279
        Text::reset();
 
280
        leds_off();
 
281
 
 
282
        static SwitcherMajorMode switcher;
 
283
        static SettingsMajorMode settings( _button );
 
284
 
 
285
        // add major modes
 
286
        int mode = 0;
 
287
        _modes[ mode++ ] = &switcher;
 
288
        _modes[ mode++ ] = &settings;
 
289
        _modes[ mode ] = 0;
 
290
 
 
291
        // activate the current major mode
 
292
        _modes[ _mode ]->activate();
292
293
}
293
294
 
294
295