Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor all footprinter functions by updating its scheme, all footprints now have default value of num_pins #83

Merged
merged 12 commits into from
Nov 20, 2024
2 changes: 1 addition & 1 deletion src/fn/axial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { silkscreenRef, type SilkscreenRef } from "../helpers/silkscreenRef"

export const axial_def = z.object({
fn: z.string(),
num_pins: z.any().transform(() => 2),
num_pins: z.literal(2).default(2),
AnasSarkiz marked this conversation as resolved.
Show resolved Hide resolved
p: length.optional().default("2.54mm"),
id: length.optional().default("0.7mm"),
od: length.optional().default("1mm"),
Expand Down
12 changes: 1 addition & 11 deletions src/fn/bga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { type SilkscreenRef, silkscreenRef } from "src/helpers/silkscreenRef"
export const bga_def = z
.object({
fn: z.string(),
num_pins: z.number(),
num_pins: z.number().optional().default(64),
grid: dim2d.optional(),
p: distance.default("0.8mm"),
w: length.optional(),
Expand Down Expand Up @@ -106,16 +106,6 @@ export const bga = (
missing_pin_nums.push(1)
}

if (num_pins_missing !== missing_pin_nums.length) {
throw new Error(
`not able to create bga component, unable to determine missing pins (try specifying them with "missing+1+2+..."\n\n${JSON.stringify(
parameters,
null,
" ",
)}`,
)
}

const missing_pin_nums_set = new Set(missing_pin_nums)

let missing_pins_passed = 0
Expand Down
2 changes: 1 addition & 1 deletion src/fn/cap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { type PassiveDef, passive } from "../helpers/passive-fn"

export const cap = (
parameters: PassiveDef,
): { circuitJson: AnySoupElement[]; parameters: any } => {
): { circuitJson: AnySoupElement[]; parameters: PassiveDef } => {
return { circuitJson: passive(parameters), parameters }
}
4 changes: 2 additions & 2 deletions src/fn/diode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { AnySoupElement } from "@tscircuit/soup"
import { passive } from "src/helpers/passive-fn"
import { passive, type PassiveDef } from "src/helpers/passive-fn"

export const diode = (parameters: {
tht: boolean
p: number
}): { circuitJson: AnySoupElement[]; parameters: any } => {
}): { circuitJson: AnySoupElement[]; parameters: PassiveDef } => {
return { circuitJson: passive(parameters), parameters }
}
2 changes: 1 addition & 1 deletion src/fn/dip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const extendDipDef = (newDefaults: { w?: string; p?: string }) =>
.object({
fn: z.string(),
dip: z.literal(true),
num_pins: z.number(),
num_pins: z.number().optional().default(6),
wide: z.boolean().optional(),
narrow: z.boolean().optional(),
w: length.optional(),
Expand Down
2 changes: 1 addition & 1 deletion src/fn/led.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { type PassiveDef, passive } from "../helpers/passive-fn"

export const led = (
parameters: PassiveDef,
): { circuitJson: AnySoupElement[]; parameters: any } => {
): { circuitJson: AnySoupElement[]; parameters: PassiveDef } => {
return { circuitJson: passive(parameters), parameters }
}
4 changes: 2 additions & 2 deletions src/fn/pinrow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { z } from "zod"
import { number, z } from "zod"
import { length, type AnySoupElement } from "@tscircuit/soup"
AnasSarkiz marked this conversation as resolved.
Show resolved Hide resolved
import { platedhole } from "../helpers/platedhole"
import { silkscreenRef, type SilkscreenRef } from "src/helpers/silkscreenRef"

export const pinrow_def = z.object({
fn: z.string(),
num_pins: z.number(),
num_pins: number().optional().default(6),
p: length.default("0.1in").describe("pitch"),
id: length.default("1.0mm").describe("inner diameter"),
od: length.default("1.5mm").describe("outer diameter"),
Expand Down
10 changes: 7 additions & 3 deletions src/fn/pushbutton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import { silkscreenRef, type SilkscreenRef } from "src/helpers/silkscreenRef"

export const pushbutton_def = z.object({
fn: z.literal("pushbutton"),
w: z.literal(4.5).default(4.5),
h: z.literal(6.5).default(6.5),
id: z.literal(1).default(1),
od: z.literal(1.2).default(1.2),
})

export const pushbutton = (
raw_params: z.input<typeof pushbutton_def>,
): { circuitJson: AnyCircuitElement[]; parameters: any } => {
const parameters = pushbutton_def.parse(raw_params)

const width = 4.5
const height = 6.5
const holeDiameter = 1
const width = parameters.w
const height = parameters.h
const holeDiameter = parameters.id

const holes: AnyCircuitElement[] = [
platedhole(1, -width / 2, height / 2, holeDiameter, holeDiameter * 1.2),
Expand Down
4 changes: 2 additions & 2 deletions src/fn/quad.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnySoupElement, PcbSilkscreenPath } from "@tscircuit/soup"
import { z } from "zod"
import { optional, z } from "zod"
import { length } from "@tscircuit/soup"
import type { NowDefined } from "../helpers/zod/now-defined"
import { rectpad } from "../helpers/rectpad"
Expand All @@ -18,7 +18,7 @@ export const base_quad_def = z.object({
.transform((a) => (typeof a === "string" ? a.slice(1, -1).split(",") : a))
.pipe(z.array(pin_order_specifier))
.optional(),
num_pins: z.number(),
num_pins: z.number().optional().default(64),
w: length.optional(),
h: length.optional(),
p: length.default(length.parse("0.5mm")),
Expand Down
9 changes: 5 additions & 4 deletions src/fn/sod123.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { length } from "circuit-json"

export const sod_def = z.object({
fn: z.string(),
num_pins: z.literal(3).default(3),
w: z.string().default("2.36mm"),
h: z.string().default("1.22mm"),
pl: z.string().default("0.9mm"),
Expand All @@ -19,7 +20,7 @@ export const sod123 = (
const parameters = sod_def.parse(raw_params)
const silkscreenRefText: SilkscreenRef = silkscreenRef(
0,
Number(parameters.h) / 4 + 0.4,
length.parse(parameters.h) / 4 + 0.4,
0.3,
)

Expand Down Expand Up @@ -48,14 +49,14 @@ export const getSodCoords = (parameters: {
export const sodWithoutParsing = (parameters: z.infer<typeof sod_def>) => {
const pads: AnySoupElement[] = []

for (let i = 0; i < 2; i++) {
for (let i = 1; i <= parameters.num_pins; i++) {
const { x, y } = getSodCoords({
pn: i + 1,
pn: i,
pad_spacing: Number.parseFloat(parameters.pad_spacing),
})
pads.push(
rectpad(
i + 1,
i,
x,
y,
Number.parseFloat(parameters.pl),
Expand Down
5 changes: 3 additions & 2 deletions src/fn/soic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AnySoupElement, PcbSilkscreenPath } from "@tscircuit/soup"
import { platedhole } from "../helpers/platedhole"
import { z, type AnyZodObject } from "zod"
import { z } from "zod"
import { length } from "@tscircuit/soup"
import type { NowDefined } from "../helpers/zod/now-defined"
import { u_curve } from "../helpers/u-curve"
Expand All @@ -9,12 +9,13 @@ import { silkscreenRef, type SilkscreenRef } from "../helpers/silkscreenRef"
export const extendSoicDef = (newDefaults: {
w?: string
p?: string
num_pins?: number
legsoutside?: boolean
}) =>
z
.object({
fn: z.string(),
num_pins: z.number(),
num_pins: z.number().optional().default(8),
w: length.default(length.parse(newDefaults.w ?? "5.3mm")),
p: length.default(length.parse(newDefaults.p ?? "1.27mm")),
pw: length.optional(),
Expand Down
3 changes: 2 additions & 1 deletion src/fn/sot23.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { silkscreenRef, type SilkscreenRef } from "src/helpers/silkscreenRef"

export const sot23_def = z.object({
fn: z.string(),
num_pins: z.number().default(3),
num_pins: z.literal(3).default(3),
w: z.string().default("1.92mm"),
h: z.string().default("2.74mm"),
pl: z.string().default("0.8mm"),
Expand Down Expand Up @@ -61,6 +61,7 @@ export const sot23WithoutParsing = (parameters: z.infer<typeof sot23_def>) => {
),
)
}

const silkscreenRefText: SilkscreenRef = silkscreenRef(
0,
Number(parameters.h),
Expand Down
9 changes: 5 additions & 4 deletions src/fn/sot235.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import type { AnyCircuitElement, PcbSilkscreenPath } from "circuit-json"
import { z } from "zod"
import { rectpad } from "../helpers/rectpad"
import { silkscreenRef, type SilkscreenRef } from "src/helpers/silkscreenRef"
import { u_curve } from "src/helpers/u-curve"

export const sot235_def = z.object({
fn: z.string(),
num_pins: z.literal(5).default(5),
h: z.string().default("1.6mm"),
pl: z.string().default("1mm"),
pw: z.string().default("0.7mm"),
p: z.string().default("0.95mm"),
})
const num_pins = 5

export const sot235 = (
raw_params: z.input<typeof sot235_def>,
): { circuitJson: AnyCircuitElement[]; parameters: any } => {
Expand Down Expand Up @@ -50,7 +50,7 @@ export const sot23_5WithoutParsing = (
parameters: z.infer<typeof sot235_def>,
) => {
const pads: AnyCircuitElement[] = []
for (let i = 1; i <= num_pins; i++) {
for (let i = 1; i <= parameters.num_pins; i++) {
const { x, y } = getCcwSot235Coords({
h: Number.parseFloat(parameters.h),
p: Number.parseFloat(parameters.p),
Expand All @@ -67,7 +67,8 @@ export const sot23_5WithoutParsing = (
)
}

const width = ((num_pins + 1) / 2) * Number.parseFloat(parameters.p)
const width =
((parameters.num_pins + 1) / 2) * Number.parseFloat(parameters.p)
const height = Number.parseFloat(parameters.h)
const silkscreenPath1: PcbSilkscreenPath = {
layer: "top",
Expand Down
10 changes: 5 additions & 5 deletions src/fn/sot723.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { AnySoupElement } from "@tscircuit/soup"
import { length, type AnySoupElement } from "@tscircuit/soup"
import { z } from "zod"
import { rectpad } from "../helpers/rectpad"
import { silkscreenRef, type SilkscreenRef } from "src/helpers/silkscreenRef"

export const sot723_def = z.object({
fn: z.string(),
num_pins: z.number().default(3),
num_pins: z.literal(3).default(3),
w: z.string().default("1.2mm"),
h: z.string().default("1.2mm"),
pl: z.string().default("0.3mm"),
Expand All @@ -18,9 +18,9 @@ export const sot723 = (
const parameters = sot723_def.parse(raw_params)
const pad = sot723WithoutParsing(parameters)
const silkscreenRefText: SilkscreenRef = silkscreenRef(
0,
Number(parameters.h),
0.3,
0.4,
length.parse(parameters.h),
0.2,
)
return {
circuitJson: [...pad, silkscreenRefText as AnySoupElement],
Expand Down
4 changes: 2 additions & 2 deletions src/fn/stampboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { silkscreenRef, type SilkscreenRef } from "src/helpers/silkscreenRef"
export const stampboard_def = z.object({
fn: z.string(),
w: length.default("22.58mm"),
left: length.optional(),
right: length.optional(),
left: length.optional().default(20),
right: length.optional().default(20),
top: length.optional(),
bottom: length.optional(),
p: length.default(length.parse("2.54mm")),
Expand Down
4 changes: 2 additions & 2 deletions src/fn/stampreceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { platedhole } from "src/helpers/platedhole"
export const stampreceiver_def = z.object({
fn: z.string(),
w: length.default("22.58mm"),
left: length.optional(),
right: length.optional(),
left: length.optional().default(20),
right: length.optional().default(20),
top: length.optional(),
bottom: length.optional(),
p: length.default(length.parse("2.54mm")),
Expand Down
30 changes: 16 additions & 14 deletions src/footprinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,36 @@ type CommonPassiveOptionKey =

export type Footprinter = {
dip: (
num_pins: number,
num_pins?: number,
) => FootprinterParamsBuilder<"w" | "p" | "id" | "od" | "wide" | "narrow">
cap: () => FootprinterParamsBuilder<CommonPassiveOptionKey>
res: () => FootprinterParamsBuilder<CommonPassiveOptionKey>
diode: () => FootprinterParamsBuilder<CommonPassiveOptionKey>
led: () => FootprinterParamsBuilder<CommonPassiveOptionKey>
lr: (num_pins: number) => FootprinterParamsBuilder<"w" | "l" | "pl" | "pr">
lr: (num_pins?: number) => FootprinterParamsBuilder<"w" | "l" | "pl" | "pr">
qfp: (
num_pins: number,
num_pins?: number,
) => FootprinterParamsBuilder<"w" | "p" | "id" | "od" | "wide" | "narrow">
quad: (
num_pins: number,
num_pins?: number,
) => FootprinterParamsBuilder<
"w" | "l" | "square" | "pl" | "pr" | "pb" | "pt" | "p" | "pw" | "ph"
>
bga: (
num_pins: number,
num_pins?: number,
) => FootprinterParamsBuilder<
"grid" | "p" | "w" | "h" | "ball" | "pad" | "missing"
>
qfn: (num_pins: number) => FootprinterParamsBuilder<"w" | "h" | "p">
soic: (num_pins: number) => FootprinterParamsBuilder<"w" | "p" | "id" | "od">
mlp: (num_pins: number) => FootprinterParamsBuilder<"w" | "h" | "p">
ssop: (num_pins: number) => FootprinterParamsBuilder<"w" | "p">
tssop: (num_pins: number) => FootprinterParamsBuilder<"w" | "p">
dfn: (num_pins: number) => FootprinterParamsBuilder<"w" | "p">
pinrow: (num_pins: number) => FootprinterParamsBuilder<"p" | "id" | "od">
qfn: (num_pins?: number) => FootprinterParamsBuilder<"w" | "h" | "p">
soic: (num_pins?: number) => FootprinterParamsBuilder<"w" | "p" | "id" | "od">
mlp: (num_pins?: number) => FootprinterParamsBuilder<"w" | "h" | "p">
ssop: (num_pins?: number) => FootprinterParamsBuilder<"w" | "p">
tssop: (num_pins?: number) => FootprinterParamsBuilder<"w" | "p">
dfn: (num_pins?: number) => FootprinterParamsBuilder<"w" | "p">
pinrow: (num_pins?: number) => FootprinterParamsBuilder<"p" | "id" | "od">
axial: () => FootprinterParamsBuilder<"p" | "id" | "od">
sot235: () => FootprinterParamsBuilder<"h" | "p" | "pl" | "pw">
lqfp: (num_pins: number) => FootprinterParamsBuilder<"w" | "h" | "pl" | "pw">
lqfp: (num_pins?: number) => FootprinterParamsBuilder<"w" | "h" | "pl" | "pw">
pushbutton: () => FootprinterParamsBuilder<
"tllabel" | "trlabel" | "bllabel" | "brlabel"
>
Expand Down Expand Up @@ -183,7 +183,9 @@ export const footprinter = (): Footprinter & {
target.imperial = v // res0402, cap0603 etc.
}
} else {
target.num_pins = Number.parseFloat(v)
target.num_pins = Number.isNaN(Number.parseFloat(v))
? undefined
: Number.parseFloat(v)
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/passive-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const passive = (params: PassiveDef): AnySoupElement[] => {
if (pw === undefined) throw new Error("could not infer pad width")
if (ph === undefined) throw new Error("could not infer pad width")

const silkscreenRefText: SilkscreenRef = silkscreenRef(0, h / 2, h / 12)
const silkscreenRefText: SilkscreenRef = silkscreenRef(0, 0.9, 0.2)
if (tht) {
return [
platedhole(1, -p / 2, 0, pw, (pw * 1) / 0.8),
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/0402.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/__snapshots__/cap footprint.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/__snapshots__/cap_imperial0402.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading