summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--builddefs/build_keyboard.mk8
-rw-r--r--data/schemas/keyboard.jsonschema56
-rw-r--r--lib/python/qmk/c_parse.py118
-rw-r--r--lib/python/qmk/cli/__init__.py1
-rwxr-xr-xlib/python/qmk/cli/generate/keyboard_c.py75
-rw-r--r--lib/python/qmk/info.py45
-rwxr-xr-xlib/python/qmk/json_encoders.py4
7 files changed, 303 insertions, 4 deletions
diff --git a/builddefs/build_keyboard.mk b/builddefs/build_keyboard.mk
index 4503f018a8..dc86b232df 100644
--- a/builddefs/build_keyboard.mk
+++ b/builddefs/build_keyboard.mk
@@ -322,12 +322,18 @@ ifneq ("$(wildcard $(KEYBOARD_PATH_5)/info.json)","")
endif
CONFIG_H += $(KEYBOARD_OUTPUT)/src/info_config.h $(KEYBOARD_OUTPUT)/src/layouts.h
+KEYBOARD_SRC += $(KEYBOARD_OUTPUT)/src/default_keyboard.c
$(KEYBOARD_OUTPUT)/src/info_config.h: $(INFO_JSON_FILES)
@$(SILENT) || printf "$(MSG_GENERATING) $@" | $(AWK_CMD)
$(eval CMD=$(QMK_BIN) generate-config-h --quiet --keyboard $(KEYBOARD) --output $(KEYBOARD_OUTPUT)/src/info_config.h)
@$(BUILD_CMD)
+$(KEYBOARD_OUTPUT)/src/default_keyboard.c: $(INFO_JSON_FILES)
+ @$(SILENT) || printf "$(MSG_GENERATING) $@" | $(AWK_CMD)
+ $(eval CMD=$(QMK_BIN) generate-keyboard-c --quiet --keyboard $(KEYBOARD) --output $(KEYBOARD_OUTPUT)/src/default_keyboard.c)
+ @$(BUILD_CMD)
+
$(KEYBOARD_OUTPUT)/src/default_keyboard.h: $(INFO_JSON_FILES)
@$(SILENT) || printf "$(MSG_GENERATING) $@" | $(AWK_CMD)
$(eval CMD=$(QMK_BIN) generate-keyboard-h --quiet --keyboard $(KEYBOARD) --output $(KEYBOARD_OUTPUT)/src/default_keyboard.h)
@@ -338,7 +344,7 @@ $(KEYBOARD_OUTPUT)/src/layouts.h: $(INFO_JSON_FILES)
$(eval CMD=$(QMK_BIN) generate-layouts --quiet --keyboard $(KEYBOARD) --output $(KEYBOARD_OUTPUT)/src/layouts.h)
@$(BUILD_CMD)
-generated-files: $(KEYBOARD_OUTPUT)/src/info_config.h $(KEYBOARD_OUTPUT)/src/default_keyboard.h $(KEYBOARD_OUTPUT)/src/layouts.h
+generated-files: $(KEYBOARD_OUTPUT)/src/info_config.h $(KEYBOARD_OUTPUT)/src/default_keyboard.c $(KEYBOARD_OUTPUT)/src/default_keyboard.h $(KEYBOARD_OUTPUT)/src/layouts.h
.INTERMEDIATE : generated-files
diff --git a/data/schemas/keyboard.jsonschema b/data/schemas/keyboard.jsonschema
index aaf6f887d5..ba1a6b6cc9 100644
--- a/data/schemas/keyboard.jsonschema
+++ b/data/schemas/keyboard.jsonschema
@@ -212,6 +212,62 @@
"timeout": {"$ref": "qmk.definitions.v1#/unsigned_int"}
}
},
+ "led_matrix": {
+ "type": "object",
+ "properties": {
+ "driver": {"type": "string"},
+ "layout": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "additionalProperties": false,
+ "properties": {
+ "matrix": {
+ "type": "array",
+ "minItems": 2,
+ "maxItems": 2,
+ "items": {
+ "type": "number",
+ "min": 0,
+ "multipleOf": 1
+ }
+ },
+ "x": {"$ref": "qmk.definitions.v1#/key_unit"},
+ "y": {"$ref": "qmk.definitions.v1#/key_unit"},
+ "flags": {"$ref": "qmk.definitions.v1#/unsigned_decimal"}
+ }
+ }
+ }
+ }
+ },
+ "rgb_matrix": {
+ "type": "object",
+ "properties": {
+ "driver": {"type": "string"},
+ "layout": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "additionalProperties": false,
+ "properties": {
+ "matrix": {
+ "type": "array",
+ "minItems": 2,
+ "maxItems": 2,
+ "items": {
+ "type": "number",
+ "min": 0,
+ "multipleOf": 1
+ }
+ },
+ "x": {"$ref": "qmk.definitions.v1#/key_unit"},
+ "y": {"$ref": "qmk.definitions.v1#/key_unit"},
+ "flags": {"$ref": "qmk.definitions.v1#/unsigned_decimal"}
+ }
+ }
+ }
+ }
+ },
"rgblight": {
"type": "object",
"additionalProperties": false,
diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py
index 72be690019..359aaccbbc 100644
--- a/lib/python/qmk/c_parse.py
+++ b/lib/python/qmk/c_parse.py
@@ -1,5 +1,9 @@
"""Functions for working with config.h files.
"""
+from pygments.lexers.c_cpp import CLexer
+from pygments.token import Token
+from pygments import lex
+from itertools import islice
from pathlib import Path
import re
@@ -13,6 +17,13 @@ multi_comment_regex = re.compile(r'/\*(.|\n)*?\*/', re.MULTILINE)
layout_macro_define_regex = re.compile(r'^#\s*define')
+def _get_chunks(it, size):
+ """Break down a collection into smaller parts
+ """
+ it = iter(it)
+ return iter(lambda: tuple(islice(it, size)), ())
+
+
def strip_line_comment(string):
"""Removes comments from a single line string.
"""
@@ -170,3 +181,110 @@ def _parse_matrix_locations(matrix, file, macro_name):
matrix_locations[identifier] = [row_num, col_num]
return matrix_locations
+
+
+def _coerce_led_token(_type, value):
+ """ Convert token to valid info.json content
+ """
+ value_map = {
+ 'NO_LED': None,
+ 'LED_FLAG_ALL': 0xFF,
+ 'LED_FLAG_NONE': 0x00,
+ 'LED_FLAG_MODIFIER': 0x01,
+ 'LED_FLAG_UNDERGLOW': 0x02,
+ 'LED_FLAG_KEYLIGHT': 0x04,
+ 'LED_FLAG_INDICATOR': 0x08,
+ }
+ if _type is Token.Literal.Number.Integer:
+ return int(value)
+ if _type is Token.Literal.Number.Float:
+ return float(value)
+ if _type is Token.Literal.Number.Hex:
+ return int(value, 0)
+ if _type is Token.Name and value in value_map.keys():
+ return value_map[value]
+
+
+def _parse_led_config(file, matrix_cols, matrix_rows):
+ """Return any 'raw' led/rgb matrix config
+ """
+ file_contents = file.read_text(encoding='utf-8')
+ file_contents = comment_remover(file_contents)
+ file_contents = file_contents.replace('\\\n', '')
+
+ matrix_raw = []
+ position_raw = []
+ flags = []
+
+ found_led_config = False
+ bracket_count = 0
+ section = 0
+ for _type, value in lex(file_contents, CLexer()):
+ # Assume g_led_config..stuff..;
+ if value == 'g_led_config':
+ found_led_config = True
+ elif value == ';':
+ found_led_config = False
+ elif found_led_config:
+ # Assume bracket count hints to section of config we are within
+ if value == '{':
+ bracket_count += 1
+ if bracket_count == 2:
+ section += 1
+ elif value == '}':
+ bracket_count -= 1
+ else:
+ # Assume any non whitespace value here is important enough to stash
+ if _type in [Token.Literal.Number.Integer, Token.Literal.Number.Float, Token.Literal.Number.Hex, Token.Name]:
+ if section == 1 and bracket_count == 3:
+ matrix_raw.append(_coerce_led_token(_type, value))
+ if section == 2 and bracket_count == 3:
+ position_raw.append(_coerce_led_token(_type, value))
+ if section == 3 and bracket_count == 2:
+ flags.append(_coerce_led_token(_type, value))
+
+ # Slightly better intrim format
+ matrix = list(_get_chunks(matrix_raw, matrix_cols))
+ position = list(_get_chunks(position_raw, 2))
+ matrix_indexes = list(filter(lambda x: x is not None, matrix_raw))
+
+ # If we have not found anything - bail
+ if not section:
+ return None
+
+ # TODO: Improve crude parsing/validation
+ if len(matrix) != matrix_rows and len(matrix) != (matrix_rows / 2):
+ raise ValueError("Unable to parse g_led_config matrix data")
+ if len(position) != len(flags):
+ raise ValueError("Unable to parse g_led_config position data")
+ if len(matrix_indexes) and (max(matrix_indexes) >= len(flags)):
+ raise ValueError("OOB within g_led_config matrix data")
+
+ return (matrix, position, flags)
+
+
+def find_led_config(file, matrix_cols, matrix_rows):
+ """Search file for led/rgb matrix config
+ """
+ found = _parse_led_config(file, matrix_cols, matrix_rows)
+ if not found:
+ return None
+
+ # Expand collected content
+ (matrix, position, flags) = found
+
+ # Align to output format
+ led_config = []
+ for index, item in enumerate(position, start=0):
+ led_config.append({
+ 'x': item[0],
+ 'y': item[1],
+ 'flags': flags[index],
+ })
+ for r in range(len(matrix)):
+ for c in range(len(matrix[r])):
+ index = matrix[r][c]
+ if index is not None:
+ led_config[index]['matrix'] = [r, c]
+
+ return led_config
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index 85baa238a8..d7192631a3 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -52,6 +52,7 @@ subcommands = [
'qmk.cli.generate.dfu_header',
'qmk.cli.generate.docs',
'qmk.cli.generate.info_json',
+ 'qmk.cli.generate.keyboard_c',
'qmk.cli.generate.keyboard_h',
'qmk.cli.generate.layouts',
'qmk.cli.generate.rgb_breathe_table',
diff --git a/lib/python/qmk/cli/generate/keyboard_c.py b/lib/python/qmk/cli/generate/keyboard_c.py
new file mode 100755
index 0000000000..a9b742f323
--- /dev/null
+++ b/lib/python/qmk/cli/generate/keyboard_c.py
@@ -0,0 +1,75 @@
+"""Used by the make system to generate keyboard.c from info.json.
+"""
+from milc import cli
+
+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
+
+
+def _gen_led_config(info_data):
+ """Convert info.json content to g_led_config
+ """
+ cols = info_data['matrix_size']['cols']
+ rows = info_data['matrix_size']['rows']
+
+ config_type = None
+ if 'layout' in info_data.get('rgb_matrix', {}):
+ config_type = 'rgb_matrix'
+ elif 'layout' in info_data.get('led_matrix', {}):
+ config_type = 'led_matrix'
+
+ lines = []
+ if not config_type:
+ return lines
+
+ matrix = [['NO_LED'] * cols for i in range(rows)]
+ pos = []
+ flags = []
+
+ led_config = info_data[config_type]['layout']
+ for index, item in enumerate(led_config, start=0):
+ if 'matrix' in item:
+ (x, y) = item['matrix']
+ matrix[x][y] = str(index)
+ pos.append(f'{{ {item.get("x", 0)},{item.get("y", 0)} }}')
+ flags.append(str(item.get('flags', 0)))
+
+ if config_type == 'rgb_matrix':
+ lines.append('#ifdef RGB_MATRIX_ENABLE')
+ lines.append('#include "rgb_matrix.h"')
+ elif config_type == 'led_matrix':
+ lines.append('#ifdef LED_MATRIX_ENABLE')
+ lines.append('#include "led_matrix.h"')
+
+ lines.append('__attribute__ ((weak)) led_config_t g_led_config = {')
+ lines.append(' {')
+ for line in matrix:
+ lines.append(f' {{ {",".join(line)} }},')
+ lines.append(' },')
+ lines.append(f' {{ {",".join(pos)} }},')
+ lines.append(f' {{ {",".join(flags)} }},')
+ 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.')
+@cli.subcommand('Used by the make system to generate keyboard.c from info.json', hidden=True)
+def generate_keyboard_c(cli):
+ """Generates the keyboard.h file.
+ """
+ kb_info_json = info_json(cli.args.keyboard)
+
+ # Build the layouts.h file.
+ keyboard_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#include QMK_KEYBOARD_H', '']
+
+ keyboard_h_lines.extend(_gen_led_config(kb_info_json))
+
+ # Show the results
+ dump_lines(cli.args.output, keyboard_h_lines, cli.args.quiet)
diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py
index 49d1054519..0763433b3d 100644
--- a/lib/python/qmk/info.py
+++ b/lib/python/qmk/info.py
@@ -8,7 +8,7 @@ from dotty_dict import dotty
from milc import cli
from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS
-from qmk.c_parse import find_layouts, parse_config_h_file
+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
from qmk.keymap import list_keymaps, locate_keymap
@@ -76,6 +76,9 @@ def info_json(keyboard):
# Ensure that we have matrix row and column counts
info_data = _matrix_size(info_data)
+ # Merge in data from <keyboard.c>
+ info_data = _extract_led_config(info_data, str(keyboard))
+
# Validate against the jsonschema
try:
validate(info_data, 'qmk.api.keyboard.v1')
@@ -590,6 +593,46 @@ def _extract_rules_mk(info_data, rules):
return info_data
+def find_keyboard_c(keyboard):
+ """Find all <keyboard>.c files
+ """
+ keyboard = Path(keyboard)
+ current_path = Path('keyboards/')
+
+ files = []
+ for directory in keyboard.parts:
+ current_path = current_path / directory
+ keyboard_c_path = current_path / f'{directory}.c'
+ if keyboard_c_path.exists():
+ files.append(keyboard_c_path)
+
+ return files
+
+
+def _extract_led_config(info_data, keyboard):
+ """Scan all <keyboard>.c files for led config
+ """
+ cols = info_data['matrix_size']['cols']
+ rows = info_data['matrix_size']['rows']
+
+ # Assume what feature owns g_led_config
+ feature = "rgb_matrix"
+ if info_data.get("features", {}).get("led_matrix", False):
+ feature = "led_matrix"
+
+ # Process
+ for file in find_keyboard_c(keyboard):
+ try:
+ ret = find_led_config(file, cols, rows)
+ if ret:
+ info_data[feature] = info_data.get(feature, {})
+ info_data[feature]["layout"] = ret
+ except Exception as e:
+ _log_warning(info_data, f'led_config: {file.name}: {e}')
+
+ return info_data
+
+
def _matrix_size(info_data):
"""Add info_data['matrix_size'] if it doesn't exist.
"""
diff --git a/lib/python/qmk/json_encoders.py b/lib/python/qmk/json_encoders.py
index 40a5c1dea8..f968b3dbb2 100755
--- a/lib/python/qmk/json_encoders.py
+++ b/lib/python/qmk/json_encoders.py
@@ -75,8 +75,8 @@ class InfoJSONEncoder(QMKJSONEncoder):
"""Encode info.json dictionaries.
"""
if obj:
- if self.indentation_level == 4:
- # These are part of a layout, put them on a single line.
+ if set(("x", "y")).issubset(obj.keys()):
+ # These are part of a layout/led_config, put them on a single line.
return "{ " + ", ".join(f"{self.encode(key)}: {self.encode(element)}" for key, element in sorted(obj.items())) + " }"
else: