MustafaMulla29/usb-pd-motherboard

4-layer USB-C PD hub motherboard with 20 V USB-C input, Linux T113-S3 computer, CH338F USB hub, four individually controlled 5–20 V PD outputs, USB data/camera ports, regulated rails, and hardware watchdog/E-stop safety interlock.

Version
1.0.24
License
unset
Stars
0

Use with the tscircuit CLI

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

tsci clone MustafaMulla29/usb-pd-motherboard  # download the package files into ./usb-pd-motherboard
tsci add MustafaMulla29/usb-pd-motherboard    # add it to your project as @tsci/MustafaMulla29.usb-pd-motherboard

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.

enclosure/render-server.ts

// Local helper for the enclosure renders and the explainer video. Serves the project folder and saves what the
// pages post.   bun enclosure/render-server.ts   then open
//   http://localhost:4317/                                  enclosure views (enclosure/viewer.html)
//   http://localhost:4317/enclosure/example/system-video.html   example system + explainer video
// POST /save?name=01-front        (PNG bytes)  ->  enclosure/renders/01-front.png
// POST /save-video?name=explainer (MP4 bytes)  ->  enclosure/renders/explainer.mp4
//      with &pos=N the bytes are written at offset N (streamed video), &reset=1 empties the file first
import { closeSync, mkdirSync, openSync, writeFileSync, writeSync } from "node:fs"
import { join, normalize } from "node:path"

const project = join(import.meta.dir, "..")
const out = join(import.meta.dir, "renders")
mkdirSync(out, { recursive: true })
const clean = (s: string | null, d: string) => (s ?? d).replace(/[^a-z0-9_-]/gi, "")

Bun.serve({
  port: 4317,
  async fetch(req) {
    const url = new URL(req.url)
    if (req.method === "POST" && url.pathname === "/save") {
      writeFileSync(join(out, `${clean(url.searchParams.get("name"), "render")}.png`), new Uint8Array(await req.arrayBuffer()))
      return new Response("ok")
    }
    if (req.method === "POST" && url.pathname === "/save-video") {
      const file = join(out, `${clean(url.searchParams.get("name"), "video")}.mp4`)
      const bytes = new Uint8Array(await req.arrayBuffer())
      const pos = url.searchParams.get("pos")
      if (url.searchParams.has("reset") || pos === null) writeFileSync(file, pos === null ? bytes : new Uint8Array(0))
      if (pos !== null && bytes.length) {
        const fd = openSync(file, "r+")
        writeSync(fd, bytes, 0, bytes.length, Number(pos))
        closeSync(fd)
      }
      return new Response("ok")
    }
    if (url.pathname === "/") return Response.redirect("/enclosure/viewer.html", 302)
    const path = decodeURIComponent(url.pathname)
    const file = join(project, normalize(path).replace(/^(\.\.[/\\])+/, ""))
    if (!file.startsWith(project)) return new Response("forbidden", { status: 403 })
    const f = Bun.file(file)
    return (await f.exists()) ? new Response(f, { headers: { "Cache-Control": "no-store" } }) : new Response("not found", { status: 404 })
  },
})
console.log("render server on http://localhost:4317")