refactor: Move to LISTIFY/DT_FOREACH_PROP_ELEM macros.

This commit is contained in:
Peter Johanson 2022-09-20 21:35:40 -04:00 committed by Pete Johanson
parent 69a4c3200d
commit 243a227ff9
10 changed files with 33 additions and 31 deletions

View file

@ -28,7 +28,7 @@ struct kscan_gpio_item_config {
.label = DT_INST_GPIO_LABEL_BY_IDX(n, prop, idx), \ .label = DT_INST_GPIO_LABEL_BY_IDX(n, prop, idx), \
.pin = DT_INST_GPIO_PIN_BY_IDX(n, prop, idx), \ .pin = DT_INST_GPIO_PIN_BY_IDX(n, prop, idx), \
.flags = DT_INST_GPIO_FLAGS_BY_IDX(n, prop, idx), \ .flags = DT_INST_GPIO_FLAGS_BY_IDX(n, prop, idx), \
}, }
// Define row and col cfg // Define row and col cfg
#define _KSCAN_GPIO_INPUT_CFG_INIT(idx, n) _KSCAN_GPIO_ITEM_CFG_INIT(n, input_gpios, idx) #define _KSCAN_GPIO_INPUT_CFG_INIT(idx, n) _KSCAN_GPIO_ITEM_CFG_INIT(n, input_gpios, idx)
@ -240,8 +240,8 @@ struct kscan_gpio_item_config {
}; \ }; \
\ \
static const struct kscan_gpio_config_##n kscan_gpio_config_##n = { \ static const struct kscan_gpio_config_##n kscan_gpio_config_##n = { \
.rows = {UTIL_LISTIFY(INST_MATRIX_INPUTS(n), _KSCAN_GPIO_INPUT_CFG_INIT, n)}, \ .rows = {LISTIFY(INST_MATRIX_INPUTS(n), _KSCAN_GPIO_INPUT_CFG_INIT, (,), n)}, \
.cols = {UTIL_LISTIFY(INST_DEMUX_GPIOS(n), _KSCAN_GPIO_OUTPUT_CFG_INIT, n)}, \ .cols = {LISTIFY(INST_DEMUX_GPIOS(n), _KSCAN_GPIO_OUTPUT_CFG_INIT, (,), n)}, \
}; \ }; \
\ \
DEVICE_DT_INST_DEFINE(n, kscan_gpio_init_##n, NULL, &kscan_gpio_data_##n, \ DEVICE_DT_INST_DEFINE(n, kscan_gpio_init_##n, NULL, &kscan_gpio_data_##n, \

View file

@ -41,7 +41,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#define INST_INPUTS_LEN(n) DT_INST_PROP_LEN(n, input_gpios) #define INST_INPUTS_LEN(n) DT_INST_PROP_LEN(n, input_gpios)
#define KSCAN_DIRECT_INPUT_CFG_INIT(idx, inst_idx) \ #define KSCAN_DIRECT_INPUT_CFG_INIT(idx, inst_idx) \
GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), input_gpios, idx), GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), input_gpios, idx)
struct kscan_direct_irq_callback { struct kscan_direct_irq_callback {
const struct device *dev; const struct device *dev;
@ -327,7 +327,7 @@ static const struct kscan_driver_api kscan_direct_api = {
"ZMK_KSCAN_DEBOUNCE_RELEASE_MS or debounce-release-ms is too large"); \ "ZMK_KSCAN_DEBOUNCE_RELEASE_MS or debounce-release-ms is too large"); \
\ \
static const struct gpio_dt_spec kscan_direct_inputs_##n[] = { \ static const struct gpio_dt_spec kscan_direct_inputs_##n[] = { \
UTIL_LISTIFY(INST_INPUTS_LEN(n), KSCAN_DIRECT_INPUT_CFG_INIT, n)}; \ LISTIFY(INST_INPUTS_LEN(n), KSCAN_DIRECT_INPUT_CFG_INIT, (,), n)}; \
\ \
static struct debounce_state kscan_direct_state_##n[INST_INPUTS_LEN(n)]; \ static struct debounce_state kscan_direct_state_##n[INST_INPUTS_LEN(n)]; \
\ \

View file

@ -50,9 +50,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, pollcode, intcode) COND_CODE_1(CONFIG_ZMK_KSCAN_MATRIX_POLLING, pollcode, intcode)
#define KSCAN_GPIO_ROW_CFG_INIT(idx, inst_idx) \ #define KSCAN_GPIO_ROW_CFG_INIT(idx, inst_idx) \
GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), row_gpios, idx), GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), row_gpios, idx)
#define KSCAN_GPIO_COL_CFG_INIT(idx, inst_idx) \ #define KSCAN_GPIO_COL_CFG_INIT(idx, inst_idx) \
GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), col_gpios, idx), GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst_idx), col_gpios, idx)
enum kscan_diode_direction { enum kscan_diode_direction {
KSCAN_ROW2COL, KSCAN_ROW2COL,
@ -433,10 +433,10 @@ static const struct kscan_driver_api kscan_matrix_api = {
"ZMK_KSCAN_DEBOUNCE_RELEASE_MS or debounce-release-ms is too large"); \ "ZMK_KSCAN_DEBOUNCE_RELEASE_MS or debounce-release-ms is too large"); \
\ \
static const struct gpio_dt_spec kscan_matrix_rows_##n[] = { \ static const struct gpio_dt_spec kscan_matrix_rows_##n[] = { \
UTIL_LISTIFY(INST_ROWS_LEN(n), KSCAN_GPIO_ROW_CFG_INIT, n)}; \ LISTIFY(INST_ROWS_LEN(n), KSCAN_GPIO_ROW_CFG_INIT, (,), n)}; \
\ \
static const struct gpio_dt_spec kscan_matrix_cols_##n[] = { \ static const struct gpio_dt_spec kscan_matrix_cols_##n[] = { \
UTIL_LISTIFY(INST_COLS_LEN(n), KSCAN_GPIO_COL_CFG_INIT, n)}; \ LISTIFY(INST_COLS_LEN(n), KSCAN_GPIO_COL_CFG_INIT, (,), n)}; \
\ \
static struct debounce_state kscan_matrix_state_##n[INST_MATRIX_LEN(n)]; \ static struct debounce_state kscan_matrix_state_##n[INST_MATRIX_LEN(n)]; \
\ \

View file

@ -168,7 +168,7 @@ static int behavior_caps_word_init(const struct device *dev) {
#define PARSE_BREAK(i) \ #define PARSE_BREAK(i) \
{.page = ZMK_HID_USAGE_PAGE(i), \ {.page = ZMK_HID_USAGE_PAGE(i), \
.id = ZMK_HID_USAGE_ID(i), \ .id = ZMK_HID_USAGE_ID(i), \
.implicit_modifiers = SELECT_MODS(i)}, .implicit_modifiers = SELECT_MODS(i)}
#define BREAK_ITEM(i, n) PARSE_BREAK(DT_INST_PROP_BY_IDX(n, continue_list, i)) #define BREAK_ITEM(i, n) PARSE_BREAK(DT_INST_PROP_BY_IDX(n, continue_list, i))
@ -177,7 +177,7 @@ static int behavior_caps_word_init(const struct device *dev) {
static struct behavior_caps_word_config behavior_caps_word_config_##n = { \ static struct behavior_caps_word_config behavior_caps_word_config_##n = { \
.index = n, \ .index = n, \
.mods = DT_INST_PROP_OR(n, mods, MOD_LSFT), \ .mods = DT_INST_PROP_OR(n, mods, MOD_LSFT), \
.continuations = {UTIL_LISTIFY(DT_INST_PROP_LEN(n, continue_list), BREAK_ITEM, n)}, \ .continuations = {LISTIFY(DT_INST_PROP_LEN(n, continue_list), BREAK_ITEM, (,), n)}, \
.continuations_count = DT_INST_PROP_LEN(n, continue_list), \ .continuations_count = DT_INST_PROP_LEN(n, continue_list), \
}; \ }; \
DEVICE_DT_INST_DEFINE(n, behavior_caps_word_init, NULL, &behavior_caps_word_data_##n, \ DEVICE_DT_INST_DEFINE(n, behavior_caps_word_init, NULL, &behavior_caps_word_data_##n, \

View file

@ -166,10 +166,10 @@ static const struct behavior_driver_api behavior_macro_driver_api = {
.binding_released = on_macro_binding_released, .binding_released = on_macro_binding_released,
}; };
#define BINDING_WITH_COMMA(idx, drv_inst) ZMK_KEYMAP_EXTRACT_BINDING(idx, DT_DRV_INST(drv_inst)), #define BINDING_WITH_COMMA(idx, drv_inst) ZMK_KEYMAP_EXTRACT_BINDING(idx, DT_DRV_INST(drv_inst))
#define TRANSFORMED_BEHAVIORS(n) \ #define TRANSFORMED_BEHAVIORS(n) \
{UTIL_LISTIFY(DT_PROP_LEN(DT_DRV_INST(n), bindings), BINDING_WITH_COMMA, n)}, {LISTIFY(DT_PROP_LEN(DT_DRV_INST(n), bindings), BINDING_WITH_COMMA, (,), n)},
#define MACRO_INST(n) \ #define MACRO_INST(n) \
static struct behavior_macro_state behavior_macro_state_##n = {}; \ static struct behavior_macro_state behavior_macro_state_##n = {}; \

View file

@ -237,10 +237,10 @@ static int behavior_tap_dance_init(const struct device *dev) {
return 0; return 0;
} }
#define _TRANSFORM_ENTRY(idx, node) ZMK_KEYMAP_EXTRACT_BINDING(idx, node), #define _TRANSFORM_ENTRY(idx, node) ZMK_KEYMAP_EXTRACT_BINDING(idx, node)
#define TRANSFORMED_BINDINGS(node) \ #define TRANSFORMED_BINDINGS(node) \
{ UTIL_LISTIFY(DT_INST_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, DT_DRV_INST(node)) } { LISTIFY(DT_INST_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, (,), DT_DRV_INST(node)) }
#define KP_INST(n) \ #define KP_INST(n) \
static struct zmk_behavior_binding \ static struct zmk_behavior_binding \

View file

@ -33,13 +33,12 @@ struct conditional_layer_cfg {
int8_t then_layer; int8_t then_layer;
}; };
#define IF_LAYER_BIT(i, n) BIT(DT_PROP_BY_IDX(n, if_layers, i)) | #define IF_LAYER_BIT(node_id, prop, idx) BIT(DT_PROP_BY_IDX(node_id, prop, idx)) |
// Evaluates to conditional_layer_cfg struct initializer. // Evaluates to conditional_layer_cfg struct initializer.
#define CONDITIONAL_LAYER_DECL(n) \ #define CONDITIONAL_LAYER_DECL(n) \
{ \ { \
/* TODO: Replace UTIL_LISTIFY with DT_FOREACH_PROP_ELEM after Zepyhr 2.6.0 upgrade. */ \ .if_layers_state_mask = DT_FOREACH_PROP_ELEM(n, if_layers, IF_LAYER_BIT) 0, \
.if_layers_state_mask = UTIL_LISTIFY(DT_PROP_LEN(n, if_layers), IF_LAYER_BIT, n) 0, \
.then_layer = DT_PROP(n, then_layer), \ .then_layer = DT_PROP(n, then_layer), \
}, },

View file

@ -34,10 +34,10 @@ static uint8_t _zmk_keymap_layer_default = 0;
#define ZMK_KEYMAP_NODE DT_DRV_INST(0) #define ZMK_KEYMAP_NODE DT_DRV_INST(0)
#define ZMK_KEYMAP_LAYERS_LEN (DT_INST_FOREACH_CHILD(0, LAYER_CHILD_LEN) 0) #define ZMK_KEYMAP_LAYERS_LEN (DT_INST_FOREACH_CHILD(0, LAYER_CHILD_LEN) 0)
#define BINDING_WITH_COMMA(idx, drv_inst) ZMK_KEYMAP_EXTRACT_BINDING(idx, drv_inst), #define BINDING_WITH_COMMA(idx, drv_inst) ZMK_KEYMAP_EXTRACT_BINDING(idx, drv_inst)
#define TRANSFORMED_LAYER(node) \ #define TRANSFORMED_LAYER(node) \
{UTIL_LISTIFY(DT_PROP_LEN(node, bindings), BINDING_WITH_COMMA, node)}, {LISTIFY(DT_PROP_LEN(node, bindings), BINDING_WITH_COMMA, (,), node)},
#if ZMK_KEYMAP_HAS_SENSORS #if ZMK_KEYMAP_HAS_SENSORS
#define _TRANSFORM_SENSOR_ENTRY(idx, layer) \ #define _TRANSFORM_SENSOR_ENTRY(idx, layer) \
@ -47,12 +47,12 @@ static uint8_t _zmk_keymap_layer_default = 0;
(DT_PHA_BY_IDX(layer, sensor_bindings, idx, param1))), \ (DT_PHA_BY_IDX(layer, sensor_bindings, idx, param1))), \
.param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, sensor_bindings, idx, param2), (0), \ .param2 = COND_CODE_0(DT_PHA_HAS_CELL_AT_IDX(layer, sensor_bindings, idx, param2), (0), \
(DT_PHA_BY_IDX(layer, sensor_bindings, idx, param2))), \ (DT_PHA_BY_IDX(layer, sensor_bindings, idx, param2))), \
}, }
#define SENSOR_LAYER(node) \ #define SENSOR_LAYER(node) \
COND_CODE_1( \ COND_CODE_1( \
DT_NODE_HAS_PROP(node, sensor_bindings), \ DT_NODE_HAS_PROP(node, sensor_bindings), \
({UTIL_LISTIFY(DT_PROP_LEN(node, sensor_bindings), _TRANSFORM_SENSOR_ENTRY, node)}), \ ({LISTIFY(DT_PROP_LEN(node, sensor_bindings), _TRANSFORM_SENSOR_ENTRY, (,), node)}), \
({})), ({})),
#endif /* ZMK_KEYMAP_HAS_SENSORS */ #endif /* ZMK_KEYMAP_HAS_SENSORS */

View file

@ -13,9 +13,9 @@
#define _TRANSFORM_ENTRY(i, _) \ #define _TRANSFORM_ENTRY(i, _) \
[(KT_ROW(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i)) * ZMK_MATRIX_COLS) + \ [(KT_ROW(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i)) * ZMK_MATRIX_COLS) + \
KT_COL(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i))] = i, KT_COL(DT_PROP_BY_IDX(ZMK_KEYMAP_TRANSFORM_NODE, map, i))] = i
static uint32_t transform[] = {UTIL_LISTIFY(ZMK_KEYMAP_LEN, _TRANSFORM_ENTRY, 0)}; static uint32_t transform[] = {LISTIFY(ZMK_KEYMAP_LEN, _TRANSFORM_ENTRY, (,), 0)};
#endif #endif

View file

@ -25,12 +25,15 @@ struct sensors_data_item {
}; };
#define _SENSOR_ITEM(node) \ #define _SENSOR_ITEM(node) \
{.dev = NULL, .trigger = {.type = SENSOR_TRIG_DELTA, .chan = SENSOR_CHAN_ROTATION}}, { \
#define SENSOR_ITEM(idx, _) \ .dev = NULL, .trigger = {.type = SENSOR_TRIG_DELTA, .chan = SENSOR_CHAN_ROTATION } \
COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx), okay), \ }
(_SENSOR_ITEM(ZMK_KEYMAP_SENSORS_BY_IDX(idx))), ())
static struct sensors_data_item sensors[] = {UTIL_LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_ITEM, 0)}; #define SENSOR_ITEM(idx, _node) \
COND_CODE_1(DT_NODE_HAS_STATUS(ZMK_KEYMAP_SENSORS_BY_IDX(idx), okay), \
(_SENSOR_ITEM(ZMK_KEYMAP_SENSORS_BY_IDX(idx))), ({}))
static struct sensors_data_item sensors[] = {LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_ITEM, (, ), 0)};
static void zmk_sensors_trigger_handler(const struct device *dev, struct sensor_trigger *trigger) { static void zmk_sensors_trigger_handler(const struct device *dev, struct sensor_trigger *trigger) {
int err; int err;
@ -71,7 +74,7 @@ static int zmk_sensors_init(const struct device *_arg) {
int local_index = 0; int local_index = 0;
int absolute_index = 0; int absolute_index = 0;
UTIL_LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_INIT, 0) LISTIFY(ZMK_KEYMAP_SENSORS_LEN, SENSOR_INIT, (), 0)
return 0; return 0;
} }