/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:29:52 UTC
  • Revision ID: tim@ed.am-20120225012952-32q8gg07aovk3qxh
updated arduino.mk

Show diffs side-by-side

added added

removed removed

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