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-keyboard

In 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/mechanicalReferences.tsx

/** Inspection views pose the exported native meshes; electronics stay editable. */
import * as React from 'react'
import { Circuit } from '@tscircuit/core'
import { assembly } from 'tscircuit'
import { convertCircuitJsonToGltf } from 'circuit-json-to-gltf'
import { renderGLTFToPNGFromGLB } from 'poppygl'
import { writeFile } from 'node:fs/promises'
import { TentingStand } from './TentingStand'
import tenting from './generated/tenting.json'
import type { Half } from './mechanicalGeometry'

export function unpackGlb(bytes:Uint8Array) {
  const raw=Buffer.from(bytes),length=raw.readUInt32LE(12)
  return {document:JSON.parse(raw.subarray(20,20+length).toString()),binary:raw.subarray(20+length)}
}
export function packGlb(document:unknown,binary:Buffer) {
  const json=Buffer.from(JSON.stringify(document)),padded=Buffer.alloc(Math.ceil(json.length/4)*4,32)
  json.copy(padded)
  const header=Buffer.alloc(20)
  header.write('glTF');header.writeUInt32LE(2,4)
  header.writeUInt32LE(20+padded.length+binary.length,8)
  header.writeUInt32LE(padded.length,12);header.write('JSON',16)
  return Buffer.concat([header,padded,binary])
}
export async function renderMechanicalReferences(json:Parameters<typeof convertCircuitJsonToGltf>[0],flatGlb:Uint8Array,half:Half,folder:string) {
  const stand=new Circuit();stand.schematicDisabled=true
  stand.add(<assembly.device name="OPTIONAL_STAND"><TentingStand half={half}/></assembly.device>)
  await stand.renderUntilSettled()
  const standJson=JSON.parse(JSON.stringify(stand.getCircuitJson())
    .replaceAll(/(source_component|cad_component|cad_assembly_bom_item)_(\d+)/g,'$1_tenting_$2'))
    .filter((e:{type:string})=>['source_component','cad_component'].includes(e.type))
  const bytes=new Uint8Array(await convertCircuitJsonToGltf([...json,...standJson],{
    format:'glb',includeModels:true,boardTextureResolution:4096,
  }) as ArrayBuffer)
  const {document,binary}=unpackGlb(bytes),scene=document.scenes[document.scene??0]
  const standNodes=scene.nodes.filter((i:number)=>document.nodes[i].name?.startsWith('TENTING_'))
  if(standNodes.length!==1)throw new Error(`${half}: missing native tenting cradle`)
  const pose=tenting.halves[half]['10'],a=pose.rotationYDegrees*Math.PI/180,c=Math.cos(a),s=Math.sin(a)
  const root=document.nodes.length
  document.nodes.push({name:'TENTED_KEYBOARD',matrix:[c,s,0,0,-s,c,0,0,0,0,1,0,0,pose.liftZ,0,1],
    children:scene.nodes.filter((i:number)=>!standNodes.includes(i))})
  scene.nodes=[root,...standNodes]
  const tented=packGlb(document,binary)
  await writeFile(`${folder}/tenting-10deg.glb`,tented)
  await writeFile(`${folder}/tenting-10deg.png`,await renderGLTFToPNGFromGLB(tented,{
    width:1600,height:1200,supersampling:2,projection:'orthographic',orthographicHeight:160,
    camPos:[half==='left'?160:-160,100,-175],lookAt:[0,15,0],up:'y+',backgroundColor:'#edf1f5',ambient:.65,
  }))
  // Remove the PCB and upper assembly to reveal the actual cell in its bay.
  const bay=unpackGlb(flatGlb),bayScene=bay.document.scenes[bay.document.scene??0]
  bayScene.nodes=bayScene.nodes.filter((i:number)=>/^(CASE_BASE|BATTERY_CELL|BATTERY_LABEL)$/.test(bay.document.nodes[i].name??''))
  const battery=packGlb(bay.document,bay.binary)
  await writeFile(`${folder}/battery-bay.glb`,battery)
  await writeFile(`${folder}/battery-bay.png`,await renderGLTFToPNGFromGLB(battery,{
    width:1600,height:1200,supersampling:2,projection:'orthographic',orthographicHeight:155,
    camPos:[110,180,-130],lookAt:[0,-6,0],up:'y+',backgroundColor:'#edf1f5',ambient:.7,
  }))
}