How to create dynamic buttons in button matrix

Description

I want to create all buttons of a button matrix from strings I read from a file.

What MCU/Processor/Board and compiler are you using?
ESP32 with ESP-IDF

What LVGL version are you using?
8.3

What do you want to achieve?
I like to change my button text at run time.

What have you tried so far? and code to reproduce

static const char * btnm_map[] = {“Button 1”,“Button 2”, “Button 3”,""};
lv_btnmatrix_set_map(object, btnm_map);

I like to use
char * btnm_map[] = {“Button 1”,“Button 2”, “Button 3”,""};

You will need to create the data structures dynamically and make sure they stay in memory until the btn_matrix is destroyed (e.g. using lv_mem_alloc or malloc). The map_data is an array of pointers to \0 terminated character strings.

I used something similar to this to create a btn_map from JSON:

// Create new btnmatrix button map from file
const char** my_map_create(const char* filename)
    size_t arr_size            = sizeof(char*) * (item_count + 1);      // e.g. 3 buttons + blank item = 4
    const char** map_data_arr = (const char**)lv_mem_alloc(arr_size);   // memory size for the pointer array
    if(map_data_arr == NULL) {
        return NULL;
    }

    // find the total length of all the labels, including \0 termination bytes
    size_t buffer_size =  0;
    buffer_size       += strlen("Button 1") + 1;      // include the \0 byte
    buffer_size       += strlen("Button 2") + 1;
    buffer_size       += strlen("Button 2") + 1;
    buffer_size       += 1;                           // \0 for blank item

    char* buffer = (char*)lv_mem_alloc(buffer_size);  // memory size for all the strings
    if(buffer_addr == NULL) {
        lv_mem_free(map_data_arr);
        return NULL;
    }
    memset(buffer, 0, buffer_size); // Important, last index needs to be 0 => empty string ""

    // Fill buffer
    size_t index = 0;
    size_t pos   = 0;
    for(items) {
        // read label here...

        size_t len = strlen(label) + 1;       // include the \0 byte
        memccpy(buffer + pos, label, 0, len); // Copy the label text into the buffer
        map_data_arr[index++] = buffer + pos; // save pointer to the label in the array
        pos += len;
    }
    map_data_arr[index] = buffer + pos; // save an extra pointer to the terminating \0 byte
    return map_data_arr;
}