/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.pde

  • Committer: edam
  • Date: 2011-12-22 01:23:27 UTC
  • Revision ID: edam@waxworlds.org-20111222012327-orbuaeekm9y9ni39
renamed notes

Show diffs side-by-side

added added

removed removed

1
 
/* -*- mode: c++; compile-command: "BOARD=pro5v make"; -*- */
2
1
/*
3
 
 * propeller-clock.ino
 
2
 * propeller-clock.pde
4
3
 *
5
 
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
 
4
 * Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
6
5
 *
7
6
 * This file is part of propeller-clock (hereafter referred to as "this
8
 
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
 
7
 * program"). See http://ed.am/software/arduino/propeller-clock for more
9
8
 * information.
10
9
 *
11
10
 * This program is free software: you can redistribute it and/or modify
36
35
   13 is at the outside.
37
36
 
38
37
 * if a longer hand (and a larger clock face) is desired, pin 4 can be
39
 
   used to indirectly drive a transistor which in turn drives several
40
 
   LEDs that turn on anf off in unison in the centre of the clock.
 
38
   used to indirectly drive (via a MOSFET) multiple LEDs which turn on
 
39
   and off in unison in the centre of the clock.
41
40
 
42
41
 * a button should be attached to pin 3 that grounds it when pressed.
43
42
 
44
 
 * A DS1307 remote clock is connected via I2C on analog pins 4 and 5.
45
 
 
46
43
Implementation details:
47
44
 
48
 
 * for a schematic, see ../project/propeller-clock.sch.
 
45
 * for a schematic, see project/propeller-clock.sch.
49
46
 
50
47
 * the timing of the drawing of the clock face is recalculated with
51
48
   every rotation of the propeller.
70
67
    - pressing the button increments the field currently being set
71
68
    - pressing and holding the button for a second cycles through the
72
69
      fields that can be set
73
 
    - pressing and holding the button for 5 seconds sets the time and
74
 
      exits "time set" mode
 
70
    - press and holding the button for 5 seconds to finish
75
71
 
76
72
******************************************************************************/
77
73
 
78
74
 
79
75
#include <Bounce.h>
80
 
#include <DS1307.h>
81
 
#include <Wire.h>
82
76
 
83
77
//_____________________________________________________________________________
84
78
//                                                                         data
103
97
static bool inc_draw_mode = false;
104
98
 
105
99
// a bounce-managed button
106
 
static Bounce button( 3, 50 );
 
100
static Bounce button( 3, 5 );
107
101
 
108
102
// the time
109
103
static int time_hours = 0;
161
155
}
162
156
 
163
157
 
164
 
// turn an led on/off
165
 
void ledOn( int num, bool on )
166
 
{
167
 
        if( num < 0 || num > 9 ) return;
168
 
 
169
 
        // convert to pin no.
170
 
        num += 4;
171
 
 
172
 
        // pin 4 needs to be inverted (it's driving a PNP)
173
 
        // NOTE: PIN 4 TEMPORARILY DISABLED
174
 
        if( num == 4 ) on = true; //!on
175
 
 
176
 
        digitalWrite( num, on? HIGH : LOW );
177
 
}
178
 
 
179
 
 
180
158
// draw a segment for the test display
181
159
void drawNextSegment_test( bool reset )
182
160
{
186
164
        segment++;
187
165
 
188
166
        // turn on inside and outside LEDs
189
 
        ledOn( 0, true );
190
 
        ledOn( 9, true );
 
167
        digitalWrite( 4, HIGH );
 
168
        digitalWrite( 13, HIGH );
191
169
 
192
170
        // display segment number in binary across in the inside LEDs,
193
171
        // with the LED on pin 12 showing the least-significant bit
194
172
        for( int a = 0; a < 8; a++ )
195
 
                ledOn( 8 - a, ( segment >> a ) & 1 );
 
173
                digitalWrite( 12 - a, ( ( segment >> a ) & 1 )? HIGH : LOW );
196
174
}
197
175
 
198
176
 
199
177
// draw a segment for the time display
200
178
void drawNextSegment_time( bool reset )
201
179
{
202
 
        static int second = 0;
203
 
        static int segment = 0;
 
180
        static unsigned int second = 0;
 
181
        static unsigned int segment = 0;
204
182
 
205
183
        // handle display reset
206
184
        if( reset ) {
211
189
        // what needs to be drawn?
212
190
        bool draw_tick = !segment && second % 5 == 0;
213
191
        bool draw_second = !segment && second == time_seconds;
214
 
        bool draw_minute = !segment && second == time_minutes;
215
 
        bool draw_hour = !segment && second == time_hours;
 
192
        bool draw_minute = !segment && second == time_minute;
 
193
        bool draw_hour = !segment && second == time_hour;
216
194
 
217
195
        // set the LEDs
218
 
        ledOn( 9, true );
219
 
        ledOn( 8, draw_tick || draw_minute );
220
 
        for( int a = 6; a <= 7; a++ )
221
 
                ledOn( a, draw_minute || draw_second );
222
 
        for( int a = 0; a <= 5; a++ )
223
 
                ledOn( a, draw_minute || draw_second || draw_hour );
 
196
        digitalWrite( 13, HIGH );
 
197
        digitalWrite( 12, draw_tick || draw_minute );
 
198
        for( int a = 10; a <= 11; a++ )
 
199
                digitalWrite( a, draw_minute || draw_second );
 
200
        for( int a = 4; a <= 9; a++ )
 
201
                digitalWrite( 10, draw_minute | draw_second || draw_hour );
224
202
 
225
203
        // inc position
226
204
        if( ++segment >= NUM_SECOND_SEGMENTS ) {
325
303
 
326
304
        // set up mode-switch button on pin 3
327
305
        pinMode( 3, INPUT );
328
 
        digitalWrite( 3, HIGH );
329
 
 
330
 
        // get the time from the real-time clock
331
 
        int rtc_data[ 7 ];
332
 
        RTC.get( rtc_data, true );
333
 
        time_hours = rtc_data[ DS1307_HR ];
334
 
        time_minutes = rtc_data[ DS1307_MIN ];
335
 
        time_seconds = rtc_data[ DS1307_SEC ];
336
306
 
337
307
        // serial comms
338
308
        Serial.begin( 9600 );