Merge pull request #1630 from evhan55/fixes/hardware-extensions

Hardware extensions: fixes and menu localization
This commit is contained in:
Evelyn Eastmond 2018-10-10 15:19:11 -04:00 committed by GitHub
commit 2ade71590f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 177 additions and 47 deletions

View file

@ -266,6 +266,8 @@ class EV3Motor {
* @param {number} milliseconds - run the motor for this long. * @param {number} milliseconds - run the motor for this long.
*/ */
turnOnFor (milliseconds) { turnOnFor (milliseconds) {
if (this._power === 0) return;
const port = this._portMask(this._index); const port = this._portMask(this._index);
let n = milliseconds; let n = milliseconds;
let speed = this._power * this._direction; let speed = this._power * this._direction;
@ -323,6 +325,8 @@ class EV3Motor {
* @param {number} time - the time in milliseconds. * @param {number} time - the time in milliseconds.
*/ */
coastAfter (time) { coastAfter (time) {
if (this._power === 0) return;
// Set the motor command id to check before starting coast // Set the motor command id to check before starting coast
const commandId = uid(); const commandId = uid();
this._commandID = commandId; this._commandID = commandId;
@ -341,6 +345,8 @@ class EV3Motor {
* Set the motor to coast. * Set the motor to coast.
*/ */
coast () { coast () {
if (this._power === 0) return;
const cmd = this._parent.generateCommand( const cmd = this._parent.generateCommand(
Ev3Command.DIRECT_COMMAND_NO_REPLY, Ev3Command.DIRECT_COMMAND_NO_REPLY,
[ [

View file

@ -328,7 +328,7 @@ class MicroBit {
* @readonly * @readonly
* @enum {string} * @enum {string}
*/ */
const TiltDirection = { const MicroBitTiltDirection = {
FRONT: 'front', FRONT: 'front',
BACK: 'back', BACK: 'back',
LEFT: 'left', LEFT: 'left',
@ -341,7 +341,7 @@ const TiltDirection = {
* @readonly * @readonly
* @enum {string} * @enum {string}
*/ */
const Gestures = { const MicroBitGestures = {
MOVED: 'moved', MOVED: 'moved',
SHAKEN: 'shaken', SHAKEN: 'shaken',
JUMPED: 'jumped' JUMPED: 'jumped'
@ -352,7 +352,7 @@ const Gestures = {
* @readonly * @readonly
* @enum {string} * @enum {string}
*/ */
const Buttons = { const MicroBitButtons = {
A: 'A', A: 'A',
B: 'B', B: 'B',
ANY: 'any' ANY: 'any'
@ -363,7 +363,7 @@ const Buttons = {
* @readonly * @readonly
* @enum {string} * @enum {string}
*/ */
const PinState = { const MicroBitPinState = {
ON: 'on', ON: 'on',
OFF: 'off' OFF: 'off'
}; };
@ -401,11 +401,11 @@ class Scratch3MicroBitBlocks {
return [ return [
{ {
text: 'A', text: 'A',
value: Buttons.A value: MicroBitButtons.A
}, },
{ {
text: 'B', text: 'B',
value: Buttons.B value: MicroBitButtons.B
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -413,7 +413,7 @@ class Scratch3MicroBitBlocks {
default: 'any', default: 'any',
description: 'label for "any" element in button picker for micro:bit extension' description: 'label for "any" element in button picker for micro:bit extension'
}), }),
value: Buttons.ANY value: MicroBitButtons.ANY
} }
]; ];
} }
@ -429,7 +429,7 @@ class Scratch3MicroBitBlocks {
default: 'moved', default: 'moved',
description: 'label for moved gesture in gesture picker for micro:bit extension' description: 'label for moved gesture in gesture picker for micro:bit extension'
}), }),
value: Gestures.MOVED value: MicroBitGestures.MOVED
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -437,7 +437,7 @@ class Scratch3MicroBitBlocks {
default: 'shaken', default: 'shaken',
description: 'label for shaken gesture in gesture picker for micro:bit extension' description: 'label for shaken gesture in gesture picker for micro:bit extension'
}), }),
value: Gestures.SHAKEN value: MicroBitGestures.SHAKEN
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -445,7 +445,7 @@ class Scratch3MicroBitBlocks {
default: 'jumped', default: 'jumped',
description: 'label for jumped gesture in gesture picker for micro:bit extension' description: 'label for jumped gesture in gesture picker for micro:bit extension'
}), }),
value: Gestures.JUMPED value: MicroBitGestures.JUMPED
} }
]; ];
} }
@ -461,7 +461,7 @@ class Scratch3MicroBitBlocks {
default: 'on', default: 'on',
description: 'label for on element in pin state picker for micro:bit extension' description: 'label for on element in pin state picker for micro:bit extension'
}), }),
value: PinState.ON value: MicroBitPinState.ON
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -469,7 +469,7 @@ class Scratch3MicroBitBlocks {
default: 'off', default: 'off',
description: 'label for off element in pin state picker for micro:bit extension' description: 'label for off element in pin state picker for micro:bit extension'
}), }),
value: PinState.OFF value: MicroBitPinState.OFF
} }
]; ];
} }
@ -485,7 +485,7 @@ class Scratch3MicroBitBlocks {
default: 'front', default: 'front',
description: 'label for front element in tilt direction picker for micro:bit extension' description: 'label for front element in tilt direction picker for micro:bit extension'
}), }),
value: TiltDirection.FRONT value: MicroBitTiltDirection.FRONT
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -493,7 +493,7 @@ class Scratch3MicroBitBlocks {
default: 'back', default: 'back',
description: 'label for back element in tilt direction picker for micro:bit extension' description: 'label for back element in tilt direction picker for micro:bit extension'
}), }),
value: TiltDirection.BACK value: MicroBitTiltDirection.BACK
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -501,7 +501,7 @@ class Scratch3MicroBitBlocks {
default: 'left', default: 'left',
description: 'label for left element in tilt direction picker for micro:bit extension' description: 'label for left element in tilt direction picker for micro:bit extension'
}), }),
value: TiltDirection.LEFT value: MicroBitTiltDirection.LEFT
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -509,7 +509,7 @@ class Scratch3MicroBitBlocks {
default: 'right', default: 'right',
description: 'label for right element in tilt direction picker for micro:bit extension' description: 'label for right element in tilt direction picker for micro:bit extension'
}), }),
value: TiltDirection.RIGHT value: MicroBitTiltDirection.RIGHT
} }
]; ];
} }
@ -526,7 +526,7 @@ class Scratch3MicroBitBlocks {
default: 'any', default: 'any',
description: 'label for any direction element in tilt direction picker for micro:bit extension' description: 'label for any direction element in tilt direction picker for micro:bit extension'
}), }),
value: TiltDirection.ANY value: MicroBitTiltDirection.ANY
} }
]; ];
} }
@ -568,7 +568,7 @@ class Scratch3MicroBitBlocks {
BTN: { BTN: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'buttons', menu: 'buttons',
defaultValue: Buttons.A defaultValue: MicroBitButtons.A
} }
} }
}, },
@ -584,7 +584,7 @@ class Scratch3MicroBitBlocks {
BTN: { BTN: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'buttons', menu: 'buttons',
defaultValue: Buttons.A defaultValue: MicroBitButtons.A
} }
} }
}, },
@ -601,7 +601,7 @@ class Scratch3MicroBitBlocks {
GESTURE: { GESTURE: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'gestures', menu: 'gestures',
defaultValue: Gestures.MOVED defaultValue: MicroBitGestures.MOVED
} }
} }
}, },
@ -667,7 +667,7 @@ class Scratch3MicroBitBlocks {
DIRECTION: { DIRECTION: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'tiltDirectionAny', menu: 'tiltDirectionAny',
defaultValue: TiltDirection.ANY defaultValue: MicroBitTiltDirection.ANY
} }
} }
}, },
@ -683,7 +683,7 @@ class Scratch3MicroBitBlocks {
DIRECTION: { DIRECTION: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'tiltDirectionAny', menu: 'tiltDirectionAny',
defaultValue: TiltDirection.ANY defaultValue: MicroBitTiltDirection.ANY
} }
} }
}, },
@ -699,7 +699,7 @@ class Scratch3MicroBitBlocks {
DIRECTION: { DIRECTION: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'tiltDirection', menu: 'tiltDirection',
defaultValue: TiltDirection.FRONT defaultValue: MicroBitTiltDirection.FRONT
} }
} }
}, },
@ -887,7 +887,7 @@ class Scratch3MicroBitBlocks {
*/ */
_isTilted (direction) { _isTilted (direction) {
switch (direction) { switch (direction) {
case TiltDirection.ANY: case MicroBitTiltDirection.ANY:
return (Math.abs(this._peripheral.tiltX / 10) >= Scratch3MicroBitBlocks.TILT_THRESHOLD) || return (Math.abs(this._peripheral.tiltX / 10) >= Scratch3MicroBitBlocks.TILT_THRESHOLD) ||
(Math.abs(this._peripheral.tiltY / 10) >= Scratch3MicroBitBlocks.TILT_THRESHOLD); (Math.abs(this._peripheral.tiltY / 10) >= Scratch3MicroBitBlocks.TILT_THRESHOLD);
default: default:
@ -903,13 +903,13 @@ class Scratch3MicroBitBlocks {
*/ */
_getTiltAngle (direction) { _getTiltAngle (direction) {
switch (direction) { switch (direction) {
case TiltDirection.FRONT: case MicroBitTiltDirection.FRONT:
return Math.round(this._peripheral.tiltY / -10); return Math.round(this._peripheral.tiltY / -10);
case TiltDirection.BACK: case MicroBitTiltDirection.BACK:
return Math.round(this._peripheral.tiltY / 10); return Math.round(this._peripheral.tiltY / 10);
case TiltDirection.LEFT: case MicroBitTiltDirection.LEFT:
return Math.round(this._peripheral.tiltX / -10); return Math.round(this._peripheral.tiltX / -10);
case TiltDirection.RIGHT: case MicroBitTiltDirection.RIGHT:
return Math.round(this._peripheral.tiltX / 10); return Math.round(this._peripheral.tiltX / 10);
default: default:
log.warn(`Unknown tilt direction in _getTiltAngle: ${direction}`); log.warn(`Unknown tilt direction in _getTiltAngle: ${direction}`);

View file

@ -226,7 +226,15 @@ class WeDo2Motor {
* @param {int} value - this motor's new power level, in the range [0,100]. * @param {int} value - this motor's new power level, in the range [0,100].
*/ */
set power (value) { set power (value) {
this._power = Math.max(0, Math.min(value, 100)); const p = Math.max(0, Math.min(value, 100));
// Lego Wedo 2.0 hub only turns motors at power range [30 - 100], so
// map value from [0 - 100] to [30 - 100].
if (p === 0) {
this._power = 0;
} else {
const delta = 100 / p;
this._power = 30 + (70 / delta);
}
} }
/** /**
@ -254,6 +262,8 @@ class WeDo2Motor {
* Turn this motor on indefinitely. * Turn this motor on indefinitely.
*/ */
turnOn () { turnOn () {
if (this._power === 0) return;
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
this._index + 1, this._index + 1,
WeDo2Command.MOTOR_POWER, WeDo2Command.MOTOR_POWER,
@ -271,6 +281,8 @@ class WeDo2Motor {
* @param {number} milliseconds - run the motor for this long. * @param {number} milliseconds - run the motor for this long.
*/ */
turnOnFor (milliseconds) { turnOnFor (milliseconds) {
if (this._power === 0) return;
milliseconds = Math.max(0, milliseconds); milliseconds = Math.max(0, milliseconds);
this.turnOn(); this.turnOn();
this._setNewTimeout(this.startBraking, milliseconds); this._setNewTimeout(this.startBraking, milliseconds);
@ -281,6 +293,8 @@ class WeDo2Motor {
* // TODO: rename this to coastAfter? * // TODO: rename this to coastAfter?
*/ */
startBraking () { startBraking () {
if (this._power === 0) return;
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
this._index + 1, this._index + 1,
WeDo2Command.MOTOR_POWER, WeDo2Command.MOTOR_POWER,
@ -298,6 +312,8 @@ class WeDo2Motor {
* @param {boolean} [useLimiter=true] - if true, use the rate limiter * @param {boolean} [useLimiter=true] - if true, use the rate limiter
*/ */
turnOff (useLimiter = true) { turnOff (useLimiter = true) {
if (this._power === 0) return;
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
this._index + 1, this._index + 1,
WeDo2Command.MOTOR_POWER, WeDo2Command.MOTOR_POWER,
@ -1077,28 +1093,136 @@ class Scratch3WeDo2Blocks {
], ],
menus: { menus: {
MOTOR_ID: [ MOTOR_ID: [
WeDo2MotorLabel.DEFAULT, {
WeDo2MotorLabel.A, text: formatMessage({
WeDo2MotorLabel.B, id: 'wedo2.motorId.default',
WeDo2MotorLabel.ALL default: 'motor',
description: 'label for motor element in motor menu for LEGO WeDo 2 extension'
}),
value: WeDo2MotorLabel.DEFAULT
},
{
text: formatMessage({
id: 'wedo2.motorId.a',
default: 'motor A',
description: 'label for motor A element in motor menu for LEGO WeDo 2 extension'
}),
value: WeDo2MotorLabel.A
},
{
text: formatMessage({
id: 'wedo2.motorId.b',
default: 'motor B',
description: 'label for motor B element in motor menu for LEGO WeDo 2 extension'
}),
value: WeDo2MotorLabel.B
},
{
text: formatMessage({
id: 'wedo2.motorId.all',
default: 'all motors',
description: 'label for all motors element in motor menu for LEGO WeDo 2 extension'
}),
value: WeDo2MotorLabel.ALL
}
], ],
MOTOR_DIRECTION: [ MOTOR_DIRECTION: [
WeDo2MotorDirection.FORWARD, {
WeDo2MotorDirection.BACKWARD, text: formatMessage({
WeDo2MotorDirection.REVERSE id: 'wedo2.motorDirection.forward',
default: 'this way',
description: 'label for forward element in motor direction menu for LEGO WeDo 2 extension'
}),
value: WeDo2MotorDirection.FORWARD
},
{
text: formatMessage({
id: 'wedo2.motorDirection.backward',
default: 'that way',
description: 'label for backward element in motor direction menu for LEGO WeDo 2 extension'
}),
value: WeDo2MotorDirection.BACKWARD
},
{
text: formatMessage({
id: 'wedo2.motorDirection.reverse',
default: 'reverse',
description: 'label for reverse element in motor direction menu for LEGO WeDo 2 extension'
}),
value: WeDo2MotorDirection.REVERSE
}
], ],
TILT_DIRECTION: [ TILT_DIRECTION: [
WeDo2TiltDirection.UP, {
WeDo2TiltDirection.DOWN, text: formatMessage({
WeDo2TiltDirection.LEFT, id: 'wedo2.tiltDirection.up',
WeDo2TiltDirection.RIGHT default: 'up',
description: 'label for up element in tilt direction menu for LEGO WeDo 2 extension'
}),
value: WeDo2TiltDirection.UP
},
{
text: formatMessage({
id: 'wedo2.tiltDirection.down',
default: 'down',
description: 'label for down element in tilt direction menu for LEGO WeDo 2 extension'
}),
value: WeDo2TiltDirection.DOWN
},
{
text: formatMessage({
id: 'wedo2.tiltDirection.left',
default: 'left',
description: 'label for left element in tilt direction menu for LEGO WeDo 2 extension'
}),
value: WeDo2TiltDirection.LEFT
},
{
text: formatMessage({
id: 'wedo2.tiltDirection.right',
default: 'right',
description: 'label for right element in tilt direction menu for LEGO WeDo 2 extension'
}),
value: WeDo2TiltDirection.RIGHT
}
], ],
TILT_DIRECTION_ANY: [ TILT_DIRECTION_ANY: [
WeDo2TiltDirection.UP, {
WeDo2TiltDirection.DOWN, text: formatMessage({
WeDo2TiltDirection.LEFT, id: 'wedo2.tiltDirection.up',
WeDo2TiltDirection.RIGHT, default: 'up'
WeDo2TiltDirection.ANY }),
value: WeDo2TiltDirection.UP
},
{
text: formatMessage({
id: 'wedo2.tiltDirection.down',
default: 'down'
}),
value: WeDo2TiltDirection.DOWN
},
{
text: formatMessage({
id: 'wedo2.tiltDirection.left',
default: 'left'
}),
value: WeDo2TiltDirection.LEFT
},
{
text: formatMessage({
id: 'wedo2.tiltDirection.right',
default: 'right'
}),
value: WeDo2TiltDirection.RIGHT
},
{
text: formatMessage({
id: 'wedo2.tiltDirection.any',
default: 'any',
description: 'label for any element in tilt direction menu for LEGO WeDo 2 extension'
}),
value: WeDo2TiltDirection.ANY
}
], ],
OP: ['<', '>'] OP: ['<', '>']
} }

View file

@ -172,7 +172,7 @@ class BLE extends JSONRPCWebSocket {
} }
_sendError (/* e */) { _sendError (/* e */) {
this.disconnect(); if (this._connected) this.disconnect();
// log.error(`BLE error: ${JSON.stringify(e)}`); // log.error(`BLE error: ${JSON.stringify(e)}`);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, { this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
message: `Scratch lost connection to`, message: `Scratch lost connection to`,

View file

@ -115,7 +115,7 @@ class BT extends JSONRPCWebSocket {
} }
_sendError (/* e */) { _sendError (/* e */) {
this.disconnect(); if (this._connected) this.disconnect();
// log.error(`BT error: ${JSON.stringify(e)}`); // log.error(`BT error: ${JSON.stringify(e)}`);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, { this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
message: `Scratch lost connection to`, message: `Scratch lost connection to`,