Asymtode712/ecosystem-probe

A 5 V NE555 astable oscillator board with fuse protection, RC timing, reset pushbutton, alternating red/green LED indicators, output header, test points, and ground planes.

Version
0.1.0
License
unset
Stars
0

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone Asymtode712/ecosystem-probe  # download the package files into ./ecosystem-probe
tsci add Asymtode712/ecosystem-probe    # add it to your project as @tsci/Asymtode712.ecosystem-probe

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.

build-local.tsx

import { mkdir, writeFile } from "node:fs/promises"
import { join } from "node:path"
import { Resvg } from "../core/node_modules/@resvg/resvg-js"
import { RootCircuit } from "../core/lib"
import {
  checkCopperPourShorts,
  checkPcbTraceSelfShorts,
  checkPcbTraceStyle,
  checkSchematicPlacement,
} from "../core/node_modules/@tscircuit/checks"
import EcosystemProbeBoard from "./index"

// Load each checkout in its own runtime context. Their Circuit JSON type
// versions and `lib/*` TS path aliases are deliberately independent of core.
const loadLocal = (relativePath: string) =>
  import(new URL(relativePath, import.meta.url).href)

const { convertCircuitJsonToPcbSvg, convertCircuitJsonToSchematicSvg } =
  await loadLocal("../circuit-to-svg/lib/index.ts")
const {
  CircuitJsonToKicadPcbConverter,
  CircuitJsonToKicadProConverter,
  CircuitJsonToKicadSchConverter,
} = await loadLocal("../circuit-json-to-kicad/lib/index.ts")
const { convertCircuitJsonToGltf } = await loadLocal(
  "../circuit-json-to-gltf/lib/index.ts",
)

const outputDirectory = join(import.meta.dir, "dist", "local")
await mkdir(outputDirectory, { recursive: true })

const circuit = new RootCircuit()
circuit.add(<EcosystemProbeBoard />)
await circuit.renderUntilSettled()

const circuitJson = circuit.getCircuitJson()
await writeFile(
  join(outputDirectory, "ecosystem-probe.circuit.json"),
  JSON.stringify(circuitJson, null, 2),
)

const counts = Object.fromEntries(
  [...new Set(circuitJson.map((element) => element.type))].map((type) => [
    type,
    circuitJson.filter((element) => element.type === type).length,
  ]),
)
const diagnostics = circuitJson.filter(
  (element) =>
    element.type.endsWith("_error") || element.type.endsWith("_warning"),
)
const independentChecks = {
  copperPourShorts: checkCopperPourShorts(circuitJson),
  traceSelfShorts: checkPcbTraceSelfShorts(circuitJson),
  traceStyle: checkPcbTraceStyle(circuitJson),
  schematicPlacement: checkSchematicPlacement(circuitJson),
}

for (const [name, render] of [
  [
    "pcb-top.svg",
    () => convertCircuitJsonToPcbSvg(circuitJson, { layer: "top" }),
  ],
  [
    "pcb-bottom.svg",
    () => convertCircuitJsonToPcbSvg(circuitJson, { layer: "bottom" }),
  ],
  ["schematic.svg", () => convertCircuitJsonToSchematicSvg(circuitJson)],
] as const) {
  const svg = render()
  await writeFile(join(outputDirectory, name), svg)
  if (name === "pcb-top.svg" || name === "schematic.svg") {
    const png = new Resvg(svg, {
      fitTo: { mode: "width", value: name === "schematic.svg" ? 1600 : 1400 },
    })
      .render()
      .asPng()
    await writeFile(join(outputDirectory, name.replace(".svg", ".png")), png)
  }
}

const pcbConverter = new CircuitJsonToKicadPcbConverter(circuitJson)
pcbConverter.runUntilFinished()
await writeFile(
  join(outputDirectory, "ecosystem-probe.kicad_pcb"),
  pcbConverter.getOutputString(),
)

const schematicConverter = new CircuitJsonToKicadSchConverter(circuitJson)
schematicConverter.runUntilFinished()
for (const file of schematicConverter.getOutputFiles({
  schematicFilename: "ecosystem-probe.kicad_sch",
})) {
  await writeFile(join(outputDirectory, file.filename), file.content)
}

const projectConverter = new CircuitJsonToKicadProConverter(circuitJson, {
  projectName: "ecosystem-probe",
  schematicFilename: "ecosystem-probe.kicad_sch",
  pcbFilename: "ecosystem-probe.kicad_pcb",
  schematicSheetPlan: schematicConverter.schematicSheetPlan,
})
projectConverter.runUntilFinished()
await writeFile(
  join(outputDirectory, "ecosystem-probe.kicad_pro"),
  projectConverter.getOutputString(),
)

const glb = await convertCircuitJsonToGltf(circuitJson, {
  format: "glb",
  boardTextureResolution: 512,
  includeModels: false,
})
if (!(glb instanceof ArrayBuffer)) {
  throw new Error("3D converter did not return a GLB ArrayBuffer")
}
await writeFile(
  join(outputDirectory, "ecosystem-probe.glb"),
  new Uint8Array(glb),
)

const report = { counts, diagnostics, independentChecks }
await writeFile(
  join(outputDirectory, "report.json"),
  JSON.stringify(report, null, 2),
)
console.log(
  JSON.stringify(
    {
      counts,
      diagnostics: diagnostics.map((diagnostic) => ({
        type: diagnostic.type,
        message: "message" in diagnostic ? diagnostic.message : undefined,
      })),
      independentChecks,
    },
    null,
    2,
  ),
)

if (
  diagnostics.some((diagnostic) => diagnostic.type.endsWith("_error")) ||
  Object.values(independentChecks).some((findings) => findings.length > 0)
) {
  process.exitCode = 1
}