adds support for generic components

This commit is contained in:
darzu 2016-08-31 19:01:59 -07:00
parent 2b87b26f00
commit 1441129355
4 changed files with 45 additions and 28 deletions

View File

@ -101,7 +101,7 @@ namespace pxsim {
groundPins: ["GND"], groundPins: ["GND"],
threeVoltPins: ["+3v3"], threeVoltPins: ["+3v3"],
attachPowerOnRight: true, attachPowerOnRight: true,
onboardComponents: ["buttonpair", "ledmatrix"], onboardComponents: ["buttonpair", "ledmatrix", "speaker"],
useCrocClips: true, useCrocClips: true,
marginWhenBreadboarding: [0, 0, 80, 0], marginWhenBreadboarding: [0, 0, 80, 0],
} }
@ -294,7 +294,7 @@ namespace pxsim {
image: "/static/hardware/speaker.svg", image: "/static/hardware/speaker.svg",
width: 500, width: 500,
height: 500, height: 500,
firstPin: [180, 135], firstPin: [110, 135],
pinDist: 70, pinDist: 70,
}, },
breadboardColumnsNeeded: 5, breadboardColumnsNeeded: 5,

View File

@ -155,6 +155,10 @@ namespace pxsim.visuals {
let stateFn = builtinComponentSimState[builtinVisual]; let stateFn = builtinComponentSimState[builtinVisual];
cmp = cnstr(); cmp = cnstr();
cmp.init(this.state.bus, stateFn(this.state), this.view, cmpDesc.microbitPins, cmpDesc.otherArgs); cmp.init(this.state.bus, stateFn(this.state), this.view, cmpDesc.microbitPins, cmpDesc.otherArgs);
} else {
let vis = cmpDesc.visual as PartVisualDefinition;
cmp = new GenericPart(vis);
}
this.components.push(cmp); this.components.push(cmp);
this.view.appendChild(cmp.element); this.view.appendChild(cmp.element);
if (cmp.defs) if (cmp.defs)
@ -169,11 +173,6 @@ namespace pxsim.visuals {
svg.addClass(cmp.element, "sim-cmp"); svg.addClass(cmp.element, "sim-cmp");
cmp.updateTheme(); cmp.updateTheme();
cmp.updateState(); cmp.updateState();
} else {
let vis = cmpDesc.visual as PartVisualDefinition;
console.log("TODO PART: " + vis.image);
//TODO: support generic parts
}
return cmp; return cmp;
} }
public addWire(inst: WireInst): Wire { public addWire(inst: WireInst): Wire {

View File

@ -1,16 +1,35 @@
namespace pxsim.visuals { namespace pxsim.visuals {
export class GenericComponentView implements IBoardComponent<any> { export class GenericPart implements IBoardComponent<any> {
public style: string; public style: string = "";
public element: SVGElement; public element: SVGElement;
defs: SVGElement[]; defs: SVGElement[] = [];
init(bus: EventBus, state: any, svgEl: SVGSVGElement, gpioPins: string[], otherArgs: string[]): void {
constructor(partVisual: PartVisualDefinition) {
let imgAndSize = mkImageSVG({
image: partVisual.image,
width: partVisual.width,
height: partVisual.height,
imageUnitDist: partVisual.pinDist,
targetUnitDist: PIN_DIST
});
let img = imgAndSize.el;
let scaleFn = mkScaleFn(partVisual.pinDist, PIN_DIST);
let [pinX, pinY] = partVisual.firstPin;
let left = -scaleFn(pinX);
let top = -scaleFn(pinY);
translateEl(img, [left, top]); // So that 0,0 is on the first pin
this.element = svg.elt("g");
this.element.appendChild(img);
} }
moveToCoord(xy: Coord): void { moveToCoord(xy: Coord): void {
translateEl(this.element, xy);
} }
updateState(): void {
} //unused
updateTheme(): void { init(bus: EventBus, state: any, svgEl: SVGSVGElement, gpioPins: string[], otherArgs: string[]): void { }
} updateState(): void { }
updateTheme(): void { }
} }
} }

View File

@ -92,8 +92,7 @@ namespace pxsim.visuals {
} }
public moveToCoord(xy: Coord) { public moveToCoord(xy: Coord) {
let [x, y] = xy; translateEl(this.element, xy);
translateEl(this.element, [x, y]);
} }
public updateTheme() { public updateTheme() {