/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-05-23 23:02:50 UTC
  • Revision ID: tim@ed.am-20120523230250-3pls2u6zt3av0uam
fixed text glitch; extended all modes; added screen flip super-long press;
added button unpress debounde; moved interim button press ignoration to
settings mode; fixed left-over led issue; finished for demo!

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
 *
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"
85
 
#include "settings_mode.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"
86
88
#include "text.h"
87
89
#include "text_renderer.h"
88
90
#include "common.h"
108
110
// the button
109
111
static Button _button( 3 );
110
112
 
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
 
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;
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
 
 
154
125
// perform button events
155
126
void do_button_events()
156
127
{
161
132
                {
162
133
                case 1:
163
134
                        // short 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
 
                        }
 
135
                        _modes[ _mode ]->press();
173
136
                        break;
174
 
 
175
137
                case 2:
176
138
                        // 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
 
                        }
 
139
                        _modes[ _mode ]->long_press();
185
140
                        break;
186
 
 
187
141
                case 3:
188
142
                        // looooong press (change major mode)
189
 
                        if( ++_major_mode > 1 )
190
 
                                _major_mode = 0;
191
 
                        activate_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;
192
150
                        break;
193
151
                }
194
152
        }
199
157
void draw_next_segment( bool reset )
200
158
{
201
159
        // keep track of segment
 
160
        static int segment = 0;
202
161
#if CLOCK_FORWARD
203
 
        static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
204
162
        if( reset ) segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
205
163
#else
206
 
        static int segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
207
164
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
208
165
#endif
209
166
 
210
 
        // reset the text renderer
 
167
        // reset the text renderer's buffer
211
168
        TextRenderer::reset_buffer();
212
169
 
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
 
                }
 
170
        if( reset )
 
171
        {
 
172
                _modes[ _mode ]->draw_reset();
224
173
 
225
174
                // tell the text services we're starting a new frame
226
175
                Text::draw_reset();
227
176
        }
228
177
 
229
178
        // draw
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
 
179
        _modes[ _mode ]->draw( segment );
 
180
 
 
181
        // draw text
 
182
        Text::draw( segment );
 
183
 
 
184
        // draw text rednerer's buffer
242
185
        TextRenderer::output_buffer();
243
186
 
244
187
#if CLOCK_FORWARD
293
236
}
294
237
 
295
238
 
296
 
// ISR to handle the pulses from the fan's tachiometer
 
239
// ISR to handle the pulses from the fan's tachometer
297
240
void fan_pulse_handler()
298
241
{
299
242
        // the fan actually sends two pulses per revolution. These pulses
300
243
        // may not be exactly evenly distributed around the rotation, so
301
244
        // we can't recalculate times on every pulse. Instead, we ignore
302
245
        // every other pulse so timings are based on a complete rotation.
303
 
        static bool ignore = true;
304
 
        ignore = !ignore;
305
 
        if( !ignore )
 
246
        _pulse_ignore = !_pulse_ignore;
 
247
        if( !_pulse_ignore )
306
248
        {
307
249
                // set a new pulse time
308
250
                _new_pulse_at = micros();
313
255
// main setup
314
256
void setup()
315
257
{
316
 
        // 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
317
259
        attachInterrupt( 0, fan_pulse_handler, RISING );
318
260
        digitalWrite( 2, HIGH );
319
261
  
324
266
        // set up mode-switch button on pin 3
325
267
        pinMode( 3, INPUT );
326
268
        digitalWrite( 3, HIGH );
327
 
        static int event_times[] = { 5, 500, 4000, 0 };
 
269
        static int event_times[] = { 10, 500, 2000, 4000, 0 };
328
270
        _button.set_event_times( event_times );
329
271
 
330
272
        // initialise RTC
331
 
        Time::init();
332
 
 
333
 
        // activate the minor mode
334
 
        activate_major_mode();
 
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();
335
293
}
336
294
 
337
295