/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: Tim Marston
  • Date: 2012-05-18 18:29:50 UTC
  • Revision ID: tim@ed.am-20120518182950-t85bn9a21n72uzm8
text messages are now individually enabled and draw()n automatically

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 )
35
28
 
36
29
 
37
30
// the buffer for the message
46
39
// mode-specific message parameters
47
40
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];
48
41
 
 
42
// text scaling factors
 
43
static char _message_scale[ NUM_MESSAGE_BUFFERS ];
 
44
 
49
45
// time last frame
50
46
static unsigned long _last_millis;
51
47
 
56
52
static bool _do_draw_reset;
57
53
 
58
54
 
 
55
#define MASK_MODE_SIZE 0x07
 
56
#define MASK_MODE_POS 0x40
 
57
 
59
58
void reset_message_param( int message_num )
60
59
{
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 );
 
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
        }
94
99
}
95
100
 
96
101
 
101
106
        _last_millis = ::millis();
102
107
        _do_draw_reset = false;
103
108
 
 
109
        // clear all message modes (disable messages)
 
110
        for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
 
111
                _message_modes[ a ] = 0;
 
112
 
104
113
        // reset the text renderer's output buffer
105
114
        TextRenderer::reset_buffer();
106
115
}
107
116
 
108
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
 
109
132
void Text::draw_reset()
110
133
{
111
134
        // how many milliseconds have elapsed since last frame?
118
141
}
119
142
 
120
143
 
121
 
void Text::draw( int message_num, int segment )
 
144
static void draw_message( int message_num, int segment )
122
145
{
 
146
        TextRenderer::buffer_in_use();
 
147
 
123
148
        // perform a mid-frame reset, as necessary
124
 
        if( _do_draw_reset && segment > NUM_SEGMENTS / 2 ) {
125
 
                _do_draw_reset = false;
 
149
        if( _do_draw_reset ) // && segment > NUM_SEGMENTS / 2 )
 
150
        {
 
151
                // reset message param
 
152
                reset_message_param( message_num );
126
153
 
127
 
                if( _message_modes[ message_num ] & MODE_HSCROLL )
128
 
                {
129
 
                        // add to horizontal scroll position
 
154
                // add to horizontal scroll position
 
155
                if( _message_modes[ message_num ] & Text::MODE_HSCROLL ) {
130
156
                        _message_params[ message_num ] +=
131
157
                                _duration * TEXT_SCROLL_SPEED / 1000;
132
158
                        long excess = _message_params[ message_num ] -
133
 
                                TextRenderer::get_width( _message_lens[ message_num ] );
 
159
                                TextRenderer::get_width( _message_lens[ message_num ],
 
160
                                                _message_scale[ message_num ] );
134
161
                        if( excess >= 0 ) {
135
162
                                reset_message_param( message_num );
136
163
                                _message_params[ message_num ] += excess;
140
167
 
141
168
        // calculate the width
142
169
        int width;
143
 
        switch( _message_modes[ message_num ] & 0x1c )
 
170
        switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
144
171
        {
145
 
        case MODE_ONEQUARTER:
 
172
        case Text::MODE_ONEQUARTER:
146
173
                width = NUM_SEGMENTS / 4;
147
174
                break;
148
 
        case MODE_ONETHIRD:
 
175
        case Text::MODE_ONETHIRD:
149
176
                width = NUM_SEGMENTS / 3;
150
177
                break;
151
 
        case MODE_HALF:
 
178
        case Text::MODE_HALF:
152
179
                width = NUM_SEGMENTS / 2;
153
180
                break;
154
 
        case MODE_TWOTHIRDS:
 
181
        case Text::MODE_TWOTHIRDS:
155
182
                width = NUM_SEGMENTS * 2 / 3;
156
183
                break;
157
 
        case MODE_THREEQUARTERS:
 
184
        case Text::MODE_THREEQUARTERS:
158
185
                width = NUM_SEGMENTS * 3 / 4;
159
186
                break;
160
 
        case MODE_ALL:
 
187
        case Text::MODE_ALL:
161
188
                width = NUM_SEGMENTS;
162
189
                break;
163
190
        }
164
191
 
165
192
        // convert segment to display-space
166
 
        switch( _message_modes[ message_num ] & 0x03 )
 
193
        switch( _message_modes[ message_num ] & MASK_MODE_POS )
167
194
        {
168
 
        case MODE_TOP:
 
195
        case Text::MODE_TOP:
169
196
                if( segment < width / 2 )
170
197
                        segment += width / 2;
171
198
                else if( segment >= NUM_SEGMENTS - width / 2 )
173
200
                else
174
201
                        return;
175
202
                break;
176
 
        case MODE_BOTTOM:
 
203
        case Text::MODE_BOTTOM:
177
204
                if( segment > ( NUM_SEGMENTS - width ) / 2 &&
178
205
                        segment < ( NUM_SEGMENTS - width ) / 2 + width )
179
 
                        segment -= ( NUM_SEGMENTS - width ) / 2;
 
206
                {
 
207
                        segment = width - ( segment - ( NUM_SEGMENTS - width ) / 2 );
 
208
                }               
180
209
                else
181
210
                        return;
182
211
                break;
184
213
 
185
214
        // render
186
215
        TextRenderer::render(
187
 
                _messages[ message_num ], _message_lens[ message_num ],
 
216
                Text::_messages[ message_num ], _message_lens[ message_num ],
188
217
                (long)segment + _message_params[ message_num ],
189
 
                _message_modes[ message_num ] & 1 == 0, 0 );
190
 
}
 
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