/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 16:50:26 UTC
  • Revision ID: edam@waxworlds.org-20120228165026-pwnwo300xx2e2kg6
removed ulibc, fixed button, added text rendering

Show diffs side-by-side

added added

removed removed

27
27
#include "display.h"
28
28
 
29
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 ) );
38
 
 
39
 
// cached glyph
40
 
static char _glyph_cache[ 8 ];
41
 
 
42
 
// the character the current cached glyph is for
43
 
static char _glyph_cache_char;
44
 
 
45
 
// selected font
46
 
static int _font = 0;
47
 
 
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;
56
 
 
57
 
 
58
 
// cache a glyph
59
 
void cache_glyph( char c )
 
30
TextRenderer &TextRenderer::get_instance()
 
31
{
 
32
        static TextRenderer text_renderer;
 
33
        return text_renderer;
 
34
}
 
35
 
 
36
 
 
37
PString &TextRenderer::get_pstring()
 
38
{
 
39
        // reset renderer
 
40
        _duration = 0;
 
41
        _last_millis = ::millis();
 
42
        _scroll = -TEXT_DISPLAY_SEGMENTS;
 
43
 
 
44
        return _message;
 
45
}
 
46
 
 
47
 
 
48
void TextRenderer::draw_reset()
 
49
{
 
50
        // how many milliseconds have elapsed since last frame?
 
51
        unsigned long millis = ::millis();
 
52
        if( millis > _last_millis )
 
53
                _duration = millis - _last_millis;
 
54
        _last_millis = millis;
 
55
 
 
56
        // move on scroll pos
 
57
        _scroll += ( _duration * TEXT_SCROLL_SPEED / 1000 );
 
58
        if( _scroll > (signed long)_message.length() * 8 * TEXT_SCALE )
 
59
                _scroll -= (signed long)_message.length() * 8 * TEXT_SCALE + TEXT_DISPLAY_SEGMENTS;
 
60
}
 
61
 
 
62
 
 
63
void TextRenderer::draw_scroll( int segment )
 
64
{
 
65
        segment = xform_segment( segment );
 
66
        if( segment < 0 )
 
67
                Display::leds_off();
 
68
        else
 
69
                render( (long)segment + _scroll );
 
70
}
 
71
 
 
72
 
 
73
TextRenderer::TextRenderer()
 
74
    :
 
75
        _message( _message_buffer, sizeof( _message_buffer ) ),
 
76
        _glyph_cache_char( '\0' ),
 
77
        _font( 0 )
 
78
{
 
79
}
 
80
 
 
81
 
 
82
int TextRenderer::xform_segment( int segment )
 
83
{
 
84
        if( segment < TEXT_DISPLAY_SEGMENTS / 2 )
 
85
                return segment + TEXT_DISPLAY_SEGMENTS / 2;
 
86
        else if( segment >= NUM_SEGMENTS - TEXT_DISPLAY_SEGMENTS / 2 )
 
87
                return segment - ( NUM_SEGMENTS - TEXT_DISPLAY_SEGMENTS / 2 );
 
88
        else
 
89
                return - 1;
 
90
}
 
91
 
 
92
 
 
93
void TextRenderer::render( int x )
 
94
{
 
95
        // handle negative x
 
96
        if( x < 0 ) {
 
97
                Display::leds_off();
 
98
                return;
 
99
        }
 
100
 
 
101
        // scale font
 
102
        x /= TEXT_SCALE;
 
103
 
 
104
        // fetch a glyph
 
105
        int pos = x / 8;
 
106
        char c = ( pos >= 0 && pos < (signed)_message.length() )? _message[ pos ] : ' ';
 
107
        if( c != _glyph_cache_char ) {
 
108
                _glyph_cache_char = c;
 
109
                get_glyph( c, _glyph_cache );
 
110
        }
 
111
 
 
112
        // draw a column
 
113
        char col_data = _glyph_cache[ x % 8 ];
 
114
        for( int a = 0; a < 8; a++ )
 
115
                Display::led( 8 - a, ( col_data & ( 1 << a ) )? true : false );
 
116
}
 
117
 
 
118
 
 
119
void TextRenderer::get_glyph( char c, char *glyph )
60
120
{
61
121
        static char fonts[][ 66 * 8 ] PROGMEM = {
62
122
                { // INVD-A
330
390
                }
331
391
        };
332
392
 
333
 
        // nothing to do?
334
 
        if( c == _glyph_cache_char )
335
 
                return;
336
 
        _glyph_cache_char = c;
337
 
 
338
 
        // find glyph data position
339
393
        int pos;
340
394
        if( c >= 'A' && c <= 'Z' )
341
395
                pos = c - 'A';
354
408
        else
355
409
                pos = -1;
356
410
 
357
 
        // space? or copy glyph from progmem?
358
411
        if( pos == -1 )
359
 
                memset( _glyph_cache, 0, 8 );
360
 
        else
361
 
                memcpy_P( _glyph_cache, &( fonts[ _font ][ pos * 8 ] ), 8 );
362
 
}
363
 
 
364
 
 
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
 
{
380
 
        // handle negative x
381
 
        if( x < 0 ) {
382
 
                Display::leds_off();
383
 
                return;
384
 
        }
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
 
        // draw a column
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();
431
 
        else
432
 
                render( (long)segment + _scroll );
 
412
                memset( glyph, 0, 8 );
 
413
        else
 
414
                memcpy_P( glyph, &( fonts[ _font ][ pos * 8 ] ), 8 );
433
415
}