/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-09 20:53:26 UTC
  • Revision ID: tim@ed.am-20120509205326-23nmnh3i05hotlro
moved modes to a subdirectory

Show diffs side-by-side

added added

removed removed

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