/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: 2013-03-31 17:07:36 UTC
  • Revision ID: tim@ed.am-20130331170736-hphm2hg0y6l7w6z1
made rtc-test's DS1307 library a symlink to the main one in src/util

Show diffs side-by-side

added added

removed removed

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