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.
tests/keyboard.test.ts
import { expect,test } from 'bun:test'
import { readFileSync } from 'node:fs'
import { createHash } from 'node:crypto'
import { geometryFor, mechanicalGeometry } from '../src/mechanicalGeometry'
import { pinout } from '../src/pinout'
import pins from '../src/generated/nrf-pins.json'
import { checkConnectorAccessibleOrientation } from '@tscircuit/checks'
for(const half of ['left','right'] as const) {
const g=geometryFor(half)
const json=JSON.parse(readFileSync(`outputs/${half}/keyboard.circuit.json`,'utf8')) as any[]
const source=(name:string)=>json.find(e=>e.type==='source_component'&&e.name===name)
const component=(name:string)=>json.find(e=>e.type==='pcb_component'&&e.source_component_id===source(name)?.source_component_id)
const net=(name:string,pin:string)=>{
const p=json.find(e=>e.type==='source_port'&&e.source_component_id===source(name)?.source_component_id&&e.port_hints.includes(pin))
expect(p,`${name}.${pin} exists`).toBeDefined()
const trace=json.find(e=>e.type==='source_trace'&&e.connected_source_port_ids?.includes(p.source_port_id))
return json.find(e=>e.type==='source_net'&&trace?.connected_source_net_ids?.includes(e.source_net_id))?.name
}
test(`${half}: 29 sockets, 30 diodes and one bare MCU`,()=>{
expect(g.keys).toHaveLength(29)
expect(json.filter(e=>e.type==='source_component'&&/^SW\d+$/.test(e.name))).toHaveLength(29)
expect(json.filter(e=>e.type==='source_component'&&e.ftype==='simple_diode')).toHaveLength(30)
expect(source('U1').manufacturer_part_number).toBe('NRF52840-QIAA-R')
expect(source('SW25')).toBeUndefined()
})
test(`${half}: actual switch drills match locked source anchors`,()=>{
for(const key of g.keys) {
const holes=json.filter(e=>e.type==='pcb_hole'&&e.hole_diameter===3.4&&Math.hypot(e.x-key.x,e.y-key.y)<.001)
expect(holes).toHaveLength(1)
expect(holes[0].x).toBeCloseTo(key.x,6);expect(holes[0].y).toBeCloseTo(key.y,6)
}
const board=json.find(e=>e.type==='pcb_board')
expect(board.num_layers).toBe(4)
expect(board.outline).toEqual(g.outline)
})
test(`${half}: all thirty matrix positions have COL → switch → diode → ROW`,()=>{
for(const k of g.keys) {
const n=k.reference.slice(2)
expect(net(`SW${n}`,'pin1')).toBe(`COL${k.column}`)
expect(net(`SW${n}`,'pin2')).toBe(`KEY${n}`)
expect(net(`D${n}`,'anode')).toBe(`KEY${n}`)
expect(net(`D${n}`,'cathode')).toBe(`ROW${k.row}`)
}
expect(net('ENC1','S1')).toBe('COL0');expect(net('D_ENC','cathode')).toBe('ROW4')
})
test(`${half}: QIAA power configuration protects internal regulator nodes`,()=>{
const terminal=(pad:keyof typeof pins)=>`pin${pins[pad].pin}`
for(const pad of ['A22','B1','W1','AD14','AD23','Y2','AB2'] as const) expect(net('U1',terminal(pad))).toBe('VCC')
for(const pad of ['B5','E24'] as const) expect(net('U1',terminal(pad))).toBe('DEC4_6')
for(const pad of ['C1','D23','AC5'] as const) expect(net('U1',terminal(pad))).not.toBe('VCC')
for(const pad of ['B7','F23','EP'] as const) expect(net('U1',terminal(pad))).toBe('GND')
expect(net('U1',terminal('AD2'))).toBe('VBUS_NRF')
})
test(`${half}: raw ten-contact LCD pinout and two USB orientations`,()=>{
const display=['displaySck','displayMosi','displayCs','displayVcom','displayDisp','LCD_VDD','LCD_VDD','LCD_VDD','GND','GND']
display.forEach((n,i)=>expect(net('J_LCD',`pin${i+1}`)).toBe(n))
for(const p of ['DP1','DP2']) expect(net('J_USB',p)).toBe('USB_DP')
for(const p of ['DN1','DN2']) expect(net('J_USB',p)).toBe('USB_DM')
for(const p of ['VBUS1','VBUS2','VBUS3','VBUS4']) expect(net('J_USB',p)).toBe('VBUS')
})
test(`${half}: source compiles without unsupported properties or disconnected selectors`,()=>{
expect(json.filter(e=>/invalid_component_property|missing_footprint|trace_not_connected|external_footprint_load/.test(e.type))).toEqual([])
expect(json.filter(e=>/warning|error/.test(e.type))).toEqual([])
})
test(`${half}: default output is routed with HDI escapes and ground planes`,()=>{
expect(json.filter(e=>e.type.includes('error'))).toEqual([])
expect(json.filter(e=>e.type==='pcb_trace').length).toBeGreaterThan(800)
const microvias=json.filter(e=>e.type==='pcb_via'&&e.from_layer==='bottom'&&e.to_layer==='inner2')
const vias=json.filter(e=>e.type==='pcb_via')
expect(new Set(vias.map(v=>`${v.x.toFixed(3)},${v.y.toFixed(3)}`)).size).toBe(vias.length)
expect(microvias.length).toBeGreaterThanOrEqual(20)
for(const via of microvias){expect(via.hole_diameter).toBe(.15);expect(via.outer_diameter).toBe(.35)}
for(const layer of ['inner1','inner2'])expect(json.some(e=>e.type==='pcb_copper_pour'&&e.layer===layer)).toBe(true)
expect(net('SW_RESET','pin1')).toBe('RESET');expect(net('SW_RESET','pin2')).toBe('RESET')
expect(net('SW_RESET','pin3')).toBe('GND');expect(net('SW_RESET','pin4')).toBe('GND')
})
test(`${half}: native enclosure and edge-accessible power switch are in the assembly`,()=>{
const sign=half==='left'?1:-1,sw=component('SW_PWR')
expect(sw.footprint_origin.x).toBe(sign*63)
expect(sw.layer).toBe('bottom')
expect(component('J_USB').layer).toBe('bottom')
expect(json.filter(e=>e.type.includes('silkscreen')&&e.layer==='top')).toEqual([])
const y=sw.footprint_origin.y
const intersections=g.outline.flatMap((a,i)=>{
const b=g.outline[(i+1)%g.outline.length]
return (a.y<=y&&b.y>y)||(b.y<=y&&a.y>y)?[a.x+(y-a.y)*(b.x-a.x)/(b.y-a.y)]:[]
})
const edge=sign>0?Math.max(...intersections):Math.min(...intersections)
expect(sign*(sw.footprint_origin.x+sign*3.385-edge)).toBeGreaterThan(.8)
for(const suffix of ['_KEY','_CAP']) {
expect(json.filter(e=>e.type==='source_component'&&/^SW\d+/.test(e.name)&&e.name.endsWith(suffix))).toHaveLength(29)
}
for(const name of ['LCD_GLASS','LCD_ACTIVE','LCD_FLEX','BATTERY_CELL','ENC1_KNOB']) {
expect(json.some(e=>e.type==='cad_component'&&e.source_component_id===source(name)?.source_component_id)).toBe(true)
}
const printed=json.filter(e=>e.type==='source_component'&&e.ftype==='printedpart')
expect(printed).toHaveLength(7)
for(const name of ['CASE_BASE','CASE_PLATE','SPACER_TH3','SPACER_TH7']) {
const part=source(name)
expect(json.find(e=>e.type==='cad_component'&&e.source_component_id===part.source_component_id)?.model_jscad).toBeDefined()
}
const check=JSON.parse(readFileSync('outputs/enclosure/clearance-check.json','utf8')).halves[half]
expect(check.switchAccess.every((p:any)=>p.caseObstructionMm2===0&&p.actuatorBeyondPcbEdgeMm>.8)).toBe(true)
expect(Object.values(check.parts).every((p:any)=>p.watertight&&p.volumeMm3>0)).toBe(true)
})
}
test('mirror preserves 58 source stems and seven mounting drill centers per half',()=>{
const l=geometryFor('left'),r=geometryFor('right')
for(let i=0;i<29;i++){expect(l.keys[i].x).toBe(-r.keys[i].x);expect(l.keys[i].y).toBe(r.keys[i].y)}
expect(l.mountingHoles).toHaveLength(7)
// Upstream TH3 has a deliberate local drill offset, not the footprint anchor.
expect(r.mountingHoles[2].sourceCenter.x).toBeCloseTo(130.52712,6)
expect(r.mountingHoles[2].sourceCenter.y).toBeCloseTo(57.57364,6)
expect(mechanicalGeometry.source.commit).toBe('aae7cc0ac5ca563dab8222320ae8749ed1911e56')
})
test('GPIOs are unique and do not consume crystal, reset or NFC pins',()=>{
const all=Object.values(pinout).flat()
expect(new Set(all).size).toBe(all.length)
for(const reserved of ['P0_00','P0_01','P0_09','P0_10','P0_18']) expect(all).not.toContain(reserved)
})
// Package measurements are independent of the EasyEDA numeric pin order.
const terminals=JSON.parse(readFileSync('references/nrf-terminal-positions.json','utf8')) as Record<string,{x:number;y:number}>
for(const half of ['left','right'] as const) {
const json=JSON.parse(readFileSync(`outputs/${half}/keyboard.circuit.json`,'utf8')) as any[]
const source=(name:string)=>json.find(e=>e.type==='source_component'&&e.name===name)
const component=(name:string)=>json.find(e=>e.type==='pcb_component'&&e.source_component_id===source(name)?.source_component_id)
test(`${half}: imported MCU places every physical terminal in the audited bottom-side frame`,()=>{
const mcu=component('U1'),origin=mcu.footprint_origin
const ports=json.filter(e=>e.type==='source_port'&&e.source_component_id===source('U1').source_component_id)
for(const [terminal,xy] of Object.entries(terminals)) {
const port=ports.find(e=>e.port_hints.includes(terminal))
expect(port,terminal).toBeDefined()
const pad=json.find(e=>e.type==='pcb_port'&&e.source_port_id===port.source_port_id)
expect(Math.hypot(pad.x-(origin.x-xy.x),pad.y-(origin.y+xy.y)),terminal).toBeLessThan(.0002)
}
})
test(`${half}: imported socket contact holes align with Kailh's switch-centered pattern`,()=>{
for(const k of geometryFor(half).keys) {
const c=component(k.reference),a=(k.rotation+180)*Math.PI/180
const holes=json.filter(e=>e.type==='pcb_hole'&&e.pcb_component_id===c.pcb_component_id&&Math.abs(e.hole_diameter-3)<.001)
expect(holes).toHaveLength(2)
for(const [x,y] of [[0,5.9],[5,3.7]]) {
const px=k.x+x*Math.cos(a)-y*Math.sin(a),py=k.y+x*Math.sin(a)+y*Math.cos(a)
expect(Math.min(...holes.map(h=>Math.hypot(h.x-px,h.y-py)))).toBeLessThan(.0002)
}
}
})
test(`${half}: exported socket meshes fit every pad and locating drill`,()=>{
const report=JSON.parse(readFileSync(`outputs/${half}/socket-model-check.json`,'utf8'))
expect(report.passed).toBe(true)
expect(report.sockets).toHaveLength(29)
expect(report.maximumPegErrorMm).toBeLessThan(.002)
expect(report.minimumLandMarginMm).toBeGreaterThan(.1)
expect(report.circuitJsonSha256).toBe(createHash('sha256').update(readFileSync(`outputs/${half}/keyboard.circuit.json`)).digest('hex'))
expect(report.glbSha256).toBe(createHash('sha256').update(readFileSync(`outputs/${half}/keyboard-placement.glb`)).digest('hex'))
for(const k of geometryFor(half).keys) {
const c=component(k.reference)
const courtyards=json.filter(e=>e.type.startsWith('pcb_courtyard')&&e.pcb_component_id===c.pcb_component_id)
expect(courtyards.length).toBe(4)
expect(courtyards.every(e=>e.layer==='bottom')).toBe(true)
}
})
test(`${half}: seven A4 schematic sheets separate the functional blocks`,()=>{
const sheets=json.filter(e=>e.type==='schematic_sheet').sort((a,b)=>a.sheet_index-b.sheet_index)
expect(json.filter(e=>e.type==='schematic_element_outside_sheet_warning')).toEqual([])
expect(sheets.map(e=>e.name)).toEqual(['matrix','processor','power','display','usb','debug','radio'])
expect(sheets.every(e=>e.sheet_size==='a4')).toBe(true)
expect(json.filter(e=>e.type==='schematic_component').every(e=>e.schematic_sheet_id)).toBe(true)
})
test(`${half}: internal FPC access is explicit and external connector checks stay active`,()=>{
expect(component('J_LCD').metadata.kicad_footprint.properties.ConnectorAccess.value).toBe('internal')
expect(component('J_LCD').insertion_direction).toBe('from_top')
// Keep the real board/connector geometry without parsing unrelated CAD assets.
const selected=new Set(['J_LCD','J_USB'].flatMap(name=>[source(name),component(name)]))
const connectors=json.filter(e=>e.type==='pcb_board'||selected.has(e))
expect(checkConnectorAccessibleOrientation(connectors)).toEqual([])
const withoutRole=connectors.map(e=>e===component('J_LCD')?{...e,metadata:undefined}:e)
expect(checkConnectorAccessibleOrientation(withoutRole).some(e=>e.pcb_component_id===component('J_LCD').pcb_component_id)).toBe(true)
const turnedUsb=connectors.map(e=>e===component('J_USB')?{...e,insertion_direction:half==='left'?'from_left':'from_right'}:e)
expect(checkConnectorAccessibleOrientation(turnedUsb).some(e=>e.pcb_component_id===component('J_USB').pcb_component_id)).toBe(true)
})
test(`${half}: compiled components retain JLCPCB IDs and crystal loading`,()=>{
expect(source('U1').supplier_part_numbers.jlcpcb).toEqual(['C190794'])
expect(source('SW1').supplier_part_numbers.jlcpcb).toEqual(['C5333465'])
expect(source('ENC1').supplier_part_numbers.jlcpcb).toEqual(['C18164806'])
expect(source('D1').supplier_part_numbers.jlcpcb).toEqual(['C81598'])
expect(source('Y_HF').supplier_part_numbers.jlcpcb).toEqual(['C187794'])
expect(source('Y_LF').supplier_part_numbers.jlcpcb).toEqual(['C179635'])
expect(source('Y_HF').load_capacitance).toBe(8e-12)
expect(source('Y_LF').load_capacitance).toBe(9e-12)
})
}
test('selected supplier manifest matches the component IDs emitted for both boards',()=>{
const manifest=JSON.parse(readFileSync('imports/manifest.json','utf8'))
const declared=manifest.parts.filter((p:any)=>p.selectedInCurrentBoards).map((p:any)=>p.jlcpcb).sort()
expect(new Set(manifest.parts.map((p:any)=>p.jlcpcb)).size).toBe(manifest.parts.length)
for(const half of ['left','right']) {
const json=JSON.parse(readFileSync(`outputs/${half}/keyboard.circuit.json`,'utf8'))
const actual=[...new Set(json.flatMap((e:any)=>e.type==='source_component'?e.supplier_part_numbers?.jlcpcb??[]:[]))].sort()
expect(declared).toEqual(actual)
}
})