Skip to content

Commit

Permalink
Add tool for kle layout file modification
Browse files Browse the repository at this point in the history
  • Loading branch information
adamws committed Oct 1, 2024
1 parent efce1b5 commit 481fc40
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,13 @@ The `kbplacer` is used by additional tools available in [tools](tools/README.md)
</td>
<td><img src="resources/example-openscad-plate.png"/></td>
</tr>
<tr>
<td>
<code>kle2kle.py</code> - edit KLE layout, supports:</a></br>
labels and colors reset and automatic VIA style <code>row,column</code> matrix label annotation.
</td>
<td><img src="resources/example-kle2kle.png"/></td>
</tr>
</table>

<!-- TOC --><a name="use-in-python-projects"></a>
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,4 @@ layout2image = "python tools/layout2image.py {args}"
layout2schematic = "python tools/layout2schematic.py {args}"
layout2url = "python tools/layout2url.py {args}"
layout2openscad = "python tools/layout2openscad.py {args}"
kle2kle = "python tools/kle2kle.py {args}"
Binary file added resources/example-kle2kle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 115 additions & 0 deletions tools/kle2kle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import argparse
import json
import math
import sys
from typing import List, Tuple

from kbplacer import kle_serial
from kbplacer.kle_serial import (
Key,
Keyboard,
KeyDefault,
MatrixAnnotatedKeyboard,
parse_kle,
)


def get_key_center(key: Key) -> Tuple[float, float]:
x = key.x + (key.width / 2)
y = key.y + (key.height / 2)

rot_origin_x = key.rotation_x
rot_origin_y = key.rotation_y
angle = 1 * key.rotation_angle
angle_rad = angle * math.pi / 180

x = x - rot_origin_x
y = y - rot_origin_y

x1 = (x * math.cos(angle_rad)) - (y * math.sin(angle_rad))
y1 = (x * math.sin(angle_rad)) + (y * math.cos(angle_rad))

x = x1 + rot_origin_x
y = y1 + rot_origin_y

return x, y


def annotate_keys(keys: List[Key]) -> None:
for key in keys:
x, y = get_key_center(key)
x, y = int(x), int(y)
key.set_label(MatrixAnnotatedKeyboard.MATRIX_COORDINATES_LABEL, f"{y},{x}")


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="KLE edit")
parser.add_argument("-in", required=True, help="Layout file")
parser.add_argument("-out", required=False, help="Result file")
parser.add_argument(
"-text", required=False, action="store_true", help="Print KLE raw data"
)

parser.add_argument(
"-remove-labels", action="store_true", help="Remove all key labels"
)
parser.add_argument(
"-reset-colors", action="store_true", help="Reset colors to defaults"
)
parser.add_argument(
"-annotate",
action="store_true",
help=(
"Automatically annotate keys with row,column labels. "
"All labels must be empty. Can be combined with -remove-labels action."
),
)

args = parser.parse_args()
input_path = getattr(args, "in")
output_path = getattr(args, "out")
print_result = args.text
remove_labels = args.remove_labels
reset_colors = args.reset_colors
annotate = args.annotate

with open(input_path, "r", encoding="utf-8") as input_file:
layout = json.load(input_file)

keyboard = None
try:
keyboard = parse_kle(layout)
output_format = "KLE_RAW"
except Exception:
keyboard = Keyboard.from_json(layout)
output_format = "KLE_INTERNAL"

if keyboard == None:
print(f"Unable to get keyboard layout from file '{input_file}'")
sys.exit(1)

if remove_labels:
for k in keyboard.keys:
k.labels = []

if reset_colors:
for k in keyboard.keys:
k.color = kle_serial.DEFAULT_KEY_COLOR
k.textColor = []
k.default = KeyDefault()

if annotate:
annotate_keys(keyboard.keys)

if print_result:
raw_kle = keyboard.to_kle()
print(raw_kle)

if output_format == "KLE_INTERNAL":
result = json.loads(keyboard.to_json())
else: # KLE_RAW
result = json.loads("[" + keyboard.to_kle() + "]")

if output_path:
with open(output_path, "w", encoding="utf-8") as output_file:
json.dump(result, output_file, indent=2)

0 comments on commit 481fc40

Please sign in to comment.