/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/modes/analogue_clock_mode.cc

  • Committer: Tim Marston
  • Date: 2013-03-31 17:07:36 UTC
  • Revision ID: tim@ed.am-20130331170736-hphm2hg0y6l7w6z1
made rtc-test's DS1307 library a symlink to the main one in src/util

Show diffs side-by-side

added added

removed removed

 
1
/*
 
2
 * analogue_clock_mode.cc
 
3
 *
 
4
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
 
5
 *
 
6
 * This file is part of propeller-clock (hereafter referred to as "this
 
7
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
 
8
 * information.
 
9
 *
 
10
 * This program is free software: you can redistribute it and/or modify
 
11
 * it under the terms of the GNU Lesser General Public License as published
 
12
 * by the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU Lesser General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
 */
1
23
#include "analogue_clock_mode.h"
2
24
#include "config.h"
 
25
#include "Arduino.h"
3
26
#include "time.h"
4
 
 
5
 
 
6
 
AnalogueClockMode::AnalogueClockMode()
7
 
    :
8
 
    MinorMode( 1 )
9
 
{
10
 
}
 
27
#include "common.h"
 
28
#include "text.h"
11
29
 
12
30
 
13
31
void AnalogueClockMode::draw( int segment )
14
32
{
15
 
        Time &time = Time::get_instance();
16
 
 
17
 
        int second = segment / NUM_SECOND_SEGMENTS;
18
 
        int second_segment = segment % NUM_SECOND_SEGMENTS;
19
 
 
20
 
        // what needs to be drawn?
21
 
        bool draw_tick = ( !second_segment && second % 5 == 0 && second ) ||
22
 
                ( second == 0 && second_segment == 1 ) ||
23
 
                ( second == 59 && second_segment == NUM_SECOND_SEGMENTS - 1 );
24
 
        bool draw_second = !second_segment && second == time.get_seconds();
25
 
        bool draw_minute = !second_segment && second == time.get_minutes();
26
 
        bool draw_hour = segment == time.get_hours() * 5 * NUM_SECOND_SEGMENTS +
27
 
                ( 5 * NUM_SECOND_SEGMENTS * time.get_minutes() / 60 );
 
33
        bool draw_tick = segment && ( segment % ( NUM_SECOND_SEGMENTS * 5 ) == 0 ||
 
34
                segment == 1 || segment == NUM_SEGMENTS - 1 );
 
35
        bool draw_hour = segment == _hour_segment;
 
36
        bool draw_hour_side = 2 >=
 
37
                ( abs( _hour_segment - segment ) + NUM_SEGMENTS ) % NUM_SEGMENTS;
 
38
        bool draw_minute = segment == _minute_segment;
 
39
        bool draw_minute_side = 1 >=
 
40
                ( abs( _minute_segment - segment ) + NUM_SEGMENTS ) % NUM_SEGMENTS;
 
41
 
 
42
        bool draw_second = segment == _second_segment;
28
43
 
29
44
        // set the LEDs
30
 
        led_on( 9, true );
31
 
        led_on( 8, draw_tick || draw_second );
32
 
        for( int a = 6; a <= 7; a++ )
33
 
                led_on( a, draw_minute || draw_second );
34
 
        for( int a = 0; a <= 5; a++ )
35
 
                led_on( a, draw_minute || draw_second || draw_hour );
 
45
        switch( _flavour )
 
46
        {
 
47
        case 0:
 
48
                led( 9, true );
 
49
                led( 8, draw_tick || draw_second );
 
50
                led( 7, draw_minute || draw_second );
 
51
                led( 6, draw_minute || draw_minute_side || draw_second );
 
52
                led( 5, draw_minute || draw_minute_side ||
 
53
                                          draw_second || draw_hour );
 
54
                for( int a = 0; a <= 4; a++ )
 
55
                        led( a, draw_minute || draw_minute_side ||
 
56
                                                  draw_second || draw_hour || draw_hour_side );
 
57
                break;
 
58
        case 1:
 
59
                led( 9, draw_tick );
 
60
                led( 8, draw_second );
 
61
                led( 7, draw_minute || draw_second );
 
62
                led( 6, draw_minute || draw_minute_side || draw_second );
 
63
                led( 5, draw_minute || draw_minute_side ||
 
64
                                          draw_second || draw_hour );
 
65
                for( int a = 0; a <= 4; a++ )
 
66
                        led( a, draw_minute || draw_minute_side ||
 
67
                                                  draw_second || draw_hour || draw_hour_side );
 
68
                break;
 
69
        case 2:
 
70
                led( 9, segment <= _subsecond_segment );
 
71
                led( 8, draw_tick || draw_second );
 
72
                led( 7, draw_minute || draw_second );
 
73
                led( 6, draw_minute || draw_minute_side || draw_second );
 
74
                led( 5, draw_minute || draw_minute_side ||
 
75
                                          draw_second || draw_hour );
 
76
                for( int a = 0; a <= 4; a++ )
 
77
                        led( a, draw_minute || draw_minute_side ||
 
78
                                                  draw_second || draw_hour || draw_hour_side );
 
79
                break;
 
80
        case 3:
 
81
                led( 9, segment <= ( _second_segment +
 
82
                                NUM_SECOND_SEGMENTS * _subsecond_segment / NUM_SEGMENTS ) );
 
83
                led( 8, draw_tick );
 
84
                led( 7, draw_minute );
 
85
                led( 6, draw_minute || draw_minute_side );
 
86
                led( 5, draw_minute || draw_minute_side || draw_hour );
 
87
                for( int a = 0; a <= 4; a++ )
 
88
                        led( a, draw_minute || draw_minute_side ||
 
89
                                        draw_hour || draw_hour_side );
 
90
                break;
 
91
        }
 
92
}
 
93
 
 
94
 
 
95
void AnalogueClockMode::draw_reset()
 
96
{
 
97
        int old_second_segment = _second_segment;
 
98
 
 
99
        // calculate hand locations
 
100
        _hour_segment = ( Time::get_hours() % 12 ) * 5 * NUM_SECOND_SEGMENTS +
 
101
                ( 5 * NUM_SECOND_SEGMENTS * Time::get_minutes() / 60 );
 
102
        _minute_segment = Time::get_minutes() * NUM_SECOND_SEGMENTS +
 
103
                ( NUM_SECOND_SEGMENTS * Time::get_seconds() / 60 );
 
104
        _second_segment = Time::get_seconds() * NUM_SECOND_SEGMENTS;
 
105
 
 
106
        // calculate the subsecond segment
 
107
        if( old_second_segment != _second_segment ) {
 
108
                _second_millis = millis();
 
109
                _subsecond_segment = 0;
 
110
        }
 
111
        else {
 
112
                unsigned long elapsed = ::millis() - _second_millis;
 
113
                _subsecond_segment = elapsed * NUM_SEGMENTS / 1000;
 
114
        }
 
115
}
 
116
 
 
117
 
 
118
void AnalogueClockMode::activate()
 
119
{
 
120
        _flavour = 0;
 
121
        _second_millis = ::millis();
 
122
 
 
123
        Text::reset();
 
124
}
 
125
 
 
126
 
 
127
void AnalogueClockMode::press()
 
128
{
 
129
        if( ++_flavour >= 4 )
 
130
                _flavour = 0;
36
131
}