Get buttons from lv_btnmatrix

Hello,

How to get pointers to buttons in lv_btnmatrix ?

static const char* btnm_map[] = { “Home”, “Sleep”, “Away”, “” };
lv_obj_t* btnm1 = lv_btnmatrix_create(lv_scr_act(), NULL);
lv_btnmatrix_set_map(btnm1, btnm_map);

The btn_matrix object does not contain any btn objects:

The Button Matrix object is a lightweight way to display multiple buttons in rows and columns. Lightweight because the buttons are not actually created but just virtually drawn on the fly. This way, one button use only eight extra bytes of memory instead of the ~100-150 bytes a normal Button object plus the 100 or so bytes for the the Label object.

So you can’t get an lv_obj_t* to the individual buttons.
What is it that you want to achieve?

I would like to know which button is toggled

The way I would do it: Inside the event method:
lv_obj_t * obj = lv_event_get_target(e);
const char * txt = lv_btnmatrix_get_btn_text(obj, lv_btnmatrix_get_selected_btn(obj));

if(strcmp(txt, LV_SYMBOL_BACKSPACE) == 0) {do something}
else if(strcmp(txt, LV_SYMBOL_CUT) == 0) {do something else}
else if(strcmp(txt, "my button label") == 0) { do something else}

If the btnmatrix has one_check enabled you can use:

uint16_t  btnid = lv_btnmatrix_get_selected_btn(obj);

otherwise, itterate over all button id’s to find out which ones are checked:

lv_btnmatrix_ext_t* ext = (lv_btnmatrix_ext_t*)lv_obj_get_ext_attr(obj);

for (uint16_t btnid = 0; i < ext->btn_cnt; i++) {
    if (lv_btnmatrix_has_btn_ctrl(obj, btnid, LV_BTNMATRIX_CTRL_CHECKED)) {
        // button btnid is checked
    }
}