LVGL v8.3.0-dev
Visual Studio Code / PlatformIO
Hi,
I am trying to learn LVGL but I am hindered by my lack of knowledge of C++
I have set up a screen and it has four (4) buttons relating to four different actions. With the current code I get the following error.
.. operand types are incompatible ("lv_obj_t *" and "const char *")
I clearly do not understand what “lv_obj_t” is and how it is used.
I would like to know how I can compare the output from “lv_obj_t * label = lv_obj_get_child(btn, 0);” and use it in my code perhaps as an int or String.
This is my callback function;
static void my_event_cb(lv_event_t * event)
{
lv_obj_t * btn = lv_event_get_target(event);
lv_obj_t * label = lv_obj_get_child(btn, 0); //I know this returns something like 1070173192
if (label == "1070173192"){ //This is what I am trying to do but it is not working for me
//Turn on light 1
}
if (label == "1070173193"){
//Turn on light 2
}
//operand types are incompatible ("lv_obj_t *" and "const char *")
}
Well lv_obj_t
is a generic lvgl struct and you can’t really compare it with a char *
. If you want the labels text you should try lv_label_get_text(label) == "..."
.
Hi @dpunter ,
Here is a quick example, I am not sure of your aims but this should cover everything I hope:
#define NUM_BUTTONS 4
static lv_obj_t *buttons[NUM_BUTTONS];
static void btn_cb( lv_event_t *event ) {
lv_obj_t *btn = lv_event_get_target(event);
lv_obj_t *label = lv_obj_get_child(btn, 0); // Get a pointer to the label object containing button text
if( btn == buttons[0] ) { // Compare the object pointers to discover which object was clicked
printf( "1 Clicked!\n");
} else if( btn == buttons[1] ) {
printf( "2 Clicked!\n");
} else if( btn == buttons[2] ) {
printf( "3 Clicked!\n");
} else if( btn == buttons[3] ) {
printf( "4 Clicked!\n");
}
printf( "Button with label %s Clicked!\n", lv_label_get_text(label)); // Get the label text
}
static void create_screen( lv_obj_t *parent ) {
lv_obj_t *btn_txt;
for( uint8_t i = 0; i < NUM_BUTTONS; i++ ) {
buttons[i] = lv_btn_create( parent );
btn_txt = lv_label_create( buttons[i] );
lv_label_set_text_fmt( btn_txt, "Button %d", i+1 );
lv_obj_set_pos( buttons[i], 20 + (120 * i), 20 );
lv_obj_add_event_cb(buttons[i], btn_cb, LV_EVENT_CLICKED, NULL);
}
}
create_screen( lv_scr_act() );
Kind Regards,
Pete
Pete,
Fantastic, that works great.
Now I will go and study the code provided and try to learn something!