-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
run.py
115 lines (89 loc) · 4.27 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from util.serial import serialize, deserialize
from util.util import read_file, write_file, gen_uid, MCU_DICT
from util.converters import kbd_to_qmk_info, kbd_to_vial, kbd_to_keymap, layout_str_to_layout_dict, keycodes_md_to_keycode_dict, generate_keycode_conversion_dict, kbd_to_main_config, extract_matrix_pins
import json
import requests
import os
import datetime
from util.json_encoders import * # from qmk_firmware/lib/python/qmk/json_encoders.py, for generating info.json
# Input an exported json of a KLE that follows the guide (See README.md)
input_kle_json_path = 'test-json.json'
read_content = read_file(input_kle_json_path)
# Deserialize json
deserialized_path = 'deserialized.json'
keyboard = deserialize(json.loads(read_content))
# To test de/serialization. Should generate same kle json as provided
serialized_path = 'serialized.json'
srlzd = serialize(keyboard)
write_file(serialized_path, json.dumps(srlzd, ensure_ascii=False, indent=2, cls=KLEJSONEncoder))
# Test parity:
print(f"Deserialized and Serialized JSONs are identical: {read_file(input_kle_json_path) == read_file(serialized_path)}")
# Generate a QMK info.json file used for QMK Configurator
name = 'Slime88'
maintainer = 'Zykrah'
manufacturer = 'MANUFACTURER'
url = ""
vid = "0xFEED"
pid = "0x0001"
ver = "0.0.1"
layers=4
mcu_choice = "RP2040" # choose from MCU_PRESETS
try: # KiCAD Netlist (for pins)
netlist = read_file('slime88.net')
except FileNotFoundError:
netlist = None
mcu_dict = MCU_DICT[mcu_choice]
mcu = mcu_dict['mcu']
bootloader = mcu_dict['bootloader']
board = mcu_dict['board']
if netlist:
output_pin_pref = mcu_dict['output_pin_pref']
schem_pin_pref = mcu_dict['schem_pin_pref']
diode_dir = "COL2ROW" # default
if mcu_choice != 'None' and netlist:
try:
pin_dict = extract_matrix_pins(netlist, mcu, output_pin_pref, schem_pin_pref)
except Exception as e:
raise Exception(f"Invalid netlist provided!, {e}")
elif mcu_choice == 'None' and netlist:
raise Exception("You need to choose a MCU preset to utilise the netlist function!")
else:
pin_dict = {}
qmk_info_path = 'info.json'
qmk_info_content = kbd_to_qmk_info(keyboard, name, maintainer, url, vid, pid, ver, mcu, bootloader, board, pin_dict, diode_dir, manufacturer)
write_file(qmk_info_path, json.dumps(qmk_info_content, indent=4, separators=(', ', ': '), sort_keys=False, cls=InfoJSONEncoder))
# Generate a VIAL json file used to identify a keyboard in VIAL. Same as via but with encoders and no required product/vendor ID
# Also generate a config.h file with a randomly generated UID and unlocking combo (if included)
vial_vendor_id = '0x7A79'
vial_product_id = '0x0000'
vial_uid = gen_uid()
vial_json_path = 'vial.json'
vial_config_h_path = 'config.h'
vial_json_content, vial_config_h = kbd_to_vial(keyboard, vial_uid, vial_vendor_id, vial_product_id)
write_file(vial_json_path, json.dumps(vial_json_content, ensure_ascii=False, indent=2, cls=KLEJSONEncoder))
write_file(vial_config_h_path, vial_config_h)
#keyboard_h_content = kbd_to_layout_macro(keyboard)
keymap_c_path = 'keymap.c'
layout_dict = layout_str_to_layout_dict(read_file('vil.json'))
def retrieve_keycodes_md(link=None):
print('retrieving keycodes.md from github')
if not link:
link = "https://raw.githubusercontent.com/qmk/qmk_firmware/master/docs/keycodes.md"
return requests.get(link).text
keycodes_md_path = 'keycodes.md'
if not os.path.exists(keycodes_md_path):
write_file(keycodes_md_path, retrieve_keycodes_md())
timestamp = os.path.getmtime(keycodes_md_path)
datestamp = datetime.datetime.fromtimestamp(timestamp)
# Update keycodes.md if it's old (HAVE TEMPORARILY SET TO 2 YEARS)
if datestamp < datetime.datetime.now() - datetime.timedelta(weeks=104):
content = retrieve_keycodes_md()
keycodes_dict = keycodes_md_to_keycode_dict(content)
write_file(keycodes_md_path, content)
else: # Otherwise just use the cached version
keycodes_dict = keycodes_md_to_keycode_dict(read_file(keycodes_md_path))
conversion_dict = generate_keycode_conversion_dict(read_file('deprecated_keycodes.txt'))
keymap_c_content = kbd_to_keymap(keyboard, layers, 1, layout_dict, keycodes_dict, conversion_dict)
write_file(keymap_c_path, keymap_c_content)
main_config_h_content = kbd_to_main_config(keyboard, layers)
write_file('config.h', main_config_h_content)