/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-04-29 15:27:19 UTC
  • Revision ID: tim@ed.am-20120429152719-4cu2t9lx7bxpbml1
added adjustable text scaling factor

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
        // reset text
 
130
        Text::reset();
 
131
        leds_off();
 
132
 
 
133
        // give the mode a chance to init
 
134
        switch( _minor_mode ) {
 
135
        case ANALOGUE_CLOCK_IDX: analogue_clock_activate(); break;
 
136
        case DIGITAL_CLOCK_IDX: digital_clock_activate(); break;
 
137
        }
 
138
}
 
139
 
 
140
 
 
141
// activate major mode
 
142
void activate_major_mode()
 
143
{
 
144
        // reset text
 
145
        Text::reset();
 
146
        leds_off();
 
147
 
 
148
        // reset buttons
 
149
        _button.set_press_mode( _major_mode != SETTINGS_MODE_IDX );
 
150
 
 
151
        // give the mode a chance to init
 
152
        switch( _major_mode ) {
 
153
        case MAIN_MODE_IDX: activate_minor_mode(); break;
 
154
        case SETTINGS_MODE_IDX: settings_mode_activate(); break;
 
155
        }
 
156
}
 
157
 
 
158
 
125
159
// perform button events
126
160
void do_button_events()
127
161
{
132
166
                {
133
167
                case 1:
134
168
                        // short press
135
 
                        _modes[ _mode ]->press();
 
169
                        switch( _major_mode ) {
 
170
                        case MAIN_MODE_IDX:
 
171
                                switch( _minor_mode ) {
 
172
                                case ANALOGUE_CLOCK_IDX: analogue_clock_press(); break;
 
173
                                case DIGITAL_CLOCK_IDX: digital_clock_press(); break;
 
174
                                }
 
175
                                break;
 
176
                        case SETTINGS_MODE_IDX: settings_mode_press(); break;
 
177
                        }
136
178
                        break;
 
179
 
137
180
                case 2:
138
181
                        // long press
139
 
                        _modes[ _mode ]->long_press();
 
182
                        switch( _major_mode ) {
 
183
                        case MAIN_MODE_IDX:
 
184
                                if( ++_minor_mode >= 3 )
 
185
                                        _minor_mode = 0;
 
186
                                activate_minor_mode();
 
187
                                break;
 
188
                        case SETTINGS_MODE_IDX: settings_mode_long_press(); break;
 
189
                        }
140
190
                        break;
 
191
 
141
192
                case 3:
142
193
                        // 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;
 
194
                        if( ++_major_mode > 1 )
 
195
                                _major_mode = 0;
 
196
                        activate_major_mode();
150
197
                        break;
151
198
                }
152
199
        }
157
204
void draw_next_segment( bool reset )
158
205
{
159
206
        // keep track of segment
160
 
        static int segment = 0;
161
207
#if CLOCK_FORWARD
 
208
        static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
162
209
        if( reset ) segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
163
210
#else
 
211
        static int segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
164
212
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
165
213
#endif
166
214
 
167
 
        // reset the text renderer's buffer
 
215
        // reset the text renderer
168
216
        TextRenderer::reset_buffer();
169
217
 
170
 
        if( reset )
171
 
        {
172
 
                _modes[ _mode ]->draw_reset();
 
218
        // frame reset
 
219
        if( reset ) {
 
220
                switch( _major_mode ) {
 
221
                case MAIN_MODE_IDX:
 
222
                        switch( _minor_mode ) {
 
223
                        case ANALOGUE_CLOCK_IDX: analogue_clock_draw_reset(); break;
 
224
                        case DIGITAL_CLOCK_IDX: digital_clock_draw_reset(); break;
 
225
                        }
 
226
                        break;
 
227
                case SETTINGS_MODE_IDX: settings_mode_draw_reset(); break;
 
228
                }
173
229
 
174
230
                // tell the text services we're starting a new frame
175
231
                Text::draw_reset();
176
232
        }
177
233
 
178
234
        // draw
179
 
        _modes[ _mode ]->draw( segment );
180
 
 
181
 
        // draw text
182
 
        Text::draw( segment );
183
 
 
184
 
        // draw text rednerer's buffer
 
235
        switch( _major_mode ) {
 
236
        case MAIN_MODE_IDX:
 
237
                switch( _minor_mode ) {
 
238
                case ANALOGUE_CLOCK_IDX: analogue_clock_draw( segment ); break;
 
239
                case DIGITAL_CLOCK_IDX: digital_clock_draw( segment ); break;
 
240
                case TEST_PATTERN_IDX: test_pattern_draw( segment ); break;
 
241
                }
 
242
                break;
 
243
        case SETTINGS_MODE_IDX: settings_mode_draw( segment ); break;
 
244
        }
 
245
 
 
246
        // draw any text that was rendered
185
247
        TextRenderer::output_buffer();
186
248
 
187
249
#if CLOCK_FORWARD
236
298
}
237
299
 
238
300
 
239
 
// ISR to handle the pulses from the fan's tachometer
 
301
// ISR to handle the pulses from the fan's tachiometer
240
302
void fan_pulse_handler()
241
303
{
242
304
        // the fan actually sends two pulses per revolution. These pulses
243
305
        // may not be exactly evenly distributed around the rotation, so
244
306
        // we can't recalculate times on every pulse. Instead, we ignore
245
307
        // every other pulse so timings are based on a complete rotation.
246
 
        _pulse_ignore = !_pulse_ignore;
247
 
        if( !_pulse_ignore )
 
308
        static bool ignore = true;
 
309
        ignore = !ignore;
 
310
        if( !ignore )
248
311
        {
249
312
                // set a new pulse time
250
313
                _new_pulse_at = micros();
255
318
// main setup
256
319
void setup()
257
320
{
258
 
        // set up an interrupt handler on pin 2 to notice fan pulses
 
321
        // set up an interrupt handler on pin 2 to nitice fan pulses
259
322
        attachInterrupt( 0, fan_pulse_handler, RISING );
260
323
        digitalWrite( 2, HIGH );
261
324
  
266
329
        // set up mode-switch button on pin 3
267
330
        pinMode( 3, INPUT );
268
331
        digitalWrite( 3, HIGH );
269
 
        static int event_times[] = { 10, 500, 2000, 4000, 0 };
 
332
        static int event_times[] = { 5, 500, 4000, 0 };
270
333
        _button.set_event_times( event_times );
271
334
 
272
335
        // initialise RTC
273
 
        Time::load_time();
 
336
        Time::init();
274
337
 
275
338
        // init text renderer
276
339
        TextRenderer::init();
277
340
 
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();
 
341
        // activate the minor mode
 
342
        activate_major_mode();
293
343
}
294
344
 
295
345