/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: 2013-03-31 17:07:36 UTC
  • Revision ID: tim@ed.am-20130331170736-hphm2hg0y6l7w6z1
made rtc-test's DS1307 library a symlink to the main one in src/util

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