/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-03-09 23:42:20 UTC
  • Revision ID: tim@ed.am-20120309234220-xr1vxzve0o5n2oss
added support for eclipse project and converted to a manual Makefile

Show diffs side-by-side

added added

removed removed

75
75
 
76
76
******************************************************************************/
77
77
 
 
78
#include "config.h"
 
79
#include "display.h"
78
80
#include "button.h"
79
 
#include "config.h"
80
81
#include "time.h"
81
 
#include "mode_switcher.h"
 
82
#include "switcher_major_mode.h"
82
83
#include "drawer.h"
 
84
#include "Arduino.h"
83
85
 
84
86
//_____________________________________________________________________________
85
87
//                                                                         data
106
108
// major mode
107
109
static int major_mode = 0;
108
110
 
 
111
#define MAX_MAJOR_MODES 5
 
112
 
109
113
// major modes
110
 
static std::vector< MajorMode * > major_modes;
 
114
static MajorMode *major_modes[ MAX_MAJOR_MODES ] = { 0 };
111
115
 
112
116
//_____________________________________________________________________________
113
117
//                                                                         code
114
118
 
115
119
 
116
 
// check for button presses
117
 
void checkButtons()
 
120
// perform button events
 
121
void doButtonEvents()
118
122
{
119
 
        // update buttons
120
 
        int event = button.update();
121
 
 
122
 
        // handle any events
123
 
        switch( event ) {
124
 
        case 1:
125
 
                major_modes[ major_mode ]->short_press();
126
 
                break;
127
 
        case 2:
128
 
                major_modes[ major_mode ]->long_press();
129
 
                break;
130
 
        case 3:
131
 
                if( ++major_mode >= major_modes.size() )
132
 
                        major_mode = 0;
133
 
                major_modes[ major_mode ]->activate();
134
 
                break;
 
123
        // loop through pending events
 
124
        while( int event = button.get_event() )
 
125
        {
 
126
                switch( event )
 
127
                {
 
128
                case 1:
 
129
                        // short press
 
130
                        major_modes[ major_mode ]->press();
 
131
                        break;
 
132
 
 
133
                case 2:
 
134
                        // long press
 
135
                        major_modes[ major_mode ]->long_press();
 
136
                        break;
 
137
 
 
138
                case 3:
 
139
                        // looooong press (change major mode)
 
140
                        do {
 
141
                                if( ++major_mode >= MAX_MAJOR_MODES )
 
142
                                        major_mode = 0;
 
143
                        } while( major_modes[ major_mode ] == NULL );
 
144
                        major_modes[ major_mode ]->activate();
 
145
                        break;
 
146
 
 
147
                }
135
148
        }
136
149
}
137
150
 
184
197
 
185
198
// wait until it is time to draw the next segment or a new pulse has
186
199
// occurred
187
 
void waitTillNextSegment( bool reset )
 
200
void waitTillEndOfSegment( bool reset )
188
201
{
189
202
        static unsigned long end_time = 0;
190
203
 
236
249
        // set up mode-switch button on pin 3
237
250
        pinMode( 3, INPUT );
238
251
        digitalWrite( 3, HIGH );
239
 
        button.add_event_at( 5, 1 );
240
 
        button.add_event_at( 1000, 2 );
241
 
        button.add_event_at( 4000, 3 );
242
 
 
243
 
        // serial comms
244
 
        Serial.begin( 9600 );
 
252
        static int event_times[] = { 5, 500, 4000, 0 };
 
253
        button.set_event_times( event_times );
245
254
 
246
255
        // set up major modes
247
 
        static ModeSwitcher mode_switcher;
248
 
        major_modes.push_back( &mode_switcher );
 
256
        static SwitcherMajorMode switcher_major_mode;
 
257
        int mode = 0;
 
258
        major_modes[ mode++ ] = &switcher_major_mode;
249
259
        major_modes[ 0 ]->activate();
250
260
}
251
261
 
256
266
        // if there has been a new pulse, we'll be resetting the display
257
267
        bool reset = new_pulse_at? true : false;
258
268
 
 
269
        // update button
 
270
        button.update();
 
271
 
259
272
        // only do this stuff at the start of a display cycle, to ensure
260
273
        // that no state changes mid-display
261
274
        if( reset )
262
275
        {
263
 
                // check buttons
264
 
                checkButtons();
 
276
                // calculate segment times
 
277
                calculateSegmentTimes();
265
278
 
266
279
                // keep track of time
267
280
                Time &time = Time::get_instance();
268
281
                time.update();
 
282
 
 
283
                // perform button events
 
284
                doButtonEvents();
269
285
        }
270
286
 
271
287
        // draw this segment
272
288
        drawNextSegment( reset );
273
289
 
274
 
        // do we need to recalculate segment times?
275
 
        if( reset )
276
 
                calculateSegmentTimes();
277
 
 
278
290
        // wait till it's time to draw the next segment
279
 
        waitTillNextSegment( reset );
 
291
        waitTillEndOfSegment( reset );
280
292
}