Skip to content

Commit

Permalink
Allow encoder config from info.json (qmk#17295)
Browse files Browse the repository at this point in the history
  • Loading branch information
zvecr committed Jun 21, 2022
1 parent 4c39bad commit 1a400d8
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/mappings/info_rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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": "https://json-schema.org/draft/2020-12/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 @@ -363,6 +389,15 @@
}
}
},
"encoder": {
"type": "object",
"additionalProperties": false,
"properties": {
"right": {
"$ref": "#/definitions/encoder_config"
}
}
},
"main": {
"type": "string",
"enum": ["eeprom", "left", "matrix_grid", "pin", "right"]
Expand Down
36 changes: 36 additions & 0 deletions docs/reference_info_json.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,39 @@ Example:
```

The device version is a BCD (binary coded decimal) value, in the format `MMmr`, so the below value would look like `0x0100` in the generated code. This also means the maximum valid values for each part are `99.9.9`, despite it being a hexadecimal value under the hood.

### Encoders

This section controls the basic [rotary encoder](feature_encoders.md) support.

The following items can be set. Not every value is required.

* `pin_a`
* __Required__. A pad definition
* `pin_b`
* __Required__. B pad definition
* `resolution`
* How many pulses the encoder registers between each detent

Examples:

```json
{
"encoder": {
"rotary": [
{ "pin_a": "B5", "pin_b": "A2" }
]
}
}
```

```json
{
"encoder": {
"rotary": [
{ "pin_a": "B5", "pin_b": "A2", "resolution": 4 }
{ "pin_a": "B6", "pin_b": "A3", "resolution": 2 }
]
}
}
```
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 @@ -218,6 +218,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

0 comments on commit 1a400d8

Please sign in to comment.