2016-09-02 00:37:39 +02:00
|
|
|
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
|
|
|
|
|
2016-08-30 20:55:00 +02:00
|
|
|
namespace pxsim {
|
2016-09-13 06:29:55 +02:00
|
|
|
export class DalBoard extends CoreBoard {
|
2016-08-30 20:55:00 +02:00
|
|
|
// state & update logic for component services
|
|
|
|
ledMatrixState: LedMatrixState;
|
|
|
|
edgeConnectorState: EdgeConnectorState;
|
|
|
|
serialState: SerialState;
|
|
|
|
accelerometerState: AccelerometerState;
|
|
|
|
compassState: CompassState;
|
|
|
|
thermometerState: ThermometerState;
|
|
|
|
lightSensorState: LightSensorState;
|
|
|
|
buttonPairState: ButtonPairState;
|
|
|
|
radioState: RadioState;
|
2016-11-01 16:16:03 +01:00
|
|
|
// TODO: not singletons
|
2016-08-30 20:55:00 +02:00
|
|
|
neopixelState: NeoPixelState;
|
2016-11-07 20:11:38 +01:00
|
|
|
fileSystem: FileSystemState;
|
2016-08-30 20:55:00 +02:00
|
|
|
|
2017-03-01 23:24:59 +01:00
|
|
|
// visual
|
|
|
|
view: SVGElement;
|
|
|
|
|
2016-08-30 20:55:00 +02:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
|
|
|
|
// components
|
2016-11-07 20:11:38 +01:00
|
|
|
this.fileSystem = new FileSystemState();
|
2016-09-13 06:29:55 +02:00
|
|
|
this.builtinParts["ledmatrix"] = this.ledMatrixState = new LedMatrixState(runtime);
|
|
|
|
this.builtinParts["buttonpair"] = this.buttonPairState = new ButtonPairState({
|
2016-09-10 07:56:26 +02:00
|
|
|
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
|
|
|
|
});
|
2016-09-13 21:48:07 +02:00
|
|
|
this.builtinParts["edgeconnector"] = this.edgeConnectorState = new EdgeConnectorState({
|
|
|
|
pins: [
|
|
|
|
DAL.MICROBIT_ID_IO_P0,
|
|
|
|
DAL.MICROBIT_ID_IO_P1,
|
|
|
|
DAL.MICROBIT_ID_IO_P2,
|
|
|
|
DAL.MICROBIT_ID_IO_P3,
|
|
|
|
DAL.MICROBIT_ID_IO_P4,
|
|
|
|
DAL.MICROBIT_ID_IO_P5,
|
|
|
|
DAL.MICROBIT_ID_IO_P6,
|
|
|
|
DAL.MICROBIT_ID_IO_P7,
|
|
|
|
DAL.MICROBIT_ID_IO_P8,
|
|
|
|
DAL.MICROBIT_ID_IO_P9,
|
|
|
|
DAL.MICROBIT_ID_IO_P10,
|
|
|
|
DAL.MICROBIT_ID_IO_P11,
|
|
|
|
DAL.MICROBIT_ID_IO_P12,
|
|
|
|
DAL.MICROBIT_ID_IO_P13,
|
|
|
|
DAL.MICROBIT_ID_IO_P14,
|
|
|
|
DAL.MICROBIT_ID_IO_P15,
|
|
|
|
DAL.MICROBIT_ID_IO_P16,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
DAL.MICROBIT_ID_IO_P19,
|
2016-11-01 16:16:03 +01:00
|
|
|
DAL.MICROBIT_ID_IO_P20
|
2017-01-13 08:09:53 +01:00
|
|
|
],
|
|
|
|
servos: {
|
|
|
|
"P0": DAL.MICROBIT_ID_IO_P0,
|
|
|
|
"P1": DAL.MICROBIT_ID_IO_P1,
|
|
|
|
"P2": DAL.MICROBIT_ID_IO_P2,
|
2018-06-09 07:37:27 +02:00
|
|
|
"P3": DAL.MICROBIT_ID_IO_P3,
|
|
|
|
"P4": DAL.MICROBIT_ID_IO_P4,
|
|
|
|
"P5": DAL.MICROBIT_ID_IO_P5,
|
|
|
|
"P6": DAL.MICROBIT_ID_IO_P6,
|
|
|
|
"P7": DAL.MICROBIT_ID_IO_P7,
|
|
|
|
"P8": DAL.MICROBIT_ID_IO_P8,
|
|
|
|
"P9": DAL.MICROBIT_ID_IO_P9,
|
|
|
|
"P10": DAL.MICROBIT_ID_IO_P10,
|
|
|
|
"P11": DAL.MICROBIT_ID_IO_P11,
|
|
|
|
"P12": DAL.MICROBIT_ID_IO_P12,
|
|
|
|
"P13": DAL.MICROBIT_ID_IO_P13,
|
|
|
|
"P14": DAL.MICROBIT_ID_IO_P14,
|
|
|
|
"P15": DAL.MICROBIT_ID_IO_P15,
|
|
|
|
"P16": DAL.MICROBIT_ID_IO_P16,
|
|
|
|
"P19": DAL.MICROBIT_ID_IO_P19
|
2017-01-13 08:09:53 +01:00
|
|
|
}
|
2016-09-13 21:48:07 +02:00
|
|
|
});
|
2016-09-13 06:29:55 +02:00
|
|
|
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();
|
2017-01-13 08:09:53 +01:00
|
|
|
this.builtinParts["microservo"] = this.edgeConnectorState;
|
2016-09-13 18:44:58 +02:00
|
|
|
|
|
|
|
this.builtinVisuals["buttonpair"] = () => new visuals.ButtonPairView();
|
|
|
|
this.builtinVisuals["ledmatrix"] = () => new visuals.LedMatrixView();
|
2016-11-01 16:16:03 +01:00
|
|
|
this.builtinVisuals["neopixel"] = () => new visuals.NeoPixelView();
|
|
|
|
this.builtinVisuals["microservo"] = () => new visuals.MicroServoView();
|
2016-09-13 18:44:58 +02:00
|
|
|
|
|
|
|
this.builtinPartVisuals["buttonpair"] = (xy: visuals.Coord) => visuals.mkBtnSvg(xy);
|
|
|
|
this.builtinPartVisuals["ledmatrix"] = (xy: visuals.Coord) => visuals.mkLedMatrixSvg(xy, 8, 8);
|
2016-11-01 16:16:03 +01:00
|
|
|
this.builtinPartVisuals["neopixel"] = (xy: visuals.Coord) => visuals.mkNeoPixelPart(xy);
|
|
|
|
this.builtinPartVisuals["microservo"] = (xy: visuals.Coord) => visuals.mkMicroServoPart(xy);
|
2016-08-30 20:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
receiveMessage(msg: SimulatorMessage) {
|
|
|
|
if (!runtime || runtime.dead) return;
|
|
|
|
|
|
|
|
switch (msg.type || "") {
|
|
|
|
case "eventbus":
|
2018-10-08 18:46:23 +02:00
|
|
|
const ev = <SimulatorEventBusMessage>msg;
|
2016-08-30 20:55:00 +02:00
|
|
|
this.bus.queue(ev.id, ev.eventid, ev.value);
|
|
|
|
break;
|
|
|
|
case "serial":
|
2018-10-08 18:46:23 +02:00
|
|
|
const data = (<SimulatorSerialMessage>msg).data || "";
|
2017-03-24 17:51:07 +01:00
|
|
|
this.serialState.receiveData(data);
|
2016-08-30 20:55:00 +02:00
|
|
|
break;
|
|
|
|
case "radiopacket":
|
2018-10-08 18:46:23 +02:00
|
|
|
const packet = <SimulatorRadioPacketMessage>msg;
|
2017-05-17 01:19:52 +02:00
|
|
|
this.radioState.receivePacket(packet);
|
2016-08-30 20:55:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initAsync(msg: SimulatorRunMessage): Promise<void> {
|
2016-09-02 00:37:39 +02:00
|
|
|
super.initAsync(msg);
|
|
|
|
|
2016-09-13 18:59:34 +02:00
|
|
|
const options = (msg.options || {}) as RuntimeOptions;
|
2016-08-30 20:55:00 +02:00
|
|
|
|
2016-09-14 00:32:12 +02:00
|
|
|
const boardDef = msg.boardDefinition;
|
2016-09-13 18:59:34 +02:00
|
|
|
const cmpsList = msg.parts;
|
2016-09-14 00:32:12 +02:00
|
|
|
const cmpDefs = msg.partDefinitions || {};
|
2016-09-13 18:59:34 +02:00
|
|
|
const fnArgs = msg.fnArgs;
|
2016-08-30 20:55:00 +02:00
|
|
|
|
2016-11-01 16:16:03 +01:00
|
|
|
const opts: visuals.BoardHostOpts = {
|
2016-08-31 20:14:16 +02:00
|
|
|
state: this,
|
|
|
|
boardDef: boardDef,
|
2016-09-09 10:23:39 +02:00
|
|
|
partsList: cmpsList,
|
|
|
|
partDefs: cmpDefs,
|
2016-08-31 20:34:49 +02:00
|
|
|
fnArgs: fnArgs,
|
|
|
|
maxWidth: "100%",
|
|
|
|
maxHeight: "100%",
|
2017-04-20 08:44:47 +02:00
|
|
|
highContrast: msg.highContrast
|
2016-09-13 18:59:34 +02:00
|
|
|
};
|
2016-09-26 22:26:43 +02:00
|
|
|
const viewHost = new visuals.BoardHost(pxsim.visuals.mkBoardView({
|
2017-04-20 08:44:47 +02:00
|
|
|
visual: boardDef.visual,
|
2019-02-22 00:41:32 +01:00
|
|
|
boardDef: boardDef,
|
2017-04-20 08:44:47 +02:00
|
|
|
highContrast: msg.highContrast
|
2016-09-26 22:26:43 +02:00
|
|
|
}), opts);
|
2016-08-30 20:55:00 +02:00
|
|
|
|
|
|
|
document.body.innerHTML = ""; // clear children
|
2017-03-01 23:24:59 +01:00
|
|
|
document.body.appendChild(this.view = viewHost.getView());
|
2016-08-30 20:55:00 +02:00
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2017-03-01 23:24:59 +01:00
|
|
|
|
|
|
|
screenshot(): string {
|
|
|
|
return svg.toDataUri(new XMLSerializer().serializeToString(this.view));
|
|
|
|
}
|
2016-08-30 20:55:00 +02:00
|
|
|
}
|
2016-09-10 00:01:29 +02:00
|
|
|
|
|
|
|
export function initRuntimeWithDalBoard() {
|
|
|
|
U.assert(!runtime.board);
|
|
|
|
let b = new DalBoard();
|
|
|
|
runtime.board = b;
|
|
|
|
runtime.postError = (e) => {
|
|
|
|
led.setBrightness(255);
|
|
|
|
let img = board().ledMatrixState.image;
|
|
|
|
img.clear();
|
|
|
|
img.set(0, 4, 255);
|
|
|
|
img.set(1, 3, 255);
|
|
|
|
img.set(2, 3, 255);
|
|
|
|
img.set(3, 3, 255);
|
|
|
|
img.set(4, 4, 255);
|
|
|
|
img.set(0, 0, 255);
|
|
|
|
img.set(1, 0, 255);
|
|
|
|
img.set(0, 1, 255);
|
|
|
|
img.set(1, 1, 255);
|
|
|
|
img.set(3, 0, 255);
|
|
|
|
img.set(4, 0, 255);
|
|
|
|
img.set(3, 1, 255);
|
|
|
|
img.set(4, 1, 255);
|
|
|
|
runtime.updateDisplay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pxsim.initCurrentRuntime) {
|
|
|
|
pxsim.initCurrentRuntime = initRuntimeWithDalBoard;
|
2016-09-13 06:29:55 +02:00
|
|
|
}
|
2016-09-10 00:01:29 +02:00
|
|
|
|
|
|
|
export function board() {
|
|
|
|
return runtime.board as DalBoard;
|
2016-09-13 06:29:55 +02:00
|
|
|
}
|
2016-08-30 20:55:00 +02:00
|
|
|
}
|