Trouble changing font size

What MCU/Processor/Board and compiler are you using?

ESP32-S3

What LVGL version are you using?

8.3

What do you want to achieve?

Create a function to easily change font size. Rather than writing lines of code over and over to change the label font size, I figured a function to do it would be more compact and cleaner… but of course it isn’t working.

int RPM = 0;
int AFR = 0;
int MAP = 0;

static char rpmLabelText[32];
static char afrLabelText[32];
static char mapLabelText[32];

lv_obj_t *rpmLabel;
lv_obj_t *afrLabel;
lv_obj_t *mapLabel;

LV_FONT_DECLARE(lv_font_montserrat_12);
LV_FONT_DECLARE(lv_font_montserrat_16);
LV_FONT_DECLARE(lv_font_montserrat_20);

void set_object_font_size(lv_obj_t * obj, int font_size) {
 const lv_font_t * font;
 switch (font_size) {
   case 12:
     font = &lv_font_montserrat_12;
     break;
   case 16:
     font = &lv_font_montserrat_16;
     break;
   case 20:
     font = &lv_font_montserrat_20;
     break;
   default:
     LV_LOG_WARN("Unsupported font size. Defaulting to 16.");
     font = &lv_font_montserrat_16;
   break;
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_text_font(&style, font);
    lv_obj_add_style(obj, &style, LV_PART_MAIN);
  }
}

void (createScreens) {
screen2 = lv_obj_create(NULL); 
 lv_obj_set_size(screen2, 480, 480);
 lv_obj_align(screen2, LV_ALIGN_CENTER, 0, 0);
 lv_obj_set_style_bg_color(screen2, lv_palette_main(LV_PALETTE_AMBER), LV_PART_MAIN);

 rpmLabel = lv_label_create(screen2);
 lv_obj_align(rpmLabel, LV_ALIGN_CENTER, 0, 0);     
 set_object_font_size(rpmLabel, 20);

 afrLabel = lv_label_create(screen2);
 lv_obj_align(afrLabel, LV_ALIGN_CENTER, -100, 0);
 set_object_font_size(afrLabel, 16);

 mapLabel = lv_label_create(screen2);
 lv_obj_align(mapLabel, LV_ALIGN_CENTER, 100, 0); 
 set_object_font_size(mapLabel, 12);
}

Instead of reconfiguring the same static style for all objects, use a local change in the objects text font property

void set_object_font_size(lv_obj_t * obj, int font_size) {
 const lv_font_t * font;
 switch (font_size) {
   case 12:
     font = &lv_font_montserrat_12;
     break;
   case 16:
     font = &lv_font_montserrat_16;
     break;
   case 20:
     font = &lv_font_montserrat_20;
     break;
   default:
     LV_LOG_WARN("Unsupported font size. Defaulting to 16.");
     font = &lv_font_montserrat_16;
   break;
  }
  lv_obj_set_style_text_font(obj, font, LV_PART_MAIN);
}

Ok, I get the simplicity and elegance of not redefining the style every time the function is called but this code throws an error:

/Users/***/Library/Arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld: /private/var/folders/q7/smghyfjs7594bhw2d0mnv_gc0000gn/T/arduino/sketches/271950101ACEAA406FFF967672C8B1BA/sketch/objs.a(ui.ino.cpp.o):(.literal._Z20set_object_font_sizeP9_lv_obj_ti+0x0): undefined reference to `lv_font_montserrat_16'
/Users/***/Library/Arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld: /private/var/folders/q7/smghyfjs7594bhw2d0mnv_gc0000gn/T/arduino/sketches/271950101ACEAA406FFF967672C8B1BA/sketch/objs.a(ui.ino.cpp.o):(.literal._Z20set_object_font_sizeP9_lv_obj_ti+0x4): undefined reference to `lv_font_montserrat_20'
/Users/***/Library/Arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld: /private/var/folders/q7/smghyfjs7594bhw2d0mnv_gc0000gn/T/arduino/sketches/271950101ACEAA406FFF967672C8B1BA/sketch/objs.a(ui.ino.cpp.o):(.literal._Z20set_object_font_sizeP9_lv_obj_ti+0x8): undefined reference to `lv_font_montserrat_12'
collect2: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

In my lv_conf.h I have this set:

#define LV_FONT_MONTSERRAT_8  0
#define LV_FONT_MONTSERRAT_10 0
#define LV_FONT_MONTSERRAT_12 1
#define LV_FONT_MONTSERRAT_14 1
#define LV_FONT_MONTSERRAT_16 1
#define LV_FONT_MONTSERRAT_18 0
#define LV_FONT_MONTSERRAT_20 0
#define LV_FONT_MONTSERRAT_22 1
#define LV_FONT_MONTSERRAT_24 1
#define LV_FONT_MONTSERRAT_26 0
#define LV_FONT_MONTSERRAT_28 0
#define LV_FONT_MONTSERRAT_30 0
#define LV_FONT_MONTSERRAT_32 0
#define LV_FONT_MONTSERRAT_34 0
#define LV_FONT_MONTSERRAT_36 0
#define LV_FONT_MONTSERRAT_38 0
#define LV_FONT_MONTSERRAT_40 0
#define LV_FONT_MONTSERRAT_42 0
#define LV_FONT_MONTSERRAT_44 0
#define LV_FONT_MONTSERRAT_46 0
#define LV_FONT_MONTSERRAT_48 0

Backing up and simply trying to change the font size locally, outsize of a function, this works, without any use of “LV_FONT_DECLARE()”

 voltLabel = lv_label_create(screen2);
 lv_obj_align(voltLabel, LV_ALIGN_CENTER, -75, 50);
 lv_obj_set_style_text_font(voltLabel, &lv_font_montserrat_24, LV_PART_MAIN);

but this doesn’t:

 voltLabel = lv_label_create(screen2);
 lv_obj_align(voltLabel, LV_ALIGN_CENTER, -75, 50);
 lv_obj_set_style_text_font(voltLabel, &lv_font_montserrat_40, LV_PART_MAIN);

with error:

Compilation error: 'lv_font_montserrat_40' was not declared in this scope

This is my lv_conf.h, both font sizes 24 and 40 are set to “1” so I’m not sure what’s going on here… why are some fonts being recognized but others are not.

I just don’t get it.

#define LV_FONT_MONTSERRAT_8  0
#define LV_FONT_MONTSERRAT_10 0
#define LV_FONT_MONTSERRAT_12 0
#define LV_FONT_MONTSERRAT_14 0
#define LV_FONT_MONTSERRAT_16 0
#define LV_FONT_MONTSERRAT_18 0
#define LV_FONT_MONTSERRAT_20 0
#define LV_FONT_MONTSERRAT_22 0
#define LV_FONT_MONTSERRAT_24 1
#define LV_FONT_MONTSERRAT_26 1
#define LV_FONT_MONTSERRAT_28 0
#define LV_FONT_MONTSERRAT_30 1
#define LV_FONT_MONTSERRAT_32 0
#define LV_FONT_MONTSERRAT_34 0
#define LV_FONT_MONTSERRAT_36 0
#define LV_FONT_MONTSERRAT_38 0
#define LV_FONT_MONTSERRAT_40 1
#define LV_FONT_MONTSERRAT_42 0
#define LV_FONT_MONTSERRAT_44 0
#define LV_FONT_MONTSERRAT_46 0
#define LV_FONT_MONTSERRAT_48 0

I’ve tried changing many things in lv_conf.h like the default font size but nothing works.

It seems like lv_conf.h isn’t being updated…

I’m using Arduino IDE, could this be an issue?

What can I do to force a recompile of lv_conf.h?

maybe there is another lv_conf.h file in your project