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 27 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
19 changes: 19 additions & 0 deletions keyboards/handwired/onekey/encodertest/encoder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
"keyboard": "handwired/onekey",
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
"keymap": "encoder",
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
"layout": "LAYOUT",
"layers": [
[
"KC_ENTER"
]
],
"author": "",
"notes": "",
"encoders": [
{
"index": 0,
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
"clockwise": "KC_UP",
"counter": "KC_DOWN"
}
]
}
12 changes: 12 additions & 0 deletions keyboards/handwired/onekey/encodertest/no_encoder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
"keyboard": "handwired/onekey",
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
"keymap": "no_encoder",
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
"layout": "LAYOUT",
"layers": [
[
"KC_ENTER"
]
],
"author": "",
"notes": ""
}
5 changes: 5 additions & 0 deletions lib/python/qmk/cli/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ def compile(cli):
command = None

if cli.args.filename:

# If a configurator JSON was provided generate a keymap and compile it
# FIXME(skullydazed): add code to check and warn if the keymap already exists when compiling a json keymap.

# Parse the configurator json
user_keymap = parse_configurator_json(cli.args.filename)
keymap_path = qmk.path.keymap(user_keymap['keyboard'])

# Compile the keymap
command = compile_configurator_json(user_keymap)
xplusplus marked this conversation as resolved.
Show resolved Hide resolved

cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])
Expand Down
4 changes: 3 additions & 1 deletion lib/python/qmk/cli/flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ 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)

# Convert the JSON into a C file and write it to disk.
command = compile_configurator_json(user_keymap, bootloader=cli.args.bootloader)
xplusplus marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
6 changes: 3 additions & 3 deletions lib/python/qmk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ 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.get('keyboard'), user_keymap.get('keymap'), user_keymap.get('layout'), user_keymap.get('layers'), user_keymap.get('encoders'))
xplusplus marked this conversation as resolved.
Show resolved Hide resolved

# Return a command that can be run to make the keymap and flash if given
if bootloader is None:
return create_make_command(user_keymap['keyboard'], user_keymap['keymap'])
return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader)
return create_make_command(user_keymap.get('keyboard'), user_keymap.get('keymap'))
return create_make_command(user_keymap.get('keyboard'), user_keymap.get('keymap'), bootloader)
xplusplus marked this conversation as resolved.
Show resolved Hide resolved


def parse_configurator_json(configurator_file):
Expand Down
57 changes: 53 additions & 4 deletions lib/python/qmk/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
__KEYMAP_GOES_HERE__
};

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

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}
"""
xplusplus marked this conversation as resolved.
Show resolved Hide resolved


Expand Down Expand Up @@ -48,7 +61,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 @@ -60,6 +73,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 @@ -74,10 +90,40 @@ 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)

encoder_func = ''
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
if encoders:
encoders_txt = []
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
for encoder_set in encoders:
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
curr_encoder = ENCODER_IF.replace('_ELIF_', 'if' if encoder_set.get('index') == 0 else 'else if')
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_set.get('index')))
xplusplus marked this conversation as resolved.
Show resolved Hide resolved

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)

encoder_func = '\n'.join(encoders_txt)[:-1]
xplusplus marked this conversation as resolved.
Show resolved Hide resolved

def write(keyboard, keymap, layout, layers):
keymap_c = keymap_c.replace('__ENCODERS_GO_HERE__', encoder_func)
xplusplus marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -94,8 +140,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
6 changes: 6 additions & 0 deletions lib/python/qmk/tests/test_cli_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ def test_cformat():


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

# json compile
test_json = ['encoder', 'no_encoder']
for j in test_json:
assert check_subcommand('compile', f'keyboards/handwired/onekey/encodertest/{j}.json').returncode == 0 # with encoder

xplusplus marked this conversation as resolved.
Show resolved Hide resolved

def test_flash():
assert check_subcommand('flash', '-b').returncode == 1
Expand Down