/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/text.cc

  • Committer: Dan
  • Date: 2011-11-18 15:43:57 UTC
  • Revision ID: dan@waxworlds.org-20111118154357-ro53w0hsow466tkf
fixed display wdth for hands on clock face

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * text.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
 
 */
23
 
#include "text.h"
24
 
#include "text_renderer.h"
25
 
#include "config.h"
26
 
#include "Arduino.h"
27
 
#include <avr/pgmspace.h>
28
 
#include "display.h"
29
 
 
30
 
 
31
 
// the buffer for the message
32
 
char Text::_messages[ NUM_MESSAGE_BUFFERS ][ MESSAGE_BUFFER_LEN ];
33
 
 
34
 
// message buffer lengths
35
 
static int _message_lens[ NUM_MESSAGE_BUFFERS ];
36
 
 
37
 
// the mode that each message represents
38
 
static char _message_modes[ NUM_MESSAGE_BUFFERS ];
39
 
 
40
 
// mode-specific message parameters
41
 
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];
42
 
 
43
 
// time last frame
44
 
static unsigned long _last_millis;
45
 
 
46
 
// duration last frame (in case we can't calculate it this frame!)
47
 
static unsigned long _duration;
48
 
 
49
 
// recalculate message params, based on _duration
50
 
static bool _do_draw_reset;
51
 
 
52
 
 
53
 
void reset_message_param( int message_num )
54
 
{
55
 
        if( _message_modes[ message_num ] & Text::MODE_HSCROLL )
56
 
        {
57
 
                switch( _message_modes[ message_num ] & 0x1c )
58
 
                {
59
 
                case Text::MODE_ONEQUARTER:
60
 
                        _message_params[ message_num ] = -NUM_SEGMENTS / 4; break;
61
 
                case Text::MODE_ONETHIRD:
62
 
                        _message_params[ message_num ] = -NUM_SEGMENTS / 3; break;
63
 
                case Text::MODE_HALF:
64
 
                        _message_params[ message_num ] = -NUM_SEGMENTS / 2; break;
65
 
                case Text::MODE_TWOTHIRDS:
66
 
                        _message_params[ message_num ] = -NUM_SEGMENTS * 2 / 3; break;
67
 
                case Text::MODE_THREEQUARTERS:
68
 
                        _message_params[ message_num ] = -NUM_SEGMENTS * 3 / 4; break;
69
 
                case Text::MODE_ALL:
70
 
                        _message_params[ message_num ] = -NUM_SEGMENTS; break;
71
 
                }
72
 
        }
73
 
        else
74
 
        {
75
 
                switch( _message_modes[ message_num ] & 0x1c )
76
 
                {
77
 
                case Text::MODE_ONEQUARTER:
78
 
                        _message_params[ message_num ] = NUM_SEGMENTS / 4; break;
79
 
                case Text::MODE_ONETHIRD:
80
 
                        _message_params[ message_num ] = NUM_SEGMENTS / 3; break;
81
 
                case Text::MODE_HALF:
82
 
                        _message_params[ message_num ] = NUM_SEGMENTS / 2; break;
83
 
                case Text::MODE_TWOTHIRDS:
84
 
                        _message_params[ message_num ] = NUM_SEGMENTS * 2 / 3; break;
85
 
                case Text::MODE_THREEQUARTERS:
86
 
                        _message_params[ message_num ] = NUM_SEGMENTS * 3 / 4; break;
87
 
                case Text::MODE_ALL:
88
 
                        _message_params[ message_num ] = NUM_SEGMENTS; break;
89
 
                }
90
 
                _message_params[ message_num ] = -( _message_params[ message_num ] -
91
 
                        TextRenderer::get_width( _message_lens[ message_num ] ) ) / 2;
92
 
        }
93
 
}
94
 
 
95
 
 
96
 
void Text::set_message( int message_num, PString &pstring )
97
 
{
98
 
        // update message
99
 
        _message_lens[ message_num ] = pstring.length();
100
 
}
101
 
 
102
 
 
103
 
void Text::reset_message( int message_num, char mode )
104
 
{
105
 
        _message_modes[ message_num ] = mode;
106
 
        reset_message_param( message_num );
107
 
}
108
 
 
109
 
 
110
 
void Text::reset()
111
 
{
112
 
        // reset renderer
113
 
        _duration = 0;
114
 
        _last_millis = ::millis();
115
 
        _do_draw_reset = false;
116
 
 
117
 
        // reset the text renderer's output buffer
118
 
        TextRenderer::reset_buffer();
119
 
}
120
 
 
121
 
 
122
 
void Text::draw_reset()
123
 
{
124
 
        // how many milliseconds have elapsed since last frame?
125
 
        unsigned long millis = ::millis();
126
 
        if( millis > _last_millis )
127
 
                _duration = millis - _last_millis;
128
 
        _last_millis = millis;
129
 
 
130
 
        _do_draw_reset = true;
131
 
}
132
 
 
133
 
 
134
 
void Text::draw( int message_num, int segment )
135
 
{
136
 
        // perform a mid-frame reset, as necessary
137
 
        if( _do_draw_reset && segment > NUM_SEGMENTS / 2 ) {
138
 
                _do_draw_reset = false;
139
 
 
140
 
                if( _message_modes[ message_num ] & MODE_HSCROLL )
141
 
                {
142
 
                        // add to horizontal scroll position
143
 
                        _message_params[ message_num ] +=
144
 
                                _duration * TEXT_SCROLL_SPEED / 1000;
145
 
                        long excess = _message_params[ message_num ] -
146
 
                                TextRenderer::get_width( _message_lens[ message_num ] );
147
 
                        if( excess >= 0 ) {
148
 
                                reset_message_param( message_num );
149
 
                                _message_params[ message_num ] += excess;
150
 
                        }
151
 
                }
152
 
        }
153
 
 
154
 
        // calculate the width
155
 
        int width;
156
 
        switch( _message_modes[ message_num ] & 0x1c )
157
 
        {
158
 
        case MODE_ONEQUARTER:
159
 
                width = NUM_SEGMENTS / 4;
160
 
                break;
161
 
        case MODE_ONETHIRD:
162
 
                width = NUM_SEGMENTS / 3;
163
 
                break;
164
 
        case MODE_HALF:
165
 
                width = NUM_SEGMENTS / 2;
166
 
                break;
167
 
        case MODE_TWOTHIRDS:
168
 
                width = NUM_SEGMENTS * 2 / 3;
169
 
                break;
170
 
        case MODE_THREEQUARTERS:
171
 
                width = NUM_SEGMENTS * 3 / 4;
172
 
                break;
173
 
        case MODE_ALL:
174
 
                width = NUM_SEGMENTS;
175
 
                break;
176
 
        }
177
 
 
178
 
        // convert segment to display-space
179
 
        switch( _message_modes[ message_num ] & 0x03 )
180
 
        {
181
 
        case MODE_TOP:
182
 
                if( segment < width / 2 )
183
 
                        segment += width / 2;
184
 
                else if( segment >= NUM_SEGMENTS - width / 2 )
185
 
                        segment -= ( NUM_SEGMENTS - width / 2 );
186
 
                else
187
 
                        return;
188
 
                break;
189
 
        case MODE_BOTTOM:
190
 
                if( segment > ( NUM_SEGMENTS - width ) / 2 &&
191
 
                        segment < ( NUM_SEGMENTS - width ) / 2 + width )
192
 
                {
193
 
                        segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
194
 
                }               
195
 
                else
196
 
                        return;
197
 
                break;
198
 
        }
199
 
 
200
 
        // render
201
 
        TextRenderer::render(
202
 
                _messages[ message_num ], _message_lens[ message_num ],
203
 
                (long)segment + _message_params[ message_num ],
204
 
                ( _message_modes[ message_num ] & 1 ) == 0, 0 );
205
 
}