aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoel Challis <[email protected]>2024-06-18 03:44:22 +0100
committerGitHub <[email protected]>2024-06-18 03:44:22 +0100
commit53a0cdc4467fb688faa2708fe75daa26686e918d (patch)
tree0a1fb6640478bf29ca2baaf1003fb0fcc78f8cc1 /lib
parent938badc3b0a8b71647e2463241f2e3fe8578f32a (diff)
downloadqmk_firmware-53a0cdc4467fb688faa2708fe75daa26686e918d.tar.gz
qmk_firmware-53a0cdc4467fb688faa2708fe75daa26686e918d.zip
Implement data driven joysticks (#22947)
Diffstat (limited to 'lib')
-rwxr-xr-xlib/python/qmk/cli/generate/keyboard_c.py38
-rw-r--r--lib/python/qmk/constants.py2
-rw-r--r--lib/python/qmk/info.py15
3 files changed, 52 insertions, 3 deletions
diff --git a/lib/python/qmk/cli/generate/keyboard_c.py b/lib/python/qmk/cli/generate/keyboard_c.py
index 5a6c967486..228b320942 100755
--- a/lib/python/qmk/cli/generate/keyboard_c.py
+++ b/lib/python/qmk/cli/generate/keyboard_c.py
@@ -6,7 +6,7 @@ from qmk.info import info_json
from qmk.commands import dump_lines
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import normpath
-from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
+from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, JOYSTICK_AXES
def _gen_led_configs(info_data):
@@ -91,6 +91,41 @@ def _gen_matrix_mask(info_data):
return lines
+def _gen_joystick_axes(info_data):
+ """Convert info.json content to joystick_axes
+ """
+ if 'axes' not in info_data.get('joystick', {}):
+ return []
+
+ axes = info_data['joystick']['axes']
+ axes_keys = list(axes.keys())
+
+ lines = []
+ lines.append('#ifdef JOYSTICK_ENABLE')
+ lines.append('joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT] = {')
+
+ # loop over all available axes - injecting virtual axis for those not specified
+ for index, cur in enumerate(JOYSTICK_AXES):
+ # bail out if we have generated all requested axis
+ if len(axes_keys) == 0:
+ break
+
+ axis = 'virtual'
+ if cur in axes:
+ axis = axes[cur]
+ axes_keys.remove(cur)
+
+ if axis == 'virtual':
+ lines.append(f" [{index}] = JOYSTICK_AXIS_VIRTUAL,")
+ else:
+ lines.append(f" [{index}] = JOYSTICK_AXIS_IN({axis['input_pin']}, {axis['low']}, {axis['rest']}, {axis['high']}),")
+
+ 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.')
@@ -105,6 +140,7 @@ def generate_keyboard_c(cli):
keyboard_h_lines.extend(_gen_led_configs(kb_info_json))
keyboard_h_lines.extend(_gen_matrix_mask(kb_info_json))
+ keyboard_h_lines.extend(_gen_joystick_axes(kb_info_json))
# Show the results
dump_lines(cli.args.output, keyboard_h_lines, cli.args.quiet)
diff --git a/lib/python/qmk/constants.py b/lib/python/qmk/constants.py
index 90e4452f2b..b6f46180b2 100644
--- a/lib/python/qmk/constants.py
+++ b/lib/python/qmk/constants.py
@@ -320,3 +320,5 @@ LICENSE_TEXTS = [
you may not use this file except in compliance with the License.
"""]),
]
+
+JOYSTICK_AXES = ['x', 'y', 'z', 'rx', 'ry', 'rz']
diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py
index 833271c09c..091a11854c 100644
--- a/lib/python/qmk/info.py
+++ b/lib/python/qmk/info.py
@@ -7,7 +7,7 @@ from dotty_dict import dotty
from milc import cli
-from qmk.constants import COL_LETTERS, ROW_LETTERS, CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS
+from qmk.constants import COL_LETTERS, ROW_LETTERS, CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS, JOYSTICK_AXES
from qmk.c_parse import find_layouts, parse_config_h_file, find_led_config
from qmk.json_schema import deep_update, json_load, validate
from qmk.keyboard import config_h, rules_mk
@@ -249,8 +249,9 @@ def info_json(keyboard):
info_data = _extract_rules_mk(info_data, rules_mk(str(keyboard)))
info_data = _extract_config_h(info_data, config_h(str(keyboard)))
- # Ensure that we have matrix row and column counts
+ # Ensure that we have various calculated values
info_data = _matrix_size(info_data)
+ info_data = _joystick_axis_count(info_data)
# Merge in data from <keyboard.c>
info_data = _extract_led_config(info_data, str(keyboard))
@@ -800,6 +801,16 @@ def _matrix_size(info_data):
return info_data
+def _joystick_axis_count(info_data):
+ """Add info_data['joystick.axis_count'] if required
+ """
+ if 'axes' in info_data.get('joystick', {}):
+ axes_keys = info_data['joystick']['axes'].keys()
+ info_data['joystick']['axis_count'] = max(JOYSTICK_AXES.index(a) for a in axes_keys) + 1 if axes_keys else 0
+
+ return info_data
+
+
def _check_matrix(info_data):
"""Check the matrix to ensure that row/column count is consistent.
"""