From 9f0b1678321668788bd46c98662ae938012dad7f Mon Sep 17 00:00:00 2001 From: devsaurus Date: Wed, 4 Feb 2015 00:08:39 +0100 Subject: [PATCH] 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 --- app/include/user_config.h | 9 ++++++++ app/modules/u8g.c | 45 ++++++++++++++++++++++++++++---------- app/u8glib/u8g.h | 5 +++-- app/u8glib/u8g_font_data.c | 4 ---- ld/eagle.app.v6.ld | 4 ++++ 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/app/include/user_config.h b/app/include/user_config.h index 00744eff..6e00e550 100644 --- a/app/include/user_config.h +++ b/app/include/user_config.h @@ -89,4 +89,13 @@ #define LED_HIGH_COUNT_DEFAULT 10 #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__ */ diff --git a/app/modules/u8g.c b/app/modules/u8g.c index a070f0e3..85333aae 100644 --- a/app/modules/u8g.c +++ b/app/modules/u8g.c @@ -20,14 +20,30 @@ typedef struct lu8g_userdata // Font look-up array -#define LU8G_FONT_6X10 0 - -const static u8g_fntpgm_uint8_t *font_array[] = +static const 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 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) return 0; - uint8_t stack = 2; - unsigned fontnr = luaL_checkinteger( L, stack ); - stack++; - - u8g_SetFont( &(lud->u8g), font_array[fontnr] ); + lua_Integer fontnr = luaL_checkinteger( L, 2 ); + if ((fontnr >= 0) && (fontnr < (sizeof( font_array ) / sizeof( u8g_fntpgm_uint8_t )))) + u8g_SetFont( &(lud->u8g), font_array[fontnr] ); return 0; } @@ -520,7 +534,13 @@ const LUA_REG_TYPE lu8g_map[] = { { LSTRKEY( "ssd1306_128x64_i2c" ), LFUNCVAL ( lu8g_ssd1306_128x64_i2c ) }, #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 ) }, #endif { LNILKEY, LNILVAL } @@ -540,8 +560,11 @@ LUALIB_API int ICACHE_FLASH_ATTR luaopen_u8g( lua_State *L ) lua_setmetatable( L, -2 ); // Module constants + // 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 luaL_newmetatable(L, "u8g.display"); diff --git a/app/u8glib/u8g.h b/app/u8glib/u8g.h index b7d928c6..7fdd7118 100644 --- a/app/u8glib/u8g.h +++ b/app/u8glib/u8g.h @@ -93,7 +93,7 @@ extern "C" { #endif #ifndef U8G_FONT_SECTION -# define U8G_FONT_SECTION(name) +# define U8G_FONT_SECTION(name) U8G_SECTION(".font_data." name) #endif @@ -114,7 +114,8 @@ typedef uint8_t u8g_fntpgm_uint8_t; #define PROGMEM typedef uint8_t u8g_pgm_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)) #endif diff --git a/app/u8glib/u8g_font_data.c b/app/u8glib/u8g_font_data.c index a1852f11..09108089 100644 --- a/app/u8glib/u8g_font_data.c +++ b/app/u8glib/u8g_font_data.c @@ -1,4 +1,3 @@ -#if 0 /* 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 @@ -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, 128,128,128,128,128,2,71,87,192,32,64,48,64,32,192,6, 66,82,80,160,255}; -#endif /* Fontname: -Misc-Fixed-Medium-R-Normal--10-100-75-75-C-60-ISO10646-1 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, 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}; -#if 0 /* Fontname: -Misc-Fixed-Medium-R-Normal--10-100-75-75-C-60-ISO10646-1 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, 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}; -#endif diff --git a/ld/eagle.app.v6.ld b/ld/eagle.app.v6.ld index 5495a179..ebdddd6e 100644 --- a/ld/eagle.app.v6.ld +++ b/ld/eagle.app.v6.ld @@ -72,6 +72,10 @@ SECTIONS *(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text) *(.literal.* .text.*) *(.rodata2.text) + + /* put font data into irom0 */ + *(.font_data.*) + _irom0_text_end = ABSOLUTE(.); _flash_used_end = ABSOLUTE(.); } >irom0_0_seg :irom0_0_phdr