Clean up color sensing using IDs

This commit is contained in:
Eric Rosenbaum 2019-04-12 16:56:09 -04:00
parent 66ff92433e
commit c25b84d510

View file

@ -101,18 +101,30 @@ const BoostPort = {
}; };
/** /**
* Enum for indexed colors read by the Boost Vision Sensor * Ids for each color sensed by the Boost Vision Sensor
*/
const COLOR_ID_ANY = 'any';
const COLOR_ID_NONE = 'none';
const COLOR_ID_RED = 'red';
const COLOR_ID_BLUE = 'blue';
const COLOR_ID_GREEN = 'green';
const COLOR_ID_YELLOW = 'yellow';
const COLOR_ID_WHITE = 'white';
const COLOR_ID_BLACK = 'black';
/**
* Object to look up a color ID by color index
* @readonly * @readonly
* @enum {number} * @enum {number}
*/ */
const BoostColor = { const BoostColorIdByIndex = {
NONE: 255, 255: COLOR_ID_NONE,
RED: 9, 9: COLOR_ID_RED,
BLUE: 3, 3: COLOR_ID_BLUE,
GREEN: 5, 5: COLOR_ID_GREEN,
YELLOW: 7, 7: COLOR_ID_YELLOW,
WHITE: 10, 10: COLOR_ID_WHITE,
BLACK: 0 0: COLOR_ID_BLACK
}; };
/** /**
@ -600,8 +612,8 @@ class Boost {
this._sensors = { this._sensors = {
tiltX: 0, tiltX: 0,
tiltY: 0, tiltY: 0,
color: BoostColor.NONE, color: COLOR_ID_NONE,
previousColor: BoostColor.NONE previousColor: COLOR_ID_NONE
}; };
/** /**
@ -661,6 +673,13 @@ class Boost {
return this._sensors.color; return this._sensors.color;
} }
/**
* @return {number} - the previous color value received from the vision sensor.
*/
get previousColor () {
return this._sensors.previousColor;
}
/** /**
* Access a particular motor on this peripheral. * Access a particular motor on this peripheral.
* @param {int} index - the index of the desired motor. * @param {int} index - the index of the desired motor.
@ -770,8 +789,8 @@ class Boost {
this._sensors = { this._sensors = {
tiltX: 0, tiltX: 0,
tiltY: 0, tiltY: 0,
color: BoostColor.NONE, color: COLOR_ID_NONE,
previousColor: BoostColor.NONE previousColor: COLOR_ID_NONE
}; };
if (this._ble) { if (this._ble) {
@ -929,12 +948,16 @@ class Boost {
if (this._colorSamples.length > BoostColorSampleSize) { if (this._colorSamples.length > BoostColorSampleSize) {
this._colorSamples.pop(); this._colorSamples.pop();
if (this._colorSamples.every((v, i, arr) => v === arr[0])) { if (this._colorSamples.every((v, i, arr) => v === arr[0])) {
this._sensors.color = this._colorSamples[0]; const firstSample = this._colorSamples[0];
} else { if (BoostColorIdByIndex[firstSample]) {
this._sensors.color = BoostColor.NONE; this._sensors.previousColor = this._sensors.color;
this._sensors.color = BoostColorIdByIndex[firstSample];
} }
} else { } else {
this._sensors.color = BoostColor.NONE; this._sensors.color = COLOR_ID_NONE;
}
} else {
this._sensors.color = COLOR_ID_NONE;
} }
break; break;
case BoostIO.MOTOREXT: case BoostIO.MOTOREXT:
@ -1051,7 +1074,7 @@ class Boost {
this._sensors.tiltX = this._sensors.tiltY = 0; this._sensors.tiltX = this._sensors.tiltY = 0;
} }
if (type === BoostIO.COLOR) { if (type === BoostIO.COLOR) {
this._sensors.color = BoostColor.NONE; this._sensors.color = COLOR_ID_NONE;
} }
this._ports[portID] = 'none'; this._ports[portID] = 'none';
this._motors[portID] = null; this._motors[portID] = null;
@ -1096,21 +1119,6 @@ const BoostTiltDirection = {
ANY: 'any' ANY: 'any'
}; };
/**
* Enum for vision sensor colors.
* @readonly
* @enum {string}
*/
const BoostColorLabel = {
ANY: 'any',
RED: 'red',
BLUE: 'blue',
GREEN: 'green',
YELLOW: 'yellow',
BLACK: 'black',
WHITE: 'white'
};
/** /**
* Scratch 3.0 blocks to interact with a LEGO Boost peripheral. * Scratch 3.0 blocks to interact with a LEGO Boost peripheral.
*/ */
@ -1296,7 +1304,7 @@ class Scratch3BoostBlocks {
COLOR: { COLOR: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'COLOR', menu: 'COLOR',
defaultValue: BoostColorLabel.ANY defaultValue: COLOR_ID_ANY
} }
} }
}, },
@ -1545,61 +1553,60 @@ class Scratch3BoostBlocks {
{ {
text: formatMessage({ text: formatMessage({
id: 'boost.color.red', id: 'boost.color.red',
default: BoostColorLabel.RED, default: 'red',
description: BoostColorLabel.RED description: 'the color red'
}), }),
value: BoostColorLabel.RED value: COLOR_ID_RED
}, },
{ {
text: formatMessage({ text: formatMessage({
id: 'boost.color.blue', id: 'boost.color.blue',
default: BoostColorLabel.BLUE, default: 'blue',
description: BoostColorLabel.BLUE description: 'the color blue'
}), }),
value: BoostColorLabel.BLUE value: COLOR_ID_BLUE
}, },
{ {
text: formatMessage({ text: formatMessage({
id: 'boost.color.green', id: 'boost.color.green',
default: BoostColorLabel.GREEN, default: 'green',
description: BoostColorLabel.GREEN description: 'the color green'
}), }),
value: BoostColorLabel.GREEN value: COLOR_ID_GREEN
}, },
{ {
text: formatMessage({ text: formatMessage({
id: 'boost.color.yellow', id: 'boost.color.yellow',
default: BoostColorLabel.YELLOW, default: 'yellow',
description: BoostColorLabel.YELLOW description: 'the color yellow'
}), }),
value: BoostColorLabel.YELLOW value: COLOR_ID_YELLOW
}, },
{ {
text: formatMessage({ text: formatMessage({
id: 'boost.color.white', id: 'boost.color.white',
default: BoostColorLabel.WHITE, default: 'white',
desription: BoostColorLabel.WHITE desription: 'the color white'
}), }),
value: BoostColorLabel.WHITE value: COLOR_ID_WHITE
}, },
{ {
text: formatMessage({ text: formatMessage({
id: 'boost.color.black', id: 'boost.color.black',
default: BoostColorLabel.BLACK, default: 'black',
description: BoostColorLabel.BLACK description: 'the color black'
}), }),
value: BoostColorLabel.BLACK value: COLOR_ID_BLACK
}, },
{ {
text: formatMessage({ text: formatMessage({
id: 'boost.color.any', id: 'boost.color.any',
default: BoostColorLabel.ANY, default: 'any color',
description: BoostColorLabel.ANY description: 'any color'
}), }),
value: BoostColorLabel.ANY value: COLOR_ID_ANY
} }
], ]
OP: ['<', '>']
} }
}; };
} }
@ -1919,44 +1926,29 @@ class Scratch3BoostBlocks {
} }
/** /**
* Test whether the tilt sensor is currently tilted. * Test whether the vision sensor is detecting a certain color.
* @param {object} args - the block's arguments. * @param {object} args - the block's arguments.
* @property {Color} COLOR - the color to test. * @return {boolean} - true when the color sensor senses the specified color.
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
*/ */
whenColor (args) { whenColor (args) {
return this._isColor(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. * Test whether the vision sensor is detecting a certain color.
* @param {number} args - the color to test. * @param {string} colorId - the id of the color to test.
* @return {boolean} - true when the color sensor senses the specified color. * @return {boolean} - true when the color sensor senses the specified color.
* @private * @private
*/ */
_isColor (args) { _isColor (colorId) {
switch (args.COLOR) { switch (colorId) {
case BoostColorLabel.ANY: case COLOR_ID_ANY:
if (Object.keys(BoostColor).find(key => BoostColor[key]) if (this._peripheral.color === this._peripheral.previousColor) {
.toLowerCase() !== this.getColor()) {
if (this.getColor() === this._peripheral._sensors.previousColor) {
return false; return false;
} }
this._peripheral._sensors.previousColor = this.getColor();
return true; return true;
}
break;
default: default:
return this.getColor() === args.COLOR; return colorId === this._peripheral.color;
} }
} }