/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"
 
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;
 
118
 
 
119
// interupt handler's "ignore every other" flag
 
120
static bool _pulse_ignore = true;
116
121
 
117
122
//_____________________________________________________________________________
118
123
//                                                                         code
119
124
 
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
125
// perform button events
131
126
void do_button_events()
132
127
{
137
132
                {
138
133
                case 1:
139
134
                        // 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
 
                        }
 
135
                        _modes[ _mode ]->press();
148
136
                        break;
149
 
 
150
137
                case 2:
151
138
                        // 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
 
                        }
 
139
                        _modes[ _mode ]->long_press();
161
140
                        break;
162
 
 
163
141
                case 3:
164
142
                        // 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();
 
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;
171
150
                        break;
172
151
                }
173
152
        }
178
157
void draw_next_segment( bool reset )
179
158
{
180
159
        // keep track of segment
 
160
        static int segment = 0;
181
161
#if CLOCK_FORWARD
182
 
        static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
183
162
        if( reset ) segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
184
163
#else
185
 
        static int segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
186
164
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
187
165
#endif
188
166
 
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
 
                }
 
167
        // reset the text renderer's buffer
 
168
        TextRenderer::reset_buffer();
 
169
 
 
170
        if( reset )
 
171
        {
 
172
                _modes[ _mode ]->draw_reset();
 
173
 
 
174
                // tell the text services we're starting a new frame
 
175
                Text::draw_reset();
199
176
        }
200
177
 
201
178
        // 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
 
        }
 
179
        _modes[ _mode ]->draw( segment );
 
180
 
 
181
        // draw text
 
182
        Text::draw( segment );
 
183
 
 
184
        // draw text rednerer's buffer
 
185
        TextRenderer::output_buffer();
211
186
 
212
187
#if CLOCK_FORWARD
213
188
        if( ++segment >= NUM_SEGMENTS ) segment = 0;
261
236
}
262
237
 
263
238
 
264
 
// ISR to handle the pulses from the fan's tachiometer
 
239
// ISR to handle the pulses from the fan's tachometer
265
240
void fan_pulse_handler()
266
241
{
267
242
        // the fan actually sends two pulses per revolution. These pulses
268
243
        // may not be exactly evenly distributed around the rotation, so
269
244
        // we can't recalculate times on every pulse. Instead, we ignore
270
245
        // every other pulse so timings are based on a complete rotation.
271
 
        static bool ignore = true;
272
 
        ignore = !ignore;
273
 
        if( !ignore )
 
246
        _pulse_ignore = !_pulse_ignore;
 
247
        if( !_pulse_ignore )
274
248
        {
275
249
                // set a new pulse time
276
250
                _new_pulse_at = micros();
281
255
// main setup
282
256
void setup()
283
257
{
284
 
        // 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
285
259
        attachInterrupt( 0, fan_pulse_handler, RISING );
286
260
        digitalWrite( 2, HIGH );
287
261
  
292
266
        // set up mode-switch button on pin 3
293
267
        pinMode( 3, INPUT );
294
268
        digitalWrite( 3, HIGH );
295
 
        static int event_times[] = { 5, 500, 4000, 0 };
 
269
        static int event_times[] = { 10, 500, 2000, 4000, 0 };
296
270
        _button.set_event_times( event_times );
297
271
 
298
272
        // initialise RTC
299
 
        Time::init();
300
 
 
301
 
        // activate the minor mode
302
 
        switch( _major_mode ) {
303
 
        case MAIN_MODE_IDX: activate_minor_mode(); break;
304
 
        }
 
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();
305
293
}
306
294
 
307
295