/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 14:40:04 UTC
  • Revision ID: tim@ed.am-20120518144004-uuz2zrjmyr78wqd5
updated emacs compile command and readjusted clock shift

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"
 
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"
 
88
#include "text.h"
 
89
#include "text_renderer.h"
 
90
#include "common.h"
85
91
 
86
92
//_____________________________________________________________________________
87
93
//                                                                         data
104
110
// the button
105
111
static Button _button( 3 );
106
112
 
107
 
// modes
108
 
static int _major_mode = 0;
109
 
static int _minor_mode = 0;
110
 
 
111
 
#define MAIN_MODE_IDX 0
112
 
 
113
 
#define ANALOGUE_CLOCK_IDX 0
114
 
#define DIGITAL_CLOCK_IDX 1
115
 
#define TEST_PATTERN_IDX 2
 
113
// major modes
 
114
static MajorMode *_modes[ 3 ];
 
115
 
 
116
// current major mode
 
117
static int _mode = 0;
116
118
 
117
119
//_____________________________________________________________________________
118
120
//                                                                         code
119
121
 
120
 
 
121
 
// activate the current minor mode
122
 
void activate_minor_mode()
123
 
{
124
 
        switch( _minor_mode ) {
125
 
        case ANALOGUE_CLOCK_IDX: analogue_clock_activate(); break;
126
 
        case DIGITAL_CLOCK_IDX: digital_clock_activate(); break;
127
 
        }
128
 
}
129
 
 
130
122
// perform button events
131
123
void do_button_events()
132
124
{
137
129
                {
138
130
                case 1:
139
131
                        // short press
140
 
                        switch( _major_mode ) {
141
 
                        case MAIN_MODE_IDX:
142
 
                                switch( _minor_mode ) {
143
 
                                case ANALOGUE_CLOCK_IDX: analogue_clock_press(); break;
144
 
                                case DIGITAL_CLOCK_IDX: digital_clock_press(); break;
145
 
                                }
146
 
                                break;
147
 
                        }
 
132
                        _modes[ _mode ]->press();
148
133
                        break;
149
 
 
150
134
                case 2:
151
135
                        // long press
152
 
                        switch( _major_mode ) {
153
 
                        case MAIN_MODE_IDX:
154
 
                                if( ++_minor_mode >= 3 )
155
 
                                        _minor_mode = 0;
156
 
                                switch( _minor_mode ) {
157
 
                                case DIGITAL_CLOCK_IDX: digital_clock_activate(); break;
158
 
                                }
159
 
                                break;
160
 
                        }
 
136
                        _modes[ _mode ]->long_press();
161
137
                        break;
162
 
 
163
138
                case 3:
164
139
                        // looooong press (change major mode)
165
 
                        if( ++_major_mode > 0 )
166
 
                                _major_mode = 0;
167
 
                        switch( _major_mode ) {
168
 
                        case MAIN_MODE_IDX: _minor_mode = 0; break;
169
 
                        }
170
 
                        activate_minor_mode();
 
140
                        _modes[ _mode ]->deactivate();
 
141
                        if( !_modes[ ++_mode ] ) _mode = 0;
 
142
                        _modes[ _mode ]->activate();
171
143
                        break;
172
144
                }
173
145
        }
186
158
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
187
159
#endif
188
160
 
189
 
        // frame reset
190
 
        if( reset ) {
191
 
                switch( _major_mode ) {
192
 
                case MAIN_MODE_IDX:
193
 
                        switch( _minor_mode ) {
194
 
                        case ANALOGUE_CLOCK_IDX: analogue_clock_draw_reset(); break;
195
 
                        case DIGITAL_CLOCK_IDX: digital_clock_draw_reset(); break;
196
 
                        }
197
 
                        break;
198
 
                }
 
161
        // reset the text renderer's buffer
 
162
        TextRenderer::reset_buffer();
 
163
 
 
164
        if( reset )
 
165
        {
 
166
                _modes[ _mode ]->draw_reset();
 
167
 
 
168
                // tell the text services we're starting a new frame
 
169
                Text::draw_reset();
199
170
        }
200
171
 
201
172
        // draw
202
 
        switch( _major_mode ) {
203
 
        case MAIN_MODE_IDX:
204
 
                switch( _minor_mode ) {
205
 
                case ANALOGUE_CLOCK_IDX: analogue_clock_draw( segment ); break;
206
 
                case DIGITAL_CLOCK_IDX: digital_clock_draw( segment ); break;
207
 
                case TEST_PATTERN_IDX: test_pattern_draw( segment ); break;
208
 
                }
209
 
                break;
210
 
        }
 
173
        _modes[ _mode ]->draw( segment );
 
174
 
 
175
        // TODO: remove this hack
 
176
        Text::post_draw();
 
177
 
 
178
        // draw text rednerer's buffer
 
179
        TextRenderer::output_buffer();
211
180
 
212
181
#if CLOCK_FORWARD
213
182
        if( ++segment >= NUM_SEGMENTS ) segment = 0;
261
230
}
262
231
 
263
232
 
264
 
// ISR to handle the pulses from the fan's tachiometer
 
233
// ISR to handle the pulses from the fan's tachometer
265
234
void fan_pulse_handler()
266
235
{
267
236
        // the fan actually sends two pulses per revolution. These pulses
281
250
// main setup
282
251
void setup()
283
252
{
284
 
        // 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
285
254
        attachInterrupt( 0, fan_pulse_handler, RISING );
286
255
        digitalWrite( 2, HIGH );
287
256
  
298
267
        // initialise RTC
299
268
        Time::init();
300
269
 
301
 
        // activate the minor mode
302
 
        switch( _major_mode ) {
303
 
        case MAIN_MODE_IDX: activate_minor_mode(); break;
304
 
        }
 
270
        // init text renderer
 
271
        TextRenderer::init();
 
272
 
 
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();
305
288
}
306
289
 
307
290