// SAME Analog Modular Ecosystem
//
// / SPDX-FileCopyrightText: 2026 Denis Chevalier <perso@denischevalier.fr>
// / SPDX-License-Identifier: CC-BY-SA-4.0
// / SPDX-License-Identifier: CERN-OHL-S-2.0+
// / SPDX-License-Identifier: GPL-3.0-or-later
//
// The prose, explanatory text, rendered figures, tables, and mathematical
// content of this specification are licensed under CC BY-SA 4.0. If a later
// version of CC BY-SA is published, the author grants permission to distribute
// this work under that later version as well.
//
// Hardware designs contained herein (schematics, PCB layouts, mechanical
// drawings, and CAD models) are licensed under CERN-OHL-S-2.0+.
//
// All executable code, helper libraries (`lib/*`), metrology assertions, and
// embedded verification scripts throughout the source documents are licensed
// under the GNU General Public License v3.0 or later (GPL-3.0-or-later).

#import "../lib/unify.typ": qty
#import "@preview/cetz:0.5.2"

#figure(
  cetz.canvas({
    import cetz.draw: *

    let dx = 1.0
    let top-y = 0.55
    let bot-y = -0.55

    // Outer mounting flange / metal ears
    rect(
      (-2.0, -1.4),
      (14.0, 1.4),
      rx: 0.2,
      fill: luma(245),
      stroke: 1pt + luma(100),
    )

    // Left and right chassis mounting screw holes
    circle((-1.3, 0), radius: 0.35, fill: luma(220), stroke: 1pt + luma(120))
    circle((-1.3, 0), radius: 0.20, fill: white, stroke: 0.8pt + luma(100))
    circle((13.3, 0), radius: 0.35, fill: luma(220), stroke: 1pt + luma(120))
    circle((13.3, 0), radius: 0.20, fill: white, stroke: 0.8pt + luma(100))

    // D-Sub trapezoidal shroud outline
    line(
      (-0.7, 1.15),
      (12.7, 1.15),
      (12.2, -1.15),
      (-0.2, -1.15),
      close: true,
      fill: luma(230),
      stroke: 1.5pt + black,
    )

    // Shell Earth ground connection reference
    line((-0.45, -1.15), (-0.45, -2.0), stroke: (
      paint: luma(120),
      thickness: 0.7pt,
      dash: "dashed",
    ))
    content(
      (-0.45, -2.05),
      text(size: 7.5pt, weight: "bold")[Shell: Chassis Earth],
      angle: -50deg,
      anchor: "north-west",
    )

    // Top row pins (Pins 1 to 13)
    let top-pins = (
      ("1", [$plus qty(15.00, "V")$]),
      ("2", [$plus qty(15.00, "V")$]),
      ("3", [$qty(-15.00, "V")$]),
      ("4", [$qty(-15.00, "V")$]),
      ("5", [AGND]),
      ("6", [AGND]),
      ("7", [AGND]),
      ("8", [$plus qty(10.0000, "V")$ Force]),
      ("9", [$plus qty(10.0000, "V")$ Sense]),
      ("10", [$qty(-10.0000, "V")$ Force]),
      ("11", [$qty(-10.0000, "V")$ Sense]),
      ("12", [AGND]),
      ("13", [AGND]),
    )

    // Bottom row pins (Pins 14 to 25)
    let bot-pins = (
      ("14", [AGND]),
      ("15", [AGND]),
      ("16", [AGND]),
      ("17", [DGND]),
      ("18", [DGND]),
      ("19", [$plus qty(5.0, "VD")$]),
      ("20", [$qty(-5.0, "VD")$]),
      ("21", [Stagger]),
      ("22", [_Reserved_]),
      ("23", [_Reserved_]),
      ("24", [_Reserved_]),
      ("25", [Chassis Earth]),
    )

    // Draw Top Pins (1-13)
    for (i, p) in top-pins.enumerate() {
      let x = i * dx
      let p-num = p.at(0)
      let p-sig = p.at(1)

      circle((x, top-y), radius: 0.26, fill: white, stroke: 1pt + black)
      content((x, top-y), text(size: 7pt, weight: "bold")[#p-num])

      line((x, top-y + 0.26), (x, 1.8), stroke: (
        paint: luma(120),
        thickness: 0.6pt,
      ))
      content(
        (x, 1.85),
        text(size: 7.5pt)[#p-sig],
        angle: 50deg,
        anchor: "south-west",
      )
    }

    // Draw Bottom Pins (14-25)
    for (j, p) in bot-pins.enumerate() {
      let x = (j + 0.5) * dx
      let p-num = p.at(0)
      let p-sig = p.at(1)

      circle((x, bot-y), radius: 0.26, fill: white, stroke: 1pt + black)
      content((x, bot-y), text(size: 7pt, weight: "bold")[#p-num])

      line((x, bot-y - 0.26), (x, -1.8), stroke: (
        paint: luma(120),
        thickness: 0.6pt,
      ))
      content(
        (x, -1.85),
        text(size: 7.5pt)[#p-sig],
        angle: -50deg,
        anchor: "north-west",
      )
    }
  }),
  caption: [DB-25 backplane connector physical pinout and signal allocation diagram.],
) <figure-backplane-connector-diagram>
