Several question about button matrix

Description

  1. if I create a button matrix, its width may be 600, it consists of 3 buttons(like A, B, C),so each button’s width is 200. how to hide one the last button©, but each button’s width is still 200, I just hide the last button, but not change the whole size of the button matrix.

2.below is my code, I want to set the button matrix should has one button pressed everytime.
I set the button A pressed first, when I run the code, I clicked the button A, it turn to the unpressed. What I want is even if I always clicked the same button of in a button matrix, it should be pressed always.

What MCU/Processor/Board and compiler are you using?

eclipse

What do you want to achieve?

What have you tried so far?

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

static void btnm_event_cb(lv_obj_t* btnm, lv_event_t event)
{
    if (event != LV_EVENT_CLICKED)
        return;
}

void main ()
{
static const char* btnm_map[] = {"CH A", "CH B", "CH C", ""};

    btnm = lv_btnm_create(parent, NULL);
    lv_btnm_set_map(btnm, btnm_map);
    lv_obj_set_size(btnm, 600, 33);
    lv_btnm_set_btn_ctrl_all(btnm, LV_BTNM_CTRL_TGL_ENABLE);
    lv_btnm_set_one_toggle(btnm, true);
    lv_btnm_set_pressed(btnm, 0);
    lv_obj_align(btnm, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_event_cb(btnm, btnm_event_cb);

    static lv_style_t style_btnm_bg;
    lv_style_copy(&style_btnm_bg, &lv_style_pretty);
    style_btnm_bg.body.padding.top = 0;
    style_btnm_bg.body.padding.bottom = 0;
    style_btnm_bg.body.padding.left = 0;
    style_btnm_bg.body.padding.right = 0;
    style_btnm_bg.body.padding.inner = 0;

    static lv_style_t style_btnm_rel;
    lv_style_copy(&style_btnm_rel, &lv_style_btn_rel);
    style_btnm_rel.body.radius = 0;
    style_btnm_rel.body.padding.top = 0;
    style_btnm_rel.body.padding.bottom = 0;
    style_btnm_rel.body.padding.left = 0;
    style_btnm_rel.body.padding.right = 0;
    style_btnm_rel.body.border.part = LV_BORDER_LEFT | LV_BORDER_RIGHT;
    style_btnm_rel.body.border.width = 1;

    static lv_style_t style_btnm_pr;
    lv_style_copy(&style_btnm_pr, &lv_style_btn_pr);
    style_btnm_pr.body.radius = 0;
    style_btnm_pr.body.border.part = LV_BORDER_LEFT | LV_BORDER_RIGHT;
    style_btnm_pr.body.border.width = 1;

    static lv_style_t style_btnm_tgl_rel;
    lv_style_copy(&style_btnm_tgl_rel, &lv_style_btn_tgl_rel);
    style_btnm_tgl_rel.body.radius = 0;
    style_btnm_tgl_rel.body.border.part = LV_BORDER_LEFT | LV_BORDER_RIGHT;
    style_btnm_tgl_rel.body.border.width = 1;

    static lv_style_t style_btnm_tgl_pr;
    lv_style_copy(&style_btnm_tgl_pr, &lv_style_btn_tgl_pr);
    style_btnm_tgl_pr.body.radius = 0;
    style_btnm_tgl_pr.body.border.part = LV_BORDER_LEFT | LV_BORDER_RIGHT;
    style_btnm_tgl_pr.body.border.width = 1;

    lv_btnm_set_style(btnm, LV_BTNM_STYLE_BG, &style_btnm_bg);
    lv_btnm_set_style(btnm, LV_BTNM_STYLE_BTN_REL, &style_btnm_rel);
    lv_btnm_set_style(btnm, LV_BTNM_STYLE_BTN_PR, &style_btnm_pr);
    lv_btnm_set_style(btnm, LV_BTNM_STYLE_BTN_TGL_REL, &style_btnm_tgl_rel);
    lv_btnm_set_style(btnm, LV_BTNM_STYLE_BTN_TGL_PR, &style_btnm_tgl_pr);
}

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.
hide
always_pressed

Use LV_BTNM_CTRL_HIDDEN to hide btn C and LV_BTNM_CTRL_INACTIVE for button A.

@Ferdz_Reyes is correct use the function:

lv_btnm_set_btn_ctrl(btnm , 2, LV_BTNM_CTRL_HIDDEN); // 2 being the index of the button to hide

Enabling toggling is done the same way:

lv_btnm_set_btn_ctrl(btnm , 0, LV_BTNM_CTRL_TGL_ENABLE); // 0 is button index
1 Like

How to know which button I clicked ?

That information is available in the documentation.

LV_EVENT_VALUE_CHANGED sent when the button is pressed/released or repeated after long press. The event data is set to ID of the pressed/released button.

You can retrieve the event data with lv_event_get_data.

Every time I clicked the botton of btnm, I want to keep one button pressed. For example, I create 3 buttons A, B,C, at first I initial A is pressed by the lv_btnm_set_pressed(btnm, 0), then if I click it A again, it released and the 3 buttons all stay released, it’s not what I want.So I want to know each time the button I clicked , then set the pressed the button pressed. and if I hide the button C or button C and B, when I clicked the B and C button area, it shouldn’ t work if I have pressed the button A.

here is my event code, but it seems doesn’t work.

void btnm_event_cb(lv_obj_t* btnm, lv_event_t event)
{
    uint16_t* btn_id = (uin16_t*)lv_event_get_data();

    lv_btnm_set_pressed(btnm, *btn_id);
}

Sorry its not very clear what you are trying to achieve. Can you maybe show a sequence of images that you want to happen?

There are many events that can be fired inside btnm_event_cb. You should always check for the one you want (LV_EVENT_VALUE_CHANGED).

This would be very useful.