/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/text.cc

  • Committer: edam
  • Date: 2012-02-25 01:31:17 UTC
  • Revision ID: tim@ed.am-20120225013117-53ed8yahoreoms76
updated software to include drawing abstraction infrastructure

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
40
 
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];
41
 
 
42
 
// text scaling factors
43
 
static char _message_scale[ NUM_MESSAGE_BUFFERS ];
44
 
 
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
 
 
54
 
 
55
 
#define MASK_MODE_SIZE 0x07
56
 
#define MASK_MODE_POS 0x40
57
 
 
58
 
void reset_message_param( int message_num )
59
 
{
60
 
        if( _message_modes[ message_num ] & Text::MODE_HSCROLL )
61
 
        {
62
 
                switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
63
 
                {
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;
68
 
                case Text::MODE_HALF:
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;
74
 
                case Text::MODE_ALL:
75
 
                        _message_params[ message_num ] = -NUM_SEGMENTS; break;
76
 
                }
77
 
        }
78
 
        else
79
 
        {
80
 
                switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
81
 
                {
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;
86
 
                case Text::MODE_HALF:
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;
92
 
                case Text::MODE_ALL:
93
 
                        _message_params[ message_num ] = NUM_SEGMENTS; break;
94
 
                }
95
 
                _message_params[ message_num ] = -( _message_params[ message_num ] -
96
 
                        TextRenderer::get_width( _message_lens[ message_num ],
97
 
                                        _message_scale[ message_num ] ) ) / 2;
98
 
        }
99
 
}
100
 
 
101
 
 
102
 
void Text::reset()
103
 
{
104
 
        // reset renderer
105
 
        _duration = 0;
106
 
        _last_millis = ::millis();
107
 
        _do_draw_reset = false;
108
 
 
109
 
        // clear all message modes (disable messages)
110
 
        for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
111
 
                _message_modes[ a ] = 0;
112
 
 
113
 
        // reset the text renderer's output buffer
114
 
        TextRenderer::reset_buffer();
115
 
}
116
 
 
117
 
 
118
 
void Text::set_up_message( int message_num, char mode, char scale )
119
 
{
120
 
        _message_modes[ message_num ] = mode;
121
 
        _message_scale[ message_num ] = scale;
122
 
}
123
 
 
124
 
 
125
 
void Text::set_message_text( int message_num, PString &pstring )
126
 
{
127
 
        // update message
128
 
        _message_lens[ message_num ] = pstring.length();
129
 
}
130
 
 
131
 
 
132
 
void Text::draw_reset()
133
 
{
134
 
        // how many milliseconds have elapsed since last frame?
135
 
        unsigned long millis = ::millis();
136
 
        if( millis > _last_millis )
137
 
                _duration = millis - _last_millis;
138
 
        _last_millis = millis;
139
 
 
140
 
        _do_draw_reset = true;
141
 
}
142
 
 
143
 
 
144
 
static void draw_message( int message_num, int segment )
145
 
{
146
 
        TextRenderer::buffer_in_use();
147
 
 
148
 
        // perform a mid-frame reset, as necessary
149
 
        if( _do_draw_reset ) // && segment > NUM_SEGMENTS / 2 )
150
 
        {
151
 
                // reset message param
152
 
                reset_message_param( message_num );
153
 
 
154
 
                // add to horizontal scroll position
155
 
                if( _message_modes[ message_num ] & Text::MODE_HSCROLL ) {
156
 
                        _message_params[ message_num ] +=
157
 
                                _duration * TEXT_SCROLL_SPEED / 1000;
158
 
                        long excess = _message_params[ message_num ] -
159
 
                                TextRenderer::get_width( _message_lens[ message_num ],
160
 
                                                _message_scale[ message_num ] );
161
 
                        if( excess >= 0 ) {
162
 
                                reset_message_param( message_num );
163
 
                                _message_params[ message_num ] += excess;
164
 
                        }
165
 
                }
166
 
        }
167
 
 
168
 
        // calculate the width
169
 
        int width;
170
 
        switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
171
 
        {
172
 
        case Text::MODE_ONEQUARTER:
173
 
                width = NUM_SEGMENTS / 4;
174
 
                break;
175
 
        case Text::MODE_ONETHIRD:
176
 
                width = NUM_SEGMENTS / 3;
177
 
                break;
178
 
        case Text::MODE_HALF:
179
 
                width = NUM_SEGMENTS / 2;
180
 
                break;
181
 
        case Text::MODE_TWOTHIRDS:
182
 
                width = NUM_SEGMENTS * 2 / 3;
183
 
                break;
184
 
        case Text::MODE_THREEQUARTERS:
185
 
                width = NUM_SEGMENTS * 3 / 4;
186
 
                break;
187
 
        case Text::MODE_ALL:
188
 
                width = NUM_SEGMENTS;
189
 
                break;
190
 
        }
191
 
 
192
 
        // convert segment to display-space
193
 
        switch( _message_modes[ message_num ] & MASK_MODE_POS )
194
 
        {
195
 
        case Text::MODE_TOP:
196
 
                if( segment < width / 2 )
197
 
                        segment += width / 2;
198
 
                else if( segment >= NUM_SEGMENTS - width / 2 )
199
 
                        segment -= ( NUM_SEGMENTS - width / 2 );
200
 
                else
201
 
                        return;
202
 
                break;
203
 
        case Text::MODE_BOTTOM:
204
 
                if( segment > ( NUM_SEGMENTS - width ) / 2 &&
205
 
                        segment < ( NUM_SEGMENTS - width ) / 2 + width )
206
 
                {
207
 
                        segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
208
 
                }               
209
 
                else
210
 
                        return;
211
 
                break;
212
 
        }
213
 
 
214
 
        // render
215
 
        TextRenderer::render(
216
 
                Text::_messages[ message_num ], _message_lens[ message_num ],
217
 
                (long)segment + _message_params[ message_num ],
218
 
                ( _message_modes[ message_num ] & MASK_MODE_POS ) == Text::MODE_BOTTOM,
219
 
                0, _message_scale[ message_num ] );
220
 
}
221
 
 
222
 
 
223
 
void Text::draw( int segment )
224
 
{
225
 
        // draw any active messages
226
 
        for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
227
 
                if( _message_modes[ a ] & MASK_MODE_SIZE )
228
 
                        draw_message( a, segment );
229
 
 
230
 
        // turn off any requested draw reset after all messages are drawn
231
 
        _do_draw_reset = false;
232
 
}
233