Handle the case where multiple motors or multiple sensors are connected to the same port. (#181)
This commit is contained in:
		
				
					committed by
					
						
						Peli de Halleux
					
				
			
			
				
	
			
			
			
						parent
						
							1e460eef9e
						
					
				
				
					commit
					de91dc6ab7
				
			@@ -18,7 +18,7 @@ namespace pxsim {
 | 
			
		||||
        brickNode: BrickNode;
 | 
			
		||||
        outputNodes: MotorNode[] = [];
 | 
			
		||||
 | 
			
		||||
        private motorMap: pxt.Map<number> = {
 | 
			
		||||
        public motorMap: pxt.Map<number> = {
 | 
			
		||||
            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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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}`);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user