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.
src/routing/index.ts
import { fanoutTracePath } from '@tscircuit/props'
import left from './left.json'
import right from './right.json'
import type { Half } from '../mechanicalGeometry'
import type {
GenericLocalAutorouter, SimpleRouteJson, SimplifiedPcbTrace,
AutorouterCompleteEvent, AutorouterErrorEvent, AutorouterProgressEvent,
} from '@tscircuit/core'
// Native wire/via paths, independently checked for complete copper connectivity.
export const routesFor = (half: Half) => (half === 'left' ? left : right).paths.map(path =>
fanoutTracePath.parse({ connection: path.connection, route: path.route }),
)
type Events = { complete: AutorouterCompleteEvent; error: AutorouterErrorEvent; progress: AutorouterProgressEvent }
type NetAssignedTrace = SimplifiedPcbTrace & { source_trace_id: string }
/** Native router adapter for verified external copper, including branched through
* vias with unused outer-layer lands. Complete saved-phase paths currently demand
* a wire on both outer layers of each through via. The independent geometry check
* checks physical pad/copper contacts on every layer instead.
*/
class PersistedRouter implements GenericLocalAutorouter {
isRouting = false
private events = new EventTarget()
constructor(public input: SimpleRouteJson, private traces: SimplifiedPcbTrace[]) {}
on<T extends keyof Events>(event: T, callback: (event: Events[T]) => void) {
this.events.addEventListener(event, e => callback((e as CustomEvent<Events[T]>).detail))
}
solveSync() { return this.traces }
stop() { this.isRouting = false }
start() {
this.isRouting = true
this.events.dispatchEvent(new CustomEvent<AutorouterCompleteEvent>('complete', { detail: { type: 'complete', traces: this.traces } }))
this.isRouting = false
}
}
export const persistedRouting = (half: Half) => async (input: SimpleRouteJson): Promise<GenericLocalAutorouter> => {
const data = half === 'left' ? left : right
const ports: Record<string, { x: number; y: number; net: string; layers: string[] }> = data.ports
const nets: Record<string, string> = data.netIds
const connections: Record<string, string> = data.connectionNets
// Any changed netlist or moved terminal requires a fresh routing solution.
for (const connection of input.connections) for (const point of connection.pointsToConnect) {
const saved = point.pcb_port_id ? ports[point.pcb_port_id] : undefined
if (!saved || Math.hypot(saved.x - point.x, saved.y - point.y) > .0001 || connections[connection.name] !== saved.net) {
throw new Error(`Routing is stale: ${JSON.stringify({point,saved,connection:connection.name,net:saved?nets[saved.net]:undefined})}; regenerate and validate both halves`)
}
}
const parsed = routesFor(half)
const traces: NetAssignedTrace[] = data.paths.flatMap((path, i) => {
const route = parsed[i].route
const vias = route.filter(p => p.route_type === 'via')
// A standalone via is copper on its whole layer span. Artificial zero-length
// outer wires would incorrectly describe dangling branches on unused layers.
const pieces = vias.length && route.every(p => Math.hypot(p.x-vias[0].x,p.y-vias[0].y)<1e-7)
? [vias]
: (() => {
const result: typeof route[] = []; let start=0
for(let j=1;j<route.length-1;j++) if(Object.values(ports).some(p=>p.net===path.net&&Math.hypot(p.x-route[j].x,p.y-route[j].y)<.0001)) {
result.push(route.slice(start,j+1));start=j
}
result.push(route.slice(start));return result
})()
return pieces.map((route,j)=>({
type: 'pcb_trace', pcb_trace_id: `${half}_routed_${i}_${j}`,
connection_name: nets[path.net], source_trace_id: nets[path.net], route,
}))
})
// Explicit contacts within via-in-pad lands. Copper stays inside the existing
// pad/via union; this gives native port indexing an endpoint on the pad layer.
const stack=['top','inner1','inner2','bottom']
for(const [id,port] of Object.entries(ports)) {
const via=data.paths.flatMap((path,i)=>path.net===port.net?parsed[i].route.filter(p=>p.route_type==='via'):[])
.find(p=>Math.hypot(p.x-port.x,p.y-port.y)<.001)
if(!via)continue
const [lo,hi]=[stack.indexOf(via.from_layer),stack.indexOf(via.to_layer)].sort((a,b)=>a-b)
const layer=(['top','inner1','inner2','bottom'] as const).find(l=>port.layers.includes(l)&&stack.indexOf(l)>=lo&&stack.indexOf(l)<=hi)
if(!layer)continue
traces.push({type:'pcb_trace',pcb_trace_id:`${half}_pad_contact_${id}`,source_trace_id:nets[port.net],connection_name:nets[port.net],route:[
{route_type:'wire',x:port.x,y:port.y,width:.145,layer},
{route_type:'wire',x:via.x,y:via.y,width:.145,layer},
]})
}
return new PersistedRouter(input, traces)
}