2017-12-01 10:31:04 -05:00
|
|
|
const ArgumentType = require('../../extension-support/argument-type');
|
|
|
|
const BlockType = require('../../extension-support/block-type');
|
2018-08-14 11:45:40 -04:00
|
|
|
const Cast = require('../../util/cast');
|
2018-08-15 14:21:42 -04:00
|
|
|
const formatMessage = require('format-message');
|
2017-12-01 10:31:04 -05:00
|
|
|
const color = require('../../util/color');
|
|
|
|
const log = require('../../util/log');
|
2018-08-13 17:10:55 -04:00
|
|
|
const BLESession = require('../../io/bleSession');
|
|
|
|
const Base64Util = require('../../util/base64-util');
|
2018-08-14 11:45:40 -04:00
|
|
|
const MathUtil = require('../../util/math-util');
|
2017-05-03 19:39:31 -04:00
|
|
|
|
2017-11-06 10:00:13 -05:00
|
|
|
/**
|
|
|
|
* Icon svg to be displayed at the left edge of each extension block, encoded as a data URI.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2017-11-06 10:10:41 -05:00
|
|
|
// eslint-disable-next-line max-len
|
2018-08-14 13:51:17 -04:00
|
|
|
const iconURI = 'data:image/png;base64,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';
|
2017-11-06 10:00:13 -05:00
|
|
|
|
2018-08-13 17:10:55 -04:00
|
|
|
const UUID = {
|
|
|
|
DEVICE_SERVICE: '00001523-1212-efde-1523-785feabcd123',
|
|
|
|
IO_SERVICE: '00004f0e-1212-efde-1523-785feabcd123',
|
|
|
|
ATTACHED_IO: '00001527-1212-efde-1523-785feabcd123',
|
|
|
|
INPUT_VALUES: '00001560-1212-efde-1523-785feabcd123',
|
|
|
|
INPUT_COMMAND: '00001563-1212-efde-1523-785feabcd123',
|
|
|
|
OUTPUT_COMMAND: '00001565-1212-efde-1523-785feabcd123'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum for WeDo2 sensor and output types.
|
|
|
|
* @readonly
|
|
|
|
* @enum {number}
|
|
|
|
*/
|
|
|
|
const WeDo2Types = {
|
|
|
|
MOTOR: 1,
|
|
|
|
PIEZO: 22,
|
|
|
|
LED: 23,
|
|
|
|
TILT: 34,
|
|
|
|
DISTANCE: 35
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum for connection/port ids assigned to internal WeDo2 output devices.
|
|
|
|
* @readonly
|
|
|
|
* @enum {number}
|
|
|
|
*/
|
|
|
|
const WeDo2ConnectIDs = {
|
|
|
|
LED: 6,
|
|
|
|
PIEZO: 5
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum for ids for various output commands on the WeDo2.
|
|
|
|
* @readonly
|
|
|
|
* @enum {number}
|
|
|
|
*/
|
|
|
|
const WeDo2Commands = {
|
|
|
|
MOTOR_POWER: 1,
|
|
|
|
PLAY_TONE: 2,
|
|
|
|
STOP_TONE: 3,
|
|
|
|
WRITE_RGB: 4,
|
|
|
|
SET_VOLUME: 255
|
|
|
|
};
|
|
|
|
|
2018-08-14 13:44:05 -04:00
|
|
|
/**
|
|
|
|
* Enum for modes for input sensors on the WeDo2.
|
|
|
|
* @enum {number}
|
|
|
|
*/
|
|
|
|
const WeDo2Modes = {
|
|
|
|
TILT: 0, // angle
|
|
|
|
DISTANCE: 0 // detect
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum for units for input sensors on the WeDo2.
|
|
|
|
* @enum {number}
|
|
|
|
*/
|
|
|
|
const WeDo2Units = {
|
|
|
|
TILT: 0, // raw
|
|
|
|
DISTANCE: 1 // percent
|
|
|
|
};
|
|
|
|
|
2017-05-03 19:39:31 -04:00
|
|
|
/**
|
|
|
|
* Manage power, direction, and timers for one WeDo 2.0 motor.
|
|
|
|
*/
|
|
|
|
class WeDo2Motor {
|
|
|
|
/**
|
|
|
|
* Construct a WeDo2Motor instance.
|
|
|
|
* @param {WeDo2} parent - the WeDo 2.0 device which owns this motor.
|
|
|
|
* @param {int} index - the zero-based index of this motor on its parent device.
|
|
|
|
*/
|
|
|
|
constructor (parent, index) {
|
|
|
|
/**
|
|
|
|
* The WeDo 2.0 device which owns this motor.
|
|
|
|
* @type {WeDo2}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._parent = parent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The zero-based index of this motor on its parent device.
|
|
|
|
* @type {int}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._index = index;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This motor's current direction: 1 for "this way" or -1 for "that way"
|
|
|
|
* @type {number}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._direction = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This motor's current power level, in the range [0,100].
|
|
|
|
* @type {number}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._power = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this motor currently moving?
|
|
|
|
* @type {boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._isOn = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the motor has been turned on or is actively braking for a specific duration, this is the timeout ID for
|
|
|
|
* the end-of-action handler. Cancel this when changing plans.
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._pendingTimeoutId = null;
|
|
|
|
|
2018-08-14 12:35:59 -04:00
|
|
|
/**
|
|
|
|
* The starting time for the pending timeout.
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._pendingTimeoutStartTime = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The delay/duration of the pending timeout.
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._pendingTimeoutDelay = null;
|
|
|
|
|
2017-05-03 19:39:31 -04:00
|
|
|
this.startBraking = this.startBraking.bind(this);
|
|
|
|
this.setMotorOff = this.setMotorOff.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number} - the duration of active braking after a call to startBraking(). Afterward, turn the motor off.
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
static get BRAKE_TIME_MS () {
|
|
|
|
return 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {int} - this motor's current direction: 1 for "this way" or -1 for "that way"
|
|
|
|
*/
|
|
|
|
get direction () {
|
|
|
|
return this._direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {int} value - this motor's new direction: 1 for "this way" or -1 for "that way"
|
|
|
|
*/
|
|
|
|
set direction (value) {
|
|
|
|
if (value < 0) {
|
|
|
|
this._direction = -1;
|
|
|
|
} else {
|
|
|
|
this._direction = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {int} - this motor's current power level, in the range [0,100].
|
|
|
|
*/
|
|
|
|
get power () {
|
|
|
|
return this._power;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {int} value - this motor's new power level, in the range [0,100].
|
|
|
|
*/
|
|
|
|
set power (value) {
|
|
|
|
this._power = Math.max(0, Math.min(value, 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean} - true if this motor is currently moving, false if this motor is off or braking.
|
|
|
|
*/
|
|
|
|
get isOn () {
|
|
|
|
return this._isOn;
|
|
|
|
}
|
|
|
|
|
2018-08-14 12:35:59 -04:00
|
|
|
/**
|
|
|
|
* @return {boolean} - time, in milliseconds, of when the pending timeout began.
|
|
|
|
*/
|
|
|
|
get pendingTimeoutStartTime () {
|
|
|
|
return this._pendingTimeoutStartTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean} - delay, in milliseconds, of the pending timeout.
|
|
|
|
*/
|
|
|
|
get pendingTimeoutDelay () {
|
|
|
|
return this._pendingTimeoutDelay;
|
|
|
|
}
|
|
|
|
|
2017-05-03 19:39:31 -04:00
|
|
|
/**
|
|
|
|
* Turn this motor on indefinitely.
|
|
|
|
*/
|
|
|
|
setMotorOn () {
|
2018-08-13 17:10:55 -04:00
|
|
|
const cmd = new Uint8Array(4);
|
|
|
|
cmd[0] = this._index + 1; // connect id
|
|
|
|
cmd[1] = WeDo2Commands.MOTOR_POWER; // command
|
|
|
|
cmd[2] = 1; // 1 byte to follow
|
|
|
|
cmd[3] = this._power * this._direction; // power in range 0-100
|
|
|
|
|
|
|
|
this._parent._send(UUID.OUTPUT_COMMAND, Base64Util.uint8ArrayToBase64(cmd));
|
|
|
|
|
2017-05-03 19:39:31 -04:00
|
|
|
this._isOn = true;
|
|
|
|
this._clearTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn this motor on for a specific duration.
|
|
|
|
* @param {number} milliseconds - run the motor for this long.
|
|
|
|
*/
|
|
|
|
setMotorOnFor (milliseconds) {
|
|
|
|
milliseconds = Math.max(0, milliseconds);
|
|
|
|
this.setMotorOn();
|
|
|
|
this._setNewTimeout(this.startBraking, milliseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start active braking on this motor. After a short time, the motor will turn off.
|
|
|
|
*/
|
|
|
|
startBraking () {
|
2018-08-13 17:10:55 -04:00
|
|
|
const cmd = new Uint8Array(4);
|
|
|
|
cmd[0] = this._index + 1; // connect id
|
|
|
|
cmd[1] = WeDo2Commands.MOTOR_POWER; // command
|
|
|
|
cmd[2] = 1; // 1 byte to follow
|
|
|
|
cmd[3] = 127; // power in range 0-100
|
|
|
|
|
|
|
|
this._parent._send(UUID.OUTPUT_COMMAND, Base64Util.uint8ArrayToBase64(cmd));
|
|
|
|
|
2017-05-03 19:39:31 -04:00
|
|
|
this._isOn = false;
|
|
|
|
this._setNewTimeout(this.setMotorOff, WeDo2Motor.BRAKE_TIME_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn this motor off.
|
|
|
|
*/
|
|
|
|
setMotorOff () {
|
2018-08-13 17:10:55 -04:00
|
|
|
const cmd = new Uint8Array(4);
|
|
|
|
cmd[0] = this._index + 1; // connect id
|
|
|
|
cmd[1] = WeDo2Commands.MOTOR_POWER; // command
|
|
|
|
cmd[2] = 1; // 1 byte to follow
|
|
|
|
cmd[3] = 0; // power in range 0-100
|
|
|
|
|
|
|
|
this._parent._send(UUID.OUTPUT_COMMAND, Base64Util.uint8ArrayToBase64(cmd));
|
|
|
|
|
2017-05-03 19:39:31 -04:00
|
|
|
this._isOn = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the motor action timeout, if any. Safe to call even when there is no pending timeout.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_clearTimeout () {
|
|
|
|
if (this._pendingTimeoutId !== null) {
|
|
|
|
clearTimeout(this._pendingTimeoutId);
|
|
|
|
this._pendingTimeoutId = null;
|
2018-08-14 12:35:59 -04:00
|
|
|
this._pendingTimeoutStartTime = null;
|
|
|
|
this._pendingTimeoutDelay = null;
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a new motor action timeout, after clearing an existing one if necessary.
|
|
|
|
* @param {Function} callback - to be called at the end of the timeout.
|
|
|
|
* @param {int} delay - wait this many milliseconds before calling the callback.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_setNewTimeout (callback, delay) {
|
|
|
|
this._clearTimeout();
|
|
|
|
const timeoutID = setTimeout(() => {
|
|
|
|
if (this._pendingTimeoutId === timeoutID) {
|
|
|
|
this._pendingTimeoutId = null;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}, delay);
|
|
|
|
this._pendingTimeoutId = timeoutID;
|
2018-08-14 12:35:59 -04:00
|
|
|
this._pendingTimeoutStartTime = Date.now();
|
|
|
|
this._pendingTimeoutDelay = delay;
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-13 17:10:55 -04:00
|
|
|
* Manage communication with a WeDo 2.0 device over a Bluetooth Low Energy client socket.
|
2017-05-03 19:39:31 -04:00
|
|
|
*/
|
|
|
|
class WeDo2 {
|
|
|
|
|
2018-08-13 17:10:55 -04:00
|
|
|
constructor (runtime, extensionId) {
|
2017-05-03 19:39:31 -04:00
|
|
|
|
|
|
|
/**
|
2018-08-13 17:10:55 -04:00
|
|
|
* The Scratch 3.0 runtime used to trigger the green flag button.
|
|
|
|
* @type {Runtime}
|
2017-05-03 19:39:31 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-08-13 17:10:55 -04:00
|
|
|
this._runtime = runtime;
|
|
|
|
this._runtime.on('PROJECT_STOP_ALL', this._stopAll.bind(this));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The device ports that connect to motors and sensors.
|
|
|
|
* @type {string[]}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._ports = ['none', 'none']; // TODO: rename?
|
2017-05-03 19:39:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The motors which this WeDo 2.0 could possibly have.
|
2018-07-31 09:01:37 -04:00
|
|
|
* @type {WeDo2Motor[]}
|
2017-05-03 19:39:31 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-08-13 17:10:55 -04:00
|
|
|
this._motors = [null, null];
|
2017-05-03 19:39:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The most recently received value for each sensor.
|
|
|
|
* @type {Object.<string, number>}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._sensors = {
|
|
|
|
tiltX: 0,
|
|
|
|
tiltY: 0,
|
|
|
|
distance: 0
|
|
|
|
};
|
|
|
|
|
2018-08-13 17:10:55 -04:00
|
|
|
/**
|
|
|
|
* The Bluetooth connection session for reading/writing device data.
|
|
|
|
* @type {BLESession}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._ble = null;
|
|
|
|
this._runtime.registerExtensionDevice(extensionId, this);
|
2017-05-03 19:39:31 -04:00
|
|
|
|
2018-08-13 17:10:55 -04:00
|
|
|
this._onConnect = this._onConnect.bind(this);
|
|
|
|
this._onMessage = this._onMessage.bind(this);
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number} - the latest value received for the tilt sensor's tilt about the X axis.
|
|
|
|
*/
|
|
|
|
get tiltX () {
|
|
|
|
return this._sensors.tiltX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number} - the latest value received for the tilt sensor's tilt about the Y axis.
|
|
|
|
*/
|
|
|
|
get tiltY () {
|
|
|
|
return this._sensors.tiltY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number} - the latest value received from the distance sensor.
|
|
|
|
*/
|
|
|
|
get distance () {
|
2018-08-13 17:10:55 -04:00
|
|
|
return this._sensors.distance;
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access a particular motor on this device.
|
|
|
|
* @param {int} index - the zero-based index of the desired motor.
|
|
|
|
* @return {WeDo2Motor} - the WeDo2Motor instance, if any, at that index.
|
|
|
|
*/
|
|
|
|
motor (index) {
|
|
|
|
return this._motors[index];
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:10:55 -04:00
|
|
|
/**
|
|
|
|
* Stop all the motors that are currently running.
|
|
|
|
*/
|
|
|
|
stopAllMotors () {
|
|
|
|
this._motors.forEach(motor => {
|
|
|
|
if (motor && motor.isOn) {
|
|
|
|
motor.setMotorOff();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-03 19:39:31 -04:00
|
|
|
/**
|
|
|
|
* Set the WeDo 2.0 hub's LED to a specific color.
|
|
|
|
* @param {int} rgb - a 24-bit RGB color in 0xRRGGBB format.
|
2018-08-14 11:52:07 -04:00
|
|
|
* @return {Promise} - a promise of the set led send operation.
|
2017-05-03 19:39:31 -04:00
|
|
|
*/
|
|
|
|
setLED (rgb) {
|
2018-08-13 17:10:55 -04:00
|
|
|
const cmd = new Uint8Array(6);
|
|
|
|
cmd[0] = WeDo2ConnectIDs.LED; // connect id
|
|
|
|
cmd[1] = WeDo2Commands.WRITE_RGB; // command
|
|
|
|
cmd[2] = 3; // 3 bytes to follow
|
|
|
|
cmd[3] = (rgb >> 16) & 0x000000FF;
|
|
|
|
cmd[4] = (rgb >> 8) & 0x000000FF;
|
|
|
|
cmd[5] = (rgb) & 0x000000FF;
|
|
|
|
|
2018-08-14 11:52:07 -04:00
|
|
|
return this._send(UUID.OUTPUT_COMMAND, Base64Util.uint8ArrayToBase64(cmd));
|
2018-08-13 17:10:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch off the LED on the WeDo2.
|
|
|
|
*/
|
|
|
|
stopLED () {
|
|
|
|
const cmd = new Uint8Array(6);
|
|
|
|
cmd[0] = WeDo2ConnectIDs.LED; // connect id
|
|
|
|
cmd[1] = WeDo2Commands.WRITE_RGB; // command
|
|
|
|
cmd[2] = 3; // 3 bytes to follow
|
|
|
|
cmd[3] = 0x000000; // off
|
|
|
|
cmd[4] = 0x000000;
|
|
|
|
cmd[5] = 0x000000;
|
|
|
|
|
|
|
|
this._send(UUID.OUTPUT_COMMAND, Base64Util.uint8ArrayToBase64(cmd));
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Play a tone from the WeDo 2.0 hub for a specific amount of time.
|
|
|
|
* @param {int} tone - the pitch of the tone, in Hz.
|
|
|
|
* @param {int} milliseconds - the duration of the note, in milliseconds.
|
|
|
|
*/
|
|
|
|
playTone (tone, milliseconds) {
|
2018-08-13 17:10:55 -04:00
|
|
|
const cmd = new Uint8Array(7);
|
|
|
|
cmd[0] = WeDo2ConnectIDs.PIEZO; // connect id
|
|
|
|
cmd[1] = WeDo2Commands.PLAY_TONE; // command
|
|
|
|
cmd[2] = 4; // 4 bytes to follow
|
|
|
|
cmd[3] = tone;
|
|
|
|
cmd[4] = tone >> 8;
|
|
|
|
cmd[5] = milliseconds;
|
|
|
|
cmd[6] = milliseconds >> 8;
|
|
|
|
|
|
|
|
this._send(UUID.OUTPUT_COMMAND, Base64Util.uint8ArrayToBase64(cmd));
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the tone playing from the WeDo 2.0 hub, if any.
|
|
|
|
*/
|
|
|
|
stopTone () {
|
2018-08-13 17:10:55 -04:00
|
|
|
const cmd = new Uint8Array(2);
|
|
|
|
cmd[0] = WeDo2ConnectIDs.PIEZO; // connect id
|
|
|
|
cmd[1] = WeDo2Commands.STOP_TONE; // command
|
|
|
|
|
|
|
|
this._send(UUID.OUTPUT_COMMAND, Base64Util.uint8ArrayToBase64(cmd));
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-13 17:10:55 -04:00
|
|
|
* Called by the runtime when user wants to scan for a device.
|
|
|
|
*/
|
|
|
|
// TODO: rename scan?
|
|
|
|
startDeviceScan () {
|
|
|
|
this._ble = new BLESession(this._runtime, {
|
2018-08-21 15:08:58 -04:00
|
|
|
filters: [
|
|
|
|
{services: [UUID.DEVICE_SERVICE, UUID.IO_SERVICE]}
|
|
|
|
]
|
2018-08-13 17:10:55 -04:00
|
|
|
}, this._onConnect);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the runtime when user wants to connect to a certain device.
|
|
|
|
* @param {number} id - the id of the device to connect to.
|
|
|
|
*/
|
|
|
|
// TODO: rename connect?
|
|
|
|
connectDevice (id) {
|
|
|
|
this._ble.connectDevice(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects from the current BLE session.
|
|
|
|
*/
|
|
|
|
// TODO: rename disconnect?
|
|
|
|
disconnectSession () {
|
2018-08-21 11:18:58 -04:00
|
|
|
this._ports = ['none', 'none'];
|
|
|
|
this._motors = [null, null];
|
|
|
|
this._sensors = {
|
|
|
|
tiltX: 0,
|
|
|
|
tiltY: 0,
|
|
|
|
distance: 0
|
|
|
|
};
|
|
|
|
|
2018-08-13 17:10:55 -04:00
|
|
|
this._ble.disconnectSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the runtime to detect whether the device is connected.
|
|
|
|
* @return {boolean} - the connected state.
|
|
|
|
*/
|
|
|
|
// TODO: rename isConnected
|
|
|
|
getPeripheralIsConnected () {
|
|
|
|
let connected = false;
|
|
|
|
if (this._ble) {
|
|
|
|
connected = this._ble.getPeripheralIsConnected();
|
|
|
|
}
|
|
|
|
return connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-14 11:52:07 -04:00
|
|
|
* Sets LED mode and initial color and starts reading data from device after BLE has connected.
|
2017-05-03 19:39:31 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-08-13 17:10:55 -04:00
|
|
|
_onConnect () {
|
|
|
|
// set LED input mode to RGB
|
|
|
|
this._setLEDMode()
|
2018-08-14 11:52:07 -04:00
|
|
|
.then(() => {
|
|
|
|
// set LED to blue
|
|
|
|
this.setLED(0x0000FF);
|
|
|
|
})
|
2018-08-13 17:10:55 -04:00
|
|
|
.then(() => {
|
|
|
|
// register for attached io notifications
|
2018-08-19 22:09:57 -04:00
|
|
|
this._ble.startNotifications(UUID.DEVICE_SERVICE, UUID.ATTACHED_IO, this._onMessage);
|
2018-08-13 17:10:55 -04:00
|
|
|
});
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
2018-08-13 17:10:55 -04:00
|
|
|
|
2018-08-14 13:45:55 -04:00
|
|
|
/**
|
|
|
|
* Write a message to the device BLE session.
|
|
|
|
* @param {number} uuid - the UUID of the characteristic to write to
|
|
|
|
* @param {Uint8Array} message - the message to write.
|
|
|
|
* @return {Promise} - a promise result of the write operation
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_send (uuid, message) {
|
|
|
|
if (!this.getPeripheralIsConnected()) return;
|
|
|
|
return this._ble.write(UUID.IO_SERVICE, uuid, message, 'base64');
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-13 17:10:55 -04:00
|
|
|
* Process the sensor data from the incoming BLE characteristic.
|
|
|
|
* @param {object} base64 - the incoming BLE data.
|
2017-05-03 19:39:31 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-08-13 17:10:55 -04:00
|
|
|
_onMessage (base64) {
|
|
|
|
const data = Base64Util.base64ToUint8Array(base64);
|
|
|
|
// log.info(data);
|
|
|
|
|
2018-08-14 13:44:05 -04:00
|
|
|
/**
|
|
|
|
* If first byte of data is '1' or '2', then either clear the
|
|
|
|
* sensor present in ports 1 or 2 or set their format.
|
|
|
|
*
|
|
|
|
* If first byte of data is anything else, read incoming sensor value.
|
|
|
|
*/
|
|
|
|
switch (data[0]) {
|
|
|
|
case 1:
|
|
|
|
case 2: {
|
2018-08-13 17:10:55 -04:00
|
|
|
const connectID = data[0];
|
2018-08-14 13:44:05 -04:00
|
|
|
if (data[1] === 0) {
|
|
|
|
// clear sensor or motor
|
|
|
|
this._clearPort(connectID);
|
|
|
|
} else {
|
|
|
|
// register sensor or motor
|
|
|
|
this._registerSensorOrMotor(connectID, data[3]);
|
2018-08-13 17:10:55 -04:00
|
|
|
}
|
2018-08-14 13:44:05 -04:00
|
|
|
break;
|
2018-08-13 17:10:55 -04:00
|
|
|
}
|
2018-08-14 13:44:05 -04:00
|
|
|
default: {
|
|
|
|
// read incoming sensor value
|
|
|
|
const connectID = data[1];
|
|
|
|
const type = this._ports[connectID - 1];
|
|
|
|
if (type === WeDo2Types.DISTANCE) {
|
|
|
|
this._sensors.distance = data[2];
|
2018-08-13 17:10:55 -04:00
|
|
|
}
|
|
|
|
if (type === WeDo2Types.TILT) {
|
2018-08-14 13:44:05 -04:00
|
|
|
this._sensors.tiltX = data[2];
|
|
|
|
this._sensors.tiltY = data[3];
|
2018-08-13 17:10:55 -04:00
|
|
|
}
|
2018-08-14 13:44:05 -04:00
|
|
|
break;
|
|
|
|
}
|
2018-08-13 17:10:55 -04:00
|
|
|
}
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-14 13:44:05 -04:00
|
|
|
* Clear the sensor or motor present at port 1 or 2.
|
|
|
|
* @param {number} connectID - the port to clear.
|
2017-05-03 19:39:31 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-08-14 13:44:05 -04:00
|
|
|
_clearPort (connectID) {
|
|
|
|
const type = this._ports[connectID - 1];
|
|
|
|
if (type === WeDo2Types.TILT) {
|
|
|
|
this._sensors.tiltX = this._sensors.tiltY = 0;
|
|
|
|
}
|
|
|
|
if (type === WeDo2Types.DISTANCE) {
|
|
|
|
this._sensors.distance = 0;
|
|
|
|
}
|
|
|
|
this._ports[connectID - 1] = 'none';
|
|
|
|
this._motors[connectID - 1] = null;
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-14 13:44:05 -04:00
|
|
|
* Register a new sensor or motor connected at a port. Store the type of
|
|
|
|
* sensor or motor internally, and then register for notifications on input
|
|
|
|
* values if it is a sensor.
|
|
|
|
* @param {number} connectID - the port to register a sensor or motor on.
|
|
|
|
* @param {number} type - the type ID of the sensor or motor
|
2017-05-03 19:39:31 -04:00
|
|
|
*/
|
2018-08-14 13:44:05 -04:00
|
|
|
_registerSensorOrMotor (connectID, type) {
|
|
|
|
// Record which port is connected to what type of device
|
|
|
|
this._ports[connectID - 1] = type;
|
2018-08-13 17:10:55 -04:00
|
|
|
|
2018-08-14 13:44:05 -04:00
|
|
|
// Register motor
|
|
|
|
if (type === WeDo2Types.MOTOR) {
|
|
|
|
this._motors[connectID - 1] = new WeDo2Motor(this, connectID - 1);
|
|
|
|
} else {
|
|
|
|
// Register tilt or distance sensor
|
|
|
|
const typeString = type === WeDo2Types.DISTANCE ? 'DISTANCE' : 'TILT';
|
|
|
|
const cmd = new Uint8Array(11);
|
|
|
|
cmd[0] = 1; // sensor format
|
|
|
|
cmd[1] = 2; // command type: write
|
|
|
|
cmd[2] = connectID; // connect id
|
|
|
|
cmd[3] = type; // type
|
|
|
|
cmd[4] = WeDo2Modes[typeString]; // mode
|
|
|
|
cmd[5] = 1; // delta interval, 4 bytes, 1 = continuous updates
|
|
|
|
cmd[6] = 0;
|
|
|
|
cmd[7] = 0;
|
|
|
|
cmd[8] = 0;
|
|
|
|
cmd[9] = WeDo2Units[typeString]; // unit
|
|
|
|
cmd[10] = 1; // notifications enabled: true
|
|
|
|
|
|
|
|
this._send(UUID.INPUT_COMMAND, Base64Util.uint8ArrayToBase64(cmd))
|
|
|
|
.then(() => {
|
2018-08-19 22:09:57 -04:00
|
|
|
this._ble.startNotifications(UUID.IO_SERVICE, UUID.INPUT_VALUES, this._onMessage);
|
2018-08-14 13:44:05 -04:00
|
|
|
});
|
|
|
|
}
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-13 17:10:55 -04:00
|
|
|
* Sets the input mode of the LED to RGB.
|
|
|
|
* @return {Promise} - a promise returned by the send operation.
|
2017-05-03 19:39:31 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-08-13 17:10:55 -04:00
|
|
|
_setLEDMode () {
|
|
|
|
const cmd = new Uint8Array(11);
|
|
|
|
cmd[0] = 1; // sensor format
|
|
|
|
cmd[1] = 2; // command type: 2 = write
|
|
|
|
cmd[2] = WeDo2ConnectIDs.LED; // port
|
|
|
|
cmd[3] = WeDo2Types.LED; // type
|
|
|
|
cmd[4] = 1; // mode
|
|
|
|
cmd[5] = 0; // delta interval, 4 bytes
|
|
|
|
cmd[6] = 0;
|
|
|
|
cmd[7] = 0;
|
|
|
|
cmd[8] = 0;
|
|
|
|
cmd[9] = 0; // unit = raw
|
|
|
|
cmd[10] = 0; // notifications enabled: false
|
|
|
|
|
|
|
|
return this._send(UUID.INPUT_COMMAND, Base64Util.uint8ArrayToBase64(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-19 22:09:57 -04:00
|
|
|
* Stop the tone playing and motors on the WeDo 2.0 hub.
|
2018-08-13 17:10:55 -04:00
|
|
|
*/
|
|
|
|
_stopAll () {
|
2018-08-21 11:14:16 -04:00
|
|
|
if (!this.getPeripheralIsConnected()) return;
|
2018-08-13 17:10:55 -04:00
|
|
|
this.stopTone();
|
|
|
|
this.stopAllMotors();
|
2018-08-19 22:09:57 -04:00
|
|
|
// this.stopLED();
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:05:21 -04:00
|
|
|
/**
|
|
|
|
* Enum for motor specification.
|
|
|
|
* @readonly
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
const MotorID = {
|
|
|
|
DEFAULT: 'motor',
|
|
|
|
A: 'motor A',
|
|
|
|
B: 'motor B',
|
|
|
|
ALL: 'all motors'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum for motor direction specification.
|
|
|
|
* @readonly
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
const MotorDirection = {
|
|
|
|
FORWARD: 'this way',
|
|
|
|
BACKWARD: 'that way',
|
|
|
|
REVERSE: 'reverse'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum for tilt sensor direction.
|
|
|
|
* @readonly
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
const TiltDirection = {
|
|
|
|
UP: 'up',
|
|
|
|
DOWN: 'down',
|
|
|
|
LEFT: 'left',
|
|
|
|
RIGHT: 'right',
|
|
|
|
ANY: 'any'
|
|
|
|
};
|
|
|
|
|
2017-05-03 19:39:31 -04:00
|
|
|
/**
|
|
|
|
* Scratch 3.0 blocks to interact with a LEGO WeDo 2.0 device.
|
|
|
|
*/
|
|
|
|
class Scratch3WeDo2Blocks {
|
|
|
|
|
|
|
|
/**
|
2017-10-06 12:36:27 -04:00
|
|
|
* @return {string} - the ID of this extension.
|
2017-05-03 19:39:31 -04:00
|
|
|
*/
|
2017-10-06 12:36:27 -04:00
|
|
|
static get EXTENSION_ID () {
|
2017-05-03 19:39:31 -04:00
|
|
|
return 'wedo2';
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:05:21 -04:00
|
|
|
/**
|
|
|
|
* @return {number} - the tilt sensor counts as "tilted" if its tilt angle meets or exceeds this threshold.
|
|
|
|
*/
|
|
|
|
static get TILT_THRESHOLD () {
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
|
2017-05-03 19:39:31 -04:00
|
|
|
/**
|
|
|
|
* Construct a set of WeDo 2.0 blocks.
|
|
|
|
* @param {Runtime} runtime - the Scratch 3.0 runtime.
|
|
|
|
*/
|
|
|
|
constructor (runtime) {
|
|
|
|
/**
|
|
|
|
* The Scratch 3.0 runtime.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
|
2018-08-13 17:10:55 -04:00
|
|
|
// Create a new WeDo2 device instance
|
|
|
|
this._device = new WeDo2(this.runtime, Scratch3WeDo2Blocks.EXTENSION_ID);
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
2017-10-04 15:16:27 -04:00
|
|
|
/**
|
|
|
|
* @returns {object} metadata for this extension and its blocks.
|
|
|
|
*/
|
|
|
|
getInfo () {
|
|
|
|
return {
|
2017-10-06 12:36:27 -04:00
|
|
|
id: Scratch3WeDo2Blocks.EXTENSION_ID,
|
2017-10-04 15:16:27 -04:00
|
|
|
name: 'WeDo 2.0',
|
2018-08-14 13:51:17 -04:00
|
|
|
blockIconURI: iconURI,
|
2018-08-13 17:10:55 -04:00
|
|
|
showStatusButton: true,
|
2017-10-04 15:16:27 -04:00
|
|
|
blocks: [
|
|
|
|
{
|
|
|
|
opcode: 'motorOnFor',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.motorOnFor',
|
|
|
|
default: 'turn [MOTOR_ID] on for [DURATION] seconds',
|
|
|
|
description: 'turn a motor on for some time'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
arguments: {
|
|
|
|
MOTOR_ID: {
|
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'MOTOR_ID',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: MotorID.DEFAULT
|
|
|
|
},
|
|
|
|
DURATION: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'motorOn',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.motorOn',
|
|
|
|
default: 'turn [MOTOR_ID] on',
|
|
|
|
description: 'turn a motor on indefinitely'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
arguments: {
|
|
|
|
MOTOR_ID: {
|
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'MOTOR_ID',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: MotorID.DEFAULT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'motorOff',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.motorOff',
|
|
|
|
default: 'turn [MOTOR_ID] off',
|
|
|
|
description: 'turn a motor off'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
arguments: {
|
|
|
|
MOTOR_ID: {
|
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'MOTOR_ID',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: MotorID.DEFAULT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'startMotorPower',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.startMotorPower',
|
|
|
|
default: 'set [MOTOR_ID] power to [POWER]',
|
|
|
|
description: 'set the motor\'s power and turn it on'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
arguments: {
|
|
|
|
MOTOR_ID: {
|
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'MOTOR_ID',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: MotorID.DEFAULT
|
|
|
|
},
|
|
|
|
POWER: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 100
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-10-06 13:56:38 -04:00
|
|
|
opcode: 'setMotorDirection',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.setMotorDirection',
|
2018-08-16 14:24:19 -04:00
|
|
|
default: 'set [MOTOR_ID] direction to [MOTOR_DIRECTION]',
|
2018-08-15 14:21:42 -04:00
|
|
|
description: 'set the motor\'s turn direction'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
arguments: {
|
|
|
|
MOTOR_ID: {
|
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'MOTOR_ID',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: MotorID.DEFAULT
|
|
|
|
},
|
2018-08-16 14:24:19 -04:00
|
|
|
MOTOR_DIRECTION: {
|
2017-10-04 15:16:27 -04:00
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'MOTOR_DIRECTION',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: MotorDirection.FORWARD
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'setLightHue',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.setLightHue',
|
|
|
|
default: 'set light color to [HUE]',
|
|
|
|
description: 'set the LED color'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
arguments: {
|
|
|
|
HUE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 50
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'playNoteFor',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.playNoteFor',
|
|
|
|
default: 'play note [NOTE] for [DURATION] seconds',
|
|
|
|
description: 'play a certain note for some time'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
arguments: {
|
|
|
|
NOTE: {
|
|
|
|
type: ArgumentType.NUMBER, // TODO: ArgumentType.MIDI_NOTE?
|
|
|
|
defaultValue: 60
|
|
|
|
},
|
|
|
|
DURATION: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 0.5
|
|
|
|
}
|
2018-08-16 14:24:19 -04:00
|
|
|
},
|
|
|
|
hideFromPalette: true
|
2017-10-04 15:16:27 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'whenDistance',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.whenDistance',
|
|
|
|
default: 'when distance [OP] [REFERENCE]',
|
|
|
|
description: 'check for when distance is < or > than reference'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.HAT,
|
|
|
|
arguments: {
|
|
|
|
OP: {
|
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'OP',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: '<'
|
|
|
|
},
|
|
|
|
REFERENCE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 50
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'whenTilted',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.whenTilted',
|
2018-08-16 14:24:19 -04:00
|
|
|
default: 'when tilted [TILT_DIRECTION_ANY]',
|
2018-08-15 14:21:42 -04:00
|
|
|
description: 'check when tilted in a certain direction'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
func: 'isTilted',
|
|
|
|
blockType: BlockType.HAT,
|
|
|
|
arguments: {
|
2018-08-16 14:24:19 -04:00
|
|
|
TILT_DIRECTION_ANY: {
|
2017-10-04 15:16:27 -04:00
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'TILT_DIRECTION_ANY',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: TiltDirection.ANY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'getDistance',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.getDistance',
|
|
|
|
default: 'distance',
|
|
|
|
description: 'the value returned by the distance sensor'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.REPORTER
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'isTilted',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.isTilted',
|
2018-08-16 14:24:19 -04:00
|
|
|
default: 'tilted [TILT_DIRECTION_ANY]?',
|
2018-08-15 14:21:42 -04:00
|
|
|
description: 'whether the tilt sensor is tilted'
|
|
|
|
}),
|
2017-11-06 10:17:53 -05:00
|
|
|
blockType: BlockType.BOOLEAN,
|
2017-10-04 15:16:27 -04:00
|
|
|
arguments: {
|
2018-08-16 14:24:19 -04:00
|
|
|
TILT_DIRECTION_ANY: {
|
2017-10-04 15:16:27 -04:00
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'TILT_DIRECTION_ANY',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: TiltDirection.ANY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'getTiltAngle',
|
2018-08-15 14:21:42 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'wedo2.getTiltAngle',
|
2018-08-16 14:24:19 -04:00
|
|
|
default: 'tilt angle [TILT_DIRECTION]',
|
2018-08-15 14:21:42 -04:00
|
|
|
description: 'the angle returned by the tilt sensor'
|
|
|
|
}),
|
2017-10-04 15:16:27 -04:00
|
|
|
blockType: BlockType.REPORTER,
|
|
|
|
arguments: {
|
2018-08-16 14:24:19 -04:00
|
|
|
TILT_DIRECTION: {
|
2017-10-04 15:16:27 -04:00
|
|
|
type: ArgumentType.STRING,
|
2018-08-16 14:24:19 -04:00
|
|
|
menu: 'TILT_DIRECTION',
|
2017-10-04 15:16:27 -04:00
|
|
|
defaultValue: TiltDirection.UP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
menus: {
|
2018-08-16 14:24:19 -04:00
|
|
|
MOTOR_ID: [MotorID.DEFAULT, MotorID.A, MotorID.B, MotorID.ALL],
|
|
|
|
MOTOR_DIRECTION: [MotorDirection.FORWARD, MotorDirection.BACKWARD, MotorDirection.REVERSE],
|
|
|
|
TILT_DIRECTION: [TiltDirection.UP, TiltDirection.DOWN, TiltDirection.LEFT, TiltDirection.RIGHT],
|
|
|
|
TILT_DIRECTION_ANY:
|
2017-10-04 15:16:27 -04:00
|
|
|
[TiltDirection.UP, TiltDirection.DOWN, TiltDirection.LEFT, TiltDirection.RIGHT, TiltDirection.ANY],
|
2018-08-16 14:24:19 -04:00
|
|
|
OP: ['<', '>']
|
2017-10-04 15:16:27 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:05:21 -04:00
|
|
|
/**
|
|
|
|
* Turn specified motor(s) on for a specified duration.
|
|
|
|
* @param {object} args - the block's arguments.
|
|
|
|
* @property {MotorID} MOTOR_ID - the motor(s) to activate.
|
|
|
|
* @property {int} DURATION - the amount of time to run the motors.
|
|
|
|
* @return {Promise} - a promise which will resolve at the end of the duration.
|
|
|
|
*/
|
|
|
|
motorOnFor (args) {
|
2018-08-14 11:45:40 -04:00
|
|
|
let durationMS = Cast.toNumber(args.DURATION) * 1000;
|
|
|
|
durationMS = MathUtil.clamp(durationMS, 0, 15000);
|
2017-05-05 18:05:21 -04:00
|
|
|
return new Promise(resolve => {
|
|
|
|
this._forEachMotor(args.MOTOR_ID, motorIndex => {
|
2018-08-13 17:10:55 -04:00
|
|
|
const motor = this._device.motor(motorIndex);
|
|
|
|
if (motor) {
|
|
|
|
motor.setMotorOnFor(durationMS);
|
|
|
|
}
|
2017-05-05 18:05:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Ensure this block runs for a fixed amount of time even when no device is connected.
|
2017-05-12 01:39:24 -04:00
|
|
|
setTimeout(resolve, durationMS);
|
2017-05-05 18:05:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn specified motor(s) on indefinitely.
|
|
|
|
* @param {object} args - the block's arguments.
|
|
|
|
* @property {MotorID} MOTOR_ID - the motor(s) to activate.
|
|
|
|
*/
|
|
|
|
motorOn (args) {
|
|
|
|
this._forEachMotor(args.MOTOR_ID, motorIndex => {
|
2018-08-13 17:10:55 -04:00
|
|
|
const motor = this._device.motor(motorIndex);
|
|
|
|
if (motor) {
|
|
|
|
motor.setMotorOn();
|
|
|
|
}
|
2017-05-05 18:05:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn specified motor(s) off.
|
|
|
|
* @param {object} args - the block's arguments.
|
|
|
|
* @property {MotorID} MOTOR_ID - the motor(s) to deactivate.
|
|
|
|
*/
|
|
|
|
motorOff (args) {
|
|
|
|
this._forEachMotor(args.MOTOR_ID, motorIndex => {
|
2018-08-13 17:10:55 -04:00
|
|
|
const motor = this._device.motor(motorIndex);
|
|
|
|
if (motor) {
|
|
|
|
motor.setMotorOff();
|
|
|
|
}
|
2017-05-05 18:05:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn specified motor(s) off.
|
|
|
|
* @param {object} args - the block's arguments.
|
|
|
|
* @property {MotorID} MOTOR_ID - the motor(s) to be affected.
|
|
|
|
* @property {int} POWER - the new power level for the motor(s).
|
|
|
|
*/
|
|
|
|
startMotorPower (args) {
|
|
|
|
this._forEachMotor(args.MOTOR_ID, motorIndex => {
|
|
|
|
const motor = this._device.motor(motorIndex);
|
2018-08-13 17:10:55 -04:00
|
|
|
if (motor) {
|
2018-08-14 11:45:40 -04:00
|
|
|
motor.power = MathUtil.clamp(Cast.toNumber(args.POWER), 0, 100);
|
2018-08-13 17:10:55 -04:00
|
|
|
motor.setMotorOn();
|
|
|
|
}
|
2017-05-05 18:05:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the direction of rotation for specified motor(s).
|
|
|
|
* If the direction is 'reverse' the motor(s) will be reversed individually.
|
|
|
|
* @param {object} args - the block's arguments.
|
|
|
|
* @property {MotorID} MOTOR_ID - the motor(s) to be affected.
|
2018-08-16 14:24:19 -04:00
|
|
|
* @property {MotorDirection} MOTOR_DIRECTION - the new direction for the motor(s).
|
2017-05-05 18:05:21 -04:00
|
|
|
*/
|
|
|
|
setMotorDirection (args) {
|
|
|
|
this._forEachMotor(args.MOTOR_ID, motorIndex => {
|
|
|
|
const motor = this._device.motor(motorIndex);
|
2018-08-13 17:10:55 -04:00
|
|
|
if (motor) {
|
2018-08-16 14:24:19 -04:00
|
|
|
switch (args.MOTOR_DIRECTION) {
|
2018-08-13 17:10:55 -04:00
|
|
|
case MotorDirection.FORWARD:
|
|
|
|
motor.direction = 1;
|
|
|
|
break;
|
|
|
|
case MotorDirection.BACKWARD:
|
|
|
|
motor.direction = -1;
|
|
|
|
break;
|
|
|
|
case MotorDirection.REVERSE:
|
|
|
|
motor.direction = -motor.direction;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log.warn(`Unknown motor direction in setMotorDirection: ${args.DIRECTION}`);
|
|
|
|
break;
|
|
|
|
}
|
2018-08-15 14:01:08 -04:00
|
|
|
// keep the motor on if it's running, and update the pending timeout if needed
|
|
|
|
if (motor.isOn) {
|
|
|
|
if (motor.pendingTimeoutDelay) {
|
|
|
|
motor.setMotorOnFor(motor.pendingTimeoutStartTime + motor.pendingTimeoutDelay - Date.now());
|
|
|
|
} else {
|
|
|
|
motor.setMotorOn();
|
|
|
|
}
|
2018-08-14 12:35:59 -04:00
|
|
|
}
|
2017-05-05 18:05:21 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the LED's hue.
|
|
|
|
* @param {object} args - the block's arguments.
|
|
|
|
* @property {number} HUE - the hue to set, in the range [0,100].
|
|
|
|
*/
|
|
|
|
setLightHue (args) {
|
|
|
|
// Convert from [0,100] to [0,360]
|
2018-08-19 22:09:57 -04:00
|
|
|
let inputHue = Cast.toNumber(args.HUE);
|
|
|
|
inputHue = MathUtil.wrapClamp(inputHue, 0, 100);
|
|
|
|
const hue = inputHue * 360 / 100;
|
2017-05-05 18:05:21 -04:00
|
|
|
|
|
|
|
const rgbObject = color.hsvToRgb({h: hue, s: 1, v: 1});
|
|
|
|
|
|
|
|
const rgbDecimal = color.rgbToDecimal(rgbObject);
|
|
|
|
|
|
|
|
this._device.setLED(rgbDecimal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the WeDo 2.0 hub play a MIDI note for the specified duration.
|
|
|
|
* @param {object} args - the block's arguments.
|
|
|
|
* @property {number} NOTE - the MIDI note to play.
|
|
|
|
* @property {number} DURATION - the duration of the note, in seconds.
|
|
|
|
* @return {Promise} - a promise which will resolve at the end of the duration.
|
|
|
|
*/
|
|
|
|
playNoteFor (args) {
|
2018-08-14 11:45:40 -04:00
|
|
|
let durationMS = Cast.toNumber(args.DURATION) * 1000;
|
|
|
|
durationMS = MathUtil.clamp(durationMS, 0, 3000);
|
|
|
|
const note = MathUtil.clamp(Cast.toNumber(args.NOTE), 25, 125); // valid WeDo2 sounds
|
|
|
|
if (durationMS === 0) return; // WeDo2 plays duration '0' forever
|
2017-05-05 18:05:21 -04:00
|
|
|
return new Promise(resolve => {
|
2018-08-14 11:45:40 -04:00
|
|
|
const tone = this._noteToTone(note);
|
2017-05-05 18:05:21 -04:00
|
|
|
this._device.playTone(tone, durationMS);
|
|
|
|
|
|
|
|
// Ensure this block runs for a fixed amount of time even when no device is connected.
|
|
|
|
setTimeout(resolve, durationMS);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare the distance sensor's value to a reference.
|
|
|
|
* @param {object} args - the block's arguments.
|
|
|
|
* @property {string} OP - the comparison operation: '<' or '>'.
|
|
|
|
* @property {number} REFERENCE - the value to compare against.
|
|
|
|
* @return {boolean} - the result of the comparison, or false on error.
|
|
|
|
*/
|
|
|
|
whenDistance (args) {
|
|
|
|
switch (args.OP) {
|
|
|
|
case '<':
|
2017-10-13 04:48:25 -04:00
|
|
|
case '<':
|
2018-08-14 11:45:40 -04:00
|
|
|
return this._device.distance < Cast.toNumber(args.REFERENCE);
|
2017-05-05 18:05:21 -04:00
|
|
|
case '>':
|
2017-10-13 04:48:25 -04:00
|
|
|
case '>':
|
2018-08-14 11:45:40 -04:00
|
|
|
return this._device.distance > Cast.toNumber(args.REFERENCE);
|
2017-05-05 18:05:21 -04:00
|
|
|
default:
|
|
|
|
log.warn(`Unknown comparison operator in whenDistance: ${args.OP}`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether the tilt sensor is currently tilted.
|
|
|
|
* @param {object} args - the block's arguments.
|
2018-08-16 14:24:19 -04:00
|
|
|
* @property {TiltDirection} TILT_DIRECTION_ANY - the tilt direction to test (up, down, left, right, or any).
|
2017-05-05 18:05:21 -04:00
|
|
|
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
|
|
|
|
*/
|
|
|
|
whenTilted (args) {
|
2018-08-16 14:24:19 -04:00
|
|
|
return this._isTilted(args.TILT_DIRECTION_ANY);
|
2017-05-05 18:05:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number} - the distance sensor's value, scaled to the [0,100] range.
|
|
|
|
*/
|
|
|
|
getDistance () {
|
2017-10-13 04:48:25 -04:00
|
|
|
return this._device.distance;
|
2017-05-05 18:05:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether the tilt sensor is currently tilted.
|
|
|
|
* @param {object} args - the block's arguments.
|
2018-08-16 14:24:19 -04:00
|
|
|
* @property {TiltDirection} TILT_DIRECTION_ANY - the tilt direction to test (up, down, left, right, or any).
|
2017-05-05 18:05:21 -04:00
|
|
|
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
|
|
|
|
*/
|
|
|
|
isTilted (args) {
|
2018-08-16 14:24:19 -04:00
|
|
|
return this._isTilted(args.TILT_DIRECTION_ANY);
|
2017-05-05 18:05:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} args - the block's arguments.
|
2018-08-16 14:24:19 -04:00
|
|
|
* @property {TiltDirection} TILT_DIRECTION - the direction (up, down, left, right) to check.
|
2017-05-05 18:05:21 -04:00
|
|
|
* @return {number} - the tilt sensor's angle in the specified direction.
|
|
|
|
* Note that getTiltAngle(up) = -getTiltAngle(down) and getTiltAngle(left) = -getTiltAngle(right).
|
|
|
|
*/
|
|
|
|
getTiltAngle (args) {
|
2018-08-16 14:24:19 -04:00
|
|
|
return this._getTiltAngle(args.TILT_DIRECTION);
|
2017-05-05 18:05:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether the tilt sensor is currently tilted.
|
|
|
|
* @param {TiltDirection} direction - 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.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_isTilted (direction) {
|
|
|
|
switch (direction) {
|
|
|
|
case TiltDirection.ANY:
|
|
|
|
return (Math.abs(this._device.tiltX) >= Scratch3WeDo2Blocks.TILT_THRESHOLD) ||
|
|
|
|
(Math.abs(this._device.tiltY) >= Scratch3WeDo2Blocks.TILT_THRESHOLD);
|
|
|
|
default:
|
|
|
|
return this._getTiltAngle(direction) >= Scratch3WeDo2Blocks.TILT_THRESHOLD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TiltDirection} direction - the direction (up, down, left, right) to check.
|
|
|
|
* @return {number} - the tilt sensor's angle in the specified direction.
|
|
|
|
* Note that getTiltAngle(up) = -getTiltAngle(down) and getTiltAngle(left) = -getTiltAngle(right).
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getTiltAngle (direction) {
|
|
|
|
switch (direction) {
|
|
|
|
case TiltDirection.UP:
|
2018-08-13 17:10:55 -04:00
|
|
|
return this._device.tiltY > 45 ? 256 - this._device.tiltY : -this._device.tiltY;
|
2017-05-05 18:05:21 -04:00
|
|
|
case TiltDirection.DOWN:
|
2018-08-13 17:10:55 -04:00
|
|
|
return this._device.tiltY > 45 ? this._device.tiltY - 256 : this._device.tiltY;
|
2017-05-05 18:05:21 -04:00
|
|
|
case TiltDirection.LEFT:
|
2018-08-13 17:10:55 -04:00
|
|
|
return this._device.tiltX > 45 ? 256 - this._device.tiltX : -this._device.tiltX;
|
2017-05-05 18:05:21 -04:00
|
|
|
case TiltDirection.RIGHT:
|
2018-08-13 17:10:55 -04:00
|
|
|
return this._device.tiltX > 45 ? this._device.tiltX - 256 : this._device.tiltX;
|
2017-05-05 18:05:21 -04:00
|
|
|
default:
|
|
|
|
log.warn(`Unknown tilt direction in _getTiltAngle: ${direction}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 MotorID.A:
|
|
|
|
motors = [0];
|
|
|
|
break;
|
|
|
|
case MotorID.B:
|
|
|
|
motors = [1];
|
|
|
|
break;
|
|
|
|
case MotorID.ALL:
|
|
|
|
case MotorID.DEFAULT:
|
|
|
|
motors = [0, 1];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log.warn(`Invalid motor ID: ${motorID}`);
|
|
|
|
motors = [];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (const index of motors) {
|
|
|
|
callback(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} midiNote - the MIDI note value to convert.
|
|
|
|
* @return {number} - the frequency, in Hz, corresponding to that MIDI note value.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_noteToTone (midiNote) {
|
|
|
|
// Note that MIDI note 69 is A4, 440 Hz
|
|
|
|
return 440 * Math.pow(2, (midiNote - 69) / 12);
|
|
|
|
}
|
2017-05-03 19:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Scratch3WeDo2Blocks;
|