/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_renderer.cc

  • Committer: edam
  • Date: 2012-02-28 17:03:09 UTC
  • Revision ID: edam@waxworlds.org-20120228170309-gaaj6k3prrgvwvp8
remove TextRenderer singleton and save space

Show diffs side-by-side

added added

removed removed

24
24
#include "config.h"
25
25
#include "Arduino.h"
26
26
#include <avr/pgmspace.h>
27
 
#include "common.h"
28
 
#include "nvram.h"
29
 
 
 
27
#include "display.h"
 
28
 
 
29
 
 
30
#define MAX_MESSAGE_LENGTH 64
 
31
 
 
32
 
 
33
// the buffer for the message
 
34
static char _message_buffer[ MAX_MESSAGE_LENGTH ];
 
35
 
 
36
// the current message
 
37
static PString _message( _message_buffer, sizeof( _message_buffer ) );
30
38
 
31
39
// cached glyph
32
40
static char _glyph_cache[ 8 ];
37
45
// selected font
38
46
static int _font = 0;
39
47
 
40
 
// output buffer
41
 
static unsigned char _output_buffer;
42
 
 
43
 
// does buffer need rendering?
44
 
static bool _need_render;
 
48
// time last frame
 
49
static unsigned long _last_millis;
 
50
 
 
51
// duration last frame (in case we can't calculate it this frame!)
 
52
static unsigned long _duration;
 
53
 
 
54
// scroll position (in segments )
 
55
static signed long _scroll;
45
56
 
46
57
 
47
58
// cache a glyph
48
59
void cache_glyph( char c )
49
60
{
50
 
        static char fonts[][ 67 * 8 ] PROGMEM = {
 
61
        static char fonts[][ 66 * 8 ] PROGMEM = {
51
62
                { // INVD-A
52
63
                        0x00, 0x7c, 0x7e, 0x12, 0x12, 0x12, 0x7e, 0x7c, // A
53
64
                        0x00, 0x7e, 0x7e, 0x4a, 0x4a, 0x4a, 0x7e, 0x34, // B
115
126
                        0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x00, 0x00, // :
116
127
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
117
128
                        0x00, 0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, // ,
118
 
                        0x00, 0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x00, // /
119
129
                }, { // INVD-D
120
130
                        0x00, 0x7f, 0x7f, 0x09, 0x09, 0x09, 0x7f, 0x7f, // A
121
131
                        0x00, 0x7f, 0x7f, 0x49, 0x49, 0x49, 0x7f, 0x77, // B
183
193
                        0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, // :
184
194
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
185
195
                        0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, 0x00, // ,
186
 
                        0x00, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x00, // /
187
196
                }, { // INVD-G
188
197
                        0x7c, 0x7e, 0x22, 0x7e, 0x7e, 0x7e, 0x7c, 0x00, // A
189
198
                        0x7c, 0x7e, 0x7e, 0x7e, 0x4a, 0x7e, 0x34, 0x00, // B
251
260
                        0x00, 0x00, 0x28, 0x7c, 0x7c, 0x28, 0x00, 0x00, // :
252
261
                        0x00, 0x00, 0x30, 0x78, 0x78, 0x30, 0x00, 0x00, // .
253
262
                        0x00, 0x00, 0x10, 0xb8, 0xf8, 0x70, 0x00, 0x00, // ,
254
 
                        0x60, 0x70, 0x38, 0x1c, 0x0e, 0x07, 0x03, 0x00, // /
255
263
                }, { // INVD-B
256
264
                        0x00, 0x78, 0x7f, 0x09, 0x09, 0x0f, 0x78, 0x00, // A
257
265
                        0x00, 0x7f, 0x79, 0x49, 0x49, 0x4f, 0x78, 0x00, // B
319
327
                        0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x00, 0x00, // :
320
328
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
321
329
                        0x00, 0x00, 0x00, 0x60, 0xe0, 0x00, 0x00, 0x00, // ,
322
 
                        0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, // /
323
330
                }
324
331
        };
325
332
 
344
351
                pos = 64;
345
352
        else if( c == ',' )
346
353
                pos = 65;
347
 
        else if( c == '/' )
348
 
                pos = 66;
349
354
        else
350
355
                pos = -1;
351
356
 
357
362
}
358
363
 
359
364
 
360
 
void TextRenderer::init()
361
 
{
362
 
        Nvram::load( Nvram::NVRAM_FONT, _font );
363
 
        if( _font < 0 || _font >= 4 ) _font = 0;
364
 
}
365
 
 
366
 
 
367
 
int TextRenderer::get_width( int message_len, int scale )
368
 
{
369
 
        return message_len * 8 * scale;
370
 
}
371
 
 
372
 
 
373
 
void TextRenderer::render( const char *message, int message_len, int x,
374
 
                                                   bool y_flip, int y_shift, int scale )
375
 
{
376
 
        char glyph_col = 0;
377
 
 
 
365
// convert segment to display-space segment
 
366
int xform_segment( int segment )
 
367
{
 
368
        if( segment < TEXT_DISPLAY_SEGMENTS / 2 )
 
369
                return segment + TEXT_DISPLAY_SEGMENTS / 2;
 
370
        else if( segment >= NUM_SEGMENTS - TEXT_DISPLAY_SEGMENTS / 2 )
 
371
                return segment - ( NUM_SEGMENTS - TEXT_DISPLAY_SEGMENTS / 2 );
 
372
        else
 
373
                return - 1;
 
374
}
 
375
 
 
376
 
 
377
// draw a column of pixels from the current message
 
378
void render( int x )
 
379
{
378
380
        // handle negative x
379
 
        if( x >= 0 )
380
 
        {
381
 
                // scale font
382
 
                x /= scale;
383
 
 
384
 
                int pos = x / 8;
385
 
                if( pos < message_len )
386
 
                {
387
 
                        // ensure correct glyph is cached and pull out column of data
388
 
                        cache_glyph( message[ pos ] );
389
 
                        glyph_col = _glyph_cache[ x % 8 ];
390
 
                }
 
381
        if( x < 0 ) {
 
382
                Display::leds_off();
 
383
                return;
391
384
        }
392
385
 
 
386
        // scale font
 
387
        x /= TEXT_SCALE;
 
388
 
 
389
        // make sure the correct glyph is cached
 
390
        int pos = x / 8;
 
391
        cache_glyph( ( pos >= 0 && pos < (signed)_message.length() )? _message[ pos ] : ' ' );
 
392
 
393
393
        // draw a column
394
 
        if( y_flip )
395
 
                for( int a = 7; a >= 0; a-- ) {
396
 
                        if( glyph_col & 1 )
397
 
                                _output_buffer |= 1 << ( a + y_shift );
398
 
                        glyph_col >>= 1;
399
 
                }
 
394
        char col_data = _glyph_cache[ x % 8 ];
 
395
        for( int a = 0; a < 8; a++ )
 
396
                Display::led( 8 - a, ( col_data & ( 1 << a ) )? true : false );
 
397
}
 
398
 
 
399
 
 
400
PString &TextRenderer::get_pstring()
 
401
{
 
402
        // reset renderer
 
403
        _duration = 0;
 
404
        _last_millis = ::millis();
 
405
        _scroll = -TEXT_DISPLAY_SEGMENTS;
 
406
 
 
407
        return _message;
 
408
}
 
409
 
 
410
 
 
411
void TextRenderer::draw_reset()
 
412
{
 
413
        // how many milliseconds have elapsed since last frame?
 
414
        unsigned long millis = ::millis();
 
415
        if( millis > _last_millis )
 
416
                _duration = millis - _last_millis;
 
417
        _last_millis = millis;
 
418
 
 
419
        // move on scroll pos
 
420
        _scroll += ( _duration * TEXT_SCROLL_SPEED / 1000 );
 
421
        if( _scroll > (signed long)_message.length() * 8 * TEXT_SCALE )
 
422
                _scroll -= (signed long)_message.length() * 8 * TEXT_SCALE + TEXT_DISPLAY_SEGMENTS;
 
423
}
 
424
 
 
425
 
 
426
void TextRenderer::draw_scroll( int segment )
 
427
{
 
428
        segment = xform_segment( segment );
 
429
        if( segment < 0 )
 
430
                Display::leds_off();
400
431
        else
401
 
                for( int a = 0; a < 8; a++ ) {
402
 
                        if( glyph_col & 1 )
403
 
                                _output_buffer |= 1 << ( a + y_shift );
404
 
                        glyph_col >>= 1;
405
 
                }
406
 
}
407
 
 
408
 
 
409
 
void TextRenderer::reset_buffer()
410
 
{
411
 
        _output_buffer = 0;
412
 
        _need_render = false;
413
 
}
414
 
 
415
 
 
416
 
void TextRenderer::buffer_in_use()
417
 
{
418
 
        _need_render = true;
419
 
}
420
 
 
421
 
 
422
 
void TextRenderer::output_buffer()
423
 
{
424
 
        if( !_need_render ) return;
425
 
 
426
 
        // outer led on
427
 
        led( 9, true );
428
 
 
429
 
        // output buffer
430
 
        for( int a = 8; a >= 0; a-- ) {
431
 
                led( a, ( _output_buffer & 1 )? true : false );
432
 
                _output_buffer >>= 1;
433
 
        }
434
 
        _output_buffer = 0;
435
 
}
436
 
 
437
 
 
438
 
int TextRenderer::get_font()
439
 
{
440
 
        return _font;
441
 
}
442
 
 
443
 
 
444
 
void TextRenderer::inc_font()
445
 
{
446
 
        if( ++_font >= 4 )
447
 
                _font = 0;
448
 
        Nvram::save( Nvram::NVRAM_FONT, _font );
 
432
                render( (long)segment + _scroll );
449
433
}