improve font handling

* macros established to build a table of fonts which get compiled into the image
* font data is mapped to irom0 to save dram space
* quirky function to retrieve font data 4-byte aligned from irom0
This commit is contained in:
devsaurus 2015-02-04 00:08:39 +01:00
parent 12a546ce3d
commit 9f0b167832
5 changed files with 50 additions and 17 deletions

View File

@ -89,4 +89,13 @@
#define LED_HIGH_COUNT_DEFAULT 10 #define LED_HIGH_COUNT_DEFAULT 10
#define LED_LOW_COUNT_DEFAULT 0 #define LED_LOW_COUNT_DEFAULT 0
// Configure U8glib fonts
// add a U8G_FONT_TABLE_ENTRY for each font you want to compile into the image
#define U8G_FONT_TABLE_ENTRY(font)
#define U8G_FONT_TABLE \
U8G_FONT_TABLE_ENTRY(font_6x10) \
U8G_FONT_TABLE_ENTRY(font_chikita)
#undef U8G_FONT_TABLE_ENTRY
#endif /* __USER_CONFIG_H__ */ #endif /* __USER_CONFIG_H__ */

View File

@ -20,14 +20,30 @@ typedef struct lu8g_userdata
// Font look-up array // Font look-up array
#define LU8G_FONT_6X10 0 static const u8g_fntpgm_uint8_t *font_array[] =
const static u8g_fntpgm_uint8_t *font_array[] =
{ {
u8g_font_6x10 #undef U8G_FONT_TABLE_ENTRY
#define U8G_FONT_TABLE_ENTRY(font) u8g_ ## font ,
U8G_FONT_TABLE
NULL
}; };
// function to read 4-byte aligned from program memory AKA irom0
u8g_pgm_uint8_t u8g_pgm_read(const u8g_pgm_uint8_t *adr)
{
uint32_t iadr = (uint32_t)adr;
// set up pointer to 4-byte aligned memory location
uint32_t *ptr = (uint32_t *)(iadr & ~0x3);
// read 4-byte aligned
uint32_t pgm_data = *ptr;
// return the correct byte within the retrieved 32bit word
return pgm_data >> ((iadr % 4) * 8);
}
// helper function: retrieve and check userdata argument // helper function: retrieve and check userdata argument
static lu8g_userdata_t *get_lud( lua_State *L ) static lu8g_userdata_t *get_lud( lua_State *L )
{ {
@ -53,12 +69,10 @@ static int lu8g_setFont( lua_State *L )
if ((lud = get_lud( L )) == NULL) if ((lud = get_lud( L )) == NULL)
return 0; return 0;
uint8_t stack = 2;
unsigned fontnr = luaL_checkinteger( L, stack ); lua_Integer fontnr = luaL_checkinteger( L, 2 );
stack++; if ((fontnr >= 0) && (fontnr < (sizeof( font_array ) / sizeof( u8g_fntpgm_uint8_t ))))
u8g_SetFont( &(lud->u8g), font_array[fontnr] );
u8g_SetFont( &(lud->u8g), font_array[fontnr] );
return 0; return 0;
} }
@ -520,7 +534,13 @@ const LUA_REG_TYPE lu8g_map[] =
{ {
{ LSTRKEY( "ssd1306_128x64_i2c" ), LFUNCVAL ( lu8g_ssd1306_128x64_i2c ) }, { LSTRKEY( "ssd1306_128x64_i2c" ), LFUNCVAL ( lu8g_ssd1306_128x64_i2c ) },
#if LUA_OPTIMIZE_MEMORY > 0 #if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "font_6x10" ), LNUMVAL( LU8G_FONT_6X10 ) }, //{ LSTRKEY( "font_6x10" ), LNUMVAL( LU8G_FONT_6X10 ) },
// Register fonts
#undef U8G_FONT_TABLE_ENTRY
#define U8G_FONT_TABLE_ENTRY(font) { LSTRKEY( #font ), LNUMVAL( __COUNTER__ ) },
U8G_FONT_TABLE
{ LSTRKEY( "__metatable" ), LROVAL( lu8g_map ) }, { LSTRKEY( "__metatable" ), LROVAL( lu8g_map ) },
#endif #endif
{ LNILKEY, LNILVAL } { LNILKEY, LNILVAL }
@ -540,8 +560,11 @@ LUALIB_API int ICACHE_FLASH_ATTR luaopen_u8g( lua_State *L )
lua_setmetatable( L, -2 ); lua_setmetatable( L, -2 );
// Module constants // Module constants
// Register fonts // Register fonts
MOD_REG_NUMBER( L, "font_6x10", LU8G_FONT_6X10 ); #undef U8G_FONT_TABLE_ENTRY
#define U8G_FONT_TABLE_ENTRY(font) MOD_REG_NUMBER( L, #font, __COUNTER__ );
U8G_FONT_TABLE
// create metatable // create metatable
luaL_newmetatable(L, "u8g.display"); luaL_newmetatable(L, "u8g.display");

View File

@ -93,7 +93,7 @@ extern "C" {
#endif #endif
#ifndef U8G_FONT_SECTION #ifndef U8G_FONT_SECTION
# define U8G_FONT_SECTION(name) # define U8G_FONT_SECTION(name) U8G_SECTION(".font_data." name)
#endif #endif
@ -114,7 +114,8 @@ typedef uint8_t u8g_fntpgm_uint8_t;
#define PROGMEM #define PROGMEM
typedef uint8_t u8g_pgm_uint8_t; typedef uint8_t u8g_pgm_uint8_t;
typedef uint8_t u8g_fntpgm_uint8_t; typedef uint8_t u8g_fntpgm_uint8_t;
#define u8g_pgm_read(adr) (*(const u8g_pgm_uint8_t *)(adr)) //#define u8g_pgm_read(adr) (*(const u8g_pgm_uint8_t *)(adr))
u8g_pgm_uint8_t u8g_pgm_read(const u8g_pgm_uint8_t *adr);
#define U8G_PSTR(s) ((u8g_pgm_uint8_t *)(s)) #define U8G_PSTR(s) ((u8g_pgm_uint8_t *)(s))
#endif #endif

View File

@ -1,4 +1,3 @@
#if 0
/* /*
Fontname: -FreeType-04b03b-Medium-R-Normal--8-80-72-72-P-39-ISO10646-1 Fontname: -FreeType-04b03b-Medium-R-Normal--8-80-72-72-P-39-ISO10646-1
Copyright: 19992003 / yuji oshimoÿo / 04@dsg4.com / www.04.jp.org Copyright: 19992003 / yuji oshimoÿo / 04@dsg4.com / www.04.jp.org
@ -1861,7 +1860,6 @@ const u8g_fntpgm_uint8_t u8g_font_5x8r[805] U8G_FONT_SECTION("u8g_font_5x8r") =
64,240,2,71,87,48,64,32,192,32,64,48,34,22,86,128, 64,240,2,71,87,48,64,32,192,32,64,48,34,22,86,128,
128,128,128,128,128,2,71,87,192,32,64,48,64,32,192,6, 128,128,128,128,128,2,71,87,192,32,64,48,64,32,192,6,
66,82,80,160,255}; 66,82,80,160,255};
#endif
/* /*
Fontname: -Misc-Fixed-Medium-R-Normal--10-100-75-75-C-60-ISO10646-1 Fontname: -Misc-Fixed-Medium-R-Normal--10-100-75-75-C-60-ISO10646-1
Copyright: Public domain terminal emulator font. Share and enjoy. Copyright: Public domain terminal emulator font. Share and enjoy.
@ -1992,7 +1990,6 @@ const u8g_fntpgm_uint8_t u8g_font_6x10[1866] U8G_FONT_SECTION("u8g_font_6x10") =
80,0,136,136,136,152,104,0,89,105,16,32,136,136,152,104, 80,0,136,136,136,152,104,0,89,105,16,32,136,136,152,104,
8,136,112,0,88,104,128,240,136,136,136,240,128,128,0,89, 8,136,112,0,88,104,128,240,136,136,136,240,128,128,0,89,
105,80,0,136,136,152,104,8,136,112}; 105,80,0,136,136,152,104,8,136,112};
#if 0
/* /*
Fontname: -Misc-Fixed-Medium-R-Normal--10-100-75-75-C-60-ISO10646-1 Fontname: -Misc-Fixed-Medium-R-Normal--10-100-75-75-C-60-ISO10646-1
Copyright: Public domain terminal emulator font. Share and enjoy. Copyright: Public domain terminal emulator font. Share and enjoy.
@ -88465,4 +88462,3 @@ const u8g_fntpgm_uint8_t u8g_font_unifontr[1485] U8G_FONT_SECTION("u8g_font_unif
3,8,1,8,98,146,140,16,16,32,16,0,254,170,170,0, 3,8,1,8,98,146,140,16,16,32,16,0,254,170,170,0,
1,128,0,0,1,128,0,115,209,202,16,75,209,202,16,115, 1,128,0,0,1,128,0,115,209,202,16,75,209,202,16,115,
223,128,0,0,1,128,0,0,1,128,0,85,85}; 223,128,0,0,1,128,0,0,1,128,0,85,85};
#endif

View File

@ -72,6 +72,10 @@ SECTIONS
*(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text) *(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text)
*(.literal.* .text.*) *(.literal.* .text.*)
*(.rodata2.text) *(.rodata2.text)
/* put font data into irom0 */
*(.font_data.*)
_irom0_text_end = ABSOLUTE(.); _irom0_text_end = ABSOLUTE(.);
_flash_used_end = ABSOLUTE(.); _flash_used_end = ABSOLUTE(.);
} >irom0_0_seg :irom0_0_phdr } >irom0_0_seg :irom0_0_phdr