Set selecter string for rollers and dropdowns

When I’m developing my project I stumbeled onto a limitation of LVGL, it’s such a great library so I didn’t think there were any.

I have rollers, and as you may know rollers have Index ID and string (name that you see).
I save my value and delete the roller as it’s in a popup. When I load the popup again I want the roller to be set to the saved value. Problem is that the only way to set the roller is via the ID. Via this function
lv_roller_set_selected. There should exist a function that’s named lv_roller_set_selected_str which let’s you set the selected value by the actual name instead of the ID.

I looked into the lv_roller_set_selected function but I’m afraid it’s a bit to complicated for me to make on own. Let’s hope they create this request :slight_smile:

Is there a reason why the ID is hard to save and restore? I think the name version would be much slower, especially if you have many options in the roller.

Thanks for replying so fast, appreciate that!
Okay so the roller has 30 - 150, no reason to have less then 30 as it wont be used in reality.
That will make the ID range 0 - 120, I save the actual 30 - 150 value, which is the String in this case. When I then want to set the roller to that selected value I cant since I need to set it by the ID.

In other parts of my project I will then compare the temperature value (30 - 150) with IF statements, which makes them important in code.

Any good tips for a good solution to this? I think a seperate function would be nice but you might be right with the slowdown thing.

I could be missing something, but couldn’t you just subtract 30 from the temperature and use that as the ID?

2 Likes

I can, and that’s my solution to this, would just be nice with a function to call the String directly. If I knew enough about LVGL core I would write one myself.

1 Like

I’m trying to think of whether the slowdown matters. How frequently would you be calling the function?

1 Like

Since I’m also going to use drop downs which is basically the same thing and has the same logic, I’m going to use this function quite often. My specific project will be inside my summer car. But I plan to continue using LVGL as it’s a very well made library.

So to give a more definitive answer, perhaps 20% of the time that the MCU is running, this will be used.

Hi,

Actually, you don’t have to know much about LVGL’s internals to implement it. All this can be done via API functions:

    const char * opts = lv_roller_get_options(roller1);
    uint32_t id = find_id(opts, "55");  //You need to implement it
    lv_roller_set_selected(roller1, id);

Of course, you can wrap it into a set_selected_str(roller, "55") function.


Although, it seems logical to have a function like this I see a couple of issues with it:

  • In case of a multilanguage UI it might be less clear what string to search for
  • If the options are changes the select functions need to be modified too
  • It more likely that people mistype the string and wonder why it doesn’t work
1 Like

Hello,

Looks like a good solution. I guess I’ll have to get into it more in order to write the find_id function. What I’m missing is a way to extract string and id from a specific roller value. They don’t seem to be comparable since there’s only a way to get the ID. So how would one be able to compare them and thus solving the equation?

Mistyping happens all the time, that’s why we read the documentation right? So I still think such a function would be logical. If not then why can we get the ID in the first place? :slight_smile:

At the same time I’m very new to LVGL so I may have a very different way of looking at it.

The ID is much harder to mistype without noticing. On the other hand I could spell Apple as Aple and not notice.

I would suggest adapting this logic.

2 Likes

That’s a very good solution!
I am going to use that and build myself a function. Thank you all for the help, I’m surprised over how well the community works for this library. I will for sure stay on this and hopefully I will be able to help out someone else in the future!

1 Like

If you do make a function please feel free to open a pull request!

1 Like

On a side note, I really wait to release v8 especially for having the lv_components repo. It will a great place for stuff the are useful and great but for any reason we don’t want to add them in the core lib.

1 Like