/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: 2012-03-21 19:37:33 UTC
  • Revision ID: tim@ed.am-20120321193733-29euxt0t0h9dwsj3
added .dep directories to bzrignore

Show diffs side-by-side

added added

removed removed

1
 
/* -*- mode: c++; compile-command: "make"; -*- */
 
1
/* -*- mode: c++; compile-command: "BOARD=pro5v make"; -*- */
2
2
/*
3
3
 * propeller-clock.ino
4
4
 *
79
79
#include "button.h"
80
80
#include "time.h"
81
81
#include "Arduino.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"
 
82
#include "analogue_clock.h"
 
83
#include "digital_clock.h"
 
84
#include "test_pattern.h"
 
85
#include "settings_mode.h"
88
86
#include "text.h"
89
87
#include "text_renderer.h"
90
88
#include "common.h"
110
108
// the button
111
109
static Button _button( 3 );
112
110
 
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;
 
111
// modes
 
112
static int _major_mode = 0;
 
113
static int _minor_mode = 0;
 
114
 
 
115
#define MAIN_MODE_IDX 1
 
116
#define SETTINGS_MODE_IDX 0
 
117
 
 
118
#define ANALOGUE_CLOCK_IDX 0
 
119
#define DIGITAL_CLOCK_IDX 1
 
120
#define TEST_PATTERN_IDX 2
121
121
 
122
122
//_____________________________________________________________________________
123
123
//                                                                         code
124
124
 
 
125
 
 
126
// activate the current minor mode
 
127
void activate_minor_mode()
 
128
{
 
129
        switch( _minor_mode ) {
 
130
        case ANALOGUE_CLOCK_IDX: analogue_clock_activate(); break;
 
131
        case DIGITAL_CLOCK_IDX: digital_clock_activate(); break;
 
132
        }
 
133
 
 
134
        // reset text
 
135
        Text::reset();
 
136
        leds_off();
 
137
}
 
138
 
 
139
 
 
140
// activate major mode
 
141
void activate_major_mode()
 
142
{
 
143
        switch( _major_mode ) {
 
144
        case MAIN_MODE_IDX: activate_minor_mode(); break;
 
145
        case SETTINGS_MODE_IDX: settings_mode_activate(); break;
 
146
        }
 
147
 
 
148
        // reset text
 
149
        Text::reset();
 
150
        leds_off();
 
151
}
 
152
 
 
153
 
125
154
// perform button events
126
155
void do_button_events()
127
156
{
132
161
                {
133
162
                case 1:
134
163
                        // short press
135
 
                        _modes[ _mode ]->press();
 
164
                        switch( _major_mode ) {
 
165
                        case MAIN_MODE_IDX:
 
166
                                switch( _minor_mode ) {
 
167
                                case ANALOGUE_CLOCK_IDX: analogue_clock_press(); break;
 
168
                                case DIGITAL_CLOCK_IDX: digital_clock_press(); break;
 
169
                                }
 
170
                                break;
 
171
                        case SETTINGS_MODE_IDX: settings_mode_press(); break;
 
172
                        }
136
173
                        break;
 
174
 
137
175
                case 2:
138
176
                        // long press
139
 
                        _modes[ _mode ]->long_press();
 
177
                        switch( _major_mode ) {
 
178
                        case MAIN_MODE_IDX:
 
179
                                if( ++_minor_mode >= 3 )
 
180
                                        _minor_mode = 0;
 
181
                                activate_minor_mode();
 
182
                                break;
 
183
                        case SETTINGS_MODE_IDX: settings_mode_long_press(); break;
 
184
                        }
140
185
                        break;
 
186
 
141
187
                case 3:
142
188
                        // looooong press (change major 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;
 
189
                        if( ++_major_mode > 1 )
 
190
                                _major_mode = 0;
 
191
                        activate_major_mode();
150
192
                        break;
151
193
                }
152
194
        }
157
199
void draw_next_segment( bool reset )
158
200
{
159
201
        // keep track of segment
160
 
        static int segment = 0;
161
202
#if CLOCK_FORWARD
 
203
        static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
162
204
        if( reset ) segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
163
205
#else
 
206
        static int segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
164
207
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
165
208
#endif
166
209
 
167
 
        // reset the text renderer's buffer
 
210
        // reset the text renderer
168
211
        TextRenderer::reset_buffer();
169
212
 
170
 
        if( reset )
171
 
        {
172
 
                _modes[ _mode ]->draw_reset();
 
213
        // frame reset
 
214
        if( reset ) {
 
215
                switch( _major_mode ) {
 
216
                case MAIN_MODE_IDX:
 
217
                        switch( _minor_mode ) {
 
218
                        case ANALOGUE_CLOCK_IDX: analogue_clock_draw_reset(); break;
 
219
                        case DIGITAL_CLOCK_IDX: digital_clock_draw_reset(); break;
 
220
                        }
 
221
                        break;
 
222
                case SETTINGS_MODE_IDX: settings_mode_draw_reset(); break;
 
223
                }
173
224
 
174
225
                // tell the text services we're starting a new frame
175
226
                Text::draw_reset();
176
227
        }
177
228
 
178
229
        // draw
179
 
        _modes[ _mode ]->draw( segment );
180
 
 
181
 
        // draw text
182
 
        Text::draw( segment );
183
 
 
184
 
        // draw text rednerer's buffer
 
230
        switch( _major_mode ) {
 
231
        case MAIN_MODE_IDX:
 
232
                switch( _minor_mode ) {
 
233
                case ANALOGUE_CLOCK_IDX: analogue_clock_draw( segment ); break;
 
234
                case DIGITAL_CLOCK_IDX: digital_clock_draw( segment ); break;
 
235
                case TEST_PATTERN_IDX: test_pattern_draw( segment ); break;
 
236
                }
 
237
                break;
 
238
        case SETTINGS_MODE_IDX: settings_mode_draw( segment ); break;
 
239
        }
 
240
 
 
241
        // draw any text that was rendered
185
242
        TextRenderer::output_buffer();
186
243
 
187
244
#if CLOCK_FORWARD
236
293
}
237
294
 
238
295
 
239
 
// ISR to handle the pulses from the fan's tachometer
 
296
// ISR to handle the pulses from the fan's tachiometer
240
297
void fan_pulse_handler()
241
298
{
242
299
        // the fan actually sends two pulses per revolution. These pulses
243
300
        // may not be exactly evenly distributed around the rotation, so
244
301
        // we can't recalculate times on every pulse. Instead, we ignore
245
302
        // every other pulse so timings are based on a complete rotation.
246
 
        _pulse_ignore = !_pulse_ignore;
247
 
        if( !_pulse_ignore )
 
303
        static bool ignore = true;
 
304
        ignore = !ignore;
 
305
        if( !ignore )
248
306
        {
249
307
                // set a new pulse time
250
308
                _new_pulse_at = micros();
255
313
// main setup
256
314
void setup()
257
315
{
258
 
        // set up an interrupt handler on pin 2 to notice fan pulses
 
316
        // set up an interrupt handler on pin 2 to nitice fan pulses
259
317
        attachInterrupt( 0, fan_pulse_handler, RISING );
260
318
        digitalWrite( 2, HIGH );
261
319
  
266
324
        // set up mode-switch button on pin 3
267
325
        pinMode( 3, INPUT );
268
326
        digitalWrite( 3, HIGH );
269
 
        static int event_times[] = { 10, 500, 2000, 4000, 0 };
 
327
        static int event_times[] = { 5, 500, 4000, 0 };
270
328
        _button.set_event_times( event_times );
271
329
 
272
330
        // 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();
 
331
        Time::init();
 
332
 
 
333
        // activate the minor mode
 
334
        activate_major_mode();
293
335
}
294
336
 
295
337