rushabhcodes/wireless-split-keyboard
Wireless Split Keyboard — custom routed nRF52840 split PCBs with a printable enclosure
- Version
- 0.4.5
- License
- unset
- Stars
- 0
Use with the tscircuit CLI
tsci is the tscircuit CLI. Install it with npm install -g tscircuit.
tsci clone rushabhcodes/wireless-split-keyboard # download the package files into ./wireless-split-keyboard
tsci add rushabhcodes/wireless-split-keyboard # add it to your project as @tsci/rushabhcodes.wireless-split-keyboardIn a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.
scripts/import-routing-session.py
"""Convert newly routed Specctra copper to native saved tscircuit paths.
The session is generated from this project's component/netlist geometry only.
"""
from pathlib import Path
import sys,json,re,hashlib,math
from sexpr import parse,field,fields
from routing_model import BoardModel,LAYERS,DSN
root=Path(__file__).resolve().parents[1]
half=sys.argv[1] if len(sys.argv)>1 else 'left'
folder=root/f'.cache/routing/{half}'
m=BoardModel(root/f'outputs/{half}/keyboard.circuit.json')
raw=(folder/'routed.ses').read_text()
tree=parse(re.sub(r'\(string_quote\s+"\)', '',raw))
routes=fields(tree,'routes')[0]
resolution=field(routes,'resolution',['um',1]);scale={'um':.001,'mm':1,'mil':.0254}[resolution[0]]/float(resolution[1])
layers={v:k for k,v in DSN.items()}
paths=[];seen=set()
anchors={n:sorted(ps,key=lambda p:(p['ref']!='U1',p['ref'],p['id']))[0]['selector'] for n,ps in m.netpads.items()}
def add(net,route):
v=next((p for p in route if p['route_type']=='via'),None)
key=json.dumps([net,round(v['x'],3),round(v['y'],3),v['from_layer'],v['to_layer'],v['via_diameter'],v['via_hole_diameter']]) if v else json.dumps(route,sort_keys=True)
if key in seen:return
seen.add(key);paths.append({'net':net,'connection':anchors[net],'route':route})
def wire(net,layer,width,xy):
add(net,[{'route_type':'wire','x':round(x,7),'y':round(y,7),'width':round(width,7),'layer':layer} for x,y in xy])
def via(net,x,y,kind):
if 'Micro' in kind:fr,to,d,h='bottom','inner2',.35,.15
elif 'Thermal' in kind:fr,to,d,h='top','bottom',.6,.3
else:fr,to,d,h='top','bottom',.5,.25
p={'x':round(x,7),'y':round(y,7)}
add(net,[{'route_type':'wire',**p,'width':.145,'layer':fr},
{'route_type':'via',**p,'from_layer':fr,'to_layer':to,'via_diameter':d,'via_hole_diameter':h},
{'route_type':'wire',**p,'width':.145,'layer':to}])
network=fields(routes,'network_out')[0]
for netnode in fields(network,'net'):
net=netnode[1]
for w in fields(netnode,'wire'):
p=field(w,'path');xy=list(zip(p[2::2],p[3::2]))
wire(net,layers[p[0]],p[1]*scale,[(x*scale,y*scale) for x,y in xy])
for v in fields(netnode,'via'):via(net,v[2]*scale,v[3]*scale,v[1])
# Protected input wires/vias may be omitted by a session writer. Merge exactly.
seed=json.loads((folder/'seed.json').read_text())
for w in seed['traces']:wire(w['net'],w['layer'],w['width'],[(p['x'],p['y']) for p in w['points']])
for v in seed['vias']:via(v['net'],v['x'],v['y'],v['kind'])
repair=root/f'src/routing/{half}-repairs.json'
if repair.exists():
patch=json.loads(repair.read_text())
def removed(path):
for signature in patch.get('removeRoutes',[]):
route=path['route']
if path['net']!=signature['net'] or len(route)!=len(signature['points']):continue
if all(p['route_type']=='wire' and p['layer']==signature['layer'] and math.hypot(p['x']-xy[0],p['y']-xy[1])<.001 for p,xy in zip(route,signature['points'])):return True
return False
paths=[p for p in paths if p['net'] not in patch['removeNets'] and not removed(p)]+patch['paths']
out=Path(sys.argv[2]) if len(sys.argv)>2 else root/f'src/routing/{half}.json'
out.parent.mkdir(parents=True,exist_ok=True)
out.write_text(json.dumps({'format':1,'engine':'Freerouting 2.6.0','sessionSha256':hashlib.sha256(raw.encode()).hexdigest(),
'netIds':{n['name']:n['source_net_id'] for n in m.bytype['source_net']},
'connectionNets':m.idnet,
'ports':{pid:{'x':p['x'],'y':p['y'],'layers':p['layers'],'net':m.portnet[p['source_port_id']]} for pid,p in m.ports.items() if p.get('source_port_id') in m.portnet},
'paths':paths},indent=2)+'\n')
print(half,len(paths),'saved copper paths',len({p['net'] for p in paths}),'nets')