/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 21:24:17 UTC
  • Revision ID: tim@ed.am-20120321212417-dnba5l1oheddeyxw
fixed time centring and display in settings mode

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