gokul/cm4-media-carrier

A tscircuit package created by gokul.

Version
0.1.0
License
unset
Stars
0

high-speed-paths.ts

type Point = { x: number; y: number }

/** Offset each straight segment and intersect neighboring offset lines. */
function offsetPath(points: Point[], distance: number): Point[] {
  const normals = points.slice(1).map((point, index) => {
    const previous = points[index]!
    const length = Math.hypot(point.x - previous.x, point.y - previous.y)
    return { x: -(point.y - previous.y) / length, y: (point.x - previous.x) / length }
  })
  return points.map((point, index) => {
    const before = normals[Math.max(0, index - 1)]!
    const after = normals[Math.min(normals.length - 1, index)]!
    const scale = distance / (1 + before.x * after.x + before.y * after.y)
    return { x: point.x + (before.x + after.x) * scale, y: point.y + (before.y + after.y) * scale }
  })
}

export function hdmiPath(pairIndex: number, positive: boolean): Point[] {
  const sourceCenterY = 1.5 - 1.2 * pairIndex
  const targetCenterY = 3 - 1.5 * pairIndex
  const sign = positive ? 1 : -1
  const centerline = [
    { x: 18.5, y: sourceCenterY },
    { x: 20, y: sourceCenterY },
    { x: 20 + targetCenterY - sourceCenterY, y: targetCenterY },
    { x: 22.5, y: targetCenterY },
  ]
  return [
    { x: 18.5, y: sourceCenterY + sign * 0.2 },
    ...offsetPath(centerline, sign * (0.1041 + 0.1501) / 2),
    { x: 23, y: targetCenterY + sign * 0.5 },
    { x: 24.27, y: targetCenterY + sign * 0.5 },
  ]
}

export function usbPath(positive: boolean): Point[] {
  // Both terminal pairs face left in the same P/N order.
  const sign = positive ? 1 : -1
  const centerline = [
    { x: 15.42, y: 14.7 },
    { x: 14, y: 14.7 },
    { x: 8.375, y: 20.325 },
    { x: -28.89, y: 20.325 },
  ]
  return [
    { x: 15.42, y: 14.7 - sign * 0.2 },
    ...offsetPath(centerline, sign * (0.1356 + 0.1501) / 2),
    { x: -29.49, y: 20.325 - sign * 0.325 },
    { x: -30.17, y: 20.325 - sign * 0.325 },
  ]
}

export const highSpeedTracePaths = [
  ...["HDMI_D2", "HDMI_D1", "HDMI_D0", "HDMI_CLK"].flatMap((name, index) =>
    [true, false].map(positive => ({
      connection: `.J_CM4B > .pin${[70,76,82,88][index]! + (positive ? 0 : 2)}`,
      route: hdmiPath(index, positive).map(point => ({
        ...point, route_type: "wire" as const, layer: "top" as const, width: 0.1041,
      })),
    }))),
  ...[true, false].map(positive => ({
    connection: `.J_CM4B > .pin${positive ? 5 : 3}`,
    route: usbPath(positive).map(point => ({
      ...point, route_type: "wire" as const, layer: "top" as const, width: 0.1356,
    })),
  })),
]