/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: 2013-03-31 17:07:36 UTC
  • Revision ID: tim@ed.am-20130331170736-hphm2hg0y6l7w6z1
made rtc-test's DS1307 library a symlink to the main one in src/util

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
#include "nvram.h"
 
29
 
38
30
 
39
31
// cached glyph
40
32
static char _glyph_cache[ 8 ];
45
37
// selected font
46
38
static int _font = 0;
47
39
 
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;
 
40
// output buffer
 
41
static unsigned char _output_buffer;
 
42
 
 
43
// does buffer need rendering?
 
44
static bool _need_render;
56
45
 
57
46
 
58
47
// cache a glyph
59
48
void cache_glyph( char c )
60
49
{
61
 
        static char fonts[][ 66 * 8 ] PROGMEM = {
 
50
        static char fonts[][ 67 * 8 ] PROGMEM = {
62
51
                { // INVD-A
63
52
                        0x00, 0x7c, 0x7e, 0x12, 0x12, 0x12, 0x7e, 0x7c, // A
64
53
                        0x00, 0x7e, 0x7e, 0x4a, 0x4a, 0x4a, 0x7e, 0x34, // B
126
115
                        0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x00, 0x00, // :
127
116
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
128
117
                        0x00, 0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, // ,
 
118
                        0x00, 0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x00, // /
129
119
                }, { // INVD-D
130
120
                        0x00, 0x7f, 0x7f, 0x09, 0x09, 0x09, 0x7f, 0x7f, // A
131
121
                        0x00, 0x7f, 0x7f, 0x49, 0x49, 0x49, 0x7f, 0x77, // B
193
183
                        0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, // :
194
184
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
195
185
                        0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, 0x00, // ,
 
186
                        0x00, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x00, // /
196
187
                }, { // INVD-G
197
188
                        0x7c, 0x7e, 0x22, 0x7e, 0x7e, 0x7e, 0x7c, 0x00, // A
198
189
                        0x7c, 0x7e, 0x7e, 0x7e, 0x4a, 0x7e, 0x34, 0x00, // B
260
251
                        0x00, 0x00, 0x28, 0x7c, 0x7c, 0x28, 0x00, 0x00, // :
261
252
                        0x00, 0x00, 0x30, 0x78, 0x78, 0x30, 0x00, 0x00, // .
262
253
                        0x00, 0x00, 0x10, 0xb8, 0xf8, 0x70, 0x00, 0x00, // ,
 
254
                        0x60, 0x70, 0x38, 0x1c, 0x0e, 0x07, 0x03, 0x00, // /
263
255
                }, { // INVD-B
264
256
                        0x00, 0x78, 0x7f, 0x09, 0x09, 0x0f, 0x78, 0x00, // A
265
257
                        0x00, 0x7f, 0x79, 0x49, 0x49, 0x4f, 0x78, 0x00, // B
327
319
                        0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x00, 0x00, // :
328
320
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
329
321
                        0x00, 0x00, 0x00, 0x60, 0xe0, 0x00, 0x00, 0x00, // ,
 
322
                        0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, // /
330
323
                }
331
324
        };
332
325
 
351
344
                pos = 64;
352
345
        else if( c == ',' )
353
346
                pos = 65;
 
347
        else if( c == '/' )
 
348
                pos = 66;
354
349
        else
355
350
                pos = -1;
356
351
 
362
357
}
363
358
 
364
359
 
365
 
// convert segment to display-space segment
366
 
int xform_segment( int segment )
367
 
{
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;
374
 
}
375
 
 
376
 
 
377
 
// draw a column of pixels from the current message
378
 
void render( int x )
379
 
{
 
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
 
380
378
        // handle negative x
381
 
        if( x < 0 ) {
382
 
                Display::leds_off();
383
 
                return;
 
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
                }
384
391
        }
385
392
 
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
393
        // 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();
 
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
                }
431
400
        else
432
 
                render( (long)segment + _scroll );
 
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
        // outer led on
 
427
        led( 9, true );
 
428
 
 
429
        // output buffer
 
430
        for( int a = 8; a >= 0; a-- ) {
 
431
                led( a, ( _output_buffer & 1 )? true : false );
 
432
                _output_buffer >>= 1;
 
433
        }
 
434
        _output_buffer = 0;
 
435
}
 
436
 
 
437
 
 
438
int TextRenderer::get_font()
 
439
{
 
440
        return _font;
 
441
}
 
442
 
 
443
 
 
444
void TextRenderer::inc_font()
 
445
{
 
446
        if( ++_font >= 4 )
 
447
                _font = 0;
 
448
        Nvram::save( Nvram::NVRAM_FONT, _font );
433
449
}