/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-23 00:26:32 UTC
  • Revision ID: edam@waxworlds.org-20120223002632-kkwrdwijfmv45f0j
conrtol segment number from one place and reverse the order the segments are drawn (backwards clock!)

Show diffs side-by-side

added added

removed removed

 
1
/* -*- mode: c++; compile-command: "BOARD=pro5v make"; -*- */
1
2
/*
2
3
 * propeller-clock.ino
3
4
 *
102
103
static bool inc_draw_mode = false;
103
104
 
104
105
// a bounce-managed button
105
 
static Bounce button( 3, 5 );
 
106
static Bounce button( 3, 50 );
106
107
 
107
108
// the time
108
109
static int time_hours = 0;
114
115
#define NUM_SECOND_SEGMENTS 5
115
116
#define NUM_SEGMENTS ( 60 * NUM_SECOND_SEGMENTS )
116
117
 
 
118
// clock direction
 
119
#define CLOCK_FORWARD 0
 
120
 
117
121
//_____________________________________________________________________________
118
122
//                                                                         code
119
123
 
160
164
}
161
165
 
162
166
 
 
167
// turn an led on/off
 
168
void ledOn( int num, bool on )
 
169
{
 
170
        if( num < 0 || num > 9 ) return;
 
171
 
 
172
        // convert to pin no.
 
173
        num += 4;
 
174
 
 
175
        // pin 4 needs to be inverted (it's driving a PNP)
 
176
        // NOTE: PIN 4 TEMPORARILY DISABLED
 
177
//      if( num == 4 ) on = true;
 
178
if( num == 4 ) on = !on;
 
179
 
 
180
        digitalWrite( num, on? HIGH : LOW );
 
181
}
 
182
 
 
183
 
163
184
// draw a segment for the test display
164
 
void drawNextSegment_test( bool reset )
 
185
void drawNextSegment_test( int segment )
165
186
{
166
 
        // keep track of segment
167
 
        static unsigned int segment = 0;
168
 
        if( reset ) segment = 0;
169
 
        segment++;
170
 
 
171
187
        // turn on inside and outside LEDs
172
 
        digitalWrite( 4, HIGH );
173
 
        digitalWrite( 13, HIGH );
 
188
        ledOn( 9, true );
174
189
 
175
190
        // display segment number in binary across in the inside LEDs,
176
191
        // with the LED on pin 12 showing the least-significant bit
177
 
        for( int a = 0; a < 8; a++ )
178
 
                digitalWrite( 12 - a, ( ( segment >> a ) & 1 )? HIGH : LOW );
 
192
        for( int a = 0; a < 9; a++ )
 
193
                ledOn( 8 - a, ( segment >> a ) & 1 );
179
194
}
180
195
 
181
196
 
182
197
// draw a segment for the time display
183
 
void drawNextSegment_time( bool reset )
 
198
void drawNextSegment_time( int segment )
184
199
{
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
 
        }
 
200
        int second = segment / NUM_SECOND_SEGMENTS;
 
201
        int second_segment = segment % NUM_SECOND_SEGMENTS;
193
202
 
194
203
        // what needs to be drawn?
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;
 
204
        bool draw_tick = !second_segment && second % 5 == 0;
 
205
        bool draw_second = !second_segment && second == time_seconds;
 
206
        bool draw_minute = !second_segment && second == time_minutes;
 
207
        bool draw_hour = !second_segment && second == time_hours;
199
208
 
200
209
        // set the LEDs
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
 
        }
 
210
        ledOn( 9, true );
 
211
        ledOn( 8, draw_tick || draw_minute );
 
212
        for( int a = 6; a <= 7; a++ )
 
213
                ledOn( a, draw_minute || draw_second );
 
214
        for( int a = 0; a <= 5; a++ )
 
215
                ledOn( a, draw_minute || draw_second || draw_hour );
213
216
}
214
217
 
215
218
 
218
221
{
219
222
        static int draw_mode = 0;
220
223
 
 
224
        // keep track of segment
 
225
#if CLOCK_FORWARD
 
226
        static int segment = 0;
 
227
        if( reset ) segment = 0;
 
228
#else
 
229
        static int segment = NUM_SEGMENTS - 1;
 
230
        if( reset ) segment = NUM_SEGMENTS - 1;
 
231
#endif
 
232
 
221
233
        // handle mode switch requests
222
234
        if( reset && inc_draw_mode ) {
223
235
                inc_draw_mode = false;
228
240
 
229
241
        // draw the segment
230
242
        switch( draw_mode ) {
231
 
        case 0: drawNextSegment_test( reset ); break;
232
 
        case 1: drawNextSegment_time( reset ); break;
 
243
        case 0: drawNextSegment_test( segment ); break;
 
244
        case 1: drawNextSegment_time( segment ); break;
233
245
        }
 
246
 
 
247
#if CLOCK_FORWARD
 
248
        segment++;
 
249
#else
 
250
        segment--;
 
251
#endif
234
252
}
235
253
 
236
254
 
308
326
 
309
327
        // set up mode-switch button on pin 3
310
328
        pinMode( 3, INPUT );
 
329
        digitalWrite( 3, HIGH );
311
330
 
312
331
        // get the time from the real-time clock
313
332
        int rtc_data[ 7 ];