Toggle Button Label Text (FUNCTION CALLING EVENT)

Issues trying to toggle a button label. I am using Arduino IDE + VSC.

ESP32 S3

LVGL 8.3

Want the event of clicking a button to perform the following:

  • Change the current label text;

So far:

Code to reproduce

  • at the .h helpers file
char _ui_label_get_property(lv_obj_t * target);
  • at the .c helpers file
char _ui_label_get_property(lv_obj_t *target)
{
    char *Lbl_Txt = lv_label_get_text(target);
    return Lbl_Txt;
}
  • at the main .c file
void ui_event_Button9(lv_event_t * e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);

    if (event_code == LV_EVENT_CLICKED)
    {
        if (_ui_label_get_property(ui_Label17) == ">>")
        {
            lv_label_set_text(ui_Label16, "A");
            _ui_label_set_property(ui_Label17, _UI_LABEL_PROPERTY_TEXT, "||");
        }
        else
        {
            lv_label_set_text(ui_Label16, "B");
            _ui_label_set_property(ui_Label17, _UI_LABEL_PROPERTY_TEXT, ">>");
        }
    }
}

20240404_134837

i think you should use strcmp() to comapre strings
EDIT: instead of comparing pointers to strings
strcmp(_ui_label_get_property(ui_Label17), ">>")

1 Like

Or you can compare first characters only, but try == on strings in C language is elementary mistake. You need learn more basics.

1 Like

Forgot to mention I was a C rookie…but learned a new thing.

I did the proper comparison using the strcmp() function and now, whenever I click the button, the board restarts itself.

What might be the case here? Variable type conflict?

all this is bad you mix char and char * = c basics

dont create dupli func use directly lv_label_get_text … and show code

1 Like


image

Dont edit ui_helper files, is overwrited with next export ui.
And right code for your event

if (event_code == LV_EVENT_CLICKED)
    {
     if(strcmp(lv_label_get_text(ui_Label17),">>") == 0)
...

overlooked something. should be
char * _ui_label_get_property(lv_obj_t *target)

Got it working.

Removed helper functions and fixed the char vs. char* mix-up.

Current code:

void ui_event_Button9(lv_event_t * e)
{
lv_event_code_t event_code = lv_event_get_code(e);
lv_obj_t * target = lv_event_get_target(e);

if(event_code == LV_EVENT_CLICKED)
{
    char *Str_Lbl;

    (strcmp(lv_label_get_text(ui_Label17), ">>") == 0) ? (Str_Lbl = "||") : (Str_Lbl = ">>");
    
    _ui_label_set_property(ui_Label17, _UI_LABEL_PROPERTY_TEXT, Str_Lbl);
}

}