/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: Dan
  • Date: 2011-11-02 22:26:25 UTC
  • Revision ID: dan@waxworlds.org-20111102222625-fo8prx1bigkjecbt
just deleted the test file

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 int _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
 
// recalculate messages parameters (after message reset)
55
 
static unsigned char _message_recalc_flags;
56
 
 
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 ] & 0x1c )
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 ] & 0x1c )
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::set_message( int message_num, PString &pstring )
103
 
{
104
 
        // update message
105
 
        _message_lens[ message_num ] = pstring.length();
106
 
}
107
 
 
108
 
 
109
 
void Text::reset_message( int message_num, char mode, int scale )
110
 
{
111
 
        _message_modes[ message_num ] = mode;
112
 
        _message_recalc_flags |= 1 << message_num;
113
 
        _message_scale[ message_num ] = scale;
114
 
}
115
 
 
116
 
 
117
 
void Text::reset()
118
 
{
119
 
        // reset renderer
120
 
        _duration = 0;
121
 
        _last_millis = ::millis();
122
 
        _do_draw_reset = false;
123
 
        _message_recalc_flags = 0;
124
 
 
125
 
        // reset the text renderer's output buffer
126
 
        TextRenderer::reset_buffer();
127
 
}
128
 
 
129
 
 
130
 
void Text::draw_reset()
131
 
{
132
 
        // how many milliseconds have elapsed since last frame?
133
 
        unsigned long millis = ::millis();
134
 
        if( millis > _last_millis )
135
 
                _duration = millis - _last_millis;
136
 
        _last_millis = millis;
137
 
 
138
 
        _do_draw_reset = true;
139
 
}
140
 
 
141
 
 
142
 
void Text::draw( int message_num, int segment )
143
 
{
144
 
        TextRenderer::buffer_in_use();
145
 
 
146
 
        // perform a mid-frame reset, as necessary
147
 
        if( _do_draw_reset && segment > NUM_SEGMENTS / 2 ) {
148
 
                _do_draw_reset = false;
149
 
 
150
 
                // reset message param?
151
 
                if( _message_recalc_flags & ( 1 << message_num ) ) {
152
 
                        _message_recalc_flags &= ~( 1 << message_num );
153
 
                        reset_message_param( message_num );
154
 
                }
155
 
 
156
 
                // add to horizontal scroll position
157
 
                if( _message_modes[ message_num ] & MODE_HSCROLL ) {
158
 
                        _message_params[ message_num ] +=
159
 
                                _duration * TEXT_SCROLL_SPEED / 1000;
160
 
                        long excess = _message_params[ message_num ] -
161
 
                                TextRenderer::get_width( _message_lens[ message_num ],
162
 
                                                _message_scale[ message_num ] );
163
 
                        if( excess >= 0 ) {
164
 
                                reset_message_param( message_num );
165
 
                                _message_params[ message_num ] += excess;
166
 
                        }
167
 
                }
168
 
        }
169
 
 
170
 
        // calculate the width
171
 
        int width;
172
 
        switch( _message_modes[ message_num ] & 0x1c )
173
 
        {
174
 
        case MODE_ONEQUARTER:
175
 
                width = NUM_SEGMENTS / 4;
176
 
                break;
177
 
        case MODE_ONETHIRD:
178
 
                width = NUM_SEGMENTS / 3;
179
 
                break;
180
 
        case MODE_HALF:
181
 
                width = NUM_SEGMENTS / 2;
182
 
                break;
183
 
        case MODE_TWOTHIRDS:
184
 
                width = NUM_SEGMENTS * 2 / 3;
185
 
                break;
186
 
        case MODE_THREEQUARTERS:
187
 
                width = NUM_SEGMENTS * 3 / 4;
188
 
                break;
189
 
        case MODE_ALL:
190
 
                width = NUM_SEGMENTS;
191
 
                break;
192
 
        }
193
 
 
194
 
        // convert segment to display-space
195
 
        switch( _message_modes[ message_num ] & 0x03 )
196
 
        {
197
 
        case MODE_TOP:
198
 
                if( segment < width / 2 )
199
 
                        segment += width / 2;
200
 
                else if( segment >= NUM_SEGMENTS - width / 2 )
201
 
                        segment -= ( NUM_SEGMENTS - width / 2 );
202
 
                else
203
 
                        return;
204
 
                break;
205
 
        case MODE_BOTTOM:
206
 
                if( segment > ( NUM_SEGMENTS - width ) / 2 &&
207
 
                        segment < ( NUM_SEGMENTS - width ) / 2 + width )
208
 
                {
209
 
                        segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
210
 
                }               
211
 
                else
212
 
                        return;
213
 
                break;
214
 
        }
215
 
 
216
 
        // render
217
 
        TextRenderer::render(
218
 
                _messages[ message_num ], _message_lens[ message_num ],
219
 
                (long)segment + _message_params[ message_num ],
220
 
                ( _message_modes[ message_num ] & 1 ) == 0, 0,
221
 
                _message_scale[ message_num ] );
222
 
}