Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow encoder config from info.json #17295

Merged
merged 6 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/mappings/info_rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"BOOTLOADER": {"info_key": "bootloader", "warn_duplicate": false},
"BLUETOOTH": {"info_key": "bluetooth.driver"},
"CAPS_WORD_ENABLE": {"info_key": "caps_word.enabled", "value_type": "bool"},
"ENCODER_ENABLE": {"info_key": "encoder.enabled", "value_type": "bool"},
"FIRMWARE_FORMAT": {"info_key": "build.firmware_format"},
"KEYBOARD_SHARED_EP": {"info_key": "usb.shared_endpoint.keyboard", "value_type": "bool"},
"MOUSE_SHARED_EP": {"info_key": "usb.shared_endpoint.mouse", "value_type": "bool"},
Expand Down
35 changes: 35 additions & 0 deletions data/schemas/keyboard.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "qmk.keyboard.v1",
"title": "Keyboard Information",
"definitions": {
"encoder_config": {
"type": "object",
"properties": {
"rotary": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["pin_a", "pin_b"],
"properties": {
"pin_a": {"$ref": "qmk.definitions.v1#/mcu_pin"},
"pin_b": {"$ref": "qmk.definitions.v1#/mcu_pin"},
"resolution": {"$ref": "qmk.definitions.v1#/unsigned_int"}
}
}
}
}
}
},
"type": "object",
"properties": {
"keyboard_name": {"$ref": "qmk.definitions.v1#/text_identifier"},
Expand Down Expand Up @@ -113,6 +133,12 @@
"type": "array",
"items": {"$ref": "qmk.definitions.v1#/filename"}
},
"encoder": {
"$ref": "#/definitions/encoder_config",
"properties": {
"enabled": {"type": "boolean"}
}
},
"features": {"$ref": "qmk.definitions.v1#/boolean_array"},
"indicators": {
"type": "object",
Expand Down Expand Up @@ -362,6 +388,15 @@
}
}
},
"encoder": {
"type": "object",
"additionalProperties": false,
"properties": {
"right": {
"$ref": "#/definitions/encoder_config"
}
}
},
"main": {
"type": "string",
"enum": ["eeprom", "left", "matrix_grid", "pin", "right"]
Expand Down
3 changes: 0 additions & 3 deletions keyboards/zvecr/zv48/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
//#define SELECT_SOFT_SERIAL_SPEED 0
#define SERIAL_USART_SPEED 921600

#define ENCODERS_PAD_A { B5 }
#define ENCODERS_PAD_B { A2 }

#define RGB_DI_PIN B1
#define RGBLED_NUM 48
#define RGBLED_SPLIT {24, 24}
Expand Down
5 changes: 5 additions & 0 deletions keyboards/zvecr/zv48/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
"LAYOUT_ortho_4x12": {
"layout": [{"x":0, "y":0}, {"x":1, "y":0}, {"x":2, "y":0}, {"x":3, "y":0}, {"x":4, "y":0}, {"x":5, "y":0}, {"x":6, "y":0}, {"x":7, "y":0}, {"x":8, "y":0}, {"x":9, "y":0}, {"x":10, "y":0}, {"x":11, "y":0}, {"x":0, "y":1}, {"x":1, "y":1}, {"x":2, "y":1}, {"x":3, "y":1}, {"x":4, "y":1}, {"x":5, "y":1}, {"x":6, "y":1}, {"x":7, "y":1}, {"x":8, "y":1}, {"x":9, "y":1}, {"x":10, "y":1}, {"x":11, "y":1}, {"x":0, "y":2}, {"x":1, "y":2}, {"x":2, "y":2}, {"x":3, "y":2}, {"x":4, "y":2}, {"x":5, "y":2}, {"x":6, "y":2}, {"x":7, "y":2}, {"x":8, "y":2}, {"x":9, "y":2}, {"x":10, "y":2}, {"x":11, "y":2}, {"x":0, "y":3}, {"x":1, "y":3}, {"x":2, "y":3}, {"x":3, "y":3}, {"x":4, "y":3}, {"x":5, "y":3}, {"x":6, "y":3}, {"x":7, "y":3}, {"x":8, "y":3}, {"x":9, "y":3}, {"x":10, "y":3}, {"x":11, "y":3}]
}
},
"encoder": {
"rotary": [
{ "pin_a": "B5", "pin_b": "A2" }
]
}
}
29 changes: 29 additions & 0 deletions lib/python/qmk/cli/generate/config_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ def generate_config_items(kb_info_json, config_h_lines):
config_h_lines.append(f'#endif // {config_key}')


def generate_encoder_config(encoder_json, config_h_lines, postfix=''):
"""Generate the config.h lines for encoders."""
a_pads = []
b_pads = []
resolutions = []
for encoder in encoder_json.get("rotary", []):
a_pads.append(encoder["pin_a"])
b_pads.append(encoder["pin_b"])
resolutions.append(str(encoder.get("resolution", 4)))

config_h_lines.append(f'#ifndef ENCODERS_PAD_A{postfix}')
config_h_lines.append(f'# define ENCODERS_PAD_A{postfix} {{ { ", ".join(a_pads) } }}')
config_h_lines.append(f'#endif // ENCODERS_PAD_A{postfix}')

config_h_lines.append(f'#ifndef ENCODERS_PAD_B{postfix}')
config_h_lines.append(f'# define ENCODERS_PAD_B{postfix} {{ { ", ".join(b_pads) } }}')
config_h_lines.append(f'#endif // ENCODERS_PAD_B{postfix}')

config_h_lines.append(f'#ifndef ENCODER_RESOLUTIONS{postfix}')
config_h_lines.append(f'# define ENCODER_RESOLUTIONS{postfix} {{ { ", ".join(resolutions) } }}')
config_h_lines.append(f'#endif // ENCODER_RESOLUTIONS{postfix}')


def generate_split_config(kb_info_json, config_h_lines):
"""Generate the config.h lines for split boards."""
if 'primary' in kb_info_json['split']:
Expand Down Expand Up @@ -173,6 +196,9 @@ def generate_split_config(kb_info_json, config_h_lines):
if 'right' in kb_info_json['split'].get('matrix_pins', {}):
config_h_lines.append(matrix_pins(kb_info_json['split']['matrix_pins']['right'], '_RIGHT'))

if 'right' in kb_info_json['split'].get('encoder', {}):
generate_encoder_config(kb_info_json['split']['encoder']['right'], config_h_lines, '_RIGHT')


@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")
Expand All @@ -198,6 +224,9 @@ def generate_config_h(cli):
if 'matrix_pins' in kb_info_json:
config_h_lines.append(matrix_pins(kb_info_json['matrix_pins']))

if 'encoder' in kb_info_json:
generate_encoder_config(kb_info_json['encoder'], config_h_lines)

if 'split' in kb_info_json:
generate_split_config(kb_info_json, config_h_lines)

Expand Down
58 changes: 58 additions & 0 deletions lib/python/qmk/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,62 @@ def _extract_audio(info_data, config_c):
info_data['audio'] = {'pins': audio_pins}


def _extract_encoders_values(config_c, postfix=''):
"""Common encoder extraction logic
"""
a_pad = config_c.get(f'ENCODERS_PAD_A{postfix}', '').replace(' ', '')[1:-1]
b_pad = config_c.get(f'ENCODERS_PAD_B{postfix}', '').replace(' ', '')[1:-1]
resolutions = config_c.get(f'ENCODER_RESOLUTIONS{postfix}', '').replace(' ', '')[1:-1]

default_resolution = config_c.get('ENCODER_RESOLUTION', '4')

if a_pad and b_pad:
a_pad = list(filter(None, a_pad.split(',')))
b_pad = list(filter(None, b_pad.split(',')))
resolutions = list(filter(None, resolutions.split(',')))
resolutions += [default_resolution] * (len(a_pad) - len(resolutions))

encoders = []
for index in range(len(a_pad)):
encoders.append({'pin_a': a_pad[index], 'pin_b': b_pad[index], "resolution": int(resolutions[index])})

return encoders


def _extract_encoders(info_data, config_c):
"""Populate data about encoder pins
"""
encoders = _extract_encoders_values(config_c)
if encoders:
if 'encoder' not in info_data:
info_data['encoder'] = {}

if 'rotary' in info_data['encoder']:
_log_warning(info_data, 'Encoder config is specified in both config.h and info.json (encoder.rotary) (Value: %s), the config.h value wins.' % info_data['encoder']['rotary'])

info_data['encoder']['rotary'] = encoders


def _extract_split_encoders(info_data, config_c):
"""Populate data about split encoder pins
"""
encoders = _extract_encoders_values(config_c, '_RIGHT')
if encoders:
if 'split' not in info_data:
info_data['split'] = {}

if 'encoder' not in info_data['split']:
info_data['split']['encoder'] = {}

if 'right' not in info_data['split']['encoder']:
info_data['split']['encoder']['right'] = {}

if 'rotary' in info_data['split']['encoder']['right']:
_log_warning(info_data, 'Encoder config is specified in both config.h and info.json (encoder.rotary) (Value: %s), the config.h value wins.' % info_data['split']['encoder']['right']['rotary'])

info_data['split']['encoder']['right']['rotary'] = encoders


def _extract_secure_unlock(info_data, config_c):
"""Populate data about the secure unlock sequence
"""
Expand Down Expand Up @@ -506,6 +562,8 @@ def _extract_config_h(info_data, config_c):
_extract_split_main(info_data, config_c)
_extract_split_transport(info_data, config_c)
_extract_split_right_pins(info_data, config_c)
_extract_encoders(info_data, config_c)
_extract_split_encoders(info_data, config_c)
_extract_device_version(info_data)

return info_data
Expand Down