Skip to content

Commit 53ad524

Browse files
committed
Remove handling specific libraries
From now on, pass everything what is needed via arguments (or CLI interface). This allows to use footprints which were not explicitly added so the version of kle2netlist does not have to be updated constantly). There are drawbacks though: - no ISO specific switch added anymore (don't want to add yet another argument for `--iso-switch-footprint`) - fixed list of key widths - some libs might not have dedicated footprints for all of them Both of these points could be solved by some footprints directories searching but this is not implemented yet.
1 parent d373cae commit 53ad524

File tree

4 files changed

+116
-221
lines changed

4 files changed

+116
-221
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Options
2929
--layout Path to kle layout file [default: None] [required]
3030
--output-dir Output directory, created if not existing [default: .]
3131
--name Netlist name without file extension [default: keyboard]
32-
--switch-library -swl Switch library [default: kiswitch/keyswitch-kicad-library]
33-
--switch-footprint -swf Switch footprint [default: MX]
34-
--diode-footprint -df Diode footprint [default: D_SOD-323F]
32+
--switch-footprint -swf Switch footprint f-string [default: PCM_Switch_Keyboard_Cherry_MX:SW_Cherry_MX_PCB_{:.2f}u]
33+
--stabilizer-footprint -stf Stabilizer footprint [default: PCM_Mounting_Keyboard_Stabilizer:Stabilizer_Cherry_MX_{:.2f}u]
34+
--diode-footprint -df Diode footprint [default: Diode_SMD:D_SOD-123F]
3535
--lib-path -l Path to symbol library [default: None]
3636
--controller-circuit Add ATmega32U4-AU minimal circuitry
3737
--no-xml Skip xml netlist generation

src/kle2netlist/__main__.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#
33
# SPDX-License-Identifier: MIT
44
import json
5-
from enum import Enum
65
from pathlib import Path
76
from typing import List, Optional
87

@@ -12,13 +11,6 @@
1211
from kle2netlist._version import __version__
1312
from kle2netlist.skidl import build_circuit, generate_netlist
1413

15-
16-
class SwitchType(str, Enum):
17-
mx = "MX"
18-
alps = "Alps"
19-
mx_alps_hybrid = "MX/Alps Hybrid"
20-
21-
2214
app = typer.Typer(
2315
name="kle2netlist",
2416
help="KiCad netlist generator for mechanical keyboards ",
@@ -43,17 +35,20 @@ def main(
4335
name: str = typer.Option(
4436
"keyboard", "--name", help="Netlist name without file extension"
4537
),
46-
switch_library: str = typer.Option(
47-
"kiswitch/keyswitch-kicad-library",
48-
"-swl",
49-
"--switch-library",
50-
help="Switch library",
38+
switch_footprint: str = typer.Option(
39+
"PCM_Switch_Keyboard_Cherry_MX:SW_Cherry_MX_PCB_{:.2f}u",
40+
"-swf",
41+
"--switch-footprint",
42+
help="Switch footprint f-string",
5143
),
52-
switch_footprint: SwitchType = typer.Option(
53-
SwitchType.mx, "-swf", "--switch-footprint", help="Switch footprint"
44+
stabilizer_footprint: str = typer.Option(
45+
"PCM_Mounting_Keyboard_Stabilizer:Stabilizer_Cherry_MX_{:.2f}u",
46+
"-stf",
47+
"--stabilizer-footprint",
48+
help="Stabilizer footprint f-string, optional",
5449
),
5550
diode_footprint: str = typer.Option(
56-
"D_SOD-323F", "-df", "--diode-footprint", help="Diode footprint"
51+
"Diode_SMD:D_SOD-123F", "-df", "--diode-footprint", help="Diode footprint"
5752
),
5853
lib_paths: Optional[List[str]] = typer.Option(
5954
None, "-l", "--lib-path", help="Path to symbol library"
@@ -94,8 +89,8 @@ def main(
9489
json_layout = json.loads(f.read())
9590
build_circuit(
9691
json_layout,
97-
switch_library=switch_library,
9892
switch_footprint=switch_footprint,
93+
stabilizer_footprint=stabilizer_footprint,
9994
diode_footprint=diode_footprint,
10095
additional_search_path=lib_paths,
10196
controller_circuit=controller_circuit,

src/kle2netlist/skidl.py

Lines changed: 47 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -8,102 +8,6 @@
88

99
import skidl
1010

11-
KISWITCH_LIBRARY_METADATA = {
12-
"modules": {
13-
"MX": {
14-
"name": "Switch_Keyboard_Cherry_MX",
15-
"footprint-nameformat": "SW_Cherry_MX_PCB_{:.2f}u",
16-
"iso-enter": "SW_Cherry_MX_PCB_ISOEnter",
17-
},
18-
"Alps": {
19-
"name": "Switch_Keyboard_Alps_Matias",
20-
"footprint-nameformat": "SW_Alps_Matias_{:.2f}u",
21-
"iso-enter": "SW_Alps_Matias_ISOEnter",
22-
},
23-
"MX/Alps Hybrid": {
24-
"name": "Switch_Keyboard_Hybrid",
25-
"footprint-nameformat": "SW_Hybrid_Cherry_MX_Alps_{:.2f}u",
26-
"iso-enter": "SW_Hybrid_Cherry_MX_Alps_ISOEnter",
27-
},
28-
"Hotswap Kailh MX": {
29-
"name": "Switch_Keyboard_Hotswap_Kailh",
30-
"footprint-nameformat": "SW_Hotswap_Kailh_MX_{:.2f}u",
31-
"iso-enter": "SW_Hotswap_Kailh_MX_ISOEnter",
32-
}
33-
},
34-
"supported-widths": [
35-
1,
36-
1.25,
37-
1.5,
38-
1.75,
39-
2,
40-
2.25,
41-
2.5,
42-
2.75,
43-
3,
44-
4,
45-
4.5,
46-
5.5,
47-
6,
48-
6.25,
49-
6.5,
50-
7,
51-
],
52-
"supported-stabilizers": [
53-
2,
54-
3,
55-
6,
56-
6.25,
57-
7,
58-
8,
59-
],
60-
}
61-
62-
SUPPORTED_LIBRARIES = {
63-
"ai03-2725/MX_Alps_Hybrid": {
64-
"modules": {
65-
"MX": {
66-
"name": "MX_Only",
67-
"footprint-nameformat": "MXOnly-{:g}U-NoLED",
68-
"iso-enter": "MXOnly-ISO",
69-
},
70-
"Alps": {
71-
"name": "Alps_Only",
72-
"footprint-nameformat": "ALPS-{:g}U",
73-
},
74-
"MX/Alps Hybrid": {
75-
"name": "MX_Alps_Hybrid",
76-
"footprint-nameformat": "MX-{:g}U-NoLED",
77-
"iso-enter": "MX-ISO",
78-
},
79-
},
80-
"supported-widths": [
81-
1,
82-
1.25,
83-
1.5,
84-
1.75,
85-
2,
86-
2.25,
87-
2.5,
88-
2.75,
89-
3,
90-
6,
91-
6.25,
92-
6.5,
93-
7,
94-
8,
95-
9,
96-
10,
97-
],
98-
"supported-stabilizers": [], # stabilizers are part of switch footprint
99-
},
100-
# tested with https://github.com/perigoso/keyswitch-kicad-library/releases/tag/v2.2
101-
"perigoso/keyswitch-kicad-library": KISWITCH_LIBRARY_METADATA,
102-
# perigoso library has been moved to new location:
103-
# tested with https://github.com/kiswitch/keyswitch-kicad-library/releases/tag/v2.4
104-
"kiswitch/keyswitch-kicad-library": KISWITCH_LIBRARY_METADATA,
105-
}
106-
10711
ATMEGA32U4AU_PIN_ASSIGN_ORDER = [
10812
"PB0",
10913
"PB1",
@@ -153,66 +57,66 @@ def find_closest_smaller_or_equal(lst, target):
15357
return lst[index - 1]
15458

15559

156-
def add_stabilizer(reference, key_width):
157-
# this function works only for perigoso/keyswitch-kicad-library
158-
supported_stabilizer_size = SUPPORTED_LIBRARIES["perigoso/keyswitch-kicad-library"][
159-
"supported-stabilizers"
160-
]
161-
stabilizer_width = find_closest_smaller_or_equal(
162-
supported_stabilizer_size, key_width
163-
)
60+
def add_stabilizer(reference, stabilizer_footprint, key_width):
61+
supported_stabilizers = [2, 3, 6, 6.25, 7, 8]
62+
stabilizer_width = find_closest_smaller_or_equal(supported_stabilizers, key_width)
16463

16564
if stabilizer_width:
166-
stabilizer_footprint = (
167-
f"Mounting_Keyboard_Stabilizer:Stabilizer_Cherry_MX_{stabilizer_width:.2f}u"
168-
)
65+
stabilizer_footprint = f"{stabilizer_footprint}".format(stabilizer_width)
16966
stabilizer = skidl.Part(
17067
"Mechanical", "MountingHole", footprint=stabilizer_footprint
17168
)
17269
stabilizer.ref = reference
17370

17471

175-
def add_iso_enter_switch(switch_module, diode_footprint):
176-
module_name = switch_module["name"]
177-
178-
try:
179-
switch_footprint = switch_module["iso-enter"]
180-
except KeyError:
181-
footprint_format = switch_module["footprint-nameformat"]
182-
switch_footprint = f"{footprint_format}".format(1)
183-
184-
switch_footprint = f"{module_name}:{switch_footprint}"
72+
def add_iso_enter_switch(switch_footprint, diode_footprint, stabilizer_footprint):
73+
# use 1u switch, do not bother with detection of dedicated ISO key which
74+
# name is library dependent (and it is not passed via CLI yet)
75+
switch_footprint = f"{switch_footprint}".format(1)
18576

18677
switch = skidl.Part("Switch", "SW_Push", footprint=switch_footprint)
187-
diode = skidl.Part("Device", "D", footprint=f"Diode_SMD:{diode_footprint}")
78+
diode = skidl.Part("Device", "D", footprint=diode_footprint)
18879

189-
if module_name in ["Switch_Keyboard_Cherry_MX", "Switch_Keyboard_Hybrid"]:
190-
stabilizer_footprint = "Mounting_Keyboard_Stabilizer:Stabilizer_Cherry_MX_2.00u"
191-
stabilizer = skidl.Part(
192-
"Mechanical", "MountingHole", footprint=stabilizer_footprint
193-
)
194-
switch_reference_number = switch.ref[2:]
195-
stabilizer.ref = f"ST{switch_reference_number}"
80+
switch_reference_number = switch.ref[2:]
81+
add_stabilizer(f"ST{switch_reference_number}", stabilizer_footprint, 2)
19682

19783
return switch, diode
19884

19985

200-
def add_regular_switch(switch_module, key_width, diode_footprint):
201-
footprint_format = switch_module["footprint-nameformat"]
202-
module_name = switch_module["name"]
86+
def add_regular_switch(
87+
switch_footprint, key_width, diode_footprint, stabilizer_footprint
88+
):
89+
# probably should use some searching to see if given footprint exist,
90+
# for now just assume that any library supports following widths:
91+
supported_widths = [
92+
1,
93+
1.25,
94+
1.5,
95+
1.75,
96+
2,
97+
2.25,
98+
2.5,
99+
2.75,
100+
3,
101+
4,
102+
4.5,
103+
5.5,
104+
6,
105+
6.25,
106+
6.5,
107+
7,
108+
]
109+
if key_width not in supported_widths:
110+
key_width = 1
203111

204-
switch_footprint = f"{footprint_format}".format(key_width)
205-
switch_footprint = f"{module_name}:{switch_footprint}"
112+
switch_footprint = f"{switch_footprint}".format(key_width)
206113

207114
switch = skidl.Part("Switch", "SW_Push", footprint=switch_footprint)
208-
diode = skidl.Part("Device", "D", footprint=f"Diode_SMD:{diode_footprint}")
115+
diode = skidl.Part("Device", "D", footprint=diode_footprint)
209116

210-
if (
211-
module_name in ["Switch_Keyboard_Cherry_MX", "Switch_Keyboard_Hybrid"]
212-
and key_width >= 2
213-
):
117+
if stabilizer_footprint and key_width >= 2:
214118
switch_reference_number = switch.ref[2:]
215-
add_stabilizer(f"ST{switch_reference_number}", key_width)
119+
add_stabilizer(f"ST{switch_reference_number}", stabilizer_footprint, key_width)
216120

217121
return switch, diode
218122

@@ -224,7 +128,7 @@ def is_key_label_valid(label):
224128
return False
225129

226130

227-
def handle_switch_matrix(keys, switch_module, supported_widths, diode_footprint):
131+
def handle_switch_matrix(keys, switch_footprint, diode_footprint, stabilizer_footprint):
228132
rows = {}
229133
columns = {}
230134

@@ -252,13 +156,13 @@ def handle_switch_matrix(keys, switch_module, supported_widths, diode_footprint)
252156
columns[column] = skidl.Net(f"COL{column}")
253157

254158
if is_iso_enter(key):
255-
switch, diode = add_iso_enter_switch(switch_module, diode_footprint)
159+
switch, diode = add_iso_enter_switch(
160+
switch_footprint, diode_footprint, stabilizer_footprint
161+
)
256162
else:
257163
key_width = float(key["width"])
258164
switch, diode = add_regular_switch(
259-
switch_module,
260-
key_width if key_width in supported_widths else 1,
261-
diode_footprint,
165+
switch_footprint, key_width, diode_footprint, stabilizer_footprint
262166
)
263167

264168
rows[row] += diode[1]
@@ -417,20 +321,16 @@ def build_circuit(layout, **kwargs):
417321
skidl.lib_search_paths[skidl.KICAD].append(default_search_path)
418322

419323
try:
420-
switch_library = kwargs.get("switch_library")
421-
library = SUPPORTED_LIBRARIES[switch_library]
422-
423324
switch_footprint = kwargs.get("switch_footprint")
325+
stabilizer_footprint = kwargs.get("stabilizer_footprint")
424326
diode_footprint = kwargs.get("diode_footprint")
425-
switch_module = library["modules"][switch_footprint]
426-
supported_widths = library["supported-widths"]
427327

428328
except KeyError as err:
429329
msg = "Unsupported argument"
430330
raise RuntimeError(msg) from err
431331

432332
rows, columns = handle_switch_matrix(
433-
layout["keys"], switch_module, supported_widths, diode_footprint
333+
layout["keys"], switch_footprint, diode_footprint, stabilizer_footprint
434334
)
435335

436336
if kwargs.get("controller_circuit"):

0 commit comments

Comments
 (0)