Can't update a value on the screen

Hi,
I have a screen and I defined few buttons and 2 global variables defined as:

lv_obj_t *State, *Disp;
void setup {
...

I initialize those variables only one time:

  static lv_style_t stylTemp;
  lv_style_init(&stylTemp);
  lv_style_set_text_font(&stylTemp,&lv_font_montserrat_36);
  lv_obj_t *Disp = lv_label_create(lv_scr_act());
  lv_obj_add_style(Disp,&stylTemp,0);

  lv_label_set_text(Disp,"00.00");
  lv_obj_align(Disp,LV_ALIGN_CENTER, 0,50);
  
  lv_obj_t *State = lv_label_create(lv_scr_act());
  char stateOFF[] = "Undefined";
  lv_label_set_text(State,stateOFF);
  lv_obj_align(State,LV_ALIGN_BOTTOM_MID,0,0);

until that point, everything is fine, the display show “00.00” and “Undefined” on the bottom.

It’s getting complicated when I try to update the value. I tried:

void DspTmp(){
  if(Temp<15) Temp=15;
  if(Temp>32) Temp=32;
  char tp[6];
  snprintf(tp,sizeof tp,"%.2f",Temp); 
  lv_label_set_text_fmt(Disp, "%s", tp);
  char DspState[15];
  if(Temp < RTemp) snprintf(DspState,14,"Compressor OFF"); 
  else snprintf(DspState,14,"Compressor ON ");
  lv_label_set_text(State, DspState);
}

but this is not working… I need help again :frowning:

Thank you

OK, I reply to myself, I find a way to make it work but it’s probably not the best one:

Global var initialisation:

lv_obj_t *State, *Disp;

void setup(void)
{

at the end of the Setup:

  lv_buttons();
}

the buttons procedure:

void lv_buttons(void){
  lv_obj_t *label;
  // Button with counter Temp-
  lv_obj_t *btn1 = lv_btn_create(lv_scr_act());
  lv_obj_add_event_cb(btn1, counter_event_handler, LV_EVENT_ALL, NULL);
  //lv_obj_set_pos(btn1, 100, 100);   /*Set its position*/
  lv_obj_set_pos(btn1, 100, 100);   /*Set its position*/
  lv_obj_set_size(btn1, 120, 50);   /*Set its size*/
  label = lv_label_create(btn1);
  lv_label_set_text(label, "Temp -");
  lv_obj_center(label);
  // Button with Temp+
  lv_obj_t *btn2 = lv_btn_create(lv_scr_act());
  lv_obj_add_event_cb(btn2, counter_event_handlerAdd, LV_EVENT_ALL, NULL);

  //lv_obj_set_pos(btn1, 100, 100);   //Set its position
  lv_obj_set_pos(btn2, 250, 100);   //Set its position//
  lv_obj_set_size(btn2, 120, 50);   //Set its size//
  label = lv_label_create(btn2);
  lv_label_set_text(label, "Temp +");
  lv_obj_center(label);
  
  //create a style for the temperature
  static lv_style_t stylTemp;
  lv_style_init(&stylTemp);
  lv_style_set_text_font(&stylTemp,&lv_font_montserrat_36);
  lv_obj_t *Disp = lv_label_create(lv_scr_act()); //create the label
  lv_obj_add_style(Disp,&stylTemp,0); //apply the stle
  lv_label_set_text(Disp,"00.00"); //temporary value
  lv_obj_align(Disp,LV_ALIGN_CENTER, 0,50); // set the position
  //Second label, no style on this one
  lv_obj_t *State = lv_label_create(lv_scr_act()); 
  char stateOFF[] = "Undefined";
  lv_label_set_text(State,stateOFF);
  lv_obj_align(State,LV_ALIGN_BOTTOM_MID,0,0);
}

Then, to update the values:

void DspTmp(){
  if(Temp<15) Temp=15;//set the min value
  if(Temp>32) Temp=32;//set the max value
  char tp[6];
  snprintf(tp,sizeof tp,"%.2f",Temp); //float to char[]
  lv_obj_t *Tst = lv_obj_get_child(lv_scr_act(), 3);//get the address of the Temperature on the display
  lv_label_set_text_fmt(Tst, "%s", tp);//update the temperature
  char DspState[16];//update the state
  if(Temp < RTemp) snprintf(DspState,15,"Compressor OFF"); //check the status
  else snprintf(DspState,15,"Compressor ON ");//and define the message
  lv_obj_t *Tst1 = lv_obj_get_child(lv_scr_act(), 4); //get the adresse of the status on the display
  lv_label_set_text(Tst1, DspState);//update the texte
}

This is working but there no point to get the variable Disp and State as global if I have to address the location on the display by this way… There is probably a better way… I still need your help…
Thank you.

OK I find the problem… for the people who are looking for this solution:

1- declare the variable object as global:

lv_obj_t *State, *Disp;

void setup(void)
{

initialize by this way:

  State = lv_label_create(lv_scr_act());//Create the label (it's where I was doing mistake)
  char stateOFF[] = "Undefined";//set an initial value
  lv_label_set_text(State,stateOFF);//send the value to the label
  lv_obj_align(State,LV_ALIGN_BOTTOM_MID,0,0);//define the position

Then to update when needed:

void DspTmp(){
  char DspState[16];
  snprintf(tp, sizeof(tp), "%.2f", Temp);
  lv_label_set_text_fmt(State, "%s", tp);
}

Much better than before…

Hello Michel,

Good job fixing this. I believe you can use lv_label_set_text_fmt to skip that snprintf call.

Try lv_label_set_text_fmt(State, "%.2f", temp)

1 Like

good idea, I just start with LVGL, I need to practice :slight_smile:
thank you again for your help Tinus