mohan-bee/flight-controller-v2

A 64 mm STM32F405 flight-controller PCB with ICM-42688 IMU, SPI flash, USB-C/ESD, battery buck and 3.3 V regulation, four motor outputs, service I/O, and PETG standoff mounting hardware.

Version
0.1.0
License
ISC
Stars
0

routing/reviewed-routing.ts

import { EventEmitter } from "node:events";
import type { SimpleRouteJson } from "@tscircuit/capacity-autorouter";
import { pcb_trace } from "circuit-json";
import savedTraces from "./reviewed-traces.json";
import savedPositions from "./reviewed-port-positions.json";

// Preserve the native tscircuit route after the reviewed local copper edits.
// Placement changes require regenerating and revalidating the saved geometry.
export async function reviewedRouting(input: SimpleRouteJson) {
  const positions = new Map(Object.entries(savedPositions));
  for (const connection of input.connections) {
    for (const point of connection.pointsToConnect) {
      if (!point.pcb_port_id) continue;
      const expected = positions.get(point.pcb_port_id);
      if (!expected || Math.hypot(expected.x - point.x, expected.y - point.y) > 1e-6)
        throw new Error(`Reviewed routing is stale at ${point.pcb_port_id}`);
    }
  }
  const traces = [
    ...savedTraces.map((trace) => pcb_trace.parse(trace)),
    ...(input.traces ?? []).map((trace) => pcb_trace.parse(trace)),
  ];
  const router = new EventEmitter();
  return Object.assign(router, {
    start() {
      queueMicrotask(() => router.emit("complete", { traces }));
    },
    stop() {
      router.removeAllListeners();
    },
  });
}