refactoring dalboard

This commit is contained in:
Peli de Halleux 2016-09-12 21:29:55 -07:00
parent 61bab257eb
commit 1fa9bf12d5
4 changed files with 47 additions and 47 deletions

View File

@ -1,12 +1,40 @@
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/> /// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
namespace pxsim { namespace pxsim {
export class DalBoard extends BaseBoard { export class CoreBoard extends BaseBoard {
id: string; id: string;
// the bus // the bus
bus: pxsim.EventBus; 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 // state & update logic for component services
ledMatrixState: LedMatrixState; ledMatrixState: LedMatrixState;
edgeConnectorState: EdgeConnectorState; edgeConnectorState: EdgeConnectorState;
@ -19,37 +47,26 @@ namespace pxsim {
radioState: RadioState; radioState: RadioState;
neopixelState: NeoPixelState; neopixelState: NeoPixelState;
// updates
updateSubscribers: (() => void)[];
constructor() { constructor() {
super() super()
this.id = "b" + Math_.random(2147483647);
this.bus = new pxsim.EventBus(runtime);
// components // components
this.ledMatrixState = new LedMatrixState(runtime); this.builtinParts["ledmatrix"] = this.ledMatrixState = new LedMatrixState(runtime);
this.buttonPairState = new ButtonPairState({ this.builtinParts["buttonpair"] = this.buttonPairState = new ButtonPairState({
ID_BUTTON_A: DAL.MICROBIT_ID_BUTTON_A, ID_BUTTON_A: DAL.MICROBIT_ID_BUTTON_A,
ID_BUTTON_B: DAL.MICROBIT_ID_BUTTON_B, ID_BUTTON_B: DAL.MICROBIT_ID_BUTTON_B,
ID_BUTTON_AB: DAL.MICROBIT_ID_BUTTON_AB, ID_BUTTON_AB: DAL.MICROBIT_ID_BUTTON_AB,
BUTTON_EVT_UP: DAL.MICROBIT_BUTTON_EVT_UP, BUTTON_EVT_UP: DAL.MICROBIT_BUTTON_EVT_UP,
BUTTON_EVT_CLICK: DAL.MICROBIT_BUTTON_EVT_CLICK BUTTON_EVT_CLICK: DAL.MICROBIT_BUTTON_EVT_CLICK
}); });
this.edgeConnectorState = new EdgeConnectorState(); this.builtinParts["edgeconnector"] = this.edgeConnectorState = new EdgeConnectorState();
this.radioState = new RadioState(runtime); this.builtinParts["radio"] = this.radioState = new RadioState(runtime);
this.accelerometerState = new AccelerometerState(runtime); this.builtinParts["accelerometer"] = this.accelerometerState = new AccelerometerState(runtime);
this.serialState = new SerialState(); this.builtinParts["serial"] = this.serialState = new SerialState();
this.thermometerState = new ThermometerState(); this.builtinParts["thermometer"] = this.thermometerState = new ThermometerState();
this.lightSensorState = new LightSensorState(); this.builtinParts["lightsensor"] = this.lightSensorState = new LightSensorState();
this.compassState = new CompassState(); this.builtinParts["compass"] = this.compassState = new CompassState();
this.neopixelState = new NeoPixelState(); this.builtinParts["neopixel"] = this.neopixelState = new NeoPixelState();
// updates
this.updateSubscribers = []
this.updateView = () => {
this.updateSubscribers.forEach(sub => sub());
}
} }
receiveMessage(msg: SimulatorMessage) { receiveMessage(msg: SimulatorMessage) {
@ -71,11 +88,6 @@ namespace pxsim {
} }
} }
kill() {
super.kill();
AudioContextManager.stop();
}
initAsync(msg: SimulatorRunMessage): Promise<void> { initAsync(msg: SimulatorRunMessage): Promise<void> {
super.initAsync(msg); super.initAsync(msg);

View File

@ -58,18 +58,6 @@ namespace pxsim {
"ledmatrix": () => new visuals.LedMatrixView(), "ledmatrix": () => new visuals.LedMatrixView(),
"neopixel": () => new visuals.NeoPixelView(), "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> = { export const builtinComponentPartVisual: Map<(xy: visuals.Coord) => visuals.SVGElAndSize> = {
"buttonpair": (xy: visuals.Coord) => visuals.mkBtnSvg(xy), "buttonpair": (xy: visuals.Coord) => visuals.mkBtnSvg(xy),
"ledmatrix": (xy: visuals.Coord) => visuals.mkLedMatrixSvg(xy, 8, 8), "ledmatrix": (xy: visuals.Coord) => visuals.mkLedMatrixSvg(xy, 8, 8),

View File

@ -353,7 +353,7 @@ namespace pxsim.instructions {
}; };
} }
function mkBlankBoardAndBreadboard(boardDef: BoardDefinition, cmpDefs: Map<PartDefinition>, fnArgs: any, width: number, buildMode: boolean = false): visuals.BoardHost { 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({ let boardHost = new visuals.BoardHost({
state: state, state: state,
boardDef: boardDef, boardDef: boardDef,

View File

@ -1,6 +1,6 @@
namespace pxsim.visuals { namespace pxsim.visuals {
export interface BoardHostOpts { export interface BoardHostOpts {
state: DalBoard, state: CoreBoard,
boardDef: BoardDefinition, boardDef: BoardDefinition,
partsList?: string[], partsList?: string[],
partDefs: Map<PartDefinition>, partDefs: Map<PartDefinition>,
@ -22,7 +22,7 @@ namespace pxsim.visuals {
private partOverGroup: SVGGElement; private partOverGroup: SVGGElement;
private style: SVGStyleElement; private style: SVGStyleElement;
private defs: SVGDefsElement; private defs: SVGDefsElement;
private state: DalBoard; private state: CoreBoard;
private useCrocClips: boolean; private useCrocClips: boolean;
constructor(opts: BoardHostOpts) { constructor(opts: BoardHostOpts) {
@ -160,9 +160,9 @@ namespace pxsim.visuals {
//TODO: seperate simulation behavior from builtin visual //TODO: seperate simulation behavior from builtin visual
let builtinBehavior = partInst.simulationBehavior; let builtinBehavior = partInst.simulationBehavior;
let cnstr = builtinComponentSimVisual[builtinBehavior]; let cnstr = builtinComponentSimVisual[builtinBehavior];
let stateFn = builtinComponentSimState[builtinBehavior]; let stateFn = this.state.builtinParts[builtinBehavior];
part = cnstr(); 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 { } else {
let vis = partInst.visual as PartVisualDefinition; let vis = partInst.visual as PartVisualDefinition;
part = new GenericPart(vis); part = new GenericPart(vis);