/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-04-29 15:26:03 UTC
  • Revision ID: tim@ed.am-20120429152603-yoj8oi7wlzb0qwnx
added a 5V power regulator to schematic (since the one on our arduino was crap)

Show diffs side-by-side

added added

removed removed

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