From de91dc6ab782ae0dceada5b8dce7a102b868e26e Mon Sep 17 00:00:00 2001 From: Sam El-Husseini <16690124+samelhusseini@users.noreply.github.com> Date: Thu, 4 Jan 2018 21:57:01 -0800 Subject: [PATCH] Handle the case where multiple motors or multiple sensors are connected to the same port. (#181) --- sim/dalboard.ts | 32 +++++++++++++++++++++++++------- sim/state/input.ts | 19 +++++++++++++++---- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/sim/dalboard.ts b/sim/dalboard.ts index 01b13dff..6111f90d 100644 --- a/sim/dalboard.ts +++ b/sim/dalboard.ts @@ -18,7 +18,7 @@ namespace pxsim { brickNode: BrickNode; outputNodes: MotorNode[] = []; - private motorMap: pxt.Map = { + public motorMap: pxt.Map = { 0x01: 0, 0x02: 1, 0x04: 2, @@ -97,27 +97,40 @@ namespace pxsim { return this.brickNode; } - motorUsed(port:number, large: boolean) { - for(let i = 0; i < DAL.NUM_OUTPUTS; ++i) { + motorUsed(port: number, large: boolean) { + for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) { const p = 1 << i; if (port & p) { const motorPort = this.motorMap[p]; if (!this.outputNodes[motorPort]) this.outputNodes[motorPort] = new MotorNode(motorPort, large); - } - } + } + } + } + + hasMotor(port: number) { + for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) { + const p = 1 << i; + if (port & p) { + const motorPort = this.motorMap[p]; + const outputNode = this.outputNodes[motorPort]; + if (outputNode) + return true; + } + } + return false; } getMotor(port: number, large?: boolean): MotorNode[] { const r = []; - for(let i = 0; i < DAL.NUM_OUTPUTS; ++i) { + for (let i = 0; i < DAL.NUM_OUTPUTS; ++i) { const p = 1 << i; if (port & p) { const motorPort = this.motorMap[p]; const outputNode = this.outputNodes[motorPort]; if (outputNode) r.push(outputNode); - } + } } return r; } @@ -126,6 +139,10 @@ namespace pxsim { return this.outputNodes; } + hasSensor(port: number) { + return !!this.inputNodes[port]; + } + getSensor(port: number, type: number): SensorNode { if (!this.inputNodes[port]) { switch (type) { @@ -150,6 +167,7 @@ namespace pxsim { runtime.postError = (e) => { // TODO runtime.updateDisplay(); + console.log('runtime error: ' + e); } } diff --git a/sim/state/input.ts b/sim/state/input.ts index 1f62e95e..a025e34e 100644 --- a/sim/state/input.ts +++ b/sim/state/input.ts @@ -1,10 +1,17 @@ + +import lf = pxsim.localization.lf; + namespace pxsim.motors { export function __motorUsed(port: number, large: boolean) { //console.log("MOTOR INIT " + port); - ev3board().motorUsed(port, large); - runtime.queueDisplayUpdate(); + if (!ev3board().hasMotor(port)) { + ev3board().motorUsed(port, large); + runtime.queueDisplayUpdate(); + } else { + U.userError(`${lf("Multiple motors are connected to Port")} ${String.fromCharCode('A'.charCodeAt(0) + ev3board().motorMap[port])}`); + } } } @@ -12,7 +19,11 @@ namespace pxsim.sensors { export function __sensorUsed(port: number, type: number) { //console.log("SENSOR INIT " + port + ", type: " + type); - const sensor = ev3board().getSensor(port, type); - runtime.queueDisplayUpdate(); + if (!ev3board().hasSensor(port)) { + const sensor = ev3board().getSensor(port, type); + runtime.queueDisplayUpdate(); + } else { + U.userError(`${lf("Multiple sensors are connected to Port")} ${port + 1}`); + } } } \ No newline at end of file