Skip to content
Merged
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
50 changes: 50 additions & 0 deletions lib/sexpr/classes/Symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,39 @@ export class SymbolDuplicatePinNumbersAreJumpers extends SxPrimitiveBoolean {
}
SxClass.register(SymbolDuplicatePinNumbersAreJumpers)

export class Mirror extends SxClass {
static override token = "mirror"
static override parentToken = "symbol"
token = "mirror"

value: string

constructor(value: string) {
super()
this.value = value
}

static override fromSexprPrimitives(
primitiveSexprs: PrimitiveSExpr[],
): Mirror {
const [valuePrimitive] = primitiveSexprs
const value = toStringValue(valuePrimitive)
if (value === undefined) {
throw new Error("mirror expects a string value (x or y)")
}
return new Mirror(value)
}

override getChildren(): SxClass[] {
return []
}

override getString(): string {
return `(mirror ${this.value})`
}
}
SxClass.register(Mirror)

export class SymbolPinNumbers extends SxClass {
static override token = "pin_numbers"
static override parentToken = "symbol"
Expand Down Expand Up @@ -650,6 +683,7 @@ SxClass.register(SymbolPower)
export interface SchematicSymbolConstructorParams {
libraryId?: string
at?: AtInput
mirror?: string | Mirror
unit?: number | SymbolUnit
pinNumbers?: SymbolPinNumbers
pinNames?: SymbolPinNames
Expand Down Expand Up @@ -678,6 +712,7 @@ export class SchematicSymbol extends SxClass {

private _sxLibId?: SymbolLibId
_sxAt?: At
_sxMirror?: Mirror
_sxUnit?: SymbolUnit
_sxPinNumbers?: SymbolPinNumbers
_sxPinNames?: SymbolPinNames
Expand Down Expand Up @@ -706,6 +741,7 @@ export class SchematicSymbol extends SxClass {

if (params.libraryId !== undefined) this.libraryId = params.libraryId
if (params.at !== undefined) this.at = params.at
if (params.mirror !== undefined) this.mirror = params.mirror
if (params.unit !== undefined)
this.unit =
typeof params.unit === "number" ? params.unit : params.unit.value
Expand Down Expand Up @@ -763,6 +799,18 @@ export class SchematicSymbol extends SxClass {
this._sxAt = value !== undefined ? At.from(value) : undefined
}

get mirror(): string | undefined {
return this._sxMirror?.value
}

set mirror(value: string | Mirror | undefined) {
if (value === undefined) {
this._sxMirror = undefined
return
}
this._sxMirror = value instanceof Mirror ? value : new Mirror(value)
}

get unit(): number | undefined {
return this._sxUnit?.value
}
Expand Down Expand Up @@ -882,6 +930,7 @@ export class SchematicSymbol extends SxClass {
symbol._inlineLibId = inlineId
}
symbol._sxAt = propertyMap.at as At
symbol._sxMirror = propertyMap.mirror as Mirror
symbol._sxUnit = propertyMap.unit as SymbolUnit
symbol._sxPinNumbers = propertyMap.pin_numbers as SymbolPinNumbers
symbol._sxPinNames = propertyMap.pin_names as SymbolPinNames
Expand Down Expand Up @@ -913,6 +962,7 @@ export class SchematicSymbol extends SxClass {
const children: SxClass[] = []
if (this._sxLibId) children.push(this._sxLibId)
if (this._sxAt) children.push(this._sxAt)
if (this._sxMirror) children.push(this._sxMirror)
if (this._sxUnit) children.push(this._sxUnit)
if (this._sxPinNumbers) children.push(this._sxPinNumbers)
if (this._sxPinNames) children.push(this._sxPinNames)
Expand Down
78 changes: 76 additions & 2 deletions tests/sexpr/classes/Symbol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test("Symbol parse", () => {
(symbol "Device:R"
(at 10 20 90)
(unit 2)

(in_bom yes)
(on_board no)
(uuid 12345678-1234-1234-1234-123456789abc)
Expand All @@ -29,8 +30,6 @@ test("Symbol parse", () => {
)
`)

console.log(symbol)

expect(symbol).toBeInstanceOf(SchematicSymbol)
const sym = symbol as SchematicSymbol
expect(sym.libraryId).toBe("Device:R")
Expand Down Expand Up @@ -94,3 +93,78 @@ test("Symbol parse", () => {
)"
`)
})

test("Symbol parse 2", () => {
const [symbol] = SxClass.parse(`
(symbol
(lib_id "pic_programmer:74LS125")
(at 110.49 106.68 0)
(mirror y)
(unit 4)
(exclude_from_sim no)
(in_bom yes)
(on_board yes)
(dnp no)
(uuid "00000000-0000-0000-0000-0000442a4d6b")
(property "Reference" "U2"
(at 110.49 104.14 0)
(effects
(font
(size 1.27 1.27)
)
(justify left bottom)
)
)
(property "Value" "74HC125"
(at 105.41 110.49 0)
(effects
(font
(size 1.016 1.016)
)
(justify left top)
)
)
)
`)

expect(symbol).toBeInstanceOf(SchematicSymbol)
const sym = symbol as SchematicSymbol
expect(sym.libraryId).toBe("pic_programmer:74LS125")
expect(sym.inBom).toBe(true)
expect(sym.onBoard).toBe(true)
expect(sym.uuid).toBe("00000000-0000-0000-0000-0000442a4d6b")
expect(sym.properties).toHaveLength(2)
expect(sym.mirror).toBe("y")

expect(sym.getString()).toMatchInlineSnapshot(`
"(symbol
(lib_id "pic_programmer:74LS125")
(at 110.49 106.68 0)
(mirror y)
(unit 4)
(exclude_from_sim no)
(in_bom yes)
(on_board yes)
(dnp no)
(uuid 00000000-0000-0000-0000-0000442a4d6b)
(property "Reference" "U2"
(at 110.49 104.14 0)
(effects
(font
(size 1.27 1.27)
)
(justify left bottom)
)
)
(property "Value" "74HC125"
(at 105.41 110.49 0)
(effects
(font
(size 1.016 1.016)
)
(justify left top)
)
)
)"
`)
})