Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions src/fn/pinrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { silkscreenRef, type SilkscreenRef } from "src/helpers/silkscreenRef"
import { silkscreenPin } from "src/helpers/silkscreenPin"
import { mm } from "@tscircuit/mm"
import { determinePinlabelAnchorSide } from "src/helpers/determine-pin-label-anchor-side"
import { pin_order_specifier } from "src/helpers/zod/pin-order-specifier"
import { getPinrowStartIndex } from "src/helpers/get-pinrow-start-index"

export const pinrow_def = z
.object({
Expand All @@ -22,6 +24,12 @@ export const pinrow_def = z
od: length.default("1.5mm").describe("outer diameter"),
male: z.boolean().optional().describe("for male pin headers"),
female: z.boolean().optional().describe("for female pin headers"),
startingpin: z
.string()
.or(z.array(pin_order_specifier))
.transform((v) => (typeof v === "string" ? v.slice(1, -1).split(",") : v))
.pipe(z.array(pin_order_specifier))
.optional(),
pinlabeltextalignleft: z.boolean().optional().default(false),
pinlabeltextaligncenter: z.boolean().optional().default(false),
pinlabeltextalignright: z.boolean().optional().default(false),
Expand Down Expand Up @@ -80,6 +88,7 @@ export const pinrow = (
od,
rows,
num_pins,
startingpin,
pinlabelAnchorSide,
pinlabelverticallyinverted,
pinlabelorthogonal,
Expand All @@ -97,6 +106,9 @@ export const pinrow = (
const numPinsPerRow = Math.ceil(num_pins / rows)
const ySpacing = -p

const positions: Array<{ row: number; col: number; x: number; y: number }> =
[]

const calculateAnchorPosition = ({
xoff,
yoff,
Expand Down Expand Up @@ -198,28 +210,30 @@ export const pinrow = (
const useBGAStyle = rows > 2 && numPinsPerRow > 2

if (rows === 1) {
// Single row: left to right, pin 1 to num_pins
// Single row: left to right
const xStart = -((num_pins - 1) / 2) * p
for (let i = 0; i < num_pins; i++) {
const pinNumber = i + 1
const xoff = xStart + i * p
const posKey = `${xoff},${0}`
if (usedPositions.has(posKey)) throw new Error(`Overlap at ${posKey}`)
usedPositions.add(posKey)
addPin(pinNumber, xoff, 0)
positions.push({ row: 0, col: i, x: xoff, y: 0 })
}
} else if (useBGAStyle) {
// BGA-style: row-major numbering (left to right, top to bottom)
const xStart = -((numPinsPerRow - 1) / 2) * p
let currentPin = 1
for (let row = 0; row < rows && currentPin <= num_pins; row++) {
for (let col = 0; col < numPinsPerRow && currentPin <= num_pins; col++) {
for (let row = 0; row < rows && positions.length < num_pins; row++) {
for (
let col = 0;
col < numPinsPerRow && positions.length < num_pins;
col++
) {
const xoff = xStart + col * p
const yoff = row * ySpacing
const posKey = `${xoff},${yoff}`
if (usedPositions.has(posKey)) throw new Error(`Overlap at ${posKey}`)
usedPositions.add(posKey)
addPin(currentPin++, xoff, yoff)
positions.push({ row, col, x: xoff, y: yoff })
}
}
} else {
Expand All @@ -239,7 +253,8 @@ export const pinrow = (
const posKey = `${xoff},${yoff}`
if (usedPositions.has(posKey)) throw new Error(`Overlap at ${posKey}`)
usedPositions.add(posKey)
addPin(currentPin++, xoff, yoff)
positions.push({ row, col: left, x: xoff, y: yoff })
currentPin++
}
left++

Expand All @@ -250,7 +265,8 @@ export const pinrow = (
const posKey = `${xoff},${yoff}`
if (usedPositions.has(posKey)) throw new Error(`Overlap at ${posKey}`)
usedPositions.add(posKey)
addPin(currentPin++, xoff, yoff)
positions.push({ row: bottom, col, x: xoff, y: yoff })
currentPin++
}
bottom--

Expand All @@ -262,7 +278,8 @@ export const pinrow = (
const posKey = `${xoff},${yoff}`
if (usedPositions.has(posKey)) throw new Error(`Overlap at ${posKey}`)
usedPositions.add(posKey)
addPin(currentPin++, xoff, yoff)
positions.push({ row, col: right, x: xoff, y: yoff })
currentPin++
}
right--
}
Expand All @@ -275,20 +292,35 @@ export const pinrow = (
const posKey = `${xoff},${yoff}`
if (usedPositions.has(posKey)) throw new Error(`Overlap at ${posKey}`)
usedPositions.add(posKey)
addPin(currentPin++, xoff, yoff)
positions.push({ row: top, col, x: xoff, y: yoff })
currentPin++
}
top++
}
}

// Verify all pins were assigned

if (currentPin - 1 < num_pins) {
throw new Error(
`Missing pins: assigned ${currentPin - 1}, expected ${num_pins}`,
)
}
}

const startIndex = getPinrowStartIndex({
positions: positions.map((p) => ({ row: p.row, col: p.col })),
rows,
numPinsPerRow,
startingpin,
})

for (let i = 0; i < positions.length; i++) {
const pinNumber = ((i - startIndex + num_pins) % num_pins) + 1
const { x, y } = positions[i]
addPin(pinNumber, x, y)
}

// Add centered silkscreen reference text
const refText: SilkscreenRef = silkscreenRef(0, p, 0.5)

Expand Down
57 changes: 57 additions & 0 deletions src/helpers/get-pinrow-start-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { PinOrderSpecifier } from "./zod/pin-order-specifier"

export function getPinrowStartIndex({
positions,
rows,
numPinsPerRow,
startingpin,
}: {
positions: Array<{ row: number; col: number }>
rows: number
numPinsPerRow: number
startingpin?: PinOrderSpecifier[]
}): number {
const sfp: Record<PinOrderSpecifier, boolean> = {} as any
for (const specifier of startingpin ?? []) {
sfp[specifier] = true
}

if (!sfp.leftside && !sfp.topside && !sfp.rightside && !sfp.bottomside) {
sfp.leftside = true
}

if (!sfp.bottompin && !sfp.leftpin && !sfp.rightpin && !sfp.toppin) {
if (sfp.leftside) {
sfp.toppin = true
} else if (sfp.topside) {
sfp.rightpin = true
} else if (sfp.rightside) {
sfp.bottompin = true
} else if (sfp.bottomside) {
sfp.leftpin = true
}
}

let targetRow: number | undefined
let targetCol: number | undefined

if (sfp.toppin) targetRow = 0
else if (sfp.bottompin) targetRow = rows - 1

if (sfp.leftpin) targetCol = 0
else if (sfp.rightpin) targetCol = numPinsPerRow - 1

if (targetRow === undefined) {
if (sfp.topside) targetRow = 0
else if (sfp.bottomside) targetRow = rows - 1
}
if (targetCol === undefined) {
if (sfp.leftside) targetCol = 0
else if (sfp.rightside) targetCol = numPinsPerRow - 1
}

const idx = positions.findIndex(
(p) => p.row === targetRow && p.col === targetCol,
)
return idx === -1 ? 0 : idx
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading