/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-23 23:02:50 UTC
  • Revision ID: tim@ed.am-20120523230250-3pls2u6zt3av0uam
fixed text glitch; extended all modes; added screen flip super-long press;
added button unpress debounde; moved interim button press ignoration to
settings mode; fixed left-over led issue; finished for demo!

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