LVGL Label Issue

So, I was attempting to switch from using the LLEMU to LVGL for this year’s game. While getting use to LVGL, I found that it was fairly simple to use and could effectively create most objects and have them appear on the screen; however I’m having issues with getting labels(text) to appear on the screen. So, I went to some tutorials and tried to have them help me, but I ended up just copying the code off of one of the tutorials. Only the labels that are a child to the buttons are showing up on my screen. Anytime I try to create a standalone label with the active screen as the parent or the page as a parent, it doesn’t appear. I have attached the code for the test graphics interface. Once again, everything is showing up and working perfectly except the label “myLabel” in this example. Does anyone know why this might be happening and how I can fix it?

lv_obj_t * myButton;
lv_obj_t * myButton2;
lv_obj_t * myButtonLabel;
lv_obj_t * myButtonLabel2;
lv_obj_t * myLabel;
lv_obj_t * myPage;

lv_style_t myButtonStyleREL; //relesed style
lv_style_t myButtonStylePR; //pressed style

static lv_res_t btn_click_action(lv_obj_t * btn)
{
    uint8_t id = lv_obj_get_free_num(btn); //id usefull when there are multiple buttons

    if(id == 0)
    {
        char buffer[100];
		sprintf(buffer, "button was clicked %i milliseconds from start", pros::millis());
		lv_label_set_text(myLabel, buffer);
    }

    return LV_RES_OK;
}

/**
 * Runs initialization code. This occurs as soon as the program is started.
 *
 * All other competition modes are blocked by initialize; it is recommended
 * to keep execution time for this mode under a few seconds.
 */
void initialize() {

    int screen_width = lv_obj_get_width(lv_scr_act());
    int screen_height = lv_obj_get_height(lv_scr_act());

    //lv_tutorial_hello_world();
    //gui();
    lv_style_copy(&myButtonStyleREL, &lv_style_plain);
    myButtonStyleREL.body.main_color = LV_COLOR_MAKE(150, 0, 0);
    myButtonStyleREL.body.grad_color = LV_COLOR_MAKE(0, 0, 150);
    myButtonStyleREL.body.radius = 0;
    myButtonStyleREL.text.color = LV_COLOR_MAKE(255, 255, 255);

    lv_style_copy(&myButtonStylePR, &lv_style_plain);
    myButtonStylePR.body.main_color = LV_COLOR_MAKE(255, 0, 0);
    myButtonStylePR.body.grad_color = LV_COLOR_MAKE(0, 0, 255);
    myButtonStylePR.body.radius = 0;
    myButtonStylePR.text.color = LV_COLOR_MAKE(255, 255, 255);

    myPage = lv_page_create(lv_scr_act(),NULL);
    lv_obj_align(myPage, lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 0, screen_height * 0.1);
    lv_obj_set_width(myPage, screen_width);
    lv_obj_set_height(myPage, screen_height* 0.9);

    myButton = lv_btn_create(myPage, NULL); //create button, lv_scr_act() is deafult screen object
    lv_obj_set_free_num(myButton, 0); //set button is to 0
    lv_btn_set_action(myButton, LV_BTN_ACTION_CLICK, btn_click_action); //set function to be called on button click
    lv_btn_set_style(myButton, LV_BTN_STYLE_REL, &myButtonStyleREL); //set the relesed style
    lv_btn_set_style(myButton, LV_BTN_STYLE_PR, &myButtonStylePR); //set the pressed style
    lv_obj_set_size(myButton, 200, 50); //set the button size
    lv_obj_align(myButton, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 10); //set the position to top mid

    myButton2 = lv_btn_create(myPage, NULL); //create button, lv_scr_act() is deafult screen object
    lv_obj_set_free_num(myButton2, 1); //set button is to 0
    lv_btn_set_action(myButton2, LV_BTN_ACTION_CLICK, btn_click_action); //set function to be called on button click
    lv_btn_set_style(myButton2, LV_BTN_STYLE_REL, &myButtonStyleREL); //set the relesed style
    lv_btn_set_style(myButton2, LV_BTN_STYLE_PR, &myButtonStylePR); //set the pressed style
    lv_obj_set_size(myButton2, 200, 50); //set the button size
    lv_obj_align(myButton2, NULL, LV_ALIGN_IN_TOP_RIGHT, -10, 10); //set the position to top mid

    myButtonLabel = lv_label_create(myButton, NULL); //create label and puts it inside of the button
    lv_label_set_text(myButtonLabel, "Click the Button"); //sets label text

    myButtonLabel2 = lv_label_create(myButton2, NULL); //create label and puts it inside of the button
    lv_label_set_text(myButtonLabel2, "Click the Button"); //sets label text

    myLabel = lv_label_create(myPage, NULL); //create label and puts it on the screen
    lv_obj_align(myLabel, NULL, LV_ALIGN_IN_TOP_LEFT, 5, 0); //set the position to center
    lv_label_set_style(myLabel, &lv_style_plain);
    lv_label_set_text(myLabel, "Button has not been clicked yet"); //sets label text




}

Not full sure what your problem is. I tried your exact code and got this…

LVGL%20Label%20Issue%201

The text appears, through it is over the buttons. To fix that you would just need to change the position.

Also I see you created variables to get the screen width. There are built in constants already:
LV_HOR_RES and LV_VER_RES.

1 Like

Thanks, I will replace those variables. It appears that the text is in fact appearing on my screen but in the top section the same color as the background, but it should be where it is on yours. I’ve also previously attempted giving it different styles with different colors so I do not know why this is happening on my screen. Also do you have any Idea why mine is different than yours?

image

Nevermind, I restarted the PROS Editor and then built and uploaded the code using the buttons in the UI instead of Windows Powershell and it worked. Don’t know which one fixed it or why but…?

1 Like