/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
 
 
79
 
#include <Bounce.h>
80
 
#include <DS1307.h>
81
 
#include <Wire.h>
 
78
#include "button.h"
 
79
#include "config.h"
 
80
#include "time.h"
 
81
#include "mode_switcher.h"
 
82
#include "drawer.h"
82
83
 
83
84
//_____________________________________________________________________________
84
85
//                                                                         data
99
100
static unsigned long segment_step_sub_step = 0;
100
101
static unsigned long segment_step_sub = 0;
101
102
 
102
 
// flag to indicate that the drawing mode should be cycled to the next one
103
 
static bool inc_draw_mode = false;
104
 
 
105
 
// a bounce-managed button
106
 
static Bounce button( 3, 50 );
107
 
 
108
 
// the time
109
 
static int time_hours = 0;
110
 
static int time_minutes = 0;
111
 
static int time_seconds = 0;
112
 
 
113
 
// number of segments in a full display (rotation) is 60 (one per
114
 
// second) times the desired number of sub-divisions of a second
115
 
#define NUM_SECOND_SEGMENTS 5
116
 
#define NUM_SEGMENTS ( 60 * NUM_SECOND_SEGMENTS )
117
 
 
118
 
// clock draw direction
119
 
#define CLOCK_FORWARD 0
120
 
 
121
 
// rotate display (in segments)
122
 
#define CLOCK_SHIFT ( 58 * NUM_SECOND_SEGMENTS - 1 )
 
103
// the button
 
104
static Button button( 3 );
 
105
 
 
106
// major mode
 
107
static int major_mode = 0;
 
108
 
 
109
// major modes
 
110
static std::vector< MajorMode * > major_modes;
123
111
 
124
112
//_____________________________________________________________________________
125
113
//                                                                         code
129
117
void checkButtons()
130
118
{
131
119
        // update buttons
132
 
        button.update();
133
 
 
134
 
        // notice button presses
135
 
        if( button.risingEdge() )
136
 
                inc_draw_mode = true;
137
 
}
138
 
 
139
 
 
140
 
// keep track of time
141
 
void trackTime()
142
 
{
143
 
        // previous time and any carried-over milliseconds
144
 
        static unsigned long last_time = millis();
145
 
        static unsigned long carry = 0;
146
 
 
147
 
        // how many milliseonds have elapsed since we last checked?
148
 
        unsigned long next_time = millis();
149
 
        unsigned long delta = next_time - last_time + carry;
150
 
 
151
 
        // update the previous time and carried-over milliseconds
152
 
        last_time = next_time;
153
 
        carry = delta % 1000;
154
 
 
155
 
        // add the seconds that have passed to the time
156
 
        time_seconds += delta / 1000;
157
 
        while( time_seconds >= 60 ) {
158
 
                time_seconds -= 60;
159
 
                time_minutes++;
160
 
                if( time_minutes >= 60 ) {
161
 
                        time_minutes -= 60;
162
 
                        time_hours++;
163
 
                        if( time_hours >= 24 )
164
 
                                time_hours -= 24;
165
 
                }
 
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;
166
135
        }
167
136
}
168
137
 
169
138
 
170
 
// turn an led on/off
171
 
void ledOn( int num, bool on )
172
 
{
173
 
        if( num < 0 || num > 9 ) return;
174
 
 
175
 
        // convert to pin no.
176
 
        num += 4;
177
 
 
178
 
        // pin 4 needs to be inverted (it's driving a PNP)
179
 
        if( num == 4 ) on = !on;
180
 
 
181
 
        digitalWrite( num, on? HIGH : LOW );
182
 
}
183
 
 
184
 
 
185
 
// draw a segment for the test display
186
 
void drawNextSegment_test( int segment )
187
 
{
188
 
        // turn on outside LEDs
189
 
        ledOn( 9, true );
190
 
 
191
 
        // display segment number in binary across in the inside LEDs,
192
 
        // with the LED on pin 12 showing the least-significant bit
193
 
        for( int a = 0; a < 9; a++ )
194
 
                ledOn( 8 - a, ( segment >> a ) & 1 );
195
 
}
196
 
 
197
 
 
198
 
// draw a segment for the time display
199
 
void drawNextSegment_time( int segment )
200
 
{
201
 
        int second = segment / NUM_SECOND_SEGMENTS;
202
 
        int second_segment = segment % NUM_SECOND_SEGMENTS;
203
 
 
204
 
        // what needs to be drawn?
205
 
        bool draw_tick = ( !second_segment && second % 5 == 0 && second ) ||
206
 
                ( second == 0 && second_segment == 1 ) ||
207
 
                ( second == 59 && second_segment == NUM_SECOND_SEGMENTS - 1 );
208
 
        bool draw_second = !second_segment && second == time_seconds;
209
 
        bool draw_minute = !second_segment && second == time_minutes;
210
 
        bool draw_hour = segment == time_hours * 5 * NUM_SECOND_SEGMENTS +
211
 
                ( 5 * NUM_SECOND_SEGMENTS * time_minutes / 60 );
212
 
 
213
 
        // set the LEDs
214
 
        ledOn( 9, true );
215
 
        ledOn( 8, draw_tick || draw_second );
216
 
        for( int a = 6; a <= 7; a++ )
217
 
                ledOn( a, draw_minute || draw_second );
218
 
        for( int a = 0; a <= 5; a++ )
219
 
                ledOn( a, draw_minute || draw_second || draw_hour );
220
 
}
221
 
 
222
 
 
223
139
// draw a display segment
224
140
void drawNextSegment( bool reset )
225
141
{
226
 
        static int draw_mode = 0;
227
 
 
228
142
        // keep track of segment
229
143
#if CLOCK_FORWARD
230
144
        static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
234
148
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
235
149
#endif
236
150
 
237
 
        // handle mode switch requests
238
 
        if( reset && inc_draw_mode ) {
239
 
                inc_draw_mode = false;
240
 
                draw_mode++;
241
 
                if( draw_mode >= 2 )
242
 
                        draw_mode = 0;
243
 
        }
244
 
 
245
 
        // draw the segment
246
 
        switch( draw_mode ) {
247
 
        case 0: drawNextSegment_test( segment ); break;
248
 
        case 1: drawNextSegment_time( segment ); break;
249
 
        }
 
151
        // draw
 
152
        Drawer &drawer = major_modes[ major_mode ]->get_drawer();
 
153
        if( reset ) drawer.draw_reset();
 
154
        drawer.draw( segment );
250
155
 
251
156
#if CLOCK_FORWARD
252
157
        if( ++segment >= NUM_SEGMENTS ) segment = 0;
331
236
        // set up mode-switch button on pin 3
332
237
        pinMode( 3, INPUT );
333
238
        digitalWrite( 3, HIGH );
334
 
 
335
 
        // get the time from the real-time clock
336
 
        int rtc_data[ 7 ];
337
 
        RTC.get( rtc_data, true );
338
 
        time_hours = rtc_data[ DS1307_HR ];
339
 
        time_minutes = rtc_data[ DS1307_MIN ];
340
 
        time_seconds = rtc_data[ DS1307_SEC ];
 
239
        button.add_event_at( 5, 1 );
 
240
        button.add_event_at( 1000, 2 );
 
241
        button.add_event_at( 4000, 3 );
341
242
 
342
243
        // serial comms
343
244
        Serial.begin( 9600 );
 
245
 
 
246
        // set up major modes
 
247
        static ModeSwitcher mode_switcher;
 
248
        major_modes.push_back( &mode_switcher );
 
249
        major_modes[ 0 ]->activate();
344
250
}
345
251
 
346
252
 
358
264
                checkButtons();
359
265
 
360
266
                // keep track of time
361
 
                trackTime();
 
267
                Time &time = Time::get_instance();
 
268
                time.update();
362
269
        }
363
270
 
364
271
        // draw this segment