/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-01-18 23:22:57 UTC
  • Revision ID: edam@waxworlds.org-20120118232257-a3323t3u7nd84zbd
updated led test and added real-time clock to the schematic

Show diffs side-by-side

added added

removed removed

1
 
/* -*- mode: c++; compile-command: "BOARD=pro5v make"; -*- */
2
1
/*
3
2
 * propeller-clock.ino
4
3
 *
103
102
static bool inc_draw_mode = false;
104
103
 
105
104
// a bounce-managed button
106
 
static Bounce button( 3, 50 );
 
105
static Bounce button( 3, 5 );
107
106
 
108
107
// the time
109
108
static int time_hours = 0;
115
114
#define NUM_SECOND_SEGMENTS 5
116
115
#define NUM_SEGMENTS ( 60 * NUM_SECOND_SEGMENTS )
117
116
 
118
 
// clock draw direction
119
 
#define CLOCK_FORWARD 0
120
 
 
121
 
// rotate display (in segments)
122
 
#define CLOCK_SHIFT ( 58 * NUM_SECOND_SEGMENTS - 1 )
123
 
 
124
117
//_____________________________________________________________________________
125
118
//                                                                         code
126
119
 
167
160
}
168
161
 
169
162
 
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
163
// draw a segment for the test display
186
 
void drawNextSegment_test( int segment )
 
164
void drawNextSegment_test( bool reset )
187
165
{
188
 
        // turn on outside LEDs
189
 
        ledOn( 9, true );
 
166
        // keep track of segment
 
167
        static unsigned int segment = 0;
 
168
        if( reset ) segment = 0;
 
169
        segment++;
 
170
 
 
171
        // turn on inside and outside LEDs
 
172
        digitalWrite( 4, HIGH );
 
173
        digitalWrite( 13, HIGH );
190
174
 
191
175
        // display segment number in binary across in the inside LEDs,
192
176
        // 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 );
 
177
        for( int a = 0; a < 8; a++ )
 
178
                digitalWrite( 12 - a, ( ( segment >> a ) & 1 )? HIGH : LOW );
195
179
}
196
180
 
197
181
 
198
182
// draw a segment for the time display
199
 
void drawNextSegment_time( int segment )
 
183
void drawNextSegment_time( bool reset )
200
184
{
201
 
        int second = segment / NUM_SECOND_SEGMENTS;
202
 
        int second_segment = segment % NUM_SECOND_SEGMENTS;
 
185
        static unsigned int second = 0;
 
186
        static unsigned int segment = 0;
 
187
 
 
188
        // handle display reset
 
189
        if( reset ) {
 
190
                second = 0;
 
191
                segment = 0;
 
192
        }
203
193
 
204
194
        // 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 );
 
195
        bool draw_tick = !segment && second % 5 == 0;
 
196
        bool draw_second = !segment && second == time_seconds;
 
197
        bool draw_minute = !segment && second == time_minutes;
 
198
        bool draw_hour = !segment && second == time_hours;
212
199
 
213
200
        // 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 );
 
201
        digitalWrite( 13, HIGH );
 
202
        digitalWrite( 12, draw_tick || draw_minute );
 
203
        for( int a = 10; a <= 11; a++ )
 
204
                digitalWrite( a, draw_minute || draw_second );
 
205
        for( int a = 4; a <= 9; a++ )
 
206
                digitalWrite( 10, draw_minute | draw_second || draw_hour );
 
207
 
 
208
        // inc position
 
209
        if( ++segment >= NUM_SECOND_SEGMENTS ) {
 
210
                segment = 0;
 
211
                second++;
 
212
        }
220
213
}
221
214
 
222
215
 
225
218
{
226
219
        static int draw_mode = 0;
227
220
 
228
 
        // keep track of segment
229
 
#if CLOCK_FORWARD
230
 
        static int segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
231
 
        if( reset ) segment = ( NUM_SEGMENTS - CLOCK_SHIFT ) % NUM_SEGMENTS;
232
 
#else
233
 
        static int segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
234
 
        if( reset ) segment = NUM_SEGMENTS - 1 - CLOCK_SHIFT;
235
 
#endif
236
 
 
237
221
        // handle mode switch requests
238
222
        if( reset && inc_draw_mode ) {
239
223
                inc_draw_mode = false;
244
228
 
245
229
        // draw the segment
246
230
        switch( draw_mode ) {
247
 
        case 0: drawNextSegment_test( segment ); break;
248
 
        case 1: drawNextSegment_time( segment ); break;
 
231
        case 0: drawNextSegment_test( reset ); break;
 
232
        case 1: drawNextSegment_time( reset ); break;
249
233
        }
250
 
 
251
 
#if CLOCK_FORWARD
252
 
        if( ++segment >= NUM_SEGMENTS ) segment = 0;
253
 
#else
254
 
        if( --segment < 0 ) segment = NUM_SEGMENTS - 1;
255
 
#endif
256
234
}
257
235
 
258
236
 
330
308
 
331
309
        // set up mode-switch button on pin 3
332
310
        pinMode( 3, INPUT );
333
 
        digitalWrite( 3, HIGH );
334
311
 
335
312
        // get the time from the real-time clock
336
313
        int rtc_data[ 7 ];