MustafaMulla29/rp2040-bldc-motor-controller-new

73×72 mm 4-layer USB‑C PD (5/9/15/20 V) RP2040 controller PCB with protected power switching, MCT8329A sensorless BLDC three-phase MOSFET bridge, USB data/programming, onboard controls, and three motor outputs feeding a 2212 motor through rigid bullet adapters.

Version
0.3.100
License
unset
Stars
0

assembly/export-package-assets.ts

import { createHash } from "node:crypto"
import { mkdir, readFile, writeFile } from "node:fs/promises"
import { resolve } from "node:path"
import { hashPcbGeometry } from "../scripts/assembly-geometry"

// This is a native CAD export, not a replacement for the live PCB or its netlist.
// The browser needs only this portable asset; native assembly.cable authoring
// and the custom rigid-adapter bullet models remain in this directory.
const root = resolve(import.meta.dir, "..")
const hash = (bytes: Uint8Array) => createHash("sha256").update(bytes).digest("hex")
const board = await readFile(resolve(root, "dist/index/circuit.json"))
const report = JSON.parse(await readFile(resolve(import.meta.dir, "dist/results/assembly-report.json"), "utf8"))
const model = await readFile(resolve(import.meta.dir, "dist/results/mechanical.glb"))
if (report.pcbSourceSha256 !== hash(board) || report.nativeRenderErrors !== 0 ||
    report.nativeMotorLeads !== 3 || report.rigidAdapters !== 3 || report.nativeCables !== 5) {
  throw new Error("Rebuild and validate the complete assembly before exporting package assets")
}
if (model.subarray(0, 4).toString() !== "glTF") throw new Error("Invalid mechanical GLB")
const assets = resolve(root, "assets")
await mkdir(assets, { recursive: true })
await writeFile(resolve(assets, "connected-system.glb"), model)
await writeFile(resolve(assets, "connected-system.provenance.json"), JSON.stringify({
  authoring: "assembly/full-assembly.tsx",
  pcbEntrypoint: "index.circuit.tsx",
  packageEntrypoint: "system.circuit.tsx",
  pcbCircuitSha256: hash(board),
  pcbGeometrySha256: hashPcbGeometry(JSON.parse(board.toString())),
  mechanicalAssetSha256: hash(model),
  mechanicalAssetBytes: model.length,
  includesPcb: false,
  nativeMotorLeads: report.nativeMotorLeads,
  rigidAdapters: report.rigidAdapters,
  nativeUsbCables: report.nativeCables - report.nativeMotorLeads,
  geometryNotes: report.missing,
}, null, 2) + "\n")
console.log(`Exported package assembly: ${model.length} bytes; PCB stays native`)