/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
#include "display.h"
29
30
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 )
35
36
37
// the buffer for the message
38
char Text::_messages[ NUM_MESSAGE_BUFFERS ][ MESSAGE_BUFFER_LEN ];
39
40
// message buffer lengths
41
static int _message_lens[ NUM_MESSAGE_BUFFERS ];
42
43
// the mode that each message represents
44
static char _message_modes[ NUM_MESSAGE_BUFFERS ];
45
46
// mode-specific message parameters
47
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];
48
49
// time last frame
50
static unsigned long _last_millis;
51
52
// duration last frame (in case we can't calculate it this frame!)
53
static unsigned long _duration;
54
55
// recalculate message params, based on _duration
56
static bool _do_draw_reset;
57
58
59
void reset_message_param( int message_num )
60
{
61
	switch( _message_modes[ message_num ] )
62
	{
63
	case 'S':
64
		_message_params[ message_num ] = -BIG_SEGMENTS;
65
		break;
66
	case 's':
67
		_message_params[ message_num ] = -HALF_SEGMENTS;
68
		break;
69
	case 'B':
70
	case 'T':
71
		_message_params[ message_num ] = -( BIG_SEGMENTS -
72
			TextRenderer::get_width( _message_lens[ message_num ] ) ) / 2;
73
		break;
74
	case 'b':
75
	case 't':
76
		_message_params[ message_num ] = -( HALF_SEGMENTS -
77
			TextRenderer::get_width( _message_lens[ message_num ] ) ) / 2;
78
		break;
79
	}
80
}
81
82
83
void Text::set_message( int message_num, PString &pstring )
84
{
85
	// update message
86
	_message_lens[ message_num ] = pstring.length();
87
}
88
89
90
void Text::set_message_mode( int message_num, char mode )
91
{
92
	_message_modes[ message_num ] = mode;
93
	reset_message_param( message_num );
94
}
95
96
97
void Text::reset()
98
{
99
	// reset renderer
100
	_duration = 0;
101
	_last_millis = ::millis();
102
	_do_draw_reset = false;
103
104
	// reset the text renderer's output buffer
105
	TextRenderer::reset_buffer();
106
}
107
108
109
void Text::draw_reset()
110
{
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;
116
117
	_do_draw_reset = true;
118
}
119
120
121
void Text::draw( int message_num, int segment )
122
{
123
	// perform a mid-frame reset, as necessary
124
	if( _do_draw_reset && segment > NUM_SEGMENTS / 2 ) {
125
		_do_draw_reset = false;
126
127
		if( _message_modes[ message_num ] & MODE_HSCROLL )
128
		{
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 ] );
134
			if( excess >= 0 ) {
135
				reset_message_param( message_num );
136
				_message_params[ message_num ] += excess;
137
			}
138
		}
139
	}
140
141
	// calculate the width
142
	int width;
143
	switch( _message_modes[ message_num ] & 0x1c )
144
	{
145
	case MODE_ONEQUARTER:
146
		width = NUM_SEGMENTS / 4;
147
		break;
148
	case MODE_ONETHIRD:
149
		width = NUM_SEGMENTS / 3;
150
		break;
151
	case MODE_HALF:
152
		width = NUM_SEGMENTS / 2;
153
		break;
154
	case MODE_TWOTHIRDS:
155
		width = NUM_SEGMENTS * 2 / 3;
156
		break;
157
	case MODE_THREEQUARTERS:
158
		width = NUM_SEGMENTS * 3 / 4;
159
		break;
160
	case MODE_ALL:
161
		width = NUM_SEGMENTS;
162
		break;
163
	}
164
165
	// convert segment to display-space
166
	switch( _message_modes[ message_num ] & 0x03 )
167
	{
168
	case MODE_TOP:
169
		if( segment < width / 2 )
170
			segment += width / 2;
171
		else if( segment >= NUM_SEGMENTS - width / 2 )
172
			segment -= ( NUM_SEGMENTS - width / 2 );
173
		else
174
			return;
175
		break;
176
	case MODE_BOTTOM:
177
		if( segment > ( NUM_SEGMENTS - width ) / 2 &&
178
			segment < ( NUM_SEGMENTS - width ) / 2 + width )
179
			segment -= ( NUM_SEGMENTS - width ) / 2;
180
		else
181
			return;
182
		break;
183
	}
184
185
	// render
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 );
190
}