From 30d3089cc82d08e69018badbf7bb27c6cc4b4332 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sat, 23 Jul 2011 14:30:20 +0200 Subject: [PATCH] Allow fonts from dataflash. use "setExtFont(filename)". for flash fonts use "setIntFont(&font)". --- firmware/applications/font.c | 49 ++++--- firmware/lcd/decoder.c | 34 +++-- firmware/lcd/fonts.h | 18 +-- firmware/lcd/render.c | 272 ++++++++++++++++++++++++++++++----- firmware/lcd/render.h | 17 +++ 5 files changed, 313 insertions(+), 77 deletions(-) diff --git a/firmware/applications/font.c b/firmware/applications/font.c index 9801653..a79b055 100644 --- a/firmware/applications/font.c +++ b/firmware/applications/font.c @@ -7,6 +7,7 @@ #include "lcd/allfonts.h" #include "filesystem/ff.h" +#include "filesystem/select.h" #include "funk/nrf24l01p.h" #include "usb/usbmsc.h" @@ -75,35 +76,22 @@ void f_init(void){ lcdPrintln("Done."); }; - -static FONT fonts[] = { - &Font_7x8, - &Font_Ubuntu18pt, - &Font_Ubuntu29pt, - &Font_Ubuntu36pt, - &Font_Orbitron14pt, - &Font_3x6, - &Font_5x8, - &Font_8x8, - &Font_8x8Thin, - &Font_Invaders -}; - +char fontname[15]; void f_nick(void){ static char ctr=0; char key; - signed char x=10; - signed char y=10; + static signed char x=10; + static signed char y=10; while (1) { lcdClear(); lcdFill(255); - font=fonts[ctr%10]; - DoString(x,y,nickname); -// lcdSafeSetPixel(x,y,1); + setExtFont(fontname); - font=&Font_7x8; + DoString(x,y,nickname); + + setIntFont(&Font_7x8); lcdSetCrsr(50,50); lcdPrintInt(x); lcdPrint("x"); @@ -135,6 +123,23 @@ void f_nick(void){ }; }; +void f_font(void){ + + if( selectFile(fontname,"F0N") != 0){ + lcdPrintln("No file selected."); + return; + }; + + lcdClear(); + lcdPrintln(fontname); + setExtFont(fontname); + lcdPrintln("PUabc€"); + setIntFont(&Font_7x8); + lcdPrintln("done."); + lcdDisplay(); + while(!getInputRaw())delayms(10); +}; + /***********************************************************************/ void gotoISP(void) { @@ -178,7 +183,7 @@ void msc_menu(void){ const struct MENU_DEF menu_ISP = {"Invoke ISP", &gotoISP}; const struct MENU_DEF menu_init = {"F Init", &f_init}; const struct MENU_DEF menu_nick = {"F Nick", &f_nick}; -//const struct MENU_DEF menu_snd = {"F Send", &f_send}; +const struct MENU_DEF menu_font = {"F sel", &f_font}; const struct MENU_DEF menu_mirror = {"Mirror", &lcd_mirror}; const struct MENU_DEF menu_invert = {"Invert", &lcd_invert}; const struct MENU_DEF menu_volt = {"Akku", &adc_check}; @@ -188,7 +193,7 @@ const struct MENU_DEF menu_nop = {"---", NULL}; static menuentry menu[] = { &menu_init, &menu_nick, -// &menu_snd, + &menu_font, &menu_nop, &menu_mirror, &menu_invert, diff --git a/firmware/lcd/decoder.c b/firmware/lcd/decoder.c index d4ff7b8..7073ee4 100644 --- a/firmware/lcd/decoder.c +++ b/firmware/lcd/decoder.c @@ -1,9 +1,6 @@ #include #include -#define MAXCHR (30*20) -static uint8_t buf[MAXCHR]; - // Local function: Get next nibble. int ctr=0; // offset for next nibble int hilo=0; // 0= high nibble next, 1=low nibble next @@ -15,7 +12,11 @@ static uint8_t buf[MAXCHR]; ctr++; hilo=1-hilo; if(hilo==1){ - byte=data[ctr]; + if(efont.type==FONT_EXTERNAL){ + byte=_getFontData(GET_DATA,0); + }else{ + byte=data[ctr]; + }; val=byte>>4; }else{ val=byte&0x0f; @@ -44,7 +45,7 @@ uint8_t * pk_decode(const uint8_t * ldata,int * len){ int length=*len; // Length of character bytestream int height; // Height of character in bytes int hoff; // bit position for non-integer heights - uint8_t * bufptr=buf; // Output buffer for decoded character + uint8_t * bufptr=charBuf; // Output buffer for decoded character height=(font->u8Height-1)/8+1; hoff=font->u8Height%8; @@ -55,10 +56,17 @@ uint8_t * pk_decode(const uint8_t * ldata,int * len){ int pos=0; // Decoder internal: current bit position (0..7) int nyb; // Decoder internal: current nibble / value - if(data[ctr]>>4 == 14){ // Char starts with 1-bits. - gnn(); - curbit=1; - }; + if(efont.type==FONT_EXTERNAL){ + if(_getFontData(PEEK_DATA,0)>>4 == 14){ // Char starts with 1-bits. + gnn(); + curbit=1; + }; + }else{ + if(data[ctr]>>4 == 14){ // Char starts with 1-bits. + gnn(); + curbit=1; + }; + }; while(ctr0){ for(int y=0;y + #include #include #include #include +#include "basic/basic.h" +#include "fonts/smallfonts.h" + +#include "filesystem/ff.h" /* Global Variables */ const struct FONT_DEF * font = NULL; -char font_direction = FONT_DIR_LTR; + +struct EXTFONT efont; + +FIL file; /* current font file */ /* Exported Functions */ +void setIntFont(const struct FONT_DEF * font){ + memcpy(&efont.def,font,sizeof(struct FONT_DEF)); + efont.type=FONT_INTERNAL; + font=NULL; +}; + +void setExtFont(const char *fname){ + if(strlen(fname)>8+4) + return; + strcpy(efont.name,fname); +// memcpy(efont.name+strlen(fname),".f0n",5); + + efont.type=FONT_EXTERNAL; + font=NULL; +}; + + +int _getFontData(int type, int offset){ + UINT readbytes; + UINT res; + static uint16_t extras; + static uint16_t character; + static const void * ptr; + + if(efont.type == FONT_EXTERNAL){ + + if (type == START_FONT){ + res = f_read(&file, &efont.def.u8Width, sizeof(uint8_t), &readbytes); + res = f_read(&file, &efont.def.u8Height, sizeof(uint8_t), &readbytes); + res = f_read(&file, &efont.def.u8FirstChar, sizeof(uint8_t), &readbytes); + res = f_read(&file, &efont.def.u8LastChar, sizeof(uint8_t), &readbytes); + res = f_read(&file, &extras, sizeof(uint16_t), &readbytes); + return 0; + }; + if (type == SEEK_EXTRAS){ + f_lseek(&file,6); + return 0; + }; + if(type == GET_EXTRAS){ + uint16_t word; + res = f_read(&file, &word, sizeof(uint16_t), &readbytes); + return word; + }; + if (type == SEEK_WIDTH){ + f_lseek(&file,6+(extras*sizeof(uint16_t))); + return 0; + }; + if(type == GET_WIDTH || type == GET_DATA){ + uint8_t width; + res = f_read(&file, &width, sizeof(uint8_t), &readbytes); + return width; + }; + if(type == SEEK_DATA){ + character=offset; + f_lseek(&file,6+ + (extras*sizeof(uint16_t))+ + ((extras+font->u8LastChar-font->u8FirstChar)*sizeof(uint8_t))+ + (offset*sizeof(uint8_t)) + ); + return 0; + }; + if(type == PEEK_DATA){ + uint8_t width; + res = f_read(&file, &width, sizeof(uint8_t), &readbytes); + f_lseek(&file,6+ + (extras*sizeof(uint16_t))+ + ((extras+font->u8LastChar-font->u8FirstChar)*sizeof(uint8_t))+ + (character*sizeof(uint8_t)) + ); + return width; + }; +#ifdef NOTYET + }else{ // efont.type==FONT_INTERNAL + + if (type == START_FONT){ + memcpy(&efont.def,font,sizeof(struct FONT_DEF)); + return 0; + }; + if (type == SEEK_EXTRAS){ + ptr=efont.def.charExtra; + return 0; + }; + if(type == GET_EXTRAS){ + uint16_t word; + word=*(uint16_t*)ptr; + ptr=((uint16_t*)ptr)+1; + return word; + }; + if (type == SEEK_WIDTH){ + ptr=efont.def.charInfo; + return 0; + }; + if(type == GET_WIDTH || type == GET_DATA){ + uint8_t width; + width=*(uint8_t*)ptr; + ptr=((uint8_t*)ptr)+1; + return width; + }; + if(type == SEEK_DATA){ + if(offset==REPEAT_LAST_CHARACTER) + offset=character; + else + character=offset; + + ptr=efont.def.au8FontTable; + return 0; + }; +#endif + }; + + /* NOTREACHED */ + return 0; +}; + +int _getIndex(int c){ +#define ERRCHR (font->u8FirstChar+1) + /* Does this font provide this character? */ + if(cu8FirstChar) + c=ERRCHR; + if(c>font->u8LastChar && efont.type!=FONT_EXTERNAL && font->charExtra == NULL) + c=ERRCHR; + + if(c>font->u8LastChar && (efont.type==FONT_EXTERNAL || font->charExtra != NULL)){ + if(efont.type==FONT_EXTERNAL){ + _getFontData(SEEK_EXTRAS,0); + int cc=0; + int cache; + while( (cache=_getFontData(GET_EXTRAS,0)) < c) + cc++; + if( cache > c) + c=ERRCHR; + else + c=font->u8LastChar+cc+1; + }else{ + int cc=0; + while( font->charExtra[cc] < c) + cc++; + if(font->charExtra[cc] > c) + c=ERRCHR; + else + c=font->u8LastChar+cc+1; + }; + }; + c-=font->u8FirstChar; + return c; +}; + +uint8_t charBuf[MAXCHR]; + int DoChar(int sx, int sy, int c){ +// font=NULL; + if(font==NULL){ + if(efont.type==FONT_INTERNAL){ + font=&efont.def; + }else if (efont.type==FONT_EXTERNAL){ + UINT res; + res=f_open(&file, efont.name, FA_OPEN_EXISTING|FA_READ); + if(res){ + efont.type=0; + font=&Font_7x8; + }else{ + _getFontData(START_FONT,0); + font=&efont.def; + }; + }else{ + font=&Font_7x8; + }; + }; + /* how many bytes is it high? */ char height=(font->u8Height-1)/8+1; char hoff=(8-(font->u8Height%8))%8; @@ -18,49 +195,76 @@ int DoChar(int sx, int sy, int c){ const uint8_t * data; int width,preblank=0,postblank=0; do { /* Get Character data */ - /* Does this font provide this character? */ - if(cu8FirstChar) - c=font->u8FirstChar+1; // error - if(c>font->u8LastChar && font->charExtra == NULL) - c=font->u8FirstChar+1; // error - - if(c>font->u8LastChar && font->charExtra != NULL){ - int cc=0; - while( font->charExtra[cc] < c) - cc++; - if(font->charExtra[cc] > c) - c=font->u8FirstChar+1; // error - else - c=font->u8LastChar+cc+1; - }; + /* Get intex into character list */ + c=_getIndex(c); /* starting offset into character source data */ int toff=0; if(font->u8Width==0){ - for(int y=0;yu8FirstChar;y++) - toff+=font->charInfo[y].widthBits; - width=font->charInfo[c-font->u8FirstChar].widthBits; + if(efont.type == FONT_EXTERNAL){ + _getFontData(SEEK_WIDTH,0); + for(int y=0;yau8FontTable[toff]; + _getFontData(SEEK_DATA,toff); + UINT res; + UINT readbytes; + res = f_read(&file, charBuf, width*height, &readbytes); + if(res != FR_OK || readbytescharInfo[y].widthBits; + width=font->charInfo[c].widthBits; + + toff*=height; + data=&font->au8FontTable[toff]; + }; postblank=1; }else if(font->u8Width==1){ // NEW CODE - // Find offset and length for our character - for(int y=0;yu8FirstChar;y++) - toff+=font->charInfo[y].widthBits; - width=font->charInfo[c-font->u8FirstChar].widthBits; - - if(font->au8FontTable[toff]>>4 == 15){ // It's a raw character! - preblank = font->au8FontTable[toff+1]; - postblank= font->au8FontTable[toff+2]; - data=&font->au8FontTable[toff+3]; - width=(width-3/height); + if(efont.type == FONT_EXTERNAL){ + _getFontData(SEEK_WIDTH,0); + for(int y=0;y>4 ==15){ + res = f_read(&file, &preblank, sizeof(uint8_t), &readbytes); + res = f_read(&file, &postblank, sizeof(uint8_t), &readbytes); + width-=3; + width/=height; + res = f_read(&file, charBuf, width*height, &readbytes); + if(res != FR_OK || readbytesau8FontTable[toff],&width); - } + // Find offset and length for our character + for(int y=0;ycharInfo[y].widthBits; + width=font->charInfo[c].widthBits; + if(font->au8FontTable[toff]>>4 == 15){ // It's a raw character! + preblank = font->au8FontTable[toff+1]; + postblank= font->au8FontTable[toff+2]; + data=&font->au8FontTable[toff+3]; + width=(width-3/height); + }else{ + data=pk_decode(&font->au8FontTable[toff],&width); + } + }; + }else{ - toff=(c-font->u8FirstChar)*font->u8Width*height; + toff=(c)*font->u8Width*height; width=font->u8Width; data=&font->au8FontTable[toff]; }; diff --git a/firmware/lcd/render.h b/firmware/lcd/render.h index 563c6a0..eaa69ef 100644 --- a/firmware/lcd/render.h +++ b/firmware/lcd/render.h @@ -2,6 +2,7 @@ #define __RENDER_H_ #include "display.h" +#include "fonts.h" /* #badge 96x68 @@ -33,5 +34,21 @@ int DoIntXn(int sx, int sy, unsigned int num, unsigned int maxlen); int DoIntX(int sx, int sy, unsigned int num); int DoCharX(int sx, int sy, unsigned char num); int DoShortX(int sx, int sy, uint16_t num); +void setIntFont(const struct FONT_DEF * font); +void setExtFont(const char *file); + +#define START_FONT 0 +#define SEEK_EXTRAS 1 +#define GET_EXTRAS 2 +#define SEEK_WIDTH 3 +#define GET_WIDTH 4 +#define SEEK_DATA 5 +#define GET_DATA 6 +#define PEEK_DATA 7 + +int _getFontData(int type, int offset); + +#define MAXCHR (30*20) +extern uint8_t charBuf[MAXCHR]; #endif