/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-29 21:56:32 UTC
  • Revision ID: edam@waxworlds.org-20120229215632-kypb9491vx7bicef
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "config.h"
26
26
#include "Arduino.h"
27
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 )
28
35
 
29
36
 
30
37
// the buffer for the message
37
44
static char _message_modes[ NUM_MESSAGE_BUFFERS ];
38
45
 
39
46
// mode-specific message parameters
40
 
static signed long _message_pos[ NUM_MESSAGE_BUFFERS ];
41
 
 
42
 
// text scaling factors
43
 
static char _message_scale[ NUM_MESSAGE_BUFFERS ];
 
47
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];
44
48
 
45
49
// time last frame
46
50
static unsigned long _last_millis;
52
56
static bool _do_draw_reset;
53
57
 
54
58
 
55
 
#define MASK_MODE_SIZE 0x07
56
 
#define MASK_MODE_POS 0x40
57
 
 
58
 
void reset_message_pos( 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_pos[ message_num ] = -NUM_SEGMENTS / 4; break;
66
 
                case Text::MODE_ONETHIRD:
67
 
                        _message_pos[ message_num ] = -NUM_SEGMENTS / 3; break;
68
 
                case Text::MODE_HALF:
69
 
                        _message_pos[ message_num ] = -NUM_SEGMENTS / 2; break;
70
 
                case Text::MODE_TWOTHIRDS:
71
 
                        _message_pos[ message_num ] = -NUM_SEGMENTS * 2 / 3; break;
72
 
                case Text::MODE_THREEQUARTERS:
73
 
                        _message_pos[ message_num ] = -NUM_SEGMENTS * 3 / 4; break;
74
 
                case Text::MODE_ALL:
75
 
                        _message_pos[ 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_pos[ message_num ] = NUM_SEGMENTS / 4; break;
84
 
                case Text::MODE_ONETHIRD:
85
 
                        _message_pos[ message_num ] = NUM_SEGMENTS / 3; break;
86
 
                case Text::MODE_HALF:
87
 
                        _message_pos[ message_num ] = NUM_SEGMENTS / 2; break;
88
 
                case Text::MODE_TWOTHIRDS:
89
 
                        _message_pos[ message_num ] = NUM_SEGMENTS * 2 / 3; break;
90
 
                case Text::MODE_THREEQUARTERS:
91
 
                        _message_pos[ message_num ] = NUM_SEGMENTS * 3 / 4; break;
92
 
                case Text::MODE_ALL:
93
 
                        _message_pos[ message_num ] = NUM_SEGMENTS; break;
94
 
                }
95
 
                _message_pos[ message_num ] = -( _message_pos[ message_num ] -
96
 
                        TextRenderer::get_width( _message_lens[ message_num ],
97
 
                                        _message_scale[ message_num ] ) ) / 2;
98
 
        }
 
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 );
99
94
}
100
95
 
101
96
 
104
99
        // reset renderer
105
100
        _duration = 0;
106
101
        _last_millis = ::millis();
107
 
 
108
 
        // clear all message modes (disable messages)
109
 
        for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
110
 
                _message_modes[ a ] = 0;
 
102
        _do_draw_reset = false;
111
103
 
112
104
        // reset the text renderer's output buffer
113
105
        TextRenderer::reset_buffer();
114
106
}
115
107
 
116
108
 
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;
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 );
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();
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 );
137
 
}
138
 
 
139
 
 
140
109
void Text::draw_reset()
141
110
{
142
111
        // how many milliseconds have elapsed since last frame?
145
114
                _duration = millis - _last_millis;
146
115
        _last_millis = millis;
147
116
 
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).
150
117
        _do_draw_reset = true;
151
118
}
152
119
 
153
120
 
154
 
static void draw_message( int message_num, int segment )
 
121
void Text::draw( int message_num, int segment )
155
122
{
156
 
        TextRenderer::buffer_in_use();
 
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
        }
157
140
 
158
141
        // calculate the width
159
142
        int width;
160
 
        switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
 
143
        switch( _message_modes[ message_num ] & 0x1c )
161
144
        {
162
 
        case Text::MODE_ONEQUARTER:
 
145
        case MODE_ONEQUARTER:
163
146
                width = NUM_SEGMENTS / 4;
164
147
                break;
165
 
        case Text::MODE_ONETHIRD:
 
148
        case MODE_ONETHIRD:
166
149
                width = NUM_SEGMENTS / 3;
167
150
                break;
168
 
        case Text::MODE_HALF:
 
151
        case MODE_HALF:
169
152
                width = NUM_SEGMENTS / 2;
170
153
                break;
171
 
        case Text::MODE_TWOTHIRDS:
 
154
        case MODE_TWOTHIRDS:
172
155
                width = NUM_SEGMENTS * 2 / 3;
173
156
                break;
174
 
        case Text::MODE_THREEQUARTERS:
 
157
        case MODE_THREEQUARTERS:
175
158
                width = NUM_SEGMENTS * 3 / 4;
176
159
                break;
177
 
        case Text::MODE_ALL:
 
160
        case MODE_ALL:
178
161
                width = NUM_SEGMENTS;
179
162
                break;
180
163
        }
181
164
 
182
165
        // convert segment to display-space
183
 
        switch( _message_modes[ message_num ] & MASK_MODE_POS )
 
166
        switch( _message_modes[ message_num ] & 0x03 )
184
167
        {
185
 
        case Text::MODE_TOP:
 
168
        case MODE_TOP:
186
169
                if( segment < width / 2 )
187
170
                        segment += width / 2;
188
171
                else if( segment >= NUM_SEGMENTS - width / 2 )
190
173
                else
191
174
                        return;
192
175
                break;
193
 
        case Text::MODE_BOTTOM:
 
176
        case MODE_BOTTOM:
194
177
                if( segment > ( NUM_SEGMENTS - width ) / 2 &&
195
178
                        segment < ( NUM_SEGMENTS - width ) / 2 + width )
196
 
                {
197
 
                        segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
198
 
                }               
 
179
                        segment -= ( NUM_SEGMENTS - width ) / 2;
199
180
                else
200
181
                        return;
201
182
                break;
203
184
 
204
185
        // render
205
186
        TextRenderer::render(
206
 
                Text::_messages[ message_num ], _message_lens[ message_num ],
207
 
                (long)segment + _message_pos[ message_num ],
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
 
{
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
 
 
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
 
 
 
187
                _messages[ message_num ], _message_lens[ message_num ],
 
188
                (long)segment + _message_params[ message_num ],
 
189
                _message_modes[ message_num ] & 1 == 0, 0 );
 
190
}