/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
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
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
40
static signed long _message_pos[ NUM_MESSAGE_BUFFERS ];
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
41
79 by Tim Marston
added adjustable text scaling factor
42
// text scaling factors
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
43
static char _message_scale[ NUM_MESSAGE_BUFFERS ];
79 by Tim Marston
added adjustable text scaling factor
44
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
45
// time last frame
46
static unsigned long _last_millis;
47
48
// duration last frame (in case we can't calculate it this frame!)
49
static unsigned long _duration;
50
51
// recalculate message params, based on _duration
52
static bool _do_draw_reset;
53
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
54
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
55
#define MASK_MODE_SIZE 0x07
56
#define MASK_MODE_POS 0x40
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
57
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
58
void reset_message_pos( int message_num )
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
59
{
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
60
	if( _message_modes[ message_num ] & Text::MODE_HSCROLL )
61
	{
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
62
		switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
63
		{
64
		case Text::MODE_ONEQUARTER:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
65
			_message_pos[ message_num ] = -NUM_SEGMENTS / 4; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
66
		case Text::MODE_ONETHIRD:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
67
			_message_pos[ message_num ] = -NUM_SEGMENTS / 3; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
68
		case Text::MODE_HALF:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
69
			_message_pos[ message_num ] = -NUM_SEGMENTS / 2; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
70
		case Text::MODE_TWOTHIRDS:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
71
			_message_pos[ message_num ] = -NUM_SEGMENTS * 2 / 3; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
72
		case Text::MODE_THREEQUARTERS:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
73
			_message_pos[ message_num ] = -NUM_SEGMENTS * 3 / 4; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
74
		case Text::MODE_ALL:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
75
			_message_pos[ message_num ] = -NUM_SEGMENTS; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
76
		}
77
	}
78
	else
79
	{
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
80
		switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
81
		{
82
		case Text::MODE_ONEQUARTER:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
83
			_message_pos[ message_num ] = NUM_SEGMENTS / 4; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
84
		case Text::MODE_ONETHIRD:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
85
			_message_pos[ message_num ] = NUM_SEGMENTS / 3; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
86
		case Text::MODE_HALF:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
87
			_message_pos[ message_num ] = NUM_SEGMENTS / 2; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
88
		case Text::MODE_TWOTHIRDS:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
89
			_message_pos[ message_num ] = NUM_SEGMENTS * 2 / 3; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
90
		case Text::MODE_THREEQUARTERS:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
91
			_message_pos[ message_num ] = NUM_SEGMENTS * 3 / 4; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
92
		case Text::MODE_ALL:
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
93
			_message_pos[ message_num ] = NUM_SEGMENTS; break;
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
94
		}
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
95
		_message_pos[ message_num ] = -( _message_pos[ message_num ] -
79 by Tim Marston
added adjustable text scaling factor
96
			TextRenderer::get_width( _message_lens[ message_num ],
97
					_message_scale[ message_num ] ) ) / 2;
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
98
	}
99
}
100
101
102
void Text::reset()
103
{
104
	// reset renderer
105
	_duration = 0;
106
	_last_millis = ::millis();
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
107
108
	// clear all message modes (disable messages)
109
	for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
110
		_message_modes[ a ] = 0;
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
111
112
	// reset the text renderer's output buffer
113
	TextRenderer::reset_buffer();
114
}
115
116
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
117
void Text::set_up_message( int message_num, char mode, char scale )
118
{
119
	_message_modes[ message_num ] = mode;
120
	_message_scale[ message_num ] = scale;
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
121
122
	// horizontal scrolling messages need their position reset at start of mode
123
	if( _message_modes[ message_num ] & MODE_HSCROLL )
124
		reset_message_pos( message_num );
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
125
}
126
127
128
void Text::set_message_text( int message_num, PString &pstring )
129
{
130
	// update message
131
	_message_lens[ message_num ] = pstring.length();
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
132
133
	// non-horizontal scrolling modes need their position reset whenever the
134
	// text changes
135
	if( !( _message_modes[ message_num ] & MODE_HSCROLL ) )
136
		reset_message_pos( message_num );
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
137
}
138
139
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
140
void Text::draw_reset()
141
{
142
	// how many milliseconds have elapsed since last frame?
143
	unsigned long millis = ::millis();
144
	if( millis > _last_millis )
145
		_duration = millis - _last_millis;
146
	_last_millis = millis;
147
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
148
	// we'll need to recalculate params for the frame, but we want to do it when
149
	// we're not in the middle of a text display (to avoid tearing).
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
150
	_do_draw_reset = true;
151
}
152
153
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
154
static void draw_message( int message_num, int segment )
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
155
{
71 by Tim Marston
added time set mode, made text renderer's buffer auto reset/output
156
	TextRenderer::buffer_in_use();
157
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
158
	// calculate the width
159
	int width;
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
160
	switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
161
	{
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
162
	case Text::MODE_ONEQUARTER:
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
163
		width = NUM_SEGMENTS / 4;
164
		break;
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
165
	case Text::MODE_ONETHIRD:
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
166
		width = NUM_SEGMENTS / 3;
167
		break;
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
168
	case Text::MODE_HALF:
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
169
		width = NUM_SEGMENTS / 2;
170
		break;
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
171
	case Text::MODE_TWOTHIRDS:
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
172
		width = NUM_SEGMENTS * 2 / 3;
173
		break;
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
174
	case Text::MODE_THREEQUARTERS:
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
175
		width = NUM_SEGMENTS * 3 / 4;
176
		break;
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
177
	case Text::MODE_ALL:
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
178
		width = NUM_SEGMENTS;
179
		break;
180
	}
181
182
	// convert segment to display-space
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
183
	switch( _message_modes[ message_num ] & MASK_MODE_POS )
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
184
	{
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
185
	case Text::MODE_TOP:
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
186
		if( segment < width / 2 )
187
			segment += width / 2;
188
		else if( segment >= NUM_SEGMENTS - width / 2 )
189
			segment -= ( NUM_SEGMENTS - width / 2 );
190
		else
191
			return;
192
		break;
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
193
	case Text::MODE_BOTTOM:
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
194
		if( segment > ( NUM_SEGMENTS - width ) / 2 &&
195
			segment < ( NUM_SEGMENTS - width ) / 2 + width )
63 by edam
widenned clock hands, tweaked scales, got top & bottom text modes working
196
		{
197
			segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
198
		}		
62 by edam
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages
199
		else
200
			return;
201
		break;
202
	}
203
204
	// render
205
	TextRenderer::render(
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
206
		Text::_messages[ message_num ], _message_lens[ message_num ],
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
207
		(long)segment + _message_pos[ message_num ],
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
208
		( _message_modes[ message_num ] & MASK_MODE_POS ) == Text::MODE_BOTTOM,
209
		0, _message_scale[ message_num ] );
210
}
211
212
213
void Text::draw( int segment )
214
{
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
215
	// perform a mid-frame reset, as necessary
216
	if( _do_draw_reset && segment == NUM_SEGMENTS / 2 ) {
217
		_do_draw_reset = false;
218
219
		// go through the messages looking for an active one
220
		for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
221
			if( _message_modes[ a ] & MASK_MODE_SIZE )
222
			{
223
				// add to horizontal scroll position
224
				if( _message_modes[ a ] & Text::MODE_HSCROLL ) {
225
					_message_pos[ a ] +=
226
							_duration * TEXT_SCROLL_SPEED / 1000;
227
					long excess = _message_pos[ a ] -
228
							TextRenderer::get_width( _message_lens[ a ],
229
									_message_scale[ a ] );
230
					if( excess >= 0 ) {
231
						reset_message_pos( a );
232
						_message_pos[ a ] += excess;
233
					}
234
				}
235
			}
236
	}
237
90 by Tim Marston
text messages are now individually enabled and draw()n automatically
238
	// draw any active messages
239
	for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
240
		if( _message_modes[ a ] & MASK_MODE_SIZE )
241
			draw_message( a, segment );
242
}
243