/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-05-17 22:48:38 UTC
  • Revision ID: tim@ed.am-20120517224838-25hjd6umh35vmtwn
updated arduino.mk

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