/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-29 21:56:32 UTC
  • Revision ID: edam@waxworlds.org-20120229215632-kypb9491vx7bicef
moved some stuf round, created a re-usable pool of message buffers, genericised "modes" for messages

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
30
// cached glyph
40
31
static char _glyph_cache[ 8 ];
41
32
 
45
36
// selected font
46
37
static int _font = 0;
47
38
 
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;
 
39
// output buffer
 
40
static unsigned char _output_buffer;
56
41
 
57
42
 
58
43
// cache a glyph
362
347
}
363
348
 
364
349
 
365
 
// convert segment to display-space segment
366
 
int xform_segment( int segment )
 
350
int TextRenderer::get_width( int message_len )
367
351
{
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;
 
352
        return message_len * 8 * TEXT_SCALE;
374
353
}
375
354
 
376
355
 
377
 
// draw a column of pixels from the current message
378
 
void render( int x )
 
356
void TextRenderer::render( const char *message, int message_len, int x,
 
357
                                                   bool y_flip, int y_shift )
379
358
{
 
359
        char glyph_col = 0;
 
360
 
380
361
        // handle negative x
381
 
        if( x < 0 ) {
382
 
                Display::leds_off();
383
 
                return;
 
362
        if( x >= 0 )
 
363
        {
 
364
                // scale font
 
365
                x /= TEXT_SCALE;
 
366
 
 
367
                int pos = x / 8;
 
368
                if( pos < message_len )
 
369
                {
 
370
                        // ensure correct glyph is cached and pull out colun of data
 
371
                        cache_glyph( message[ pos ] );
 
372
                        glyph_col = _glyph_cache[ x % 8 ];
 
373
                }
384
374
        }
385
375
 
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
376
        // 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();
 
377
        if( y_flip )
 
378
                for( int a = 0; a < 8; a++ ) {
 
379
                        if( glyph_col & 1 )
 
380
                                _output_buffer |= 1 << ( a + y_shift );
 
381
                        glyph_col >>= 1;
 
382
                }
431
383
        else
432
 
                render( (long)segment + _scroll );
 
384
                for( int a = 8; a < 0; a++ ) {
 
385
                        if( glyph_col & 1 )
 
386
                                _output_buffer |= 1 << ( a + y_shift );
 
387
                        glyph_col >>= 1;
 
388
                }
 
389
}
 
390
 
 
391
 
 
392
void TextRenderer::reset_buffer()
 
393
{
 
394
        _output_buffer = 0;
 
395
}
 
396
 
 
397
 
 
398
void TextRenderer::output_buffer()
 
399
{
 
400
        for( int a = 8; a >= 0; a-- ) {
 
401
                Display::led( a, ( _output_buffer & 1 )? true : false );
 
402
                _output_buffer >>= 1;
 
403
        }
 
404
        _output_buffer = 0;
433
405
}