/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: Tim Marston
  • Date: 2012-03-21 19:37:33 UTC
  • Revision ID: tim@ed.am-20120321193733-29euxt0t0h9dwsj3
added .dep directories to bzrignore

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 "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 ) );
 
27
#include "common.h"
 
28
 
38
29
 
39
30
// cached glyph
40
31
static char _glyph_cache[ 8 ];
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;
 
41
 
 
42
// does buffer need rendering?
 
43
static bool _need_render;
56
44
 
57
45
 
58
46
// cache a glyph
362
350
}
363
351
 
364
352
 
365
 
// convert segment to display-space segment
366
 
int xform_segment( int segment )
 
353
int TextRenderer::get_width( int message_len )
367
354
{
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;
 
355
        return message_len * 8 * TEXT_SCALE;
374
356
}
375
357
 
376
358
 
377
 
// draw a column of pixels from the current message
378
 
void render( int x )
 
359
void TextRenderer::render( const char *message, int message_len, int x,
 
360
                                                   bool y_flip, int y_shift )
379
361
{
 
362
        char glyph_col = 0;
 
363
 
380
364
        // handle negative x
381
 
        if( x < 0 ) {
382
 
                Display::leds_off();
383
 
                return;
 
365
        if( x >= 0 )
 
366
        {
 
367
                // scale font
 
368
                x /= TEXT_SCALE;
 
369
 
 
370
                int pos = x / 8;
 
371
                if( pos < message_len )
 
372
                {
 
373
                        // ensure correct glyph is cached and pull out column of data
 
374
                        cache_glyph( message[ pos ] );
 
375
                        glyph_col = _glyph_cache[ x % 8 ];
 
376
                }
384
377
        }
385
378
 
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
379
        // 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();
 
380
        if( y_flip )
 
381
                for( int a = 7; a >= 0; a-- ) {
 
382
                        if( glyph_col & 1 )
 
383
                                _output_buffer |= 1 << ( a + y_shift );
 
384
                        glyph_col >>= 1;
 
385
                }
431
386
        else
432
 
                render( (long)segment + _scroll );
 
387
                for( int a = 0; a < 8; a++ ) {
 
388
                        if( glyph_col & 1 )
 
389
                                _output_buffer |= 1 << ( a + y_shift );
 
390
                        glyph_col >>= 1;
 
391
                }
 
392
}
 
393
 
 
394
 
 
395
void TextRenderer::reset_buffer()
 
396
{
 
397
        _output_buffer = 0;
 
398
        _need_render = false;
 
399
}
 
400
 
 
401
 
 
402
void TextRenderer::buffer_in_use()
 
403
{
 
404
        _need_render = true;
 
405
}
 
406
 
 
407
 
 
408
void TextRenderer::output_buffer()
 
409
{
 
410
        if( !_need_render ) return;
 
411
 
 
412
        for( int a = 8; a >= 0; a-- ) {
 
413
                led( a, ( _output_buffer & 1 )? true : false );
 
414
                _output_buffer >>= 1;
 
415
        }
 
416
        _output_buffer = 0;
 
417
}
 
418
 
 
419
 
 
420
int TextRenderer::get_font()
 
421
{
 
422
        return _font;
 
423
}
 
424
 
 
425
 
 
426
void TextRenderer::inc_font()
 
427
{
 
428
        if( ++_font >= 4 )
 
429
                _font = 0;
433
430
}