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/routing_model.py
"""Geometry and electrical nets from native tscircuit output. Millimetres, world XY."""
from pathlib import Path
from collections import defaultdict
import json,math
from shapely.geometry import Point,Polygon,box,LineString
from shapely.affinity import rotate
LAYERS=['top','inner1','inner2','bottom']
DSN={'top':'F.Cu','inner1':'In1.Cu','inner2':'In2.Cu','bottom':'B.Cu'}
class BoardModel:
def __init__(self,path):
self.json=json.loads(Path(path).read_text());self.bytype=defaultdict(list)
for e in self.json:self.bytype[e['type']].append(e)
self.sources={e['source_component_id']:e for e in self.bytype['source_component']}
self.components={e['pcb_component_id']:e for e in self.bytype['pcb_component']}
self.sourceports={e['source_port_id']:e for e in self.bytype['source_port']}
self.ports={e['pcb_port_id']:e for e in self.bytype['pcb_port']}
self.parent={}
def root(a):
self.parent.setdefault(a,a)
if self.parent[a]!=a:self.parent[a]=root(self.parent[a])
return self.parent[a]
def union(items):
if not items:return
a=root(items[0])
for v in items[1:]:self.parent[root(v)]=a
for e in self.bytype['source_trace']:union(e.get('connected_source_port_ids',[])+e.get('connected_source_net_ids',[]))
for c in self.sources.values():
for ids in c.get('internally_connected_source_port_ids',[]):union(ids)
names=defaultdict(set)
for e in self.bytype['source_net']:names[root(e['source_net_id'])].add(e['name'])
assert all(len(v)==1 for v in names.values()),'A source connection merges distinct named nets'
self.portnet={p:next(iter(names[root(p)])) for p in self.sourceports if names[root(p)]}
self.idnet={p:next(iter(names[root(p)])) for p in self.parent if names[root(p)]}
for t in self.bytype['source_trace']:
connected={self.idnet[p] for p in t.get('connected_source_port_ids',[])+t.get('connected_source_net_ids',[]) if p in self.idnet}
if connected:self.idnet[t['source_trace_id']]=next(iter(connected))
self.board=self.bytype['pcb_board'][0]
self.outline=Polygon([(p['x'],p['y']) for p in self.board['outline']])
self.pads=[];self.byref=defaultdict(list);self.netpads=defaultdict(list)
for i,e in enumerate(self.bytype['pcb_smtpad']+self.bytype['pcb_plated_hole']):
pc=self.ports.get(e.get('pcb_port_id'),{});sp=self.sourceports.get(pc.get('source_port_id'),{})
cid=e.get('pcb_component_id')
if not cid:
cid=f'mechanical_{i}'
self.sources[cid]={'name':f'MOUNT_{i}'}
self.components[cid]={'pcb_component_id':cid,'source_component_id':cid,'footprint_origin':{'x':e['x'],'y':e['y']},'center':{'x':e['x'],'y':e['y']}}
c=self.components[cid];ref=self.sources[c['source_component_id']]['name']
net=self.portnet.get(sp.get('source_port_id'))
p={'id':f'p{i}','element':e,'component':c,'ref':ref,'net':net,'selector':f'.{ref} > .pin{sp["pin_number"]}' if sp.get('pin_number') else None,'layers':e.get('layers',[e.get('layer')]),'shape':land(e),'x':e['x'],'y':e['y']}
self.pads.append(p);self.byref[ref].append(p)
if net:self.netpads[net].append(p)
def pin(self,ref,pin):
target=f'.{ref} > .pin{pin}'
return next(p for p in self.byref[ref] if p['selector']==target)
def netpin(self,ref,net):return next(p for p in self.byref[ref] if p['net']==net)
def land(e):
x,y=e['x'],e['y'];shape=e['shape']
if shape=='circle':return Point(x,y).buffer(e.get('radius',e.get('outer_diameter',e.get('hole_diameter',0))/2),quad_segs=16)
if shape=='polygon':return Polygon([(v['x'],v['y']) for v in e['points']])
w,h=e.get('width',e.get('outer_width',e.get('rect_pad_width'))),e.get('height',e.get('outer_height',e.get('rect_pad_height')))
if shape in ['pill','rotated_pill']:
if w>=h:g=LineString([(x-(w-h)/2,y),(x+(w-h)/2,y)]).buffer(h/2,quad_segs=12)
else:g=LineString([(x,y-(h-w)/2),(x,y+(h-w)/2)]).buffer(w/2,quad_segs=12)
else:g=box(x-w/2,y-h/2,x+w/2,y+h/2)
return rotate(g,e.get('ccw_rotation',e.get('rect_ccw_rotation',e.get('rotation',0))),origin=(x,y))