refactoring dalboard
This commit is contained in:
parent
61bab257eb
commit
1fa9bf12d5
@ -1,12 +1,40 @@
|
||||
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
|
||||
|
||||
namespace pxsim {
|
||||
export class DalBoard extends BaseBoard {
|
||||
export class CoreBoard extends BaseBoard {
|
||||
id: string;
|
||||
|
||||
// the bus
|
||||
bus: pxsim.EventBus;
|
||||
|
||||
// updates
|
||||
updateSubscribers: (() => void)[];
|
||||
|
||||
// builtin state
|
||||
builtinParts: Map<any>;
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.id = "b" + Math_.random(2147483647);
|
||||
this.bus = new pxsim.EventBus(runtime);
|
||||
|
||||
// updates
|
||||
this.updateSubscribers = []
|
||||
this.updateView = () => {
|
||||
this.updateSubscribers.forEach(sub => sub());
|
||||
}
|
||||
|
||||
this.builtinParts = {};
|
||||
}
|
||||
|
||||
kill() {
|
||||
super.kill();
|
||||
AudioContextManager.stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class DalBoard extends CoreBoard {
|
||||
// state & update logic for component services
|
||||
ledMatrixState: LedMatrixState;
|
||||
edgeConnectorState: EdgeConnectorState;
|
||||
@ -19,37 +47,26 @@ namespace pxsim {
|
||||
radioState: RadioState;
|
||||
neopixelState: NeoPixelState;
|
||||
|
||||
// updates
|
||||
updateSubscribers: (() => void)[];
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.id = "b" + Math_.random(2147483647);
|
||||
this.bus = new pxsim.EventBus(runtime);
|
||||
|
||||
// components
|
||||
this.ledMatrixState = new LedMatrixState(runtime);
|
||||
this.buttonPairState = new ButtonPairState({
|
||||
this.builtinParts["ledmatrix"] = this.ledMatrixState = new LedMatrixState(runtime);
|
||||
this.builtinParts["buttonpair"] = this.buttonPairState = new ButtonPairState({
|
||||
ID_BUTTON_A: DAL.MICROBIT_ID_BUTTON_A,
|
||||
ID_BUTTON_B: DAL.MICROBIT_ID_BUTTON_B,
|
||||
ID_BUTTON_AB: DAL.MICROBIT_ID_BUTTON_AB,
|
||||
BUTTON_EVT_UP: DAL.MICROBIT_BUTTON_EVT_UP,
|
||||
BUTTON_EVT_CLICK: DAL.MICROBIT_BUTTON_EVT_CLICK
|
||||
});
|
||||
this.edgeConnectorState = new EdgeConnectorState();
|
||||
this.radioState = new RadioState(runtime);
|
||||
this.accelerometerState = new AccelerometerState(runtime);
|
||||
this.serialState = new SerialState();
|
||||
this.thermometerState = new ThermometerState();
|
||||
this.lightSensorState = new LightSensorState();
|
||||
this.compassState = new CompassState();
|
||||
this.neopixelState = new NeoPixelState();
|
||||
|
||||
// updates
|
||||
this.updateSubscribers = []
|
||||
this.updateView = () => {
|
||||
this.updateSubscribers.forEach(sub => sub());
|
||||
}
|
||||
this.builtinParts["edgeconnector"] = this.edgeConnectorState = new EdgeConnectorState();
|
||||
this.builtinParts["radio"] = this.radioState = new RadioState(runtime);
|
||||
this.builtinParts["accelerometer"] = this.accelerometerState = new AccelerometerState(runtime);
|
||||
this.builtinParts["serial"] = this.serialState = new SerialState();
|
||||
this.builtinParts["thermometer"] = this.thermometerState = new ThermometerState();
|
||||
this.builtinParts["lightsensor"] = this.lightSensorState = new LightSensorState();
|
||||
this.builtinParts["compass"] = this.compassState = new CompassState();
|
||||
this.builtinParts["neopixel"] = this.neopixelState = new NeoPixelState();
|
||||
}
|
||||
|
||||
receiveMessage(msg: SimulatorMessage) {
|
||||
@ -71,11 +88,6 @@ namespace pxsim {
|
||||
}
|
||||
}
|
||||
|
||||
kill() {
|
||||
super.kill();
|
||||
AudioContextManager.stop();
|
||||
}
|
||||
|
||||
initAsync(msg: SimulatorRunMessage): Promise<void> {
|
||||
super.initAsync(msg);
|
||||
|
||||
@ -131,9 +143,9 @@ namespace pxsim {
|
||||
|
||||
if (!pxsim.initCurrentRuntime) {
|
||||
pxsim.initCurrentRuntime = initRuntimeWithDalBoard;
|
||||
}
|
||||
}
|
||||
|
||||
export function board() {
|
||||
return runtime.board as DalBoard;
|
||||
}
|
||||
}
|
||||
}
|
@ -58,18 +58,6 @@ namespace pxsim {
|
||||
"ledmatrix": () => new visuals.LedMatrixView(),
|
||||
"neopixel": () => new visuals.NeoPixelView(),
|
||||
};
|
||||
export const builtinComponentSimState: Map<(d: DalBoard) => any> = {
|
||||
"buttonpair": (d: DalBoard) => d.buttonPairState,
|
||||
"ledmatrix": (d: DalBoard) => d.ledMatrixState,
|
||||
"edgeconnector": (d: DalBoard) => d.edgeConnectorState,
|
||||
"serial": (d: DalBoard) => d.serialState,
|
||||
"radio": (d: DalBoard) => d.radioState,
|
||||
"thermometer": (d: DalBoard) => d.thermometerState,
|
||||
"accelerometer": (d: DalBoard) => d.accelerometerState,
|
||||
"compass": (d: DalBoard) => d.compassState,
|
||||
"lightsensor": (d: DalBoard) => d.lightSensorState,
|
||||
"neopixel": (d: DalBoard) => d.neopixelState,
|
||||
};
|
||||
export const builtinComponentPartVisual: Map<(xy: visuals.Coord) => visuals.SVGElAndSize> = {
|
||||
"buttonpair": (xy: visuals.Coord) => visuals.mkBtnSvg(xy),
|
||||
"ledmatrix": (xy: visuals.Coord) => visuals.mkLedMatrixSvg(xy, 8, 8),
|
||||
|
@ -353,7 +353,7 @@ namespace pxsim.instructions {
|
||||
};
|
||||
}
|
||||
function mkBlankBoardAndBreadboard(boardDef: BoardDefinition, cmpDefs: Map<PartDefinition>, fnArgs: any, width: number, buildMode: boolean = false): visuals.BoardHost {
|
||||
let state = runtime.board as pxsim.DalBoard;
|
||||
let state = runtime.board as pxsim.CoreBoard;
|
||||
let boardHost = new visuals.BoardHost({
|
||||
state: state,
|
||||
boardDef: boardDef,
|
||||
|
@ -1,6 +1,6 @@
|
||||
namespace pxsim.visuals {
|
||||
export interface BoardHostOpts {
|
||||
state: DalBoard,
|
||||
state: CoreBoard,
|
||||
boardDef: BoardDefinition,
|
||||
partsList?: string[],
|
||||
partDefs: Map<PartDefinition>,
|
||||
@ -22,7 +22,7 @@ namespace pxsim.visuals {
|
||||
private partOverGroup: SVGGElement;
|
||||
private style: SVGStyleElement;
|
||||
private defs: SVGDefsElement;
|
||||
private state: DalBoard;
|
||||
private state: CoreBoard;
|
||||
private useCrocClips: boolean;
|
||||
|
||||
constructor(opts: BoardHostOpts) {
|
||||
@ -160,9 +160,9 @@ namespace pxsim.visuals {
|
||||
//TODO: seperate simulation behavior from builtin visual
|
||||
let builtinBehavior = partInst.simulationBehavior;
|
||||
let cnstr = builtinComponentSimVisual[builtinBehavior];
|
||||
let stateFn = builtinComponentSimState[builtinBehavior];
|
||||
let stateFn = this.state.builtinParts[builtinBehavior];
|
||||
part = cnstr();
|
||||
part.init(this.state.bus, stateFn(this.state), this.view, partInst.params);
|
||||
part.init(this.state.bus, stateFn, this.view, partInst.params);
|
||||
} else {
|
||||
let vis = partInst.visual as PartVisualDefinition;
|
||||
part = new GenericPart(vis);
|
||||
@ -179,7 +179,7 @@ namespace pxsim.visuals {
|
||||
let row = getRowName(rowIdx);
|
||||
let col = getColumnName(colIdx);
|
||||
let xOffset = partInst.bbFit.xOffset / partInst.visual.pinDistance;
|
||||
let yOffset = partInst.bbFit.yOffset / partInst.visual.pinDistance;
|
||||
let yOffset = partInst.bbFit.yOffset / partInst.visual.pinDistance;
|
||||
let rowCol = <BBLoc>{
|
||||
type: "breadboard",
|
||||
row: row,
|
||||
|
Loading…
Reference in New Issue
Block a user