/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.ino

  • Committer: edam
  • Date: 2012-02-25 14:54:33 UTC
  • Revision ID: edam@waxworlds.org-20120225145433-kih8qs45x05cum46
removed Bounce library and updated/fixed new code

Show diffs side-by-side

added added

removed removed

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