Description
I’m trying to get the name of the button that was pressed in the callback function.
Perhaps I’m just looking at this wrong but I have a handful of buttons on a 3.5" touchscreen. The UI is all working and I’m having no problems updating Labels via code.
I want the buttons to fire off independent code, away from LVGL functions. An example would be a Serial.println()
call, when a button is pressed.
I figured, the likes of:
lv_obj_add_event_cb(objects.btn_up, btn_click_event_handler, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(objects.btn_down, btn_click_event_handler, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(objects.btn_left, btn_click_event_handler, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(objects.btn_right, btn_click_event_handler, LV_EVENT_CLICKED, NULL);
Should work - i.e using a single callback for all buttons, because it would appear that I can then determine the button that fired by:
lv_obj_t *btn = (lv_obj_t *)lv_event_get_target(e);
within the callback function. However, I can’t seem to wrap my head around actually using the btn variable to determine the pressed button.
I was trying to test with something like:
lv_label_set_text(objects.lbl_status, btn);
but it doesn’t work because btn is an object, not a string. All references/examples I can find only have one button, and one callback.
The ‘easy’ alternative is to have a callback function for every button but this seems illogical to me if the calling button can be referenced - I just don’t know how to use it!
What MCU/Processor/Board and compiler are you using?
STM32F407VGT6 ‘barebones’ with Arduino
What LVGL version are you using?
9.2.2
What do you want to achieve?
A single function that I can use for all button callbacks where the pressed button can be determined within the function.