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>
31
// the proportion of the display to use (in segments)
32
#define BIG_SEGMENTS ( NUM_SEGMENTS * 2 / 3 )
33
#define SMALL_SEGMENTS ( NUM_SEGMENTS - BIG_TEXT_SEGMENTS )
34
#define HALF_SEGMENTS ( NUM_SEGMENTS / 2 )
37
// the buffer for the message
38
char Text::_messages[ NUM_MESSAGE_BUFFERS ][ MESSAGE_BUFFER_LEN ];
40
// message buffer lengths
41
static int _message_lens[ NUM_MESSAGE_BUFFERS ];
43
// the mode that each message represents
44
static char _message_modes[ NUM_MESSAGE_BUFFERS ];
46
// mode-specific message parameters
47
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];
50
static unsigned long _last_millis;
52
// duration last frame (in case we can't calculate it this frame!)
53
static unsigned long _duration;
55
// recalculate message params, based on _duration
56
static bool _do_draw_reset;
59
void reset_message_param( int message_num )
61
switch( _message_modes[ message_num ] )
64
_message_params[ message_num ] = -BIG_SEGMENTS;
67
_message_params[ message_num ] = -HALF_SEGMENTS;
71
_message_params[ message_num ] = -( BIG_SEGMENTS -
72
TextRenderer::get_width( _message_lens[ message_num ] ) ) / 2;
76
_message_params[ message_num ] = -( HALF_SEGMENTS -
77
TextRenderer::get_width( _message_lens[ message_num ] ) ) / 2;
83
void Text::set_message( int message_num, PString &pstring )
86
_message_lens[ message_num ] = pstring.length();
90
void Text::set_message_mode( int message_num, char mode )
92
_message_modes[ message_num ] = mode;
93
reset_message_param( message_num );
101
_last_millis = ::millis();
102
_do_draw_reset = false;
104
// reset the text renderer's output buffer
105
TextRenderer::reset_buffer();
109
void Text::draw_reset()
111
// how many milliseconds have elapsed since last frame?
112
unsigned long millis = ::millis();
113
if( millis > _last_millis )
114
_duration = millis - _last_millis;
115
_last_millis = millis;
117
_do_draw_reset = true;
121
void Text::draw( int message_num, int segment )
123
// perform a mid-frame reset, as necessary
124
if( _do_draw_reset && segment > NUM_SEGMENTS / 2 ) {
125
_do_draw_reset = false;
127
if( _message_modes[ message_num ] & MODE_HSCROLL )
129
// add to horizontal scroll position
130
_message_params[ message_num ] +=
131
_duration * TEXT_SCROLL_SPEED / 1000;
132
long excess = _message_params[ message_num ] -
133
TextRenderer::get_width( _message_lens[ message_num ] );
135
reset_message_param( message_num );
136
_message_params[ message_num ] += excess;
141
// calculate the width
143
switch( _message_modes[ message_num ] & 0x1c )
145
case MODE_ONEQUARTER:
146
width = NUM_SEGMENTS / 4;
149
width = NUM_SEGMENTS / 3;
152
width = NUM_SEGMENTS / 2;
155
width = NUM_SEGMENTS * 2 / 3;
157
case MODE_THREEQUARTERS:
158
width = NUM_SEGMENTS * 3 / 4;
161
width = NUM_SEGMENTS;
165
// convert segment to display-space
166
switch( _message_modes[ message_num ] & 0x03 )
169
if( segment < width / 2 )
170
segment += width / 2;
171
else if( segment >= NUM_SEGMENTS - width / 2 )
172
segment -= ( NUM_SEGMENTS - width / 2 );
177
if( segment > ( NUM_SEGMENTS - width ) / 2 &&
178
segment < ( NUM_SEGMENTS - width ) / 2 + width )
179
segment -= ( NUM_SEGMENTS - width ) / 2;
186
TextRenderer::render(
187
_messages[ message_num ], _message_lens[ message_num ],
188
(long)segment + _message_params[ message_num ],
189
_message_modes[ message_num ] & 1 == 0, 0 );