- Removed unused IOs

- Renamed BoostOutputCommandFeedback to BoostPortFeedback and its values for brevity
- Removed buf2hex-function
- Removed BoostMotor._pendingPositionOrigin (unused)
- Removed Boost._led (unused)
- Simplified _onMessage-handling of BoostPortFeedback-messages
- motorOnForRotation() now returns a Promise.all rather than a single promise. This solves  two bugs:
-- when running turn ABCD for 3 rotations without motors connected to CD, the block would finish yielding immediately.
-- when running turn C for X rotations without a motor connected to C, the motor would never finish yielding.
This commit is contained in:
Kevin Andersen 2019-03-20 13:50:24 -04:00
parent 5f6c8b1efd
commit bfb61c0df4

View file

@ -56,8 +56,6 @@ const BoostIO = {
MOTOR_SYSTEM: 0x02, MOTOR_SYSTEM: 0x02,
BUTTON: 0x05, BUTTON: 0x05,
LIGHT: 0x08, LIGHT: 0x08,
LIGHT1: 0x09,
LIGHT2: 0x0A,
VOLTAGE: 0x14, VOLTAGE: 0x14,
CURRENT: 0x15, CURRENT: 0x15,
PIEZO: 0x16, PIEZO: 0x16,
@ -75,10 +73,10 @@ const BoostIO = {
* @readonly * @readonly
* @enum {number} * @enum {number}
*/ */
const BoostOutputCommandFeedback = { const BoostPortFeedback = {
BUFFER_EMPTY_COMMAND_IN_PROGRESS: 0x01, IN_PROGRESS: 0x01,
BUFFER_EMPTY_COMMAND_COMPLETED: 0x02, COMPLETED: 0x02,
CURRENT_COMMAND_DISCARDED: 0x04, DISCARDED: 0x04,
IDLE: 0x08, IDLE: 0x08,
BUSY_OR_FULL: 0x10 BUSY_OR_FULL: 0x10
}; };
@ -130,7 +128,7 @@ const BoostMessage = {
PORT_INPUT_FORMAT: 0x47, PORT_INPUT_FORMAT: 0x47,
PORT_INPUT_FORMAT_COMBINED: 0x48, PORT_INPUT_FORMAT_COMBINED: 0x48,
OUTPUT: 0x81, OUTPUT: 0x81,
PORT_OUTPUT_COMMAND_FEEDBACK: 0x82 PORT_FEEDBACK: 0x82
}; };
/** /**
@ -214,15 +212,6 @@ const BoostMode = {
UNKNOWN: 0 // Anything else will use the default mode (mode 0) UNKNOWN: 0 // Anything else will use the default mode (mode 0)
}; };
/**
* Debug function for converting bytes received to hex
* @param {array} buffer - an array of bytes
* @return {string} - a string of hex values.
*/
const buf2hex = function (buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => (`00${x.toString(16)}`).slice(-2)).join(' ');
};
/** /**
* Helper function for converting a JavaScript number to an INT32-number * Helper function for converting a JavaScript number to an INT32-number
* @param {number} number - a number * @param {number} number - a number
@ -301,7 +290,7 @@ class BoostMotor {
* @type {boolean} * @type {boolean}
* @private * @private
*/ */
this._status = BoostOutputCommandFeedback.IDLE; this._status = BoostPortFeedback.IDLE;
/** /**
* If the motor has been turned on or is actively braking for a specific duration, this is the timeout ID for * If the motor has been turned on or is actively braking for a specific duration, this is the timeout ID for
@ -325,13 +314,6 @@ class BoostMotor {
*/ */
this._pendingTimeoutDelay = null; this._pendingTimeoutDelay = null;
/**
* The origin position of a turn-based command.
* @type {number}
* @private
*/
this._pendingPositionOrigin = null;
/** /**
* The target position of a turn-based command. * The target position of a turn-based command.
* @type {number} * @type {number}
@ -406,8 +388,7 @@ class BoostMotor {
* @return {boolean} - true if this motor is currently moving, false if this motor is off or braking. * @return {boolean} - true if this motor is currently moving, false if this motor is off or braking.
*/ */
get isOn () { get isOn () {
if (this._status & (BoostOutputCommandFeedback.BUSY_OR_FULL ^ if (this._status & (BoostPortFeedback.BUSY_OR_FULL ^ BoostPortFeedback.IN_PROGRESS)) {
BoostOutputCommandFeedback.BUFFER_EMPTY_COMMAND_IN_PROGRESS)) {
return true; return true;
} }
return false; return false;
@ -608,12 +589,6 @@ class Boost {
color: BoostColor.NONE color: BoostColor.NONE
}; };
/*
** TODO: Clean up
*/
this._led = 50;
/** /**
* The Bluetooth connection socket for reading/writing peripheral data. * The Bluetooth connection socket for reading/writing peripheral data.
* @type {BLE} * @type {BLE}
@ -951,28 +926,21 @@ class Boost {
} }
break; break;
} }
case BoostMessage.PORT_OUTPUT_COMMAND_FEEDBACK: { case BoostMessage.PORT_FEEDBACK: {
// TODO: Handle messages that contain feedback from more than one port. // TODO: Handle messages that contain feedback from more than one port.
const feedback = data[4]; const feedback = data[4];
if (this.motor(portID)) { const motor = this.motor(portID);
this.motor(portID)._status = feedback; if (motor) {
} motor._status = feedback;
switch (feedback) { if (feedback === (BoostPortFeedback.COMPLETED ^ BoostPortFeedback.IDLE) &&
case BoostOutputCommandFeedback.BUFFER_EMPTY_COMMAND_COMPLETED ^ BoostOutputCommandFeedback.IDLE: { motor.pendingPromiseFunction) {
const motor = this.motor(portID);
if (motor && motor.pendingPromiseFunction) {
motor.pendingPromiseFunction(); motor.pendingPromiseFunction();
} }
break;
}
case BoostOutputCommandFeedback.BUFFER_EMPTY_COMMAND_IN_PROGRESS:
default:
log.warn(`Feedback from ${portID}: ${feedback}`);
} }
break; break;
} }
case BoostMessage.ERROR: case BoostMessage.ERROR:
log.warn(`Error in BLE message: ${buf2hex(data)}`); log.warn(`Error reported by hub: ${data}`);
break; break;
default: default:
} }
@ -1645,15 +1613,24 @@ class Scratch3BoostBlocks {
// TODO: Clamps to 100 rotations. Consider changing. // TODO: Clamps to 100 rotations. Consider changing.
const sign = Math.sign(degrees); const sign = Math.sign(degrees);
degrees = Math.abs(MathUtil.clamp(degrees, -360000, 360000)); degrees = Math.abs(MathUtil.clamp(degrees, -360000, 360000));
return new Promise(resolve => {
this._forEachMotor(args.MOTOR_ID, motorIndex => { const motors = [];
const motor = this._peripheral.motor(motorIndex); this._forEachMotor(args.MOTOR_ID, motorIndex => {
if (motor) { motors.push(motorIndex);
});
const promises = motors.map(portID => {
const motor = this._peripheral.motor(portID);
if (motor) {
return new Promise(resolve => {
motor.turnOnForDegrees(degrees, sign); motor.turnOnForDegrees(degrees, sign);
motor.pendingPromiseFunction = resolve; motor.pendingPromiseFunction = resolve;
} });
}); }
return null;
}); });
// To prevent the block from returning a value, an empty function is added to the .then
return Promise.all(promises).then(() => {});
} }
/** /**