Use lv_style_t as function parameter


Currently, I’m trying to create a Tamagotchi game for my daughter. I’m using an M5Core2 esp with Arduino software.

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

What LVGL version are you using?

v8.1 / master branch

What do you want to achieve?

I’m facing a problem when using lv_style_t static variables.
I want to create Bar elements, but I want to change only the color of some parts depending on the parameters.

Code to reproduce

// In Utils.h
inline lv_obj_t* lv_game_bar_create(lv_obj_t* parent, const lv_palette_t color, lv_style_t* style_bg, lv_style_t* style_indic) {
  lv_style_set_border_color(style_bg, lv_palette_main(color));
  lv_style_set_border_width(style_bg, 2);
  lv_style_set_pad_all(style_bg, 6); /*To make the indicator smaller*/
  lv_style_set_radius(style_bg, 6);
  lv_style_set_anim_time(style_bg, 1000);

  lv_style_set_bg_opa(style_indic, LV_OPA_COVER);
  lv_style_set_bg_color(style_indic, lv_palette_main(color));
  lv_style_set_radius(style_indic, 3);

  lv_obj_t* bar = lv_bar_create(lv_scr_act());
  lv_obj_remove_style_all(bar); /*To have a clean start*/
  lv_obj_add_style(bar, style_bg, 0);
  lv_obj_add_style(bar, style_indic, LV_PART_INDICATOR);

  lv_obj_set_size(bar, 100, 20);

  return bar;
// In my setup function
  static lv_style_t bar_mood_style_bg;
  static lv_style_t bar_mood_style_indic;

  _mood_bar = lv_game_bar_create(_screen, LV_PALETTE_BLUE, &bar_mood_style_bg, &bar_mood_style_indic);
  lv_obj_set_pos(_mood_bar, 10, 30);
  lv_bar_set_value(_mood_bar, 0, LV_ANIM_ON);

But by doing this, the style is added, but the code crash with this message:

Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400fb127  PS      : 0x00060730  A0      : 0x800d1750  A1      : 0x3ffb1f30  
A2      : 0xf3ef10b2  A3      : 0x00000064  A4      : 0x00000001  A5      : 0x3ffc8708  
A6      : 0x3ffb8798  A7      : 0x00000018  A8      : 0x800fb14e  A9      : 0x3ffb1ed0  
A10     : 0x3ffb3500  A11     : 0x400fa95c  A12     : 0x3ffb3524  A13     : 0x3ffb353c  
A14     : 0x00000001  A15     : 0x3ffc7a04  SAR     : 0x0000001a  EXCCAUSE: 0x0000001c  
EXCVADDR: 0xf3ef10d6  LBEG    : 0x400f8d01  LEND    : 0x400f8d14  LCOUNT  : 0x00000000  

ELF file SHA256: 0000000000000000

Backtrace: 0x400fb127:0x3ffb1f30 0x400d174d:0x3ffb1f50 0x400d1b9a:0x3ffb1f70 0x400d1e9d:0x3ffb1f90 0x401065bd:0x3ffb1fb0 0x4008b70a:0x3ffb1fd0

I also try to add the static variables inside the lv_game_create_bar, it works except that all bars have the same color (as expected because the var is static ^^).

What I really want is to avoid repeating parts of code, I want to be able to change the bar style without having to change the style of all bars.
I’m new in this domain or with this software (Arduino), I’m not able to see the correct backtrace or whatever.
I check the documentation and it says that the lv_style_t should be a static variable, but I have no idea why it crashed.

In that case, you may want to just not use lv_style_t variables at all. Each object has its own local style that doesn’t affect other objects.

Thanks a lot, I forget this part and thought it was easier to configure theme for elements.
It works as expected!