/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
 
 
 
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 ) );
29
38
 
30
39
// cached glyph
31
40
static char _glyph_cache[ 8 ];
36
45
// selected font
37
46
static int _font = 0;
38
47
 
39
 
// output buffer
40
 
static unsigned char _output_buffer;
 
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;
41
56
 
42
57
 
43
58
// cache a glyph
347
362
}
348
363
 
349
364
 
350
 
int TextRenderer::get_width( int message_len )
 
365
// convert segment to display-space segment
 
366
int xform_segment( int segment )
351
367
{
352
 
        return message_len * 8 * TEXT_SCALE;
 
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;
353
374
}
354
375
 
355
376
 
356
 
void TextRenderer::render( const char *message, int message_len, int x,
357
 
                                                   bool y_flip, int y_shift )
 
377
// draw a column of pixels from the current message
 
378
void render( int x )
358
379
{
359
 
        char glyph_col = 0;
360
 
 
361
380
        // handle negative x
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
 
                }
 
381
        if( x < 0 ) {
 
382
                Display::leds_off();
 
383
                return;
374
384
        }
375
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
 
376
393
        // draw a column
377
 
        if( y_flip )
378
 
                for( int a = 7; a >= 0; a-- ) {
379
 
                        if( glyph_col & 1 )
380
 
                                _output_buffer |= 1 << ( a + y_shift );
381
 
                        glyph_col >>= 1;
382
 
                }
 
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();
383
431
        else
384
 
                for( int a = 0; a < 8; 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
 
                led( a, ( _output_buffer & 1 )? true : false );
402
 
                _output_buffer >>= 1;
403
 
        }
404
 
        _output_buffer = 0;
405
 
}
406
 
 
407
 
 
408
 
void TextRenderer::select_font( int font_num )
409
 
{
410
 
        _font = font_num;
 
432
                render( (long)segment + _scroll );
411
433
}