/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
 
#include "Arduino.h"
85
83
 
86
84
//_____________________________________________________________________________
87
85
//                                                                         data
108
106
// major mode
109
107
static int major_mode = 0;
110
108
 
111
 
#define MAX_MAJOR_MODES 5
112
 
 
113
109
// major modes
114
 
static MajorMode *major_modes[ MAX_MAJOR_MODES ] = { 0 };
 
110
static std::vector< MajorMode * > major_modes;
115
111
 
116
112
//_____________________________________________________________________________
117
113
//                                                                         code
118
114
 
119
115
 
120
 
// perform button events
121
 
void doButtonEvents()
 
116
// check for button presses
 
117
void checkButtons()
122
118
{
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
 
                }
 
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;
148
135
        }
149
136
}
150
137
 
197
184
 
198
185
// wait until it is time to draw the next segment or a new pulse has
199
186
// occurred
200
 
void waitTillEndOfSegment( bool reset )
 
187
void waitTillNextSegment( bool reset )
201
188
{
202
189
        static unsigned long end_time = 0;
203
190
 
249
236
        // set up mode-switch button on pin 3
250
237
        pinMode( 3, INPUT );
251
238
        digitalWrite( 3, HIGH );
252
 
        static int event_times[] = { 5, 500, 4000, 0 };
253
 
        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 );
254
245
 
255
246
        // set up major modes
256
 
        static SwitcherMajorMode switcher_major_mode;
257
 
        int mode = 0;
258
 
        major_modes[ mode++ ] = &switcher_major_mode;
 
247
        static ModeSwitcher mode_switcher;
 
248
        major_modes.push_back( &mode_switcher );
259
249
        major_modes[ 0 ]->activate();
260
250
}
261
251
 
266
256
        // if there has been a new pulse, we'll be resetting the display
267
257
        bool reset = new_pulse_at? true : false;
268
258
 
269
 
        // update button
270
 
        button.update();
271
 
 
272
259
        // only do this stuff at the start of a display cycle, to ensure
273
260
        // that no state changes mid-display
274
261
        if( reset )
275
262
        {
276
 
                // calculate segment times
277
 
                calculateSegmentTimes();
 
263
                // check buttons
 
264
                checkButtons();
278
265
 
279
266
                // keep track of time
280
267
                Time &time = Time::get_instance();
281
268
                time.update();
282
 
 
283
 
                // perform button events
284
 
                doButtonEvents();
285
269
        }
286
270
 
287
271
        // draw this segment
288
272
        drawNextSegment( reset );
289
273
 
 
274
        // do we need to recalculate segment times?
 
275
        if( reset )
 
276
                calculateSegmentTimes();
 
277
 
290
278
        // wait till it's time to draw the next segment
291
 
        waitTillEndOfSegment( reset );
 
279
        waitTillNextSegment( reset );
292
280
}