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/build-project.ts

/** Cloud entry point: compile the sources, then render native high-resolution assets. */
import { readFile, writeFile, mkdir } from 'node:fs/promises'
import { convertCircuitJsonToGltf } from 'circuit-json-to-gltf'
import { convertCircuitJsonToSchematicSvg, convertCircuitJsonToStackedSchematicSheetsSvg } from 'circuit-to-svg'
import { renderGLTFToPNGFromGLB } from 'poppygl'
import { Resvg } from '@resvg/resvg-js'
import { renderMechanicalReferences } from './mechanicalReferences'

const cli = Bun.spawn([
  process.execPath, 'node_modules/tscircuit/cli.mjs', 'build',
  '--disable-parts-engine', '--svgs', '--pcb-png', '--all-images',
], { stdout: 'inherit', stderr: 'inherit' })
if (await cli.exited !== 0) throw new Error('tsci build failed')

for (const half of ['left', 'right']) {
  const folder = `dist/${half}`
  const json = JSON.parse(await readFile(`${folder}/circuit.json`, 'utf8'))
  const errors = json.filter((element: { type: string }) => /error|warning/.test(element.type))
  if (errors.length) throw new Error(`${half}: ${errors.length} native diagnostics`)
  console.log(`${half}: generating 4096px board textures`)
  const glb = new Uint8Array(await convertCircuitJsonToGltf(json, {
    format: 'glb', includeModels: true, boardTextureResolution: 4096,
  }) as ArrayBuffer)
  await writeFile(`${folder}/3d.glb`, glb)
  await writeFile(`${folder}/3d.png`, await renderGLTFToPNGFromGLB(glb, {
    width: 1600, height: 1200, supersampling: 2, projection: 'orthographic',
    orthographicHeight: 145, camPos: [145,180,-145], lookAt: [0,-1,0], up: 'y+', backgroundColor: '#edf1f5', ambient: .6,
  }))
  await renderMechanicalReferences(json,glb,half as 'left'|'right',folder)
  await mkdir(`${folder}/schematics`, { recursive: true })
  const sheets = json.filter((element: { type: string }) => element.type === 'schematic_sheet')
  for (const sheet of sheets) {
    const svg = convertCircuitJsonToSchematicSvg(json, {
      schematicSheetIndex: sheet.sheet_index, width: 2400, height: 1700,
    })
    const name = `${String(sheet.sheet_index + 1).padStart(2, '0')}-${sheet.name}`
    await writeFile(`${folder}/schematics/${name}.svg`, svg)
    await writeFile(`${folder}/schematics/${name}.png`, new Resvg(svg).render().asPng())
  }
  await writeFile(`${folder}/schematic.svg`, convertCircuitJsonToStackedSchematicSheetsSvg(json, {
    width: 1600, height: 1100, sheetLabelHeight: 40,
  }))
  await writeFile(`${folder}/preview-quality.json`, JSON.stringify({
    boardTextureResolution: 4096, previewWidth: 1600, previewHeight: 1200,
    schematicSheets: sheets.length, generatedFrom: 'native Circuit JSON; no image upscaling',
  }, null, 2) + '\n')
  console.log(`${half}: high-resolution GLB, preview and ${sheets.length} schematic sheets written`)
}