-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,78 @@ | ||
import { NormalComponent } from "lib/components/base-components/NormalComponent" | ||
import { chipProps } from "@tscircuit/props" | ||
import { Port } from "../primitive-components/Port" | ||
import type { BaseSymbolName } from "lib/utils/constants" | ||
|
||
export class Chip extends NormalComponent<typeof chipProps> {} | ||
export class Chip extends NormalComponent<typeof chipProps> { | ||
get config() { | ||
return { | ||
zodProps: chipProps, | ||
} | ||
} | ||
|
||
initPorts() { | ||
super.initPorts() | ||
|
||
const { _parsedProps: props } = this | ||
|
||
if (props.pinLabels) { | ||
for (const [pinNumber, label] of Object.entries(props.pinLabels)) { | ||
console.log(this.children) | ||
const port = this.selectOne(`port[pinNumber='${pinNumber}']`) | ||
const ports = this.selectAll("port") | ||
if (!port) { | ||
throw new Error( | ||
`Could not find port for pin number ${pinNumber} in chip ${this.getString()}`, | ||
) | ||
} | ||
port.props.aliases.push(port.props.name) | ||
port.props.name = label | ||
} | ||
} | ||
} | ||
|
||
doInitialSourceRender(): void { | ||
const { db } = this.project! | ||
const { _parsedProps: props } = this | ||
|
||
const source_component = db.source_component.insert({ | ||
ftype: "simple_chip", | ||
name: props.name, | ||
manufacturer_part_number: props.manufacturerPartNumber, | ||
supplier_part_numbers: props.supplierPartNumbers, | ||
}) | ||
|
||
this.source_component_id = source_component.source_component_id! | ||
} | ||
|
||
doInitialSchematicComponentRender() { | ||
const { db } = this.project! | ||
const { _parsedProps: props } = this | ||
|
||
const schematic_component = db.schematic_component.insert({ | ||
center: { x: props.schX ?? 0, y: props.schY ?? 0 }, | ||
rotation: props.schRotation ?? 0, | ||
size: { width: 3, height: 4 }, // Default size, adjust as needed | ||
source_component_id: this.source_component_id!, | ||
symbol_name: "chip", | ||
}) | ||
|
||
this.schematic_component_id = schematic_component.schematic_component_id | ||
} | ||
|
||
doInitialPcbComponentRender() { | ||
const { db } = this.project! | ||
const { _parsedProps: props } = this | ||
|
||
const pcb_component = db.pcb_component.insert({ | ||
center: { x: props.pcbX ?? 0, y: props.pcbY ?? 0 }, | ||
width: 2, // Default width, adjust as needed | ||
height: 3, // Default height, adjust as needed | ||
layer: props.layer ?? "top", | ||
rotation: props.rotation ?? 0, | ||
source_component_id: this.source_component_id!, | ||
}) | ||
|
||
this.pcb_component_id = pcb_component.pcb_component_id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { it, expect } from "bun:test" | ||
import { Project } from "lib/Project" | ||
import { Chip } from "lib/components/normal-components/Chip" | ||
import "lib/register-catalogue" | ||
|
||
it("should create a Chip component with correct properties", () => { | ||
const project = new Project() | ||
|
||
project.add( | ||
<board width="10mm" height="10mm"> | ||
<chip | ||
name="U1" | ||
footprint="soic8" | ||
pinLabels={{ | ||
"1": "VCC", | ||
}} | ||
schPortArrangement={{ | ||
leftSize: 4, | ||
rightSize: 4, | ||
}} | ||
/> | ||
</board>, | ||
) | ||
|
||
project.render() | ||
|
||
const chip = project.selectOne("chip") as Chip | ||
|
||
expect(chip).not.toBeNull() | ||
expect(chip.props.name).toBe("U1") | ||
|
||
// Check if ports are created correctly | ||
const ports = chip.children.filter((c) => c.componentName === "Port") | ||
expect(ports).toHaveLength(8) | ||
|
||
// Check specific ports | ||
const vccPort = chip.selectOne("port[name='VCC']") | ||
expect(vccPort).not.toBeNull() | ||
expect(vccPort!.props.pinNumber).toBe(1) | ||
|
||
const gndPort = chip.selectOne("port[name='GND']") | ||
expect(gndPort).not.toBeNull() | ||
expect(gndPort!.props.pinNumber).toBe(2) | ||
|
||
// Test schematic rendering | ||
expect(chip.schematic_component_id).not.toBeNull() | ||
|
||
// Test PCB rendering | ||
expect(chip.pcb_component_id).not.toBeNull() | ||
}) |