r75-firmware/tools/gen_keymap.py
orca 35e82437df Fix knob direction: clockwise is volume up
QMK's encoder driver reads B7/B6 the other way round from RK's firmware,
so copying stock's (VOLU, VOLD) pair reversed the knob. The generator now
swaps the pair. The live board was fixed through Vial's set-encoder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 10:31:54 -04:00

86 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""Generate keymaps/vial/keymap_layers.h from stock-keymap.json (the RK factory keymap read
off the keyboard), so the custom firmware keeps every stock key and Fn combo.
Changes from stock, on purpose:
* RK's lighting keys used the old underglow keycodes (0x7820-0x782A), which current QMK
only sends to rgblight; they become the RGB-matrix keycodes (RM_*).
* Factory reset (QK_CLEAR_EEPROM) moves from Fn+Space to Fn+RShift+Space (layer 4),
so a stray Fn+Space can't wipe settings.
* RK's undocumented Bluetooth/2.4G keycodes (0x7E..) do nothing on the wired board -> KC_NO.
* A sixth, empty layer is added for Vial.
* The knob directions are swapped: RK's firmware reads the encoder (B7/B6) the other
way round, so stock's (VOLU, VOLD) pair turned the wrong way under QMK's driver.
"""
import json
import os
import re
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.join(HERE, "..")
QMK = os.environ.get("QMK_HOME", os.path.expanduser("~/.local/src/vial-qmk"))
names = {}
for line in open(os.path.join(QMK, "quantum", "keycodes.h")):
m = re.match(r"\s+((?:KC|QK)_\w+) = (0x[0-9A-Fa-f]+),", line)
if m:
names.setdefault(int(m.group(2), 16), m.group(1))
names[0x0000], names[0x0001] = "KC_NO", "KC_TRNS"
def name(code):
if 0x7820 <= code <= 0x782A: # underglow -> rgb matrix
code += 0x22
if 0x7E00 <= code <= 0x7FFF: # RK wireless-only functions
return "KC_NO"
if 0x5200 <= code <= 0x521F:
return f"TO({code & 0x1F})"
if 0x5220 <= code <= 0x523F:
return f"MO({code & 0x1F})"
if code in names:
return names[code]
raise SystemExit(f"no name for keycode 0x{code:04x}")
stock = json.load(open(os.path.join(ROOT, "stock-keymap.json")))
kb = json.load(open(os.path.join(ROOT, "keyboards/rk/r75/keyboard.json")))
order = [tuple(k["matrix"]) for k in kb["layouts"]["LAYOUT"]["layout"]]
layers = [[row[:] for row in layer] for layer in stock["layers"]]
SPACE = (5, 5)
for fn in (1, 3): # move factory reset off Fn+Space
if layers[fn][SPACE[0]][SPACE[1]] == 0x7C03:
layers[fn][SPACE[0]][SPACE[1]] = 0x0001
layers[4][SPACE[0]][SPACE[1]] = 0x7C03
layers.append([[0x0001] * 15 for _ in range(6)])
LAYER_NAMES = ["WIN", "WIN_FN", "MAC", "MAC_FN", "SYS", "SPARE"]
out = ["// Generated by tools/gen_keymap.py from stock-keymap.json. Do not edit by hand.",
"#pragma once", "",
"// clang-format off",
"const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {"]
for n, layer in enumerate(layers):
keys = [name(layer[r][c]) for r, c in order]
out.append(f" [{n}] = LAYOUT( // {LAYER_NAMES[n]}")
rows, cur, last_row = [], [], order[0][0]
for (r, _), k in zip(order, keys):
if r != last_row:
rows.append(cur)
cur, last_row = [], r
cur.append(k)
rows.append(cur)
out.append(",\n".join(" " + ", ".join(rw) for rw in rows))
out.append(" ),")
out.append("};")
out.append("")
enc = [[cw, ccw] for ccw, cw in stock["encoder"]] + [[0x0001, 0x0001]] # see docstring: swapped
out.append("#ifdef ENCODER_MAP_ENABLE")
out.append("const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {")
for n, (ccw, cw) in enumerate(enc):
out.append(f" [{n}] = {{ENCODER_CCW_CW({name(ccw)}, {name(cw)})}},")
out.append("};")
out.append("#endif")
out.append("// clang-format on")
path = os.path.join(ROOT, "keyboards/rk/r75/keymaps/vial/keymap_layers.h")
open(path, "w").write("\n".join(out) + "\n")
print("wrote", path)