/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:11:15 UTC
  • Revision ID: edam@waxworlds.org-20120228171115-j3k91a3v71d3wnc1
cleanu

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;
41
 
 
42
 
// does buffer need rendering?
43
 
static bool _need_render;
 
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;
44
56
 
45
57
 
46
58
// cache a glyph
350
362
}
351
363
 
352
364
 
353
 
int TextRenderer::get_width( int message_len )
 
365
// convert segment to display-space segment
 
366
int xform_segment( int segment )
354
367
{
355
 
        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;
356
374
}
357
375
 
358
376
 
359
 
void TextRenderer::render( const char *message, int message_len, int x,
360
 
                                                   bool y_flip, int y_shift )
 
377
// draw a column of pixels from the current message
 
378
void render( int x )
361
379
{
362
 
        char glyph_col = 0;
363
 
 
364
380
        // handle negative x
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
 
                }
 
381
        if( x < 0 ) {
 
382
                Display::leds_off();
 
383
                return;
377
384
        }
378
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
 
379
393
        // draw a column
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
 
                }
 
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();
386
431
        else
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;
 
432
                render( (long)segment + _scroll );
430
433
}