/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * text.cc
 *
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
 *
 * This file is part of propeller-clock (hereafter referred to as "this
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
 * information.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "text.h"
#include "text_renderer.h"
#include "config.h"
#include "Arduino.h"
#include <avr/pgmspace.h>


// the buffer for the message
char Text::_messages[ NUM_MESSAGE_BUFFERS ][ MESSAGE_BUFFER_LEN ];

// message buffer lengths
static int _message_lens[ NUM_MESSAGE_BUFFERS ];

// the mode that each message represents
static char _message_modes[ NUM_MESSAGE_BUFFERS ];

// mode-specific message parameters
static signed long _message_pos[ NUM_MESSAGE_BUFFERS ];

// text scaling factors
static char _message_scale[ NUM_MESSAGE_BUFFERS ];

// time last frame
static unsigned long _last_millis;

// duration last frame (in case we can't calculate it this frame!)
static unsigned long _duration;

// recalculate message params, based on _duration
static bool _do_draw_reset;


#define MASK_MODE_SIZE 0x07
#define MASK_MODE_POS 0x40

void reset_message_pos( int message_num )
{
	if( _message_modes[ message_num ] & Text::MODE_HSCROLL )
	{
		switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
		{
		case Text::MODE_ONEQUARTER:
			_message_pos[ message_num ] = -NUM_SEGMENTS / 4; break;
		case Text::MODE_ONETHIRD:
			_message_pos[ message_num ] = -NUM_SEGMENTS / 3; break;
		case Text::MODE_HALF:
			_message_pos[ message_num ] = -NUM_SEGMENTS / 2; break;
		case Text::MODE_TWOTHIRDS:
			_message_pos[ message_num ] = -NUM_SEGMENTS * 2 / 3; break;
		case Text::MODE_THREEQUARTERS:
			_message_pos[ message_num ] = -NUM_SEGMENTS * 3 / 4; break;
		case Text::MODE_ALL:
			_message_pos[ message_num ] = -NUM_SEGMENTS; break;
		}
	}
	else
	{
		switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
		{
		case Text::MODE_ONEQUARTER:
			_message_pos[ message_num ] = NUM_SEGMENTS / 4; break;
		case Text::MODE_ONETHIRD:
			_message_pos[ message_num ] = NUM_SEGMENTS / 3; break;
		case Text::MODE_HALF:
			_message_pos[ message_num ] = NUM_SEGMENTS / 2; break;
		case Text::MODE_TWOTHIRDS:
			_message_pos[ message_num ] = NUM_SEGMENTS * 2 / 3; break;
		case Text::MODE_THREEQUARTERS:
			_message_pos[ message_num ] = NUM_SEGMENTS * 3 / 4; break;
		case Text::MODE_ALL:
			_message_pos[ message_num ] = NUM_SEGMENTS; break;
		}
		_message_pos[ message_num ] = -( _message_pos[ message_num ] -
			TextRenderer::get_width( _message_lens[ message_num ],
					_message_scale[ message_num ] ) ) / 2;
	}
}


void Text::reset()
{
	// reset renderer
	_duration = 0;
	_last_millis = ::millis();

	// clear all message modes (disable messages)
	for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
		_message_modes[ a ] = 0;

	// reset the text renderer's output buffer
	TextRenderer::reset_buffer();
}


void Text::set_up_message( int message_num, char mode, char scale )
{
	_message_modes[ message_num ] = mode;
	_message_scale[ message_num ] = scale;

	// horizontal scrolling messages need their position reset at start of mode
	if( _message_modes[ message_num ] & MODE_HSCROLL )
		reset_message_pos( message_num );
}


void Text::set_message_text( int message_num, PString &pstring )
{
	// update message
	_message_lens[ message_num ] = pstring.length();

	// non-horizontal scrolling modes need their position reset whenever the
	// text changes
	if( !( _message_modes[ message_num ] & MODE_HSCROLL ) )
		reset_message_pos( message_num );
}


void Text::draw_reset()
{
	// how many milliseconds have elapsed since last frame?
	unsigned long millis = ::millis();
	if( millis > _last_millis )
		_duration = millis - _last_millis;
	_last_millis = millis;

	// we'll need to recalculate params for the frame, but we want to do it when
	// we're not in the middle of a text display (to avoid tearing).
	_do_draw_reset = true;
}


static void draw_message( int message_num, int segment )
{
	TextRenderer::buffer_in_use();

	// calculate the width
	int width;
	switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
	{
	case Text::MODE_ONEQUARTER:
		width = NUM_SEGMENTS / 4;
		break;
	case Text::MODE_ONETHIRD:
		width = NUM_SEGMENTS / 3;
		break;
	case Text::MODE_HALF:
		width = NUM_SEGMENTS / 2;
		break;
	case Text::MODE_TWOTHIRDS:
		width = NUM_SEGMENTS * 2 / 3;
		break;
	case Text::MODE_THREEQUARTERS:
		width = NUM_SEGMENTS * 3 / 4;
		break;
	case Text::MODE_ALL:
		width = NUM_SEGMENTS;
		break;
	}

	// convert segment to display-space
	switch( _message_modes[ message_num ] & MASK_MODE_POS )
	{
	case Text::MODE_TOP:
		if( segment < width / 2 )
			segment += width / 2;
		else if( segment >= NUM_SEGMENTS - width / 2 )
			segment -= ( NUM_SEGMENTS - width / 2 );
		else
			return;
		break;
	case Text::MODE_BOTTOM:
		if( segment > ( NUM_SEGMENTS - width ) / 2 &&
			segment < ( NUM_SEGMENTS - width ) / 2 + width )
		{
			segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
		}		
		else
			return;
		break;
	}

	// render
	TextRenderer::render(
		Text::_messages[ message_num ], _message_lens[ message_num ],
		(long)segment + _message_pos[ message_num ],
		( _message_modes[ message_num ] & MASK_MODE_POS ) == Text::MODE_BOTTOM,
		0, _message_scale[ message_num ] );
}


void Text::draw( int segment )
{
	// perform a mid-frame reset, as necessary
	if( _do_draw_reset && segment == NUM_SEGMENTS / 2 ) {
		_do_draw_reset = false;

		// go through the messages looking for an active one
		for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
			if( _message_modes[ a ] & MASK_MODE_SIZE )
			{
				// add to horizontal scroll position
				if( _message_modes[ a ] & Text::MODE_HSCROLL ) {
					_message_pos[ a ] +=
							_duration * TEXT_SCROLL_SPEED / 1000;
					long excess = _message_pos[ a ] -
							TextRenderer::get_width( _message_lens[ a ],
									_message_scale[ a ] );
					if( excess >= 0 ) {
						reset_message_pos( a );
						_message_pos[ a ] += excess;
					}
				}
			}
	}

	// draw any active messages
	for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
		if( _message_modes[ a ] & MASK_MODE_SIZE )
			draw_message( a, segment );
}