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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user