From 98530cad3ba8733d8a100e0bc5f3cf47339c4b3e Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sun, 29 Oct 2023 01:09:02 +0100 Subject: [PATCH] Implement data driven dip switches (#22017) * Add data driven dip switches * Autogen weak matrix_mask --- data/mappings/info_config.hjson | 4 +++ data/mappings/info_rules.hjson | 1 + data/schemas/keyboard.jsonschema | 38 ++++++++++++++++++++++- docs/reference_info_json.md | 18 +++++++++++ lib/python/qmk/cli/generate/config_h.py | 8 +++++ lib/python/qmk/cli/generate/keyboard_c.py | 26 ++++++++++++++++ 6 files changed, 94 insertions(+), 1 deletion(-) diff --git a/data/mappings/info_config.hjson b/data/mappings/info_config.hjson index 66aca35e94e..9b65c73084d 100644 --- a/data/mappings/info_config.hjson +++ b/data/mappings/info_config.hjson @@ -45,6 +45,10 @@ // Combos "COMBO_TERM": {"info_key": "combo.term", "value_type": "int"}, + "DIP_SWITCH_MATRIX_GRID": {"info_key": "dip_switch.matrix_grid", "value_type": "array.array.int", "to_json": false}, + "DIP_SWITCH_PINS": {"info_key": "dip_switch.pins", "value_type": "array"}, + "DIP_SWITCH_PINS_RIGHT": {"info_key": "split.dip_switch.right.pins", "value_type": "array"}, + // Dynamic Keymap "DYNAMIC_KEYMAP_EEPROM_MAX_ADDR": {"info_key": "dynamic_keymap.eeprom_max_addr", "value_type": "int"}, "DYNAMIC_KEYMAP_LAYER_COUNT": {"info_key": "dynamic_keymap.layer_count", "value_type": "int"}, diff --git a/data/mappings/info_rules.hjson b/data/mappings/info_rules.hjson index 6a0f0a30303..02fc2bee9de 100644 --- a/data/mappings/info_rules.hjson +++ b/data/mappings/info_rules.hjson @@ -17,6 +17,7 @@ "BOOTLOADER": {"info_key": "bootloader", "warn_duplicate": false}, "BOOTMAGIC_ENABLE": {"info_key": "bootmagic.enabled", "value_type": "bool"}, "CAPS_WORD_ENABLE": {"info_key": "caps_word.enabled", "value_type": "bool"}, + "DIP_SWITCH_ENABLE": {"info_key": "dip_switch.enabled", "value_type": "bool"}, "DEBOUNCE_TYPE": {"info_key": "build.debounce_type"}, "EEPROM_DRIVER": {"info_key": "eeprom.driver"}, "ENCODER_ENABLE": {"info_key": "encoder.enabled", "value_type": "bool"}, diff --git a/data/schemas/keyboard.jsonschema b/data/schemas/keyboard.jsonschema index c1200997453..540ce4e42af 100644 --- a/data/schemas/keyboard.jsonschema +++ b/data/schemas/keyboard.jsonschema @@ -20,7 +20,15 @@ } } } - } + }, + "dip_switch_config": { + "type": "object", + "properties": { + "pins": { + "$ref": "qmk.definitions.v1#/mcu_pin_array" + } + } + }, }, "type": "object", "not": { "required": [ "vendorId", "productId" ] }, // reject via keys... @@ -245,6 +253,25 @@ "type": "array", "items": {"$ref": "qmk.definitions.v1#/filename"} }, + "dip_switch": { + "$ref": "#/definitions/dip_switch_config", + "properties": { + "enabled": {"type": "boolean"}, + "matrix_grid": { + "type": "array", + "minItems": 1, + "items": { + "type": "array", + "minItems": 2, + "maxItems": 2, + "items": { + "type": "integer", + "minimum": 0 + } + } + } + } + }, "eeprom": { "properties": { "driver": {"type": "string"}, @@ -636,6 +663,15 @@ } } }, + "dip_switch": { + "type": "object", + "additionalProperties": false, + "properties": { + "right": { + "$ref": "#/definitions/dip_switch_config" + } + } + }, "encoder": { "type": "object", "additionalProperties": false, diff --git a/docs/reference_info_json.md b/docs/reference_info_json.md index 6b60bb20c41..1d9429b54df 100644 --- a/docs/reference_info_json.md +++ b/docs/reference_info_json.md @@ -201,6 +201,20 @@ Configures the [Combo](feature_combo.md) feature. * The amount of time to recognize a combo in milliseconds. * Default: `50` (50 ms) +## DIP Switches :id=dip-switch + +Configures the [DIP Switches](feature_dip_switch.md) feature. + +* `dip_switch` + * `enabled` + * Enable the DIP Switches feature. + * Default: `false` + * `pins` + * A list of GPIO pins connected to the MCU. + * `matrix_grid` + * A list of matrix locations in the key matrix. + * Example: `[ [0,6], [1,6], [2,6] ]` + ## EEPROM :id=eeprom Configures the [EEPROM](eeprom_driver.md) driver. @@ -622,6 +636,10 @@ Configures the [Split Keyboard](feature_split_keyboard.md) feature. * `bootmagic` * `matrix` * See [Bootmagic](#bootmagic) config. + * `dip_switch` + * `right` + * `pins` + * See [DIP Switches](#dip-switch) config. * `enabled` * Enable the Split Keyboard feature. * Default: `false` diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index 828785ea481..b261d6f851d 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py @@ -72,6 +72,12 @@ def generate_matrix_size(kb_info_json, config_h_lines): config_h_lines.append(generate_define('MATRIX_ROWS', kb_info_json['matrix_size']['rows'])) +def generate_matrix_masked(kb_info_json, config_h_lines): + """"Enable matrix mask if required""" + if 'matrix_grid' in kb_info_json.get('dip_switch', {}): + config_h_lines.append(generate_define('MATRIX_MASKED')) + + def generate_config_items(kb_info_json, config_h_lines): """Iterate through the info_config map to generate basic config values. """ @@ -196,6 +202,8 @@ def generate_config_h(cli): generate_matrix_size(kb_info_json, config_h_lines) + generate_matrix_masked(kb_info_json, config_h_lines) + if 'matrix_pins' in kb_info_json: config_h_lines.append(matrix_pins(kb_info_json['matrix_pins'])) diff --git a/lib/python/qmk/cli/generate/keyboard_c.py b/lib/python/qmk/cli/generate/keyboard_c.py index 9004b41abb2..325624c9cc5 100755 --- a/lib/python/qmk/cli/generate/keyboard_c.py +++ b/lib/python/qmk/cli/generate/keyboard_c.py @@ -57,6 +57,31 @@ def _gen_led_config(info_data): return lines +def _gen_matrix_mask(info_data): + """Convert info.json content to matrix_mask + """ + cols = info_data['matrix_size']['cols'] + rows = info_data['matrix_size']['rows'] + + # Default mask to everything enabled + mask = [['1'] * cols for i in range(rows)] + + # Automatically mask out dip_switch.matrix_grid locations + matrix_grid = info_data.get('dip_switch', {}).get('matrix_grid', []) + for row, col in matrix_grid: + mask[row][col] = '0' + + lines = [] + lines.append('#ifdef MATRIX_MASKED') + lines.append('__attribute__((weak)) const matrix_row_t matrix_mask[] = {') + for i in range(rows): + lines.append(f' 0b{"".join(reversed(mask[i]))},') + lines.append('};') + lines.append('#endif') + + return lines + + @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate keyboard.c for.') @@ -70,6 +95,7 @@ def generate_keyboard_c(cli): keyboard_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#include QMK_KEYBOARD_H', ''] keyboard_h_lines.extend(_gen_led_config(kb_info_json)) + keyboard_h_lines.extend(_gen_matrix_mask(kb_info_json)) # Show the results dump_lines(cli.args.output, keyboard_h_lines, cli.args.quiet)