/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-10 13:04:29 UTC
  • Revision ID: tim@ed.am-20120310130429-310w5ejo968mc6mo
cleaned-up notes

Show diffs side-by-side

added added

removed removed

25
25
#include "Arduino.h"
26
26
#include <avr/pgmspace.h>
27
27
#include "common.h"
28
 
#include "nvram.h"
29
28
 
30
29
 
31
30
// cached glyph
40
39
// output buffer
41
40
static unsigned char _output_buffer;
42
41
 
43
 
// does buffer need rendering?
44
 
static bool _need_render;
45
 
 
46
42
 
47
43
// cache a glyph
48
44
void cache_glyph( char c )
49
45
{
50
 
        static char fonts[][ 67 * 8 ] PROGMEM = {
 
46
        static char fonts[][ 66 * 8 ] PROGMEM = {
51
47
                { // INVD-A
52
48
                        0x00, 0x7c, 0x7e, 0x12, 0x12, 0x12, 0x7e, 0x7c, // A
53
49
                        0x00, 0x7e, 0x7e, 0x4a, 0x4a, 0x4a, 0x7e, 0x34, // B
115
111
                        0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x00, 0x00, // :
116
112
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
117
113
                        0x00, 0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, // ,
118
 
                        0x00, 0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x00, // /
119
114
                }, { // INVD-D
120
115
                        0x00, 0x7f, 0x7f, 0x09, 0x09, 0x09, 0x7f, 0x7f, // A
121
116
                        0x00, 0x7f, 0x7f, 0x49, 0x49, 0x49, 0x7f, 0x77, // B
183
178
                        0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, // :
184
179
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
185
180
                        0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, 0x00, // ,
186
 
                        0x00, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x00, // /
187
181
                }, { // INVD-G
188
182
                        0x7c, 0x7e, 0x22, 0x7e, 0x7e, 0x7e, 0x7c, 0x00, // A
189
183
                        0x7c, 0x7e, 0x7e, 0x7e, 0x4a, 0x7e, 0x34, 0x00, // B
251
245
                        0x00, 0x00, 0x28, 0x7c, 0x7c, 0x28, 0x00, 0x00, // :
252
246
                        0x00, 0x00, 0x30, 0x78, 0x78, 0x30, 0x00, 0x00, // .
253
247
                        0x00, 0x00, 0x10, 0xb8, 0xf8, 0x70, 0x00, 0x00, // ,
254
 
                        0x60, 0x70, 0x38, 0x1c, 0x0e, 0x07, 0x03, 0x00, // /
255
248
                }, { // INVD-B
256
249
                        0x00, 0x78, 0x7f, 0x09, 0x09, 0x0f, 0x78, 0x00, // A
257
250
                        0x00, 0x7f, 0x79, 0x49, 0x49, 0x4f, 0x78, 0x00, // B
319
312
                        0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x00, 0x00, // :
320
313
                        0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
321
314
                        0x00, 0x00, 0x00, 0x60, 0xe0, 0x00, 0x00, 0x00, // ,
322
 
                        0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, // /
323
315
                }
324
316
        };
325
317
 
344
336
                pos = 64;
345
337
        else if( c == ',' )
346
338
                pos = 65;
347
 
        else if( c == '/' )
348
 
                pos = 66;
349
339
        else
350
340
                pos = -1;
351
341
 
357
347
}
358
348
 
359
349
 
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;
 
350
int TextRenderer::get_width( int message_len )
 
351
{
 
352
        return message_len * 8 * TEXT_SCALE;
370
353
}
371
354
 
372
355
 
373
356
void TextRenderer::render( const char *message, int message_len, int x,
374
 
                                                   bool y_flip, int y_shift, int scale )
 
357
                                                   bool y_flip, int y_shift )
375
358
{
376
359
        char glyph_col = 0;
377
360
 
379
362
        if( x >= 0 )
380
363
        {
381
364
                // scale font
382
 
                x /= scale;
 
365
                x /= TEXT_SCALE;
383
366
 
384
367
                int pos = x / 8;
385
368
                if( pos < message_len )
386
369
                {
387
 
                        // ensure correct glyph is cached and pull out column of data
 
370
                        // ensure correct glyph is cached and pull out colun of data
388
371
                        cache_glyph( message[ pos ] );
389
372
                        glyph_col = _glyph_cache[ x % 8 ];
390
373
                }
409
392
void TextRenderer::reset_buffer()
410
393
{
411
394
        _output_buffer = 0;
412
 
        _need_render = false;
413
 
}
414
 
 
415
 
 
416
 
void TextRenderer::buffer_in_use()
417
 
{
418
 
        _need_render = true;
419
395
}
420
396
 
421
397
 
422
398
void TextRenderer::output_buffer()
423
399
{
424
 
        if( !_need_render ) return;
425
 
 
426
 
        // outer led on
427
 
        led( 9, true );
428
 
 
429
 
        // output buffer
430
400
        for( int a = 8; a >= 0; a-- ) {
431
401
                led( a, ( _output_buffer & 1 )? true : false );
432
402
                _output_buffer >>= 1;
435
405
}
436
406
 
437
407
 
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 );
 
408
void TextRenderer::select_font( int font_num )
 
409
{
 
410
        _font = font_num;
449
411
}