/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: Tim Marston
  • Date: 2012-04-29 15:27:05 UTC
  • Revision ID: tim@ed.am-20120429152705-9z2o01ugaewxipq6
change our BOARD to pro5v328

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