mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
- 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:
parent
5f6c8b1efd
commit
bfb61c0df4
1 changed files with 29 additions and 52 deletions
|
@ -56,8 +56,6 @@ const BoostIO = {
|
|||
MOTOR_SYSTEM: 0x02,
|
||||
BUTTON: 0x05,
|
||||
LIGHT: 0x08,
|
||||
LIGHT1: 0x09,
|
||||
LIGHT2: 0x0A,
|
||||
VOLTAGE: 0x14,
|
||||
CURRENT: 0x15,
|
||||
PIEZO: 0x16,
|
||||
|
@ -75,10 +73,10 @@ const BoostIO = {
|
|||
* @readonly
|
||||
* @enum {number}
|
||||
*/
|
||||
const BoostOutputCommandFeedback = {
|
||||
BUFFER_EMPTY_COMMAND_IN_PROGRESS: 0x01,
|
||||
BUFFER_EMPTY_COMMAND_COMPLETED: 0x02,
|
||||
CURRENT_COMMAND_DISCARDED: 0x04,
|
||||
const BoostPortFeedback = {
|
||||
IN_PROGRESS: 0x01,
|
||||
COMPLETED: 0x02,
|
||||
DISCARDED: 0x04,
|
||||
IDLE: 0x08,
|
||||
BUSY_OR_FULL: 0x10
|
||||
};
|
||||
|
@ -130,7 +128,7 @@ const BoostMessage = {
|
|||
PORT_INPUT_FORMAT: 0x47,
|
||||
PORT_INPUT_FORMAT_COMBINED: 0x48,
|
||||
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)
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param {number} number - a number
|
||||
|
@ -301,7 +290,7 @@ class BoostMotor {
|
|||
* @type {boolean}
|
||||
* @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
|
||||
|
@ -325,13 +314,6 @@ class BoostMotor {
|
|||
*/
|
||||
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.
|
||||
* @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.
|
||||
*/
|
||||
get isOn () {
|
||||
if (this._status & (BoostOutputCommandFeedback.BUSY_OR_FULL ^
|
||||
BoostOutputCommandFeedback.BUFFER_EMPTY_COMMAND_IN_PROGRESS)) {
|
||||
if (this._status & (BoostPortFeedback.BUSY_OR_FULL ^ BoostPortFeedback.IN_PROGRESS)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -608,12 +589,6 @@ class Boost {
|
|||
color: BoostColor.NONE
|
||||
};
|
||||
|
||||
/*
|
||||
** TODO: Clean up
|
||||
*/
|
||||
|
||||
this._led = 50;
|
||||
|
||||
/**
|
||||
* The Bluetooth connection socket for reading/writing peripheral data.
|
||||
* @type {BLE}
|
||||
|
@ -951,28 +926,21 @@ class Boost {
|
|||
}
|
||||
break;
|
||||
}
|
||||
case BoostMessage.PORT_OUTPUT_COMMAND_FEEDBACK: {
|
||||
case BoostMessage.PORT_FEEDBACK: {
|
||||
// TODO: Handle messages that contain feedback from more than one port.
|
||||
const feedback = data[4];
|
||||
if (this.motor(portID)) {
|
||||
this.motor(portID)._status = feedback;
|
||||
}
|
||||
switch (feedback) {
|
||||
case BoostOutputCommandFeedback.BUFFER_EMPTY_COMMAND_COMPLETED ^ BoostOutputCommandFeedback.IDLE: {
|
||||
const motor = this.motor(portID);
|
||||
if (motor && motor.pendingPromiseFunction) {
|
||||
const motor = this.motor(portID);
|
||||
if (motor) {
|
||||
motor._status = feedback;
|
||||
if (feedback === (BoostPortFeedback.COMPLETED ^ BoostPortFeedback.IDLE) &&
|
||||
motor.pendingPromiseFunction) {
|
||||
motor.pendingPromiseFunction();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BoostOutputCommandFeedback.BUFFER_EMPTY_COMMAND_IN_PROGRESS:
|
||||
default:
|
||||
log.warn(`Feedback from ${portID}: ${feedback}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BoostMessage.ERROR:
|
||||
log.warn(`Error in BLE message: ${buf2hex(data)}`);
|
||||
log.warn(`Error reported by hub: ${data}`);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
@ -1645,15 +1613,24 @@ class Scratch3BoostBlocks {
|
|||
// TODO: Clamps to 100 rotations. Consider changing.
|
||||
const sign = Math.sign(degrees);
|
||||
degrees = Math.abs(MathUtil.clamp(degrees, -360000, 360000));
|
||||
return new Promise(resolve => {
|
||||
this._forEachMotor(args.MOTOR_ID, motorIndex => {
|
||||
const motor = this._peripheral.motor(motorIndex);
|
||||
if (motor) {
|
||||
|
||||
const motors = [];
|
||||
this._forEachMotor(args.MOTOR_ID, motorIndex => {
|
||||
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.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(() => {});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue