/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

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
 
 
30
 
 
31
 
// cached glyph
32
 
static char _glyph_cache[ 8 ];
33
 
 
34
 
// the character the current cached glyph is for
35
 
static char _glyph_cache_char;
36
 
 
37
 
// selected font
38
 
static int _font = 0;
39
 
 
40
 
// output buffer
41
 
static unsigned char _output_buffer;
42
 
 
43
 
// does buffer need rendering?
44
 
static bool _need_render;
45
 
 
46
 
 
47
 
// cache a glyph
48
 
void cache_glyph( char c )
49
 
{
50
 
        static char fonts[][ 67 * 8 ] PROGMEM = {
 
27
#include "display.h"
 
28
 
 
29
 
 
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 )
 
120
{
 
121
        static char fonts[][ 66 * 8 ] PROGMEM = {
51
122
                { // INVD-A
52
123
                        0x00, 0x7c, 0x7e, 0x12, 0x12, 0x12, 0x7e, 0x7c, // A
53
124
                        0x00, 0x7e, 0x7e, 0x4a, 0x4a, 0x4a, 0x7e, 0x34, // B
115
186
                        0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x00, 0x00, // :
116
187
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
117
188
                        0x00, 0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, // ,
118
 
                        0x00, 0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x00, // /
119
189
                }, { // INVD-D
120
190
                        0x00, 0x7f, 0x7f, 0x09, 0x09, 0x09, 0x7f, 0x7f, // A
121
191
                        0x00, 0x7f, 0x7f, 0x49, 0x49, 0x49, 0x7f, 0x77, // B
183
253
                        0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, // :
184
254
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
185
255
                        0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, 0x00, // ,
186
 
                        0x00, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x00, // /
187
256
                }, { // INVD-G
188
257
                        0x7c, 0x7e, 0x22, 0x7e, 0x7e, 0x7e, 0x7c, 0x00, // A
189
258
                        0x7c, 0x7e, 0x7e, 0x7e, 0x4a, 0x7e, 0x34, 0x00, // B
251
320
                        0x00, 0x00, 0x28, 0x7c, 0x7c, 0x28, 0x00, 0x00, // :
252
321
                        0x00, 0x00, 0x30, 0x78, 0x78, 0x30, 0x00, 0x00, // .
253
322
                        0x00, 0x00, 0x10, 0xb8, 0xf8, 0x70, 0x00, 0x00, // ,
254
 
                        0x60, 0x70, 0x38, 0x1c, 0x0e, 0x07, 0x03, 0x00, // /
255
323
                }, { // INVD-B
256
324
                        0x00, 0x78, 0x7f, 0x09, 0x09, 0x0f, 0x78, 0x00, // A
257
325
                        0x00, 0x7f, 0x79, 0x49, 0x49, 0x4f, 0x78, 0x00, // B
319
387
                        0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x00, 0x00, // :
320
388
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
321
389
                        0x00, 0x00, 0x00, 0x60, 0xe0, 0x00, 0x00, 0x00, // ,
322
 
                        0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, // /
323
390
                }
324
391
        };
325
392
 
326
 
        // nothing to do?
327
 
        if( c == _glyph_cache_char )
328
 
                return;
329
 
        _glyph_cache_char = c;
330
 
 
331
 
        // find glyph data position
332
393
        int pos;
333
394
        if( c >= 'A' && c <= 'Z' )
334
395
                pos = c - 'A';
344
405
                pos = 64;
345
406
        else if( c == ',' )
346
407
                pos = 65;
347
 
        else if( c == '/' )
348
 
                pos = 66;
349
408
        else
350
409
                pos = -1;
351
410
 
352
 
        // space? or copy glyph from progmem?
353
411
        if( pos == -1 )
354
 
                memset( _glyph_cache, 0, 8 );
355
 
        else
356
 
                memcpy_P( _glyph_cache, &( fonts[ _font ][ pos * 8 ] ), 8 );
357
 
}
358
 
 
359
 
 
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
 
 
378
 
        // 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
 
                }
391
 
        }
392
 
 
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
 
                }
400
 
        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
 
        for( int a = 8; a >= 0; a-- ) {
427
 
                led( a, ( _output_buffer & 1 )? true : false );
428
 
                _output_buffer >>= 1;
429
 
        }
430
 
        _output_buffer = 0;
431
 
}
432
 
 
433
 
 
434
 
int TextRenderer::get_font()
435
 
{
436
 
        return _font;
437
 
}
438
 
 
439
 
 
440
 
void TextRenderer::inc_font()
441
 
{
442
 
        if( ++_font >= 4 )
443
 
                _font = 0;
444
 
        Nvram::save( Nvram::NVRAM_FONT, _font );
 
412
                memset( glyph, 0, 8 );
 
413
        else
 
414
                memcpy_P( glyph, &( fonts[ _font ][ pos * 8 ] ), 8 );
445
415
}