krishnax12/usb-c-control-dial

USB-C control dial PCB with an EC11 push encoder and a 16-LED ring for volume and brightness controls. Work in progress; firmware not included and not fabrication-ready.

Version
0.0.3
License
unset
Stars
1

scripts/render-repro.tsx

import { mkdir } from "node:fs/promises"
import { relative, resolve } from "node:path"
import { RootCircuit } from "@tscircuit/core-rect-rotation"
import { Resvg } from "@resvg/resvg-js"
import { convertCircuitJsonToPcbSvg } from "circuit-to-svg"

const projectDirectory = resolve(import.meta.dir, "..")
Bun.plugin({
  name: "relative-cad-assets",
  setup(build) {
    build.onLoad({ filter: /\.(obj|step)$/ }, (asset) => ({
      contents: `export default ${JSON.stringify(`./${relative(projectDirectory, asset.path)}`)}`,
      loader: "js",
    }))
  },
})

const { default: Repro } = await import("../repro.circuit")
const circuit = new RootCircuit()
const solverEvents: string[] = []
const solverInputs: {
  solverName: string
  constructorArgs: readonly unknown[]
}[] = []
circuit.on("solver:started", (event) => {
  solverEvents.push(event.solverName)
  solverInputs.push({
    solverName: event.solverName,
    constructorArgs: event.solverConstructorArgs,
  })
  console.log(`Started ${event.solverName}`)
})
circuit.on("solver:ended", (event) =>
  console.log(`Finished ${event.solverName}`),
)
circuit.add(<Repro />)
await circuit.renderUntilSettled()
const circuitJson = circuit.getCircuitJson()
const outputDirectory = resolve(projectDirectory, "outputs/repro")
await mkdir(outputDirectory, { recursive: true })
await Bun.write(
  resolve(outputDirectory, "circuit.json"),
  JSON.stringify(circuitJson, null, 2),
)
await Bun.write(
  resolve(projectDirectory, "board.circuit.json"),
  JSON.stringify(circuitJson, null, 2),
)
await Bun.write(
  resolve(outputDirectory, "solvers.json"),
  JSON.stringify(solverEvents, null, 2),
)
await Bun.write(
  resolve(outputDirectory, "solver-inputs.json"),
  JSON.stringify(solverInputs, null, 2),
)
const cutouts = circuitJson.filter((element) => element.type === "pcb_cutout")
const diagnostics = circuitJson.filter((element) =>
  /error|warning/.test(element.type),
)
await Bun.write(
  resolve(outputDirectory, "diagnostics.json"),
  JSON.stringify(diagnostics, null, 2),
)
const views = {
  board: { layer: "inner1" as const, width: 1400, height: 1400 },
  closeup: {
    layer: "inner1" as const,
    width: 1000,
    height: 900,
    viewport: { minX: 19, maxX: 27, minY: 15, maxY: 23 },
  },
}
for (const [name, settings] of Object.entries(views)) {
  const svg = convertCircuitJsonToPcbSvg(circuitJson, {
    ...settings,
    showCourtyards: false,
    showFabricationNotes: false,
  })
  await Bun.write(resolve(outputDirectory, `${name}.svg`), svg)
  await Bun.write(
    resolve(outputDirectory, `${name}.png`),
    new Resvg(svg).render().asPng(),
  )
}
console.log(
  JSON.stringify(
    {
      core: "tscircuit/core#4378 @ 9016814 (unmerged rotation-input support)",
      cutouts,
      pcbTraces: circuitJson.filter((element) => element.type === "pcb_trace")
        .length,
      diagnostics: Object.fromEntries(
        [...new Set(diagnostics.map((element) => element.type))].map((type) => [
          type,
          diagnostics.filter((element) => element.type === type).length,
        ]),
      ),
    },
    null,
    2,
  ),
)