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
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.