/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 01:31:17 UTC
  • Revision ID: tim@ed.am-20120225013117-53ed8yahoreoms76
updated software to include drawing abstraction infrastructure

Show diffs side-by-side

added added

removed removed

76
76
******************************************************************************/
77
77
 
78
78
 
79
 
#include <Bounce.h>
80
 
#include <DS1307.h>
81
 
#include <Wire.h>
 
79
#include <button.h>
 
80
#include "config.h"
 
81
#include "time.h"
 
82
#include "mode_switcher.h"
 
83
#include "drawer.h"
82
84
 
83
85
//_____________________________________________________________________________
84
86
//                                                                         data
103
105
static bool inc_draw_mode = false;
104
106
 
105
107
// 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 )
 
108
static Button button( 3 );
123
109
 
124
110
//_____________________________________________________________________________
125
111
//                                                                         code
129
115
void checkButtons()
130
116
{
131
117
        // update buttons
132
 
        button.update();
 
118
        int event = button.update();
133
119
 
134
 
        // notice button presses
135
 
        if( button.risingEdge() )
 
120
        // handle any events
 
121
        switch( event ) {
 
122
        case 1:
136
123
                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
 
                }
 
124
                break;
166
125
        }
167
126
}
168
127
 
182
141
}
183
142
 
184
143
 
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
144
// draw a display segment
224
145
void drawNextSegment( bool reset )
225
146
{
226
 
        static int draw_mode = 0;
 
147
        static ModeSwitcher mode_switcher;
 
148
        static bool init = false;
 
149
 
 
150
        if( !init ) {
 
151
                init = true;
 
152
                mode_switcher.activate();
 
153
        }
227
154
 
228
155
        // keep track of segment
229
156
#if CLOCK_FORWARD
234
161
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
235
162
#endif
236
163
 
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
 
        }
 
164
        // draw
 
165
        Drawer &drawer = mode_switcher.get_drawer();
 
166
        if( reset ) drawer.draw_reset();
 
167
        drawer.draw( segment );
250
168
 
251
169
#if CLOCK_FORWARD
252
170
        if( ++segment >= NUM_SEGMENTS ) segment = 0;
331
249
        // set up mode-switch button on pin 3
332
250
        pinMode( 3, INPUT );
333
251
        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 ];
 
252
        button.add_event_at( 5, 1 );
 
253
        button.add_event_at( 1000, 2 );
 
254
        button.add_event_at( 4000, 3 );
341
255
 
342
256
        // serial comms
343
257
        Serial.begin( 9600 );
358
272
                checkButtons();
359
273
 
360
274
                // keep track of time
361
 
                trackTime();
 
275
                Time &time = Time::get_instance();
 
276
                time.update();
362
277
        }
363
278
 
364
279
        // draw this segment