Skip to content

Commit 1692d8b

Browse files
authored
feat: add support for mirror under the parent Symbol (#17)
1 parent 4ef59bb commit 1692d8b

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed

lib/sexpr/classes/Symbol.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,39 @@ export class SymbolDuplicatePinNumbersAreJumpers extends SxPrimitiveBoolean {
7272
}
7373
SxClass.register(SymbolDuplicatePinNumbersAreJumpers)
7474

75+
export class Mirror extends SxClass {
76+
static override token = "mirror"
77+
static override parentToken = "symbol"
78+
token = "mirror"
79+
80+
value: string
81+
82+
constructor(value: string) {
83+
super()
84+
this.value = value
85+
}
86+
87+
static override fromSexprPrimitives(
88+
primitiveSexprs: PrimitiveSExpr[],
89+
): Mirror {
90+
const [valuePrimitive] = primitiveSexprs
91+
const value = toStringValue(valuePrimitive)
92+
if (value === undefined) {
93+
throw new Error("mirror expects a string value (x or y)")
94+
}
95+
return new Mirror(value)
96+
}
97+
98+
override getChildren(): SxClass[] {
99+
return []
100+
}
101+
102+
override getString(): string {
103+
return `(mirror ${this.value})`
104+
}
105+
}
106+
SxClass.register(Mirror)
107+
75108
export class SymbolPinNumbers extends SxClass {
76109
static override token = "pin_numbers"
77110
static override parentToken = "symbol"
@@ -650,6 +683,7 @@ SxClass.register(SymbolPower)
650683
export interface SchematicSymbolConstructorParams {
651684
libraryId?: string
652685
at?: AtInput
686+
mirror?: string | Mirror
653687
unit?: number | SymbolUnit
654688
pinNumbers?: SymbolPinNumbers
655689
pinNames?: SymbolPinNames
@@ -678,6 +712,7 @@ export class SchematicSymbol extends SxClass {
678712

679713
private _sxLibId?: SymbolLibId
680714
_sxAt?: At
715+
_sxMirror?: Mirror
681716
_sxUnit?: SymbolUnit
682717
_sxPinNumbers?: SymbolPinNumbers
683718
_sxPinNames?: SymbolPinNames
@@ -706,6 +741,7 @@ export class SchematicSymbol extends SxClass {
706741

707742
if (params.libraryId !== undefined) this.libraryId = params.libraryId
708743
if (params.at !== undefined) this.at = params.at
744+
if (params.mirror !== undefined) this.mirror = params.mirror
709745
if (params.unit !== undefined)
710746
this.unit =
711747
typeof params.unit === "number" ? params.unit : params.unit.value
@@ -763,6 +799,18 @@ export class SchematicSymbol extends SxClass {
763799
this._sxAt = value !== undefined ? At.from(value) : undefined
764800
}
765801

802+
get mirror(): string | undefined {
803+
return this._sxMirror?.value
804+
}
805+
806+
set mirror(value: string | Mirror | undefined) {
807+
if (value === undefined) {
808+
this._sxMirror = undefined
809+
return
810+
}
811+
this._sxMirror = value instanceof Mirror ? value : new Mirror(value)
812+
}
813+
766814
get unit(): number | undefined {
767815
return this._sxUnit?.value
768816
}
@@ -882,6 +930,7 @@ export class SchematicSymbol extends SxClass {
882930
symbol._inlineLibId = inlineId
883931
}
884932
symbol._sxAt = propertyMap.at as At
933+
symbol._sxMirror = propertyMap.mirror as Mirror
885934
symbol._sxUnit = propertyMap.unit as SymbolUnit
886935
symbol._sxPinNumbers = propertyMap.pin_numbers as SymbolPinNumbers
887936
symbol._sxPinNames = propertyMap.pin_names as SymbolPinNames
@@ -913,6 +962,7 @@ export class SchematicSymbol extends SxClass {
913962
const children: SxClass[] = []
914963
if (this._sxLibId) children.push(this._sxLibId)
915964
if (this._sxAt) children.push(this._sxAt)
965+
if (this._sxMirror) children.push(this._sxMirror)
916966
if (this._sxUnit) children.push(this._sxUnit)
917967
if (this._sxPinNumbers) children.push(this._sxPinNumbers)
918968
if (this._sxPinNames) children.push(this._sxPinNames)

tests/sexpr/classes/Symbol.test.ts

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ test("Symbol parse", () => {
1313
(symbol "Device:R"
1414
(at 10 20 90)
1515
(unit 2)
16+
1617
(in_bom yes)
1718
(on_board no)
1819
(uuid 12345678-1234-1234-1234-123456789abc)
@@ -29,8 +30,6 @@ test("Symbol parse", () => {
2930
)
3031
`)
3132

32-
console.log(symbol)
33-
3433
expect(symbol).toBeInstanceOf(SchematicSymbol)
3534
const sym = symbol as SchematicSymbol
3635
expect(sym.libraryId).toBe("Device:R")
@@ -94,3 +93,78 @@ test("Symbol parse", () => {
9493
)"
9594
`)
9695
})
96+
97+
test("Symbol parse 2", () => {
98+
const [symbol] = SxClass.parse(`
99+
(symbol
100+
(lib_id "pic_programmer:74LS125")
101+
(at 110.49 106.68 0)
102+
(mirror y)
103+
(unit 4)
104+
(exclude_from_sim no)
105+
(in_bom yes)
106+
(on_board yes)
107+
(dnp no)
108+
(uuid "00000000-0000-0000-0000-0000442a4d6b")
109+
(property "Reference" "U2"
110+
(at 110.49 104.14 0)
111+
(effects
112+
(font
113+
(size 1.27 1.27)
114+
)
115+
(justify left bottom)
116+
)
117+
)
118+
(property "Value" "74HC125"
119+
(at 105.41 110.49 0)
120+
(effects
121+
(font
122+
(size 1.016 1.016)
123+
)
124+
(justify left top)
125+
)
126+
)
127+
)
128+
`)
129+
130+
expect(symbol).toBeInstanceOf(SchematicSymbol)
131+
const sym = symbol as SchematicSymbol
132+
expect(sym.libraryId).toBe("pic_programmer:74LS125")
133+
expect(sym.inBom).toBe(true)
134+
expect(sym.onBoard).toBe(true)
135+
expect(sym.uuid).toBe("00000000-0000-0000-0000-0000442a4d6b")
136+
expect(sym.properties).toHaveLength(2)
137+
expect(sym.mirror).toBe("y")
138+
139+
expect(sym.getString()).toMatchInlineSnapshot(`
140+
"(symbol
141+
(lib_id "pic_programmer:74LS125")
142+
(at 110.49 106.68 0)
143+
(mirror y)
144+
(unit 4)
145+
(exclude_from_sim no)
146+
(in_bom yes)
147+
(on_board yes)
148+
(dnp no)
149+
(uuid 00000000-0000-0000-0000-0000442a4d6b)
150+
(property "Reference" "U2"
151+
(at 110.49 104.14 0)
152+
(effects
153+
(font
154+
(size 1.27 1.27)
155+
)
156+
(justify left bottom)
157+
)
158+
)
159+
(property "Value" "74HC125"
160+
(at 105.41 110.49 0)
161+
(effects
162+
(font
163+
(size 1.016 1.016)
164+
)
165+
(justify left top)
166+
)
167+
)
168+
)"
169+
`)
170+
})

0 commit comments

Comments
 (0)