/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 17:03:09 UTC
  • Revision ID: edam@waxworlds.org-20120228170309-gaaj6k3prrgvwvp8
remove TextRenderer singleton and save space

Show diffs side-by-side

added added

removed removed

75
75
 
76
76
******************************************************************************/
77
77
 
78
 
 
79
 
#include <button.h>
80
78
#include "config.h"
 
79
#include "display.h"
 
80
#include "button.h"
81
81
#include "time.h"
82
 
#include "mode_switcher.h"
 
82
#include "switcher_major_mode.h"
83
83
#include "drawer.h"
84
84
 
85
85
//_____________________________________________________________________________
101
101
static unsigned long segment_step_sub_step = 0;
102
102
static unsigned long segment_step_sub = 0;
103
103
 
104
 
// flag to indicate that the drawing mode should be cycled to the next one
105
 
static bool inc_draw_mode = false;
106
 
 
107
 
// a bounce-managed button
 
104
// the button
108
105
static Button button( 3 );
109
106
 
 
107
// major mode
 
108
static int major_mode = 0;
 
109
 
 
110
#define MAX_MAJOR_MODES 5
 
111
 
 
112
// major modes
 
113
static MajorMode *major_modes[ MAX_MAJOR_MODES ] = { 0 };
 
114
 
110
115
//_____________________________________________________________________________
111
116
//                                                                         code
112
117
 
113
118
 
114
 
// check for button presses
115
 
void checkButtons()
 
119
// perform button events
 
120
void doButtonEvents()
116
121
{
117
 
        // update buttons
118
 
        int event = button.update();
119
 
 
120
 
        // handle any events
121
 
        switch( event ) {
122
 
        case 1:
123
 
                inc_draw_mode = true;
124
 
                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
                }
125
147
        }
126
148
}
127
149
 
128
150
 
129
 
// turn an led on/off
130
 
void ledOn( int num, bool on )
131
 
{
132
 
        if( num < 0 || num > 9 ) return;
133
 
 
134
 
        // convert to pin no.
135
 
        num += 4;
136
 
 
137
 
        // pin 4 needs to be inverted (it's driving a PNP)
138
 
        if( num == 4 ) on = !on;
139
 
 
140
 
        digitalWrite( num, on? HIGH : LOW );
141
 
}
142
 
 
143
 
 
144
151
// draw a display segment
145
152
void drawNextSegment( bool reset )
146
153
{
147
 
        static ModeSwitcher mode_switcher;
148
 
        static bool init = false;
149
 
 
150
 
        if( !init ) {
151
 
                init = true;
152
 
                mode_switcher.activate();
153
 
        }
154
 
 
155
154
        // keep track of segment
156
155
#if CLOCK_FORWARD
157
156
        static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
162
161
#endif
163
162
 
164
163
        // draw
165
 
        Drawer &drawer = mode_switcher.get_drawer();
 
164
        Drawer &drawer = major_modes[ major_mode ]->get_drawer();
166
165
        if( reset ) drawer.draw_reset();
167
166
        drawer.draw( segment );
168
167
 
197
196
 
198
197
// wait until it is time to draw the next segment or a new pulse has
199
198
// occurred
200
 
void waitTillNextSegment( bool reset )
 
199
void waitTillEndOfSegment( bool reset )
201
200
{
202
201
        static unsigned long end_time = 0;
203
202
 
249
248
        // set up mode-switch button on pin 3
250
249
        pinMode( 3, INPUT );
251
250
        digitalWrite( 3, HIGH );
252
 
        button.add_event_at( 5, 1 );
253
 
        button.add_event_at( 1000, 2 );
254
 
        button.add_event_at( 4000, 3 );
 
251
        static int event_times[] = { 5, 1000, 4000, 0 };
 
252
        button.set_event_times( event_times );
255
253
 
256
 
        // serial comms
257
 
        Serial.begin( 9600 );
 
254
        // set up major modes
 
255
        static SwitcherMajorMode switcher_major_mode;
 
256
        int mode = 0;
 
257
        major_modes[ mode++ ] = &switcher_major_mode;
 
258
        major_modes[ 0 ]->activate();
258
259
}
259
260
 
260
261
 
264
265
        // if there has been a new pulse, we'll be resetting the display
265
266
        bool reset = new_pulse_at? true : false;
266
267
 
 
268
        // update button
 
269
        button.update();
 
270
 
267
271
        // only do this stuff at the start of a display cycle, to ensure
268
272
        // that no state changes mid-display
269
273
        if( reset )
270
274
        {
271
 
                // check buttons
272
 
                checkButtons();
 
275
                // calculate segment times
 
276
                calculateSegmentTimes();
273
277
 
274
278
                // keep track of time
275
279
                Time &time = Time::get_instance();
276
280
                time.update();
 
281
 
 
282
                // perform button events
 
283
                doButtonEvents();
277
284
        }
278
285
 
279
286
        // draw this segment
280
287
        drawNextSegment( reset );
281
288
 
282
 
        // do we need to recalculate segment times?
283
 
        if( reset )
284
 
                calculateSegmentTimes();
285
 
 
286
289
        // wait till it's time to draw the next segment
287
 
        waitTillNextSegment( reset );
 
290
        waitTillEndOfSegment( reset );
288
291
}