2024/2/7

LVGL set font and filesystem

ref: 會遭遇兩個問題:
  • 如何讀入字型檔
  • 如何設定字型
讀入就跟 filesystem 有關。
filesystem 就跟 platform 有關,是用 windows, linux 還是 mcu,filesystem 的 api 不一樣。
lvgl 用 lv_conf.h 來設定用哪個 api:
/*File system interfaces for common APIs */

/*API for fopen, fread, etc*/
#define LV_USE_FS_STDIO 1
#if LV_USE_FS_STDIO
    #define LV_FS_STDIO_LETTER 'A'     /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
    #define LV_FS_STDIO_PATH ""         /*Set the working directory. File/directory paths will be appended to it.*/
    #define LV_FS_STDIO_CACHE_SIZE 0    /*>0 to cache this number of bytes in lv_fs_read()*/
#endif

/*API for open, read, etc*/
#define LV_USE_FS_POSIX 0
#if LV_USE_FS_POSIX
    #define LV_FS_POSIX_LETTER 'A'     /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
    #define LV_FS_POSIX_PATH ""         /*Set the working directory. File/directory paths will be appended to it.*/
    #define LV_FS_POSIX_CACHE_SIZE 0    /*>0 to cache this number of bytes in lv_fs_read()*/
#endif

/*API for CreateFile, ReadFile, etc*/
#define LV_USE_FS_WIN32 0
#if LV_USE_FS_WIN32
    #define LV_FS_WIN32_LETTER '\0'     /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
    #define LV_FS_WIN32_PATH ""         /*Set the working directory. File/directory paths will be appended to it.*/
    #define LV_FS_WIN32_CACHE_SIZE 0    /*>0 to cache this number of bytes in lv_fs_read()*/
#endif
那個 LETTER 不是代表 windows 的 A 槽,B槽。而是用來代表 device.
像我用 ubuntu,試過用 "A:~/font.bin" 不行,一定要寫full path: "A:\home\charles\font.bin",然後用的是 FS_STDIO (會用 fopen())

load font 之後放進style,再把 style 設定到 object。
設定 font 跟 style 的 code:
     lv_font_t *myfont = lv_font_load("A:/home/charles/font.bin");
     static lv_style_t style;
     lv_style_init(&style);
     lv_style_set_text_font(&style,myfont);
要注意 stype 要是 static,因為之後系統會 periodically render 畫面,那時候會 access style 變數。如果是 local 變數,已經 free 調的話,就會出現 segment fault

下面用 example 的 label 為例子,設定 style:
    lv_obj_t * label1 = lv_label_create(lv_scr_act());
    lv_label_set_long_mode(label1, LV_LABEL_LONG_WRAP);     /*Break the long lines*/
    lv_label_set_recolor(label1, true);                      /*Enable re-coloring by commands in the text*/
    lv_label_set_text(label1, "TEST MY FONT");
    lv_obj_set_width(label1, 150);  /*Set smaller width to make the lines wrap*/
    lv_obj_set_style_text_align(label1, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_align(label1, LV_ALIGN_CENTER, 0, -40);
    
    lv_obj_add_style(label1,&style,0);

沒有留言:

張貼留言