LVGL Questions

Hello everyone,

So i have been trying to implement a lvgl auto selector in my code, and so far i have made buttons and made it so that when i click the buttons, it goes to another screen.

But my issues are coming when i try to print values, of say something like the battery capacity or encoders. I tried many things but nothing really worked. So can someone please help me with this problem. Here is what i have so far, (this is my values screen(the screen where all the values are shown))

I think you are missing a call to lv_obj_align() to position your label on the screen.

I would expect something like this to work:

float battery_cap = pros::battery::get_capacity();
char battery_str[32];
::snprintf(battery_str, 32, "Battery Capacity: %f", battery_cap);

lv_obj_t * battery_label =
lv_label_create(lv_scr_act(), NULL); //create label on the screen
lv_label_set_text(battery_label, battery_str); // set label text
lv_obj_align(battery_label, NULL, LV_ALIGN_LEFT_MID, 10, 0); // set position to center

Also you may want to check out this LVGL tutorial for V5 that has various UI element examples.

1 Like

So i tried that and this seems to happen when the values update
So when i click home and then click values screen button, it updates but the previous number is not erased

You are, probably, calling it way too often.

If you have a main loop with a small delay like vex::task::sleep(20) you could wrap you battery display code into conditional statement to update it only once in a couple of seconds:

int counter = 0;
while(1) // main loop
     // read joystick
     // send commands to motors
     if( counter > 100 )
         // update status displays

Another possibility is that you may need to clear the screen area under label.
I don’t have access to V5 atm, maybe, somebody else knows how to fix it.

1 Like

yea because i tried putting a delay and its still the same issue, and the values only update if i go to my home screen and then back to the values screen, which is wierd

Is it possible that you are creating multiple labels in the same location on the screen?

You only need to create label element and attach it to the screen position once.
Then, in the body of the loop, only update label text every cycle.

1 Like

Thanks for the help @technik3k but i seem have got it working with help of people from Discord.

I think I had a problem with this earlier, but just gave up.

For the sake of other people who might have the same problem, do you think you could give a brief explanation how you got it working?

1 Like

So the first that was wrong was that i need to make that set of code a task, because like i said before, it had the trouble of updating on the spot. So a task lets it update while everything else is going on and also the LVGL expects button handlers to immediately return, so it cant draw the screen until the button handler is done. Which i fixed by adding a lambda function (you can read about this online), so something like this pros::Task([] (void *).

Then my second issue was the way i made the actual set code. So the lv_label_set_text() requires a c-style string, but what i had was a normal string. This is was a easy fix, you just had to add a c so besides this .str() it was .c_str(). As for the snprintf i just couldnt get it to work so i had to do a different way by using ostringstream.

This was basically it, btw i didnt come up with these ideas, i like to thank @nickmertin and @John_TYler for all the help and i am sorry if i didnt explain properly, so if you have questions please ask.

1 Like