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

Encoder Support in CLI qmk compile #8440

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ef946cb
initial working on python cmd
Mar 15, 2020
9e01eba
merging functions using experimental flag
Mar 15, 2020
642041b
Adding UTs
Mar 16, 2020
a37f578
Adding some cleanup
Mar 16, 2020
4784180
Deleting test files
Mar 16, 2020
635a790
Docs update. Loc needed
Mar 16, 2020
4123646
full switch
Mar 16, 2020
91d393c
Merge branch 'master' into task/lharri/encodersupport
Mar 16, 2020
f68caf7
Changing dirs so the test works correctly
Mar 17, 2020
0d5d1d4
Fixing the json
Mar 17, 2020
e587ec2
Merge branch 'master' into task/lharri/encodersupport
Mar 17, 2020
824f382
PR comments and some optimizations
Mar 19, 2020
1ffd65a
Trying to fix the weird compilation issues on the build server
Mar 19, 2020
6c08c9c
Merge branch 'master' into task/lharri/encodersupport
Mar 21, 2020
5815756
Merge branch 'master' into task/lharri/encodersupport
Mar 21, 2020
cb091b6
Removing extra newline
Mar 22, 2020
c38807a
Merge branch 'master' into task/lharri/encodersupport
Mar 22, 2020
c3787c9
Updating tap_code to tap_code16 to accomadate modifiers per fauxpark …
Mar 22, 2020
b611fff
Enforcing basic keycode usage for encoder support per fauxpark
Mar 22, 2020
190b9d1
pyformat bullshirt
Mar 22, 2020
45cc806
Merge branch 'master' into task/lharri/encodersupport
Mar 23, 2020
bba5bd4
Merge branch 'master' into task/lharri/encodersupport
Mar 28, 2020
9099337
Merge branch 'master' into task/lharri/encodersupport
Apr 5, 2020
42bbe06
Changing test folder name
Apr 5, 2020
03375b2
Merge branch 'master' into task/lharri/encodersupport
Apr 5, 2020
f6778c7
Merge branch 'master' into task/lharri/encodersupport
Apr 5, 2020
9aac181
Merge branch 'master' into task/lharri/encodersupport
Apr 10, 2020
7039ca6
Merge branch 'master' into task/lharri/encodersupport
Apr 20, 2020
c9e379e
Erovia's Latest Comments
Apr 20, 2020
7f8afc3
Merge branch 'master' into task/lharri/encodersupport
Apr 21, 2020
946b042
Removing unnecessary changes
Apr 21, 2020
433361a
Hopefully bringing this back up to master
Dec 16, 2020
2c6cd2a
Some updates
Dec 16, 2020
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
12 changes: 12 additions & 0 deletions keyboards/handwired/onekey/keymaps/default_json/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"keyboard": "handwired/onekey/pytest",
"keymap": "default_json",
"layout": "LAYOUT",
"layers": [
[
"KC_ENTER"
]
],
"author": "",
"notes": ""
}
18 changes: 18 additions & 0 deletions keyboards/handwired/onekey/keymaps/encodertest/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"keyboard": "handwired/onekey/pytest",
"keymap": "encodertest",
"layout": "LAYOUT",
"layers": [
[
"KC_ENTER"
]
],
"author": "",
"notes": "",
"encoders": [
{
"clockwise": "KC_UP",
"counter": "KC_DOWN"
}
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
]
}
2 changes: 1 addition & 1 deletion lib/python/qmk/cli/flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def flash(cli):
# Handle compiling a configurator JSON
user_keymap = parse_configurator_json(cli.args.filename)
keymap_path = qmk.path.keymap(user_keymap['keyboard'])
command = compile_configurator_json(user_keymap, cli.args.bootloader)
command = compile_configurator_json(user_keymap, bootloader=cli.args.bootloader)

cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])

Expand Down
2 changes: 1 addition & 1 deletion lib/python/qmk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def compile_configurator_json(user_keymap, bootloader=None):
A command to run to compile and flash the C file.
"""
# Write the keymap C file
qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'])
qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'], user_keymap.get('encoders'))

# Return a command that can be run to make the keymap and flash if given
if bootloader is None:
Expand Down
56 changes: 52 additions & 4 deletions lib/python/qmk/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
};
"""

xplusplus marked this conversation as resolved.
Show resolved Hide resolved
ENCODER_SUPPORT = """void encoder_update_user(uint8_t index, bool clockwise) {
__ENCODERS_GO_HERE__
};
"""

ENCODER_IF = """\t_ELIF_ (index == _INDEX_) {
\t\tif (clockwise) {
\t\t\ttap_code16(_CLOCKWISE_);
\t\t} else {
\t\t\ttap_code16(_COUNTER_);
\t\t}
\t}"""
Comment on lines +36 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ENCODER_IF = """\t_ELIF_ (index == _INDEX_) {
\t\tif (clockwise) {
\t\t\ttap_code16(_CLOCKWISE_);
\t\t} else {
\t\t\ttap_code16(_COUNTER_);
\t\t}
\t}"""
ENCODER_IF = """ _ELIF_ (index == _INDEX_) {
if (clockwise) {
tap_code16(_CLOCKWISE_);
} else {
tap_code16(_COUNTER_);
}
}"""


Comment on lines +36 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per my previous comment, this will need some logic with some checks to see what layer we're currently in and using the appropriate code.


def template(keyboard):
"""Returns the `keymap.c` template for a keyboard.
Expand Down Expand Up @@ -47,7 +60,7 @@ def _strip_any(keycode):
return keycode


def generate(keyboard, layout, layers):
def generate(keyboard, layout, layers, encoders=None):
"""Returns a keymap.c for the specified keyboard, layout, and layers.

Args:
Expand All @@ -59,6 +72,9 @@ def generate(keyboard, layout, layers):

layers
An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.

encoders
An array of encoders on the keyboard, if any. Each item includes the index and the counter- and clockwise key codes.
"""
layer_txt = []

Expand All @@ -73,10 +89,39 @@ def generate(keyboard, layout, layers):
keymap = '\n'.join(layer_txt)
keymap_c = template(keyboard)

return keymap_c.replace('__KEYMAP_GOES_HERE__', keymap)
keymap_c = keymap_c.replace('__KEYMAP_GOES_HERE__', keymap)

if encoders:
keymap_c += ENCODER_SUPPORT
encoders_txt = []
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
for encoder_num, encoder_set in enumerate(encoders):
curr_encoder = ENCODER_IF.replace('_ELIF_', 'if' if encoder_num == 0 else 'else if')
curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_num))

clockwise = parse_basic_code(encoder_set.get('clockwise'))
counter = parse_basic_code(encoder_set.get('counter'))

curr_encoder = curr_encoder.replace('_CLOCKWISE_', clockwise)
curr_encoder = curr_encoder.replace('_COUNTER_', counter)

encoders_txt.append(curr_encoder)

keymap_c = keymap_c.replace('ENCODERS_GO_HERE__', '\n'.join(encoders_txt))

def write(keyboard, keymap, layout, layers):
return keymap_c


def parse_basic_code(keycode):
"""Performs a simplistic check for a 'basic' keycode. Otherwise it returns 'KC_NO'

Args:
keycode
The keycode the user is trying to apply
"""
return keycode if keycode[:2].lower() == 'kc' else 'KC_NO'
xplusplus marked this conversation as resolved.
Show resolved Hide resolved


def write(keyboard, keymap, layout, layers, encoders=None):
"""Generate the `keymap.c` and write it to disk.

Returns the filename written to.
Expand All @@ -93,8 +138,11 @@ def write(keyboard, keymap, layout, layers):

layers
An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.

encoders
An array of encoders on the keyboard. Each item includes the index and the counter- and clockwise key codes.
"""
keymap_c = generate(keyboard, layout, layers)
keymap_c = generate(keyboard, layout, layers, encoders)
keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.c'

keymap_file.parent.mkdir(parents=True, exist_ok=True)
Expand Down
9 changes: 9 additions & 0 deletions lib/python/qmk/tests/test_cli_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ def test_cformat():
assert result.returncode == 0


def test_compile_json():
assert check_subcommand('compile', 'keyboards/handwired/onekey/keymaps/default_json/keymap.json').returncode == 0


def test_compile_json_encoder():
assert check_subcommand('compile', 'keyboards/handwired/onekey/keymaps/encodertest/keymap.json').returncode == 0


def test_compile():
# keyboard compile
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0


Expand Down