#C callback param in C++ (LVGL library)

6 messages · Page 1 of 1 (latest)

versed boneBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

honest garnet
#

There is no conversion from a lambda with capture, std::function or std::bind() expression towards a pointer to function like lv_anim_exec_xcb_t:

typedef void (*lv_anim_exec_xcb_t)(void*, int32_t)

expected by the function lv_anim_set_exec_cb().
You can only pass a lambda without capture or a free function.

The general way of passing data to such C callback is through a void * user data pointer that somehow is passed from the caller to the callback (usually with an extra parameter). Unfortunately, this one doesn't have one, the first parameter representing the variable to animate.
However I can see in the documentation that lv_anim_t has a field void *user_data, and a similar function lv_anim_custom_exec_cb_t() that passes the lv_anim_t instead as first parameter.
So I think you could do something like:

lv_anim_t a;
// […]
lv_anim_set_user_data(&a, this); // store this is the field user_data 

lv_anim_set_custom_exec_cb(&a, [](lv_anim_t* anim, int32_t val) {
    auto * self = static_cast<Meter *>(lv_anim_get_user_data(anim)); // retrieve this
    self->animCallback(anim->var, val);
});
west bear
# honest garnet There is no conversion from a lambda with capture, `std::function` or `std::bind...

Wow really good answer! I had found some stuff about the lv_anim_t's user data but couldn't get it to work. Seems to be perfect with lv_anim_set_custom_exec_cb. Still not working, getting an unhandled exception when starting the animation. Could be an error on my end. I will also be researching about this par of your answer "You can only pass a lambda without capture or a free function." since I don't fully understand, will be useful for future problems. Thank you very much, I'm in the right direction to solve this!!

versed boneBOT
#

@west bear Has your question been resolved? If so, type !solved :)

west bear
#

!solved

versed boneBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity