[v7.0] Update object values without drawing the whole screen

Description

I want to update some objects’ values without having to redraw the whole screen.

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

ESP32 and Eclipse IDE Simulator

What do you want to achieve?

I am receiving some values from sensors and from MQTT.
I need to update these values on some objects (labels, tables) and replace some images depending on the received values as well.
These objects are not part of the same screen. Some widgets are in different screens than the active one, so they don’t need to be updated.

What’s the best practice to do this? I’m still green in widget-oriented GUI.

What have you tried so far?

Nothing. I’m completely lost.
After reading the documentation, I believe I should do it in a task. But what do I do in said task?

You’d want to store the values from the sensors, etc. in variables. Then you can set the objects’ values from the variables in a task.

Yes, that is what I am doing. My question is more on what I should do on the task.

For example, if I have this code:

int stock = 0;

void stock_menu(lv_obj_t * parent){
    lv_obj_t * btn_plus = lv_btn_create(parent, NULL);
    lv_obj_set_event_cb(btn_plus, stock_btn_plus_event_handler);

    lv_obj_t * btn_minus = lv_btn_create(parent, NULL);
    lv_obj_set_event_cb(btn_minus, stock_btn_minus_event_handler);


    char aux_stock[4];
    sprintf(aux_stock, "%d", stock);

    lv_obj_t * label = lv_label_create(parent, NULL);
    lv_label_set_text(label, aux_stock);
}


static void stock_btn_plus_event_handler(lv_obj_t * btn, lv_event_t event){
	if(event == LV_EVENT_CLICKED){
		if(stock < 99){
			stock++;
		}
	}
}

static void stock_btn_minus_event_handler(lv_obj_t * btn, lv_event_t event){
	if(event == LV_EVENT_CLICKED){
		if(stock > 0){
			stock--;
		}
	}
}

How should I go about creating a task to update the label object?
The Task documentation’s example is pretty vague and only mentions creating an object (button), which isn’t what I want to do. I’m not sure how I can update a widget’s value.

EDIT: If I want to have two labels, do I need to declare two different objects or can I “recycle” the label object?

You can just change the label inside the event handler.

You need to declare two different global lv_obj_t * variables for two different labels. If you recycle the lv_obj_t * variable you won’t have a reference to the first label anymore.

Thank you! I got it!

I ended up with:

/* Declared a global label */
lv_obj_t label_stock;

/* In stock menu replaced label for the global variable */
void stock_menu(lv_obj_t * parent){
    ...
    lv_obj_t * label_stock = lv_label_create(parent, NULL);
    lv_label_set_text(label_stock, aux_stock);
    ...
}
/* And added the update on the event handlers */
static void stock_btn_plus_event_handler(lv_obj_t * btn, lv_event_t event){
	if(event == LV_EVENT_CLICKED){
		if(stock < 99){
			stock++;

            char aux_stock[4];
            sprintf(aux_stock, "%d", stock);

            lv_label_set_text(label_stock, aux_stock);

		}
	}
}
/* Did the same on the minus button event handler */

I presume that this is just a modification of your original example and not the code that you are actually running, as the comments don’t quite match the code itself. It looks like you understand the idea though. :slightly_smiling_face:

(Thought I answered this. Sorry!)

Yes, this is a simplified version of the code since the original has a lot more to it.

I did! It is all working properly now, I’m only updating the label’s text now. Thank you!