/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-28 16:50:26 UTC
  • Revision ID: edam@waxworlds.org-20120228165026-pwnwo300xx2e2kg6
removed ulibc, fixed button, added text rendering

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