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