/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
 
 
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
#include "Arduino.h"
84
85
 
85
86
//_____________________________________________________________________________
86
87
//                                                                         data
101
102
static unsigned long segment_step_sub_step = 0;
102
103
static unsigned long segment_step_sub = 0;
103
104
 
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
 
105
// the button
108
106
static Button button( 3 );
109
107
 
 
108
// major mode
 
109
static int major_mode = 0;
 
110
 
 
111
#define MAX_MAJOR_MODES 5
 
112
 
 
113
// major modes
 
114
static MajorMode *major_modes[ MAX_MAJOR_MODES ] = { 0 };
 
115
 
110
116
//_____________________________________________________________________________
111
117
//                                                                         code
112
118
 
113
119
 
114
 
// check for button presses
115
 
void checkButtons()
 
120
// perform button events
 
121
void doButtonEvents()
116
122
{
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;
 
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
                }
125
148
        }
126
149
}
127
150
 
128
151
 
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
152
// draw a display segment
145
153
void drawNextSegment( bool reset )
146
154
{
147
 
        static ModeSwitcher mode_switcher;
148
 
        static bool init = false;
149
 
 
150
 
        if( !init ) {
151
 
                init = true;
152
 
                mode_switcher.activate();
153
 
        }
154
 
 
155
155
        // keep track of segment
156
156
#if CLOCK_FORWARD
157
157
        static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
162
162
#endif
163
163
 
164
164
        // draw
165
 
        Drawer &drawer = mode_switcher.get_drawer();
 
165
        Drawer &drawer = major_modes[ major_mode ]->get_drawer();
166
166
        if( reset ) drawer.draw_reset();
167
167
        drawer.draw( segment );
168
168
 
197
197
 
198
198
// wait until it is time to draw the next segment or a new pulse has
199
199
// occurred
200
 
void waitTillNextSegment( bool reset )
 
200
void waitTillEndOfSegment( bool reset )
201
201
{
202
202
        static unsigned long end_time = 0;
203
203
 
249
249
        // set up mode-switch button on pin 3
250
250
        pinMode( 3, INPUT );
251
251
        digitalWrite( 3, HIGH );
252
 
        button.add_event_at( 5, 1 );
253
 
        button.add_event_at( 1000, 2 );
254
 
        button.add_event_at( 4000, 3 );
 
252
        static int event_times[] = { 5, 500, 4000, 0 };
 
253
        button.set_event_times( event_times );
255
254
 
256
 
        // serial comms
257
 
        Serial.begin( 9600 );
 
255
        // set up major modes
 
256
        static SwitcherMajorMode switcher_major_mode;
 
257
        int mode = 0;
 
258
        major_modes[ mode++ ] = &switcher_major_mode;
 
259
        major_modes[ 0 ]->activate();
258
260
}
259
261
 
260
262
 
264
266
        // if there has been a new pulse, we'll be resetting the display
265
267
        bool reset = new_pulse_at? true : false;
266
268
 
 
269
        // update button
 
270
        button.update();
 
271
 
267
272
        // only do this stuff at the start of a display cycle, to ensure
268
273
        // that no state changes mid-display
269
274
        if( reset )
270
275
        {
271
 
                // check buttons
272
 
                checkButtons();
 
276
                // calculate segment times
 
277
                calculateSegmentTimes();
273
278
 
274
279
                // keep track of time
275
280
                Time &time = Time::get_instance();
276
281
                time.update();
 
282
 
 
283
                // perform button events
 
284
                doButtonEvents();
277
285
        }
278
286
 
279
287
        // draw this segment
280
288
        drawNextSegment( reset );
281
289
 
282
 
        // do we need to recalculate segment times?
283
 
        if( reset )
284
 
                calculateSegmentTimes();
285
 
 
286
290
        // wait till it's time to draw the next segment
287
 
        waitTillNextSegment( reset );
 
291
        waitTillEndOfSegment( reset );
288
292
}