Updating blocks

This commit is contained in:
Eric Rosenbaum 2019-01-24 17:01:46 -05:00
parent 98b92be2d7
commit 92d2a1673a

View file

@ -26,6 +26,12 @@ const BLEUUID = {
responseChar: 'b41e6675-a329-40e0-aa01-44d2f444babe' responseChar: 'b41e6675-a329-40e0-aa01-44d2f444babe'
}; };
/**
* Threshold for pushing and pulling force, for the whenForcePushedOrPulled hat block.
* @type {number}
*/
const FORCE_THRESHOLD = 5;
/** /**
* Manage communication with a GDX-FOR peripheral over a Scratch Link client socket. * Manage communication with a GDX-FOR peripheral over a Scratch Link client socket.
*/ */
@ -314,13 +320,13 @@ class GdxFor {
} }
/** /**
* Enum for comparison operations. * Enum for pushed and pulled menu options.
* @readonly * @readonly
* @enum {string} * @enum {string}
*/ */
const ComparisonOptions = { const PushPullValues = {
LESS_THAN: 'less_than', PUSHED: 'pushed',
GREATER_THAN: 'greater_than' PULLED: 'pulled'
}; };
/** /**
@ -332,7 +338,7 @@ class Scratch3GdxForBlocks {
* @return {string} - the name of this extension. * @return {string} - the name of this extension.
*/ */
static get EXTENSION_NAME () { static get EXTENSION_NAME () {
return 'GDX-FOR'; return 'Force and Acceleration';
} }
/** /**
@ -385,21 +391,27 @@ class Scratch3GdxForBlocks {
]; ];
} }
get PUSH_PULL_MENU () {
get COMPARE_MENU () {
return [ return [
{ {
text: '<', text: formatMessage({
value: ComparisonOptions.LESS_THAN id: 'gdxfor.pushed',
default: 'pushed',
description: 'the force sensor was pushed inward'
}),
value: PushPullValues.PUSHED
}, },
{ {
text: '>', text: formatMessage({
value: ComparisonOptions.GREATER_THAN id: 'gdxfor.pulled',
default: 'pulled',
description: 'the force sensor was pulled outward'
}),
value: PushPullValues.PULLED
} }
]; ];
} }
/** /**
* Construct a set of GDX-FOR blocks. * Construct a set of GDX-FOR blocks.
* @param {Runtime} runtime - the Scratch 3.0 runtime. * @param {Runtime} runtime - the Scratch 3.0 runtime.
@ -426,66 +438,29 @@ class Scratch3GdxForBlocks {
showStatusButton: true, showStatusButton: true,
blocks: [ blocks: [
{ {
opcode: 'whenAccelerationCompare', opcode: 'whenForcePushedOrPulled',
text: formatMessage({ text: formatMessage({
id: 'gdxfor.whenAccelerationCompare', id: 'gdxfor.whenForcePushedOrPulled',
default: 'when acceleration [COMPARE] [VALUE]', default: 'when force sensor [PUSH_PULL]',
description: 'when the meters/second^2 value measured by the ' + description: 'when the force sensor is pushed or pulled'
'acceleration sensor is compared to some value'
}), }),
blockType: BlockType.HAT, blockType: BlockType.HAT,
arguments: { arguments: {
COMPARE: { PUSH_PULL: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'compareOptions', menu: 'pushPullOptions',
defaultValue: ComparisonOptions.GREATER_THAN defaultValue: PushPullValues.PUSHED
},
VALUE: {
type: ArgumentType.NUMBER,
defaultValue: 5
} }
} }
}, },
{ {
opcode: 'whenSpinSpeedCompare', opcode: 'getForce',
text: formatMessage({ text: formatMessage({
id: 'gdxfor.whenSpinSpeedCompare', id: 'gdxfor.getForce',
default: 'when spin speed [COMPARE] [VALUE]', default: 'force',
description: 'when the degrees/second value measured by the ' + description: 'gets force'
'gyroscope sensor is compared to some value'
}), }),
blockType: BlockType.HAT, blockType: BlockType.REPORTER
arguments: {
COMPARE: {
type: ArgumentType.STRING,
menu: 'compareOptions',
defaultValue: ComparisonOptions.GREATER_THAN
},
VALUE: {
type: ArgumentType.NUMBER,
defaultValue: 5
}
}
},
{
opcode: 'whenForceCompare',
text: formatMessage({
id: 'gdxfor.whenForceCompare',
default: 'when force [COMPARE] [VALUE]',
description: 'when the value measured by the force sensor is compared to some value'
}),
blockType: BlockType.HAT,
arguments: {
COMPARE: {
type: ArgumentType.STRING,
menu: 'compareOptions',
defaultValue: ComparisonOptions.GREATER_THAN
},
VALUE: {
type: ArgumentType.NUMBER,
defaultValue: 5
}
}
}, },
{ {
opcode: 'whenJumped', opcode: 'whenJumped',
@ -544,15 +519,6 @@ class Scratch3GdxForBlocks {
} }
} }
}, },
{
opcode: 'getForce',
text: formatMessage({
id: 'gdxfor.getForce',
default: 'force',
description: 'gets force'
}),
blockType: BlockType.REPORTER
},
{ {
opcode: 'isFacing', opcode: 'isFacing',
text: formatMessage({ text: formatMessage({
@ -581,14 +547,30 @@ class Scratch3GdxForBlocks {
} }
], ],
menus: { menus: {
pushPullOptions: this.PUSH_PULL_MENU,
directionOptions: this.DIRECTIONS_MENU, directionOptions: this.DIRECTIONS_MENU,
compareOptions: this.COMPARE_MENU,
tiltOptions: this.TILT_MENU, tiltOptions: this.TILT_MENU,
faceOptions: this.FACE_MENU faceOptions: this.FACE_MENU
} }
}; };
} }
whenForcePushedOrPulled (args) {
switch (args.PUSH_PULL) {
case PushPullValues.PUSHED:
return this._peripheral.getForce() < FORCE_THRESHOLD * -1;
case PushPullValues.PULLED:
return this._peripheral.getForce() > FORCE_THRESHOLD;
default:
log.warn(`unknown push/pull value in whenForcePushedOrPulled: ${args.PUSH_PULL}`);
return false;
}
}
getForce () {
return Math.round(this._peripheral.getForce());
}
/** /**
* @param {number} x - x axis vector * @param {number} x - x axis vector
* @param {number} y - y axis vector * @param {number} y - y axis vector
@ -598,58 +580,9 @@ class Scratch3GdxForBlocks {
magnitude (x, y, z) { magnitude (x, y, z) {
return Math.sqrt((x * x) + (y * y) + (z * z)); return Math.sqrt((x * x) + (y * y) + (z * z));
} }
whenAccelerationCompare (args) {
let currentVal = this.magnitude(
this._peripheral.getAccelerationX(),
this._peripheral.getAccelerationY(),
this._peripheral.getAccelerationZ()
);
// Remove acceleration due to gravity
currentVal = currentVal - 9.8;
switch (args.COMPARE) {
case ComparisonOptions.LESS_THAN:
return currentVal < Cast.toNumber(args.VALUE);
case ComparisonOptions.GREATER_THAN:
return currentVal > Cast.toNumber(args.VALUE);
default:
log.warn(`Unknown comparison operator in whenAccelerationCompare: ${args.COMPARE}`);
return false;
}
}
whenJumped () { whenJumped () {
return this.isFreeFalling(); return this.isFreeFalling();
} }
whenSpinSpeedCompare (args) {
const currentVal = this.magnitude(
this._peripheral.getSpinSpeedX(),
this._peripheral.getSpinSpeedY(),
this._peripheral.getSpinSpeedZ()
);
switch (args.COMPARE) {
case ComparisonOptions.LESS_THAN:
return currentVal < Cast.toNumber(args.VALUE);
case ComparisonOptions.GREATER_THAN:
return currentVal > Cast.toNumber(args.VALUE);
default:
log.warn(`Unknown comparison operator in whenSpinSpeedCompare: ${args.COMPARE}`);
return false;
}
}
whenForceCompare (args) {
switch (args.COMPARE) {
case ComparisonOptions.LESS_THAN:
return this._peripheral.getForce() < Cast.toNumber(args.VALUE);
case ComparisonOptions.GREATER_THAN:
return this._peripheral.getForce() > Cast.toNumber(args.VALUE);
default:
log.warn(`Unknown comparison operator in whenForceCompare: ${args.COMPARE}`);
return false;
}
}
getAcceleration (args) { getAcceleration (args) {
switch (args.DIRECTION) { switch (args.DIRECTION) {
case 'x': case 'x':
@ -684,9 +617,6 @@ class Scratch3GdxForBlocks {
log.warn(`Unknown direction in getTilt: ${args.TILT}`); log.warn(`Unknown direction in getTilt: ${args.TILT}`);
} }
} }
getForce () {
return this._peripheral.getForce();
}
isFacing (args) { isFacing (args) {
switch (args.FACING) { switch (args.FACING) {
case 'up': case 'up':