190 lines
5.8 KiB
TypeScript
190 lines
5.8 KiB
TypeScript
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
|
|
/// <reference path="../node_modules/pxt-core/localtypings/pxtarget.d.ts"/>
|
|
/// <reference path="../built/common-sim.d.ts"/>
|
|
|
|
namespace pxsim {
|
|
export enum CPlayPinName {
|
|
A0,
|
|
A1,
|
|
A2,
|
|
A3,
|
|
A4,
|
|
A5,
|
|
A6,
|
|
A7,
|
|
A8,
|
|
A9,
|
|
D4,
|
|
D5,
|
|
D6,
|
|
D7,
|
|
D8,
|
|
D13
|
|
}
|
|
|
|
export class EV3Board extends CoreBoard {
|
|
// state & update logic for component services
|
|
// neopixelState: CommonNeoPixelState;
|
|
buttonState: EV3ButtonState;
|
|
slideSwitchState: SlideSwitchState;
|
|
lightSensorState: AnalogSensorState;
|
|
thermometerState: AnalogSensorState;
|
|
thermometerUnitState: number;
|
|
microphoneState: AnalogSensorState;
|
|
edgeConnectorState: EdgeConnectorState;
|
|
capacitiveSensorState: CapacitiveSensorState;
|
|
accelerometerState: AccelerometerState;
|
|
touchButtonState: TouchButtonState;
|
|
irState: InfraredState;
|
|
|
|
view: SVGSVGElement;
|
|
|
|
outputState: EV3OutputState;
|
|
analogState: EV3AnalogState;
|
|
uartState: EV3UArtState;
|
|
motorState: EV3MotorState;
|
|
screenState: EV3ScreenState;
|
|
audioState: AudioState;
|
|
|
|
inputNodes: SensorNode[] = [];
|
|
brickNode: BrickNode;
|
|
outputNodes: MotorNode[] = [];
|
|
|
|
private motorMap: pxt.Map<number> = {
|
|
0x01: 0,
|
|
0x02: 1,
|
|
0x04: 2,
|
|
0x08: 3
|
|
}
|
|
|
|
constructor() {
|
|
super()
|
|
|
|
this.bus.setNotify(DAL.DEVICE_ID_NOTIFY, DAL.DEVICE_ID_NOTIFY_ONE);
|
|
|
|
this.brickNode = new BrickNode();
|
|
|
|
this.outputState = new EV3OutputState();
|
|
this.analogState = new EV3AnalogState();
|
|
this.uartState = new EV3UArtState();
|
|
this.motorState = new EV3MotorState();
|
|
this.screenState = new EV3ScreenState();
|
|
this.audioState = new AudioState();
|
|
}
|
|
|
|
receiveMessage(msg: SimulatorMessage) {
|
|
if (!runtime || runtime.dead) return;
|
|
|
|
switch (msg.type || "") {
|
|
case "eventbus": {
|
|
let ev = <SimulatorEventBusMessage>msg;
|
|
this.bus.queue(ev.id, ev.eventid, ev.value);
|
|
break;
|
|
}
|
|
case "serial": {
|
|
let data = (<SimulatorSerialMessage>msg).data || "";
|
|
// TODO
|
|
break;
|
|
}
|
|
case "irpacket": {
|
|
let ev = <SimulatorInfraredPacketMessage>msg;
|
|
this.irState.receive(new RefBuffer(ev.packet));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
initAsync(msg: SimulatorRunMessage): Promise<void> {
|
|
super.initAsync(msg);
|
|
|
|
const options = (msg.options || {}) as pxt.RuntimeOptions;
|
|
|
|
const boardDef = msg.boardDefinition;
|
|
const cmpsList = msg.parts;
|
|
const cmpDefs = msg.partDefinitions || {};
|
|
const fnArgs = msg.fnArgs;
|
|
|
|
const opts: visuals.BoardHostOpts = {
|
|
state: this,
|
|
boardDef: boardDef,
|
|
partsList: cmpsList,
|
|
partDefs: cmpDefs,
|
|
fnArgs: fnArgs,
|
|
maxWidth: "100%",
|
|
maxHeight: "100%",
|
|
};
|
|
const viewHost = new visuals.BoardHost(pxsim.visuals.mkBoardView({
|
|
visual: boardDef.visual
|
|
}), opts);
|
|
|
|
document.body.innerHTML = ""; // clear children
|
|
document.body.appendChild(this.view = viewHost.getView() as SVGSVGElement);
|
|
|
|
return Promise.resolve();
|
|
}
|
|
|
|
screenshot(): string {
|
|
return svg.toDataUri(new XMLSerializer().serializeToString(this.view));
|
|
}
|
|
|
|
//defaultNeopixelPin() {
|
|
// return this.edgeConnectorState.getPin(CPlayPinName.D8);
|
|
//}
|
|
|
|
getDefaultPitchPin() {
|
|
return this.edgeConnectorState.getPin(CPlayPinName.D6);
|
|
}
|
|
|
|
getBrickNode() {
|
|
return this.brickNode;
|
|
}
|
|
|
|
getMotor(port: number, large?: boolean): MotorNode[] {
|
|
if (port == 0xFF) return this.getMotors(); // Return all motors
|
|
const motorPort = this.motorMap[port];
|
|
if (this.outputNodes[motorPort] == undefined) {
|
|
this.outputNodes[motorPort] = large ?
|
|
new LargeMotorNode(motorPort) : new MediumMotorNode(motorPort);
|
|
}
|
|
return [this.outputNodes[motorPort]];
|
|
}
|
|
|
|
getMotors() {
|
|
return this.outputNodes;
|
|
}
|
|
|
|
getSensor(port: number, type: number): SensorNode {
|
|
if (this.inputNodes[port] == undefined) {
|
|
switch (type) {
|
|
case DAL.DEVICE_TYPE_GYRO: this.inputNodes[port] = new GyroSensorNode(port); break;
|
|
case DAL.DEVICE_TYPE_COLOR: this.inputNodes[port] = new ColorSensorNode(port); break;
|
|
case DAL.DEVICE_TYPE_TOUCH: this.inputNodes[port] = new TouchSensorNode(port); break;
|
|
case DAL.DEVICE_TYPE_ULTRASONIC: this.inputNodes[port] = new UltrasonicSensorNode(port); break;
|
|
}
|
|
}
|
|
return this.inputNodes[port];
|
|
}
|
|
|
|
getInputNodes() {
|
|
return this.inputNodes;
|
|
}
|
|
}
|
|
|
|
export function initRuntimeWithDalBoard() {
|
|
U.assert(!runtime.board);
|
|
let b = new EV3Board();
|
|
runtime.board = b;
|
|
runtime.postError = (e) => {
|
|
// TODO
|
|
runtime.updateDisplay();
|
|
}
|
|
}
|
|
|
|
export function ev3board(): EV3Board {
|
|
return runtime.board as EV3Board;
|
|
}
|
|
|
|
if (!pxsim.initCurrentRuntime) {
|
|
pxsim.initCurrentRuntime = initRuntimeWithDalBoard;
|
|
}
|
|
} |