24
24
#include "config.h"
25
25
#include "Arduino.h"
26
26
#include <avr/pgmspace.h>
32
static char _glyph_cache[ 8 ];
34
// the character the current cached glyph is for
35
static char _glyph_cache_char;
41
static unsigned char _output_buffer;
43
// does buffer need rendering?
44
static bool _need_render;
48
void cache_glyph( char c )
30
TextRenderer &TextRenderer::get_instance()
32
static TextRenderer text_renderer;
37
PString &TextRenderer::get_pstring()
41
_last_millis = ::millis();
42
_scroll = -TEXT_DISPLAY_SEGMENTS;
48
void TextRenderer::draw_reset()
50
// how many milliseconds have elapsed since last frame?
51
unsigned long millis = ::millis();
52
if( millis > _last_millis )
53
_duration = millis - _last_millis;
54
_last_millis = millis;
57
_scroll += ( _duration * TEXT_SCROLL_SPEED / 1000 );
58
if( _scroll > (signed long)_message.length() * 8 * TEXT_SCALE )
59
_scroll -= (signed long)_message.length() * 8 * TEXT_SCALE + TEXT_DISPLAY_SEGMENTS;
63
void TextRenderer::draw_scroll( int segment )
65
segment = xform_segment( segment );
69
render( (long)segment + _scroll );
73
TextRenderer::TextRenderer()
75
_message( _message_buffer, sizeof( _message_buffer ) ),
76
_glyph_cache_char( '\0' ),
82
int TextRenderer::xform_segment( int segment )
84
if( segment < TEXT_DISPLAY_SEGMENTS / 2 )
85
return segment + TEXT_DISPLAY_SEGMENTS / 2;
86
else if( segment >= NUM_SEGMENTS - TEXT_DISPLAY_SEGMENTS / 2 )
87
return segment - ( NUM_SEGMENTS - TEXT_DISPLAY_SEGMENTS / 2 );
93
void TextRenderer::render( int x )
106
char c = ( pos >= 0 && pos < (signed)_message.length() )? _message[ pos ] : ' ';
107
if( c != _glyph_cache_char ) {
108
_glyph_cache_char = c;
109
get_glyph( c, _glyph_cache );
113
char col_data = _glyph_cache[ x % 8 ];
114
for( int a = 0; a < 8; a++ )
115
Display::led( 8 - a, ( col_data & ( 1 << a ) )? true : false );
119
void TextRenderer::get_glyph( char c, char *glyph )
50
121
static char fonts[][ 66 * 8 ] PROGMEM = {
346
// space? or copy glyph from progmem?
348
memset( _glyph_cache, 0, 8 );
350
memcpy_P( _glyph_cache, &( fonts[ _font ][ pos * 8 ] ), 8 );
354
void TextRenderer::init()
356
Nvram::load( Nvram::NVRAM_FONT, _font );
357
if( _font < 0 || _font >= 4 ) _font = 0;
361
int TextRenderer::get_width( int message_len )
363
return message_len * 8 * TEXT_SCALE;
367
void TextRenderer::render( const char *message, int message_len, int x,
368
bool y_flip, int y_shift )
379
if( pos < message_len )
381
// ensure correct glyph is cached and pull out column of data
382
cache_glyph( message[ pos ] );
383
glyph_col = _glyph_cache[ x % 8 ];
389
for( int a = 7; a >= 0; a-- ) {
391
_output_buffer |= 1 << ( a + y_shift );
395
for( int a = 0; a < 8; a++ ) {
397
_output_buffer |= 1 << ( a + y_shift );
403
void TextRenderer::reset_buffer()
406
_need_render = false;
410
void TextRenderer::buffer_in_use()
416
void TextRenderer::output_buffer()
418
if( !_need_render ) return;
420
for( int a = 8; a >= 0; a-- ) {
421
led( a, ( _output_buffer & 1 )? true : false );
422
_output_buffer >>= 1;
428
int TextRenderer::get_font()
434
void TextRenderer::inc_font()
438
Nvram::save( Nvram::NVRAM_FONT, _font );
412
memset( glyph, 0, 8 );
414
memcpy_P( glyph, &( fonts[ _font ][ pos * 8 ] ), 8 );