Merge pull request from evhan55/fixes/hardware-extensions

Hardware extensions bug fixes
This commit is contained in:
Evelyn Eastmond 2018-09-25 20:42:30 -04:00 committed by GitHub
commit 4583cb821b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 47 deletions
src/extensions
scratch3_ev3
scratch3_microbit
scratch3_wedo2

View file

@ -158,7 +158,7 @@ class EV3Motor {
* @type {number}
* @private
*/
this._power = 100;
this._power = 50;
/**
* This motor's current position, in the range [0,360].
@ -231,14 +231,10 @@ class EV3Motor {
}
/**
* @return {int} - this motor's current position, in the range [0,360].
* @return {int} - this motor's current position, in the range [-inf,inf].
*/
get position () {
let value = this._position;
value = value % 360;
value = value < 0 ? value * -1 : value;
return value;
return this._position;
}
/**
@ -688,17 +684,18 @@ class EV3 {
// GET DEVICE LIST
byteCommands[0] = Ev3Opcode.OPINPUT_DEVICE_LIST;
byteCommands[1] = Ev3Value.NUM8; // 1 byte to follow
byteCommands[2] = 33; // 0x21 ARRAY // TODO: ????
byteCommands[3] = 96; // 0x60 CHANGED // TODO: ????
byteCommands[4] = 225; // 0xE1 size of global var - 1 byte to follow // TODO: ????
byteCommands[5] = 32; // 0x20 global var index "0" 0b00100000 // TODO: ????
byteCommands[2] = 33; // 0x21 ARRAY // TODO: document
byteCommands[3] = 96; // 0x60 CHANGED // TODO: document
byteCommands[4] = 225; // 0xE1 size of global var - 1 byte to follow // TODO: document
byteCommands[5] = 32; // 0x20 global var index "0" 0b00100000 // TODO: document
// Command and payload lengths
allocation = 33;
// Clear sensor data // TODO: is this enough?
this._updateDevices = true;
// TODO: need to clar sensor data?
} else {
// GET SENSOR VALUES FOR CONNECTED SENSORS
let index = 0;
@ -711,8 +708,8 @@ class EV3 {
byteCommands[index + 2] = i; // PORT
byteCommands[index + 3] = Ev3Value.DO_NOT_CHANGE_TYPE;
byteCommands[index + 4] = Ev3Mode[this._sensorPorts[i]];
byteCommands[index + 5] = 225; // 0xE1 one byte to follow // TODO: ????
byteCommands[index + 6] = sensorCount * 4; // global index // TODO: ????
byteCommands[index + 5] = 225; // 0xE1 one byte to follow // TODO: document
byteCommands[index + 6] = sensorCount * 4; // global index // TODO: document
index += 7;
}
sensorCount++;
@ -725,9 +722,9 @@ class EV3 {
for (let i = 0; i < 4; i++) {
byteCommands[index + 0] = Ev3Opcode.OPOUTPUT_GET_COUNT;
byteCommands[index + 1] = Ev3Value.LAYER;
byteCommands[index + 2] = i; // port
byteCommands[index + 3] = 225; // 0xE1 byte following
byteCommands[index + 4] = sensorCount * 4; // global index
byteCommands[index + 2] = i; // PORT TODO: explain incorrect documentation as 'Output bit field'
byteCommands[index + 3] = 225; // 0xE1 byte following TODO: document
byteCommands[index + 4] = sensorCount * 4; // global index TODO: document
index += 5;
sensorCount++;
}
@ -976,7 +973,7 @@ class Scratch3Ev3Blocks {
},
POWER: {
type: ArgumentType.NUMBER,
defaultValue: 50
defaultValue: 100
}
}
},
@ -1161,8 +1158,12 @@ class Scratch3Ev3Blocks {
}
const motor = this._peripheral.motor(port);
let position = 0;
if (motor) {
position = MathUtil.wrapClamp(motor.position, 0, 360);
}
return motor ? motor.position : 0;
return position;
}
whenButtonPressed (args) {

View file

@ -756,11 +756,11 @@ class Scratch3MicroBitBlocks {
*/
isButtonPressed (args) {
if (args.BTN === 'any') {
return this._peripheral.buttonA | this._peripheral.buttonB;
return (this._peripheral.buttonA | this._peripheral.buttonB) !== 0;
} else if (args.BTN === 'A') {
return this._peripheral.buttonA;
return this._peripheral.buttonA !== 0;
} else if (args.BTN === 'B') {
return this._peripheral.buttonB;
return this._peripheral.buttonB !== 0;
}
return false;
}

View file

@ -333,6 +333,8 @@ class WeDo2Motor {
const timeoutID = setTimeout(() => {
if (this._pendingTimeoutId === timeoutID) {
this._pendingTimeoutId = null;
this._pendingTimeoutStartTime = null;
this._pendingTimeoutDelay = null;
}
callback();
}, delay);
@ -544,10 +546,8 @@ class WeDo2 {
*/
stopAll () {
if (!this.isConnected()) return;
this.stopTone()
.then(() => { // TODO: Promise?
this.stopAllMotors();
});
this.stopTone();
this.stopAllMotors();
}
/**
@ -681,19 +681,13 @@ class WeDo2 {
* @private
*/
_onConnect () {
// set LED input mode to RGB
this.setLEDMode()
.then(() => { // TODO: Promise?
// set LED to blue
this.setLED(0x0000FF);
})
.then(() => { // TODO: Promise?
this._ble.startNotifications(
BLEService.DEVICE_SERVICE,
BLECharacteristic.ATTACHED_IO,
this._onMessage
);
});
this.setLEDMode();
this.setLED(0x0000FF);
this._ble.startNotifications(
BLEService.DEVICE_SERVICE,
BLECharacteristic.ATTACHED_IO,
this._onMessage
);
}
/**
@ -767,14 +761,12 @@ class WeDo2 {
true
);
this.send(BLECharacteristic.INPUT_COMMAND, cmd)
.then(() => { // TODO: Promise?
this._ble.startNotifications(
BLEService.IO_SERVICE,
BLECharacteristic.INPUT_VALUES,
this._onMessage
);
});
this.send(BLECharacteristic.INPUT_COMMAND, cmd);
this._ble.startNotifications(
BLEService.IO_SERVICE,
BLECharacteristic.INPUT_VALUES,
this._onMessage
);
}
}