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/mechanical_outputs.py
"""Independent geometry comparison, printable FIT-TEST mechanics and preview.
Uses generated Circuit JSON for the overlay, never a photograph or original PCB
copper. Meshes are prototype fit aids until battery, FPC and component heights pass.
"""
from pathlib import Path
import json,math
import numpy as np
from shapely.geometry import Polygon,Point,box
from shapely.affinity import rotate,translate
from shapely.ops import unary_union
import trimesh
ROOT=Path(__file__).resolve().parents[1]
g=json.loads((ROOT/'src/generated/mechanical-geometry.json').read_text())
OUT=ROOT/'outputs';(OUT/'mechanical').mkdir(exist_ok=True)
def extrude(poly,height,z=0):
m=trimesh.creation.extrude_polygon(poly,height,engine='earcut');m.apply_translation([0,0,z]);return m
def path(points):return 'M'+' L'.join(f'{x:.5f},{-y:.5f}' for x,y in points)+' Z'
def rect(x,y,w,h,angle=0,stroke='#38bdf8'):
return f'<rect x="{x-w/2}" y="{-y-h/2}" width="{w}" height="{h}" transform="rotate({-angle} {x} {-y})" fill="none" stroke="{stroke}" stroke-width=".3"/>'
report={'source':g['source'],'halves':{},'enclosureStatus':'FIT TEST ONLY — component/battery/FPC clearance not released'}
svg=['<svg xmlns="http://www.w3.org/2000/svg" width="1500" height="700" viewBox="-162 -78 324 151"><rect x="-162" y="-78" width="324" height="151" fill="#101820"/>']
for half,shift in [('left',-80),('right',80)]:
geo=g['halves'][half];cj=json.loads((OUT/half/'keyboard.circuit.json').read_text())
src={e['name']:e for e in cj if e['type']=='source_component'}
comps={e['source_component_id']:e for e in cj if e['type']=='pcb_component'}
board=next(e for e in cj if e['type']=='pcb_board')
diffs=[]
svg.append(f'<g transform="translate({shift} 0)">')
svg.append(f'<path d="{path([(p["x"],p["y"]) for p in geo["outline"]])}" fill="#162a30" stroke="#ffb454" stroke-width=".65"/>')
svg.append(f'<path d="{path([(p["x"],p["y"]) for p in board["outline"]])}" fill="none" stroke="#42d9b0" stroke-width=".3"/>')
for key in geo['keys']:
sc=src[key['reference']];comp=comps[sc['source_component_id']]
holes=[e for e in cj if e['type']=='pcb_hole' and abs(e['hole_diameter']-3.4)<1e-5 and math.hypot(e['x']-key['x'],e['y']-key['y'])<.001]
assert len(holes)==1
hole=holes[0];delta=math.hypot(hole['x']-key['x'],hole['y']-key['y']);diffs.append(delta)
svg.append(rect(key['x'],key['y'],18,17,key['rotation'],stroke='#63787e'))
svg.append(f'<circle cx="{key["x"]}" cy="{-key["y"]}" r="1.1" fill="none" stroke="#ffb454" stroke-width=".5"/><path d="M{hole["x"]-1},{-hole["y"]}h2 M{hole["x"]},{-hole["y"]-1}v2" stroke="#42d9b0" stroke-width=".3"/>')
for h in geo['mountingHoles']:
candidates=[e for e in cj if e['type'] in ['pcb_hole','pcb_plated_hole'] and abs(e.get('hole_diameter',0)-h['diameter'])<1e-5]
actual=min(candidates,key=lambda e:math.hypot(e['x']-h['x'],e['y']-h['y']))
diffs.append(math.hypot(actual['x']-h['x'],actual['y']-h['y']))
svg.append(f'<circle cx="{h["x"]}" cy="{-h["y"]}" r="{h["diameter"]/2}" fill="none" stroke="#eeb8ff" stroke-width=".3"/>')
enc=geo['encoder'];svg.append(f'<circle cx="{enc["x"]}" cy="{-enc["y"]}" r="6" fill="none" stroke="#42d9b0" stroke-width=".5"/>')
# Raw panel allocation: unvalidated FPC and z-height; former header remains in locked data.
dx=53 if half=='left' else -53
svg.append(rect(dx,29,14,32,stroke='#9bb9d1'))
svg.append(f'<text x="0" y="-66" text-anchor="middle" fill="#fff" font-family="sans-serif" font-size="4">{half.upper()} · {g["dimensions"]["width"]:.3f} × {g["dimensions"]["height"]:.2f} mm</text>')
svg.append('</g>')
poly=Polygon([(p['x'],p['y']) for p in geo['outline']])
holePoly=unary_union([Point(h['x'],h['y']).buffer(h['diameter']/2,quad_segs=24) for h in geo['mountingHoles']])
pcb=extrude(poly.difference(holePoly),1.6,10);pcb.export(OUT/'mechanical'/f'{half}-pcb-fit.stl')
# Base floor follows outline; 0.35 mm PCB edge clearance + 1.6 mm walls.
inner=poly.buffer(.35);outer=poly.buffer(1.95);floor=extrude(outer.difference(holePoly),1.8)
# Continuous walls initially have generous side opening for USB and switch.
access=box(55 if half=='left' else -80,-5,80 if half=='left' else -55,30)
wallPoly=outer.difference(inner).difference(access)
walls=[extrude(p,8.2,1.8) for p in (list(wallPoly.geoms) if hasattr(wallPoly,'geoms') else [wallPoly])]
bosses=[]
# Only primary chassis mounts TH3..TH7; TH1/TH2 are not assumed structural M2 mounts.
for h in geo['mountingHoles'][2:]:
post=Point(h['x'],h['y']).buffer(3.3).difference(Point(h['x'],h['y']).buffer(1.15))
bosses.append(extrude(post,8.2,1.8))
base=trimesh.boolean.union([floor,*walls,*bosses],engine='manifold');base.export(OUT/'mechanical'/f'{half}-base-FIT-TEST.stl')
cutouts=[]
for k in geo['keys']:
cutouts.append(translate(rotate(box(-7,-7,7,7),k['rotation'],origin=(0,0)),k['x'],k['y']))
cutouts.append(Point(enc['x'],enc['y']).buffer(3.4))
cutouts.append(box(dx-7.5,12.5,dx+7.5,45.5))
plate=extrude(outer.difference(unary_union(cutouts+[holePoly])),1.2,14.5)
plate.export(OUT/'mechanical'/f'{half}-plate-FIT-TEST.stl')
scene=trimesh.Scene();scene.add_geometry(base,node_name='base');scene.add_geometry(pcb,node_name='pcb-fit');scene.add_geometry(plate,node_name='plate');scene.export(OUT/'mechanical'/f'{half}-assembly-FIT-TEST.glb')
report['halves'][half]={'switchStemCount':29,'mountingHoleCount':7,'maxAnchorErrorMm':max(diffs),'outlineVerticesEqual':len(board['outline'])==len(geo['outline']) and all(math.dist((a['x'],a['y']),(b['x'],b['y']))<1e-6 for a,b in zip(board['outline'],geo['outline'])),'pcbMeshWatertight':pcb.is_watertight,'plateMeshWatertight':plate.is_watertight,'baseMeshWatertight':base.is_watertight}
svg+=['<text x="0" y="65" text-anchor="middle" fill="#ffb454" font-family="sans-serif" font-size="3.4">REFERENCE orange · NATIVE PCB mint · Keycaps gray · Mounts violet · Panel allocation blue (provisional)</text>','<text x="0" y="71" text-anchor="middle" fill="#a7bdc4" font-family="sans-serif" font-size="3">PLACEMENT DRAFT · UNROUTED · FIT-TEST MECHANICS · NOT A MANUFACTURING RELEASE</text>','</svg>']
(OUT/'mechanical-overlay.svg').write_text(''.join(svg))
(OUT/'mechanical-validation.json').write_text(json.dumps(report,indent=2)+'\n')
print(json.dumps(report['halves'],indent=2))