4
* Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
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
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.
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.
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/>.
24
#include "text_renderer.h"
27
#include <avr/pgmspace.h>
30
// the buffer for the message
31
char Text::_messages[ NUM_MESSAGE_BUFFERS ][ MESSAGE_BUFFER_LEN ];
33
// message buffer lengths
34
static int _message_lens[ NUM_MESSAGE_BUFFERS ];
36
// the mode that each message represents
37
static char _message_modes[ NUM_MESSAGE_BUFFERS ];
39
// mode-specific message parameters
40
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];
42
// text scaling factors
43
static int _message_scale[ NUM_MESSAGE_BUFFERS ];
46
static unsigned long _last_millis;
48
// duration last frame (in case we can't calculate it this frame!)
49
static unsigned long _duration;
51
// recalculate message params, based on _duration
52
static bool _do_draw_reset;
54
// recalculate messages parameters (after message reset)
55
static unsigned char _message_recalc_flags;
58
void reset_message_param( int message_num )
60
if( _message_modes[ message_num ] & Text::MODE_HSCROLL )
62
switch( _message_modes[ message_num ] & 0x1c )
64
case Text::MODE_ONEQUARTER:
65
_message_params[ message_num ] = -NUM_SEGMENTS / 4; break;
66
case Text::MODE_ONETHIRD:
67
_message_params[ message_num ] = -NUM_SEGMENTS / 3; break;
69
_message_params[ message_num ] = -NUM_SEGMENTS / 2; break;
70
case Text::MODE_TWOTHIRDS:
71
_message_params[ message_num ] = -NUM_SEGMENTS * 2 / 3; break;
72
case Text::MODE_THREEQUARTERS:
73
_message_params[ message_num ] = -NUM_SEGMENTS * 3 / 4; break;
75
_message_params[ message_num ] = -NUM_SEGMENTS; break;
80
switch( _message_modes[ message_num ] & 0x1c )
82
case Text::MODE_ONEQUARTER:
83
_message_params[ message_num ] = NUM_SEGMENTS / 4; break;
84
case Text::MODE_ONETHIRD:
85
_message_params[ message_num ] = NUM_SEGMENTS / 3; break;
87
_message_params[ message_num ] = NUM_SEGMENTS / 2; break;
88
case Text::MODE_TWOTHIRDS:
89
_message_params[ message_num ] = NUM_SEGMENTS * 2 / 3; break;
90
case Text::MODE_THREEQUARTERS:
91
_message_params[ message_num ] = NUM_SEGMENTS * 3 / 4; break;
93
_message_params[ message_num ] = NUM_SEGMENTS; break;
95
_message_params[ message_num ] = -( _message_params[ message_num ] -
96
TextRenderer::get_width( _message_lens[ message_num ],
97
_message_scale[ message_num ] ) ) / 2;
102
void Text::set_message( int message_num, PString &pstring )
105
_message_lens[ message_num ] = pstring.length();
109
void Text::reset_message( int message_num, char mode, int scale )
111
_message_modes[ message_num ] = mode;
112
_message_recalc_flags |= 1 << message_num;
113
_message_scale[ message_num ] = scale;
121
_last_millis = ::millis();
122
_do_draw_reset = false;
123
_message_recalc_flags = 0;
125
// reset the text renderer's output buffer
126
TextRenderer::reset_buffer();
130
void Text::draw_reset()
132
// how many milliseconds have elapsed since last frame?
133
unsigned long millis = ::millis();
134
if( millis > _last_millis )
135
_duration = millis - _last_millis;
136
_last_millis = millis;
138
_do_draw_reset = true;
142
void Text::post_draw()
144
_do_draw_reset = false;
148
void Text::draw( int message_num, int segment )
150
TextRenderer::buffer_in_use();
152
// perform a mid-frame reset, as necessary
153
if( _do_draw_reset ) // && segment > NUM_SEGMENTS / 2 )
155
// reset message param?
156
if( _message_recalc_flags & ( 1 << message_num ) ) {
157
_message_recalc_flags &= ~( 1 << message_num );
158
reset_message_param( message_num );
161
// add to horizontal scroll position
162
if( _message_modes[ message_num ] & MODE_HSCROLL ) {
163
_message_params[ message_num ] +=
164
_duration * TEXT_SCROLL_SPEED / 1000;
165
long excess = _message_params[ message_num ] -
166
TextRenderer::get_width( _message_lens[ message_num ],
167
_message_scale[ message_num ] );
169
reset_message_param( message_num );
170
_message_params[ message_num ] += excess;
175
// calculate the width
177
switch( _message_modes[ message_num ] & 0x1c )
179
case MODE_ONEQUARTER:
180
width = NUM_SEGMENTS / 4;
183
width = NUM_SEGMENTS / 3;
186
width = NUM_SEGMENTS / 2;
189
width = NUM_SEGMENTS * 2 / 3;
191
case MODE_THREEQUARTERS:
192
width = NUM_SEGMENTS * 3 / 4;
195
width = NUM_SEGMENTS;
199
// convert segment to display-space
200
switch( _message_modes[ message_num ] & 0x03 )
203
if( segment < width / 2 )
204
segment += width / 2;
205
else if( segment >= NUM_SEGMENTS - width / 2 )
206
segment -= ( NUM_SEGMENTS - width / 2 );
211
if( segment > ( NUM_SEGMENTS - width ) / 2 &&
212
segment < ( NUM_SEGMENTS - width ) / 2 + width )
214
segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
222
TextRenderer::render(
223
_messages[ message_num ], _message_lens[ message_num ],
224
(long)segment + _message_params[ message_num ],
225
( _message_modes[ message_num ] & 1 ) == 0, 0,
226
_message_scale[ message_num ] );