/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-18 18:29:50 UTC
  • Revision ID: tim@ed.am-20120518182950-t85bn9a21n72uzm8
text messages are now individually enabled and draw()n automatically

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 "modes/analogue_clock.h"
83
 
#include "modes/digital_clock.h"
84
 
#include "modes/test_pattern.h"
85
 
#include "modes/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;
121
118
 
122
119
//_____________________________________________________________________________
123
120
//                                                                         code
124
121
 
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
 
 
159
122
// perform button events
160
123
void do_button_events()
161
124
{
166
129
                {
167
130
                case 1:
168
131
                        // short 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
 
                        }
 
132
                        _modes[ _mode ]->press();
178
133
                        break;
179
 
 
180
134
                case 2:
181
135
                        // 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
 
                        }
 
136
                        _modes[ _mode ]->long_press();
190
137
                        break;
191
 
 
192
138
                case 3:
193
139
                        // looooong press (change major mode)
194
 
                        if( ++_major_mode > 1 )
195
 
                                _major_mode = 0;
196
 
                        activate_major_mode();
 
140
                        _modes[ _mode ]->deactivate();
 
141
                        if( !_modes[ ++_mode ] ) _mode = 0;
 
142
                        _modes[ _mode ]->activate();
197
143
                        break;
198
144
                }
199
145
        }
212
158
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
213
159
#endif
214
160
 
215
 
        // reset the text renderer
 
161
        // reset the text renderer's buffer
216
162
        TextRenderer::reset_buffer();
217
163
 
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
 
                }
 
164
        if( reset )
 
165
        {
 
166
                _modes[ _mode ]->draw_reset();
229
167
 
230
168
                // tell the text services we're starting a new frame
231
169
                Text::draw_reset();
232
170
        }
233
171
 
234
172
        // draw
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
 
173
        _modes[ _mode ]->draw( segment );
 
174
 
 
175
        // draw text
 
176
        Text::draw( segment );
 
177
 
 
178
        // draw text rednerer's buffer
247
179
        TextRenderer::output_buffer();
248
180
 
249
181
#if CLOCK_FORWARD
298
230
}
299
231
 
300
232
 
301
 
// ISR to handle the pulses from the fan's tachiometer
 
233
// ISR to handle the pulses from the fan's tachometer
302
234
void fan_pulse_handler()
303
235
{
304
236
        // the fan actually sends two pulses per revolution. These pulses
318
250
// main setup
319
251
void setup()
320
252
{
321
 
        // set up an interrupt handler on pin 2 to nitice fan pulses
 
253
        // set up an interrupt handler on pin 2 to notice fan pulses
322
254
        attachInterrupt( 0, fan_pulse_handler, RISING );
323
255
        digitalWrite( 2, HIGH );
324
256
  
333
265
        _button.set_event_times( event_times );
334
266
 
335
267
        // initialise RTC
336
 
        Time::init();
 
268
//      Time::load_time();
337
269
 
338
270
        // init text renderer
339
271
        TextRenderer::init();
340
272
 
341
 
        // activate the minor mode
342
 
        activate_major_mode();
 
273
        // reset text
 
274
        Text::reset();
 
275
        leds_off();
 
276
 
 
277
        static SwitcherMajorMode switcher;
 
278
        static SettingsMajorMode settings( _button );
 
279
 
 
280
        // add major modes
 
281
        int mode = 0;
 
282
        _modes[ mode++ ] = &switcher;
 
283
        _modes[ mode++ ] = &settings;
 
284
        _modes[ mode ] = 0;
 
285
 
 
286
        // activate the current major mode
 
287
        _modes[ _mode ]->activate();
343
288
}
344
289
 
345
290