mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 23:12:24 -05:00
Moved Scratch3BoostBlocks-functions around so that they are properly grouped, i.e. motors, tilt, color, etc
This commit is contained in:
parent
256758122d
commit
601c9d230f
1 changed files with 107 additions and 120 deletions
|
@ -943,7 +943,6 @@ class Boost {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case BoostMessage.PORT_FEEDBACK: {
|
case BoostMessage.PORT_FEEDBACK: {
|
||||||
// TODO: If extension will receive feedback from multiple ports simultaneously, this case will need to handle it.
|
|
||||||
const feedback = data[4];
|
const feedback = data[4];
|
||||||
const motor = this.motor(portID);
|
const motor = this.motor(portID);
|
||||||
if (motor) {
|
if (motor) {
|
||||||
|
@ -1758,96 +1757,6 @@ class Scratch3BoostBlocks {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the LED's hue.
|
|
||||||
* @param {object} args - the block's arguments.
|
|
||||||
* @property {number} HUE - the hue to set, in the range [0,100].
|
|
||||||
* @return {Promise} - a Promise that resolves after some delay.
|
|
||||||
*/
|
|
||||||
setLightHue (args) {
|
|
||||||
// Convert from [0,100] to [0,360]
|
|
||||||
let inputHue = Cast.toNumber(args.HUE);
|
|
||||||
inputHue = MathUtil.wrapClamp(inputHue, 0, 100);
|
|
||||||
const hue = inputHue * 360 / 100;
|
|
||||||
|
|
||||||
const rgbObject = color.hsvToRgb({h: hue, s: 1, v: 1});
|
|
||||||
|
|
||||||
const rgbDecimal = color.rgbToDecimal(rgbObject);
|
|
||||||
|
|
||||||
this._peripheral._led = inputHue;
|
|
||||||
this._peripheral.setLED(rgbDecimal);
|
|
||||||
|
|
||||||
return new Promise(resolve => {
|
|
||||||
window.setTimeout(() => {
|
|
||||||
resolve();
|
|
||||||
}, BoostBLE.sendInterval);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Change the LED's hue by a give number.
|
|
||||||
* @param {object} args - the block's arguments.
|
|
||||||
* @property {number} HUE - the hue to set, in the range [0,100].
|
|
||||||
*/
|
|
||||||
changeLightHueBy (args) {
|
|
||||||
// TODO: Clean up this block and its opcode
|
|
||||||
const n = {};
|
|
||||||
n.HUE = Cast.toNumber(args.HUE) + this._peripheral._led;
|
|
||||||
this.setLightHue(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test whether the tilt sensor is currently tilted.
|
|
||||||
* @param {object} args - the block's arguments.
|
|
||||||
* @property {TiltDirection} TILT_DIRECTION_ANY - the tilt direction to test (up, down, left, right, or any).
|
|
||||||
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
|
|
||||||
*/
|
|
||||||
whenTilted (args) {
|
|
||||||
return this._isTilted(args.TILT_DIRECTION_ANY);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test whether the tilt sensor is currently tilted.
|
|
||||||
* @param {object} args - the block's arguments.
|
|
||||||
* @property {Color} COLOR - the color to test.
|
|
||||||
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
|
|
||||||
*/
|
|
||||||
whenColor (args) {
|
|
||||||
return this._isColor(args.COLOR);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return {number} - the vision sensor's color value. Indexed LEGO brick colors.
|
|
||||||
*/
|
|
||||||
getColor () {
|
|
||||||
// To get a string representation, lookup the key of the BoostColor-enum value
|
|
||||||
return Object.keys(BoostColor).find(key => BoostColor[key] === this._peripheral.color)
|
|
||||||
.toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test whether the vision sensor is detecting a certain color.
|
|
||||||
* @param {number} args - the color to test.
|
|
||||||
* @return {boolean} - true when the color sensor senses the specified color.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
_isColor (args) {
|
|
||||||
switch (args) {
|
|
||||||
case BoostColorLabel.ANY:
|
|
||||||
if (Object.keys(BoostColor).find(key => BoostColor[key])
|
|
||||||
.toLowerCase() !== this.getColor()) {
|
|
||||||
if (this.getColor() === this._peripheral.oldColor) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
this._peripheral.oldColor = this.getColor();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return this.getColor() === color;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {object} args - the block's arguments.
|
* @param {object} args - the block's arguments.
|
||||||
* @return {number} - returns the motor's position.
|
* @return {number} - returns the motor's position.
|
||||||
|
@ -1878,6 +1787,53 @@ class Scratch3BoostBlocks {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call a callback for each motor indexed by the provided motor ID.
|
||||||
|
* @param {MotorID} motorID - the ID specifier.
|
||||||
|
* @param {Function} callback - the function to call with the numeric motor index for each motor.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_forEachMotor (motorID, callback) {
|
||||||
|
let motors;
|
||||||
|
switch (motorID) {
|
||||||
|
case BoostMotorLabel.A:
|
||||||
|
motors = [BoostPort.A];
|
||||||
|
break;
|
||||||
|
case BoostMotorLabel.B:
|
||||||
|
motors = [BoostPort.B];
|
||||||
|
break;
|
||||||
|
case BoostMotorLabel.C:
|
||||||
|
motors = [BoostPort.C];
|
||||||
|
break;
|
||||||
|
case BoostMotorLabel.D:
|
||||||
|
motors = [BoostPort.D];
|
||||||
|
break;
|
||||||
|
case BoostMotorLabel.AB:
|
||||||
|
motors = [BoostPort.A, BoostPort.B];
|
||||||
|
break;
|
||||||
|
case BoostMotorLabel.ALL:
|
||||||
|
motors = [BoostPort.A, BoostPort.B, BoostPort.C, BoostPort.D];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
log.warn(`Invalid motor ID: ${motorID}`);
|
||||||
|
motors = [];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (const index of motors) {
|
||||||
|
callback(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test whether the tilt sensor is currently tilted.
|
||||||
|
* @param {object} args - the block's arguments.
|
||||||
|
* @property {TiltDirection} TILT_DIRECTION_ANY - the tilt direction to test (up, down, left, right, or any).
|
||||||
|
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
|
||||||
|
*/
|
||||||
|
whenTilted (args) {
|
||||||
|
return this._isTilted(args.TILT_DIRECTION_ANY);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test whether the tilt sensor is currently tilted.
|
* Test whether the tilt sensor is currently tilted.
|
||||||
* @param {object} args - the block's arguments.
|
* @param {object} args - the block's arguments.
|
||||||
|
@ -1936,40 +1892,71 @@ class Scratch3BoostBlocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call a callback for each motor indexed by the provided motor ID.
|
* Test whether the tilt sensor is currently tilted.
|
||||||
* @param {MotorID} motorID - the ID specifier.
|
* @param {object} args - the block's arguments.
|
||||||
* @param {Function} callback - the function to call with the numeric motor index for each motor.
|
* @property {Color} COLOR - the color to test.
|
||||||
|
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
|
||||||
|
*/
|
||||||
|
whenColor (args) {
|
||||||
|
return this._isColor(args.COLOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {number} - the vision sensor's color value. Indexed LEGO brick colors.
|
||||||
|
*/
|
||||||
|
getColor () {
|
||||||
|
// To get a string representation, lookup the key of the BoostColor-enum value
|
||||||
|
return Object.keys(BoostColor).find(key => BoostColor[key] === this._peripheral.color)
|
||||||
|
.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test whether the vision sensor is detecting a certain color.
|
||||||
|
* @param {number} args - the color to test.
|
||||||
|
* @return {boolean} - true when the color sensor senses the specified color.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_forEachMotor (motorID, callback) {
|
_isColor (args) {
|
||||||
let motors;
|
switch (args) {
|
||||||
switch (motorID) {
|
case BoostColorLabel.ANY:
|
||||||
case BoostMotorLabel.A:
|
if (Object.keys(BoostColor).find(key => BoostColor[key])
|
||||||
motors = [BoostPort.A];
|
.toLowerCase() !== this.getColor()) {
|
||||||
break;
|
if (this.getColor() === this._peripheral.oldColor) {
|
||||||
case BoostMotorLabel.B:
|
return false;
|
||||||
motors = [BoostPort.B];
|
}
|
||||||
break;
|
this._peripheral.oldColor = this.getColor();
|
||||||
case BoostMotorLabel.C:
|
return true;
|
||||||
motors = [BoostPort.C];
|
}
|
||||||
break;
|
|
||||||
case BoostMotorLabel.D:
|
|
||||||
motors = [BoostPort.D];
|
|
||||||
break;
|
|
||||||
case BoostMotorLabel.AB:
|
|
||||||
motors = [BoostPort.A, BoostPort.B];
|
|
||||||
break;
|
|
||||||
case BoostMotorLabel.ALL:
|
|
||||||
motors = [BoostPort.A, BoostPort.B, BoostPort.C, BoostPort.D];
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
log.warn(`Invalid motor ID: ${motorID}`);
|
return this.getColor() === color;
|
||||||
motors = [];
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
for (const index of motors) {
|
|
||||||
callback(index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the LED's hue.
|
||||||
|
* @param {object} args - the block's arguments.
|
||||||
|
* @property {number} HUE - the hue to set, in the range [0,100].
|
||||||
|
* @return {Promise} - a Promise that resolves after some delay.
|
||||||
|
*/
|
||||||
|
setLightHue (args) {
|
||||||
|
// Convert from [0,100] to [0,360]
|
||||||
|
let inputHue = Cast.toNumber(args.HUE);
|
||||||
|
inputHue = MathUtil.wrapClamp(inputHue, 0, 100);
|
||||||
|
const hue = inputHue * 360 / 100;
|
||||||
|
|
||||||
|
const rgbObject = color.hsvToRgb({h: hue, s: 1, v: 1});
|
||||||
|
|
||||||
|
const rgbDecimal = color.rgbToDecimal(rgbObject);
|
||||||
|
|
||||||
|
this._peripheral._led = inputHue;
|
||||||
|
this._peripheral.setLED(rgbDecimal);
|
||||||
|
|
||||||
|
return new Promise(resolve => {
|
||||||
|
window.setTimeout(() => {
|
||||||
|
resolve();
|
||||||
|
}, BoostBLE.sendInterval);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue