arrayscc-preprocessorqmkqmk-firmware

How can I unpack an array as separate macro arguments?


I am using the QMK library, which has a LAYOUT macro that takes many parameters. It is used like so (with KC_xxx etc constants):

const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[baselayer] = LAYOUT( /* Dvorak without modifiers. Never switched to, just as base for the combos*/
    KC_QUOT   ,KC_COMM   ,KC_DOT    ,KC_P      ,KC_Y     ,                      KC_F      ,KC_G      ,KC_C      ,KC_R      ,KC_L      ,
    KC_A      ,KC_O      ,KC_E      ,KC_U      ,KC_I     ,                      KC_D      ,KC_H      ,KC_T      ,KC_N      ,KC_S      ,
    KC_SCLN   ,KC_Q      ,KC_J      ,KC_K      ,KC_X     ,XXXXXXX   ,XXXXXXX   ,KC_B      ,KC_M      ,KC_W      ,KC_V      ,KC_Z      ,
    XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX  ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX
),
//... more layers
};

I'd like to split this code in an array and use that in the macro call:

// Define the array
const uint16_t BASE[] = {
    KC_QUOT   ,KC_COMM   ,KC_DOT    ,KC_P      ,KC_Y     ,                      KC_F      ,KC_G      ,KC_C      ,KC_R      ,KC_L      ,
    KC_A      ,KC_O      ,KC_E      ,KC_U      ,KC_I     ,                      KC_D      ,KC_H      ,KC_T      ,KC_N      ,KC_S      ,
    KC_SCLN   ,KC_Q      ,KC_J      ,KC_K      ,KC_X     ,XXXXXXX   ,XXXXXXX   ,KC_B      ,KC_M      ,KC_W      ,KC_V      ,KC_Z      ,
    XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX  ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX
};

// TODO: manipulate array

// Use the array
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[baselayer] = LAYOUT(BASE), // <-- how to write this line?
//... more layers
};

How do I change the indicated line of code so that the array BASE is unpacked and its elements used as the macro arguments?

Currently the compiler tells me

error: macro "LAYOUT" requires 44 arguments, but only 1 given


Solution

  • LAYOUT is a C preprocessor macro. You can't apply an array to a C preprocessor macro. It has to stay in the preprocessor world.

    #define BASE \
        KC_QUOT   ,KC_COMM   ,KC_DOT    ,KC_P      ,KC_Y     ,                      KC_F      ,KC_G      ,KC_C      ,KC_R      ,KC_L      , \
        KC_A      ,KC_O      ,KC_E      ,KC_U      ,KC_I     ,                      KC_D      ,KC_H      ,KC_T      ,KC_N      ,KC_S      , \
        KC_SCLN   ,KC_Q      ,KC_J      ,KC_K      ,KC_X     ,XXXXXXX   ,XXXXXXX   ,KC_B      ,KC_M      ,KC_W      ,KC_V      ,KC_Z      , \
        XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX  ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX
    
    #define EXPAND_THEN_LAYOUT(x)  LAYOUT(x)
    
    const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        [baselayer] = EXPAND_THEN_LAYOUT(BASE),
    };
    

    I think I would do:

    /// Dvorak without modifiers. Never switched to, just as base for the combos.
    #define BASE_LAYOUT() LAYOUT( \
    KC_QUOT   ,KC_COMM   ,KC_DOT    ,KC_P      ,KC_Y     ,                      KC_F      ,KC_G      ,KC_C      ,KC_R      ,KC_L      , \
    KC_A      ,KC_O      ,KC_E      ,KC_U      ,KC_I     ,                      KC_D      ,KC_H      ,KC_T      ,KC_N      ,KC_S      , \
    KC_SCLN   ,KC_Q      ,KC_J      ,KC_K      ,KC_X     ,XXXXXXX   ,XXXXXXX   ,KC_B      ,KC_M      ,KC_W      ,KC_V      ,KC_Z      , \
    XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX  ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX   ,XXXXXXX \
    )
    
    const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
        [baselayer] = BASE_LAYOUT(),
    };
    

    See What is the the best way to ask follow up questions?.

    I want to insert a few values at certain index positions

    Write a macro that replaces a certain position.

    #define CHANGE_5_to_XXXX_IN(_1, _2, _3, _4, _5, ...) \
         _1, _2, _3, _4, XXXX, __VA_ARGS__
    #define CHANGE_5_to_XXXX(...)  CHANGE_5_to_XXXX_IN(__VA_ARGS__)
    
    EXPAND_THEN_LAYOUT(CHANGE_5_to_XXXX(BASE))