Reorganize color ids and indices

This commit is contained in:
Eric Rosenbaum 2019-04-16 15:24:39 -04:00
parent 07768652f9
commit 8dc4832100

View file

@ -101,30 +101,34 @@ const BoostPort = {
}; };
/** /**
* Ids for each color sensed by the Boost Vision Sensor * Ids for each color sensor value used by the extension.
* @readonly
* @enum {string}
*/ */
const COLOR_ID_ANY = 'any'; const BoostColor = {
const COLOR_ID_NONE = 'none'; ANY: 'any',
const COLOR_ID_RED = 'red'; NONE: 'none',
const COLOR_ID_BLUE = 'blue'; RED: 'red',
const COLOR_ID_GREEN = 'green'; BLUE: 'blue',
const COLOR_ID_YELLOW = 'yellow'; GREEN: 'green',
const COLOR_ID_WHITE = 'white'; YELLOW: 'yellow',
const COLOR_ID_BLACK = 'black'; WHITE: 'white',
BLACK: 'black'
};
/** /**
* Object to look up a color ID by color index * Enum for indices for each color sensed by the Boost vision sensor.
* @readonly * @readonly
* @enum {number} * @enum {number}
*/ */
const BoostColorIdByIndex = { const BoostColorIndex = {
255: COLOR_ID_NONE, [BoostColor.NONE]: 255,
9: COLOR_ID_RED, [BoostColor.RED]: 9,
3: COLOR_ID_BLUE, [BoostColor.BLUE]: 3,
5: COLOR_ID_GREEN, [BoostColor.GREEN]: 5,
7: COLOR_ID_YELLOW, [BoostColor.YELLOW]: 7,
10: COLOR_ID_WHITE, [BoostColor.WHITE]: 10,
0: COLOR_ID_BLACK [BoostColor.BLACK]: 0
}; };
/** /**
@ -612,8 +616,8 @@ class Boost {
this._sensors = { this._sensors = {
tiltX: 0, tiltX: 0,
tiltY: 0, tiltY: 0,
color: COLOR_ID_NONE, color: BoostColor.NONE,
previousColor: COLOR_ID_NONE previousColor: BoostColor.NONE
}; };
/** /**
@ -680,6 +684,16 @@ class Boost {
return this._sensors.previousColor; return this._sensors.previousColor;
} }
/**
* Look up the color id for an index received from the vision sensor.
* @param {number} index - the color index to look up.
* @return {BoostColor} the color id for this index.
*/
boostColorForIndex (index) {
const colorForIndex = Object.keys(BoostColorIndex).find(key => BoostColorIndex[key] === index);
return colorForIndex || BoostColor.NONE;
}
/** /**
* 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.
@ -789,8 +803,8 @@ class Boost {
this._sensors = { this._sensors = {
tiltX: 0, tiltX: 0,
tiltY: 0, tiltY: 0,
color: COLOR_ID_NONE, color: BoostColor.NONE,
previousColor: COLOR_ID_NONE previousColor: BoostColor.NONE
}; };
if (this._ble) { if (this._ble) {
@ -948,16 +962,13 @@ 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])) {
const firstSample = this._colorSamples[0]; this._sensors.previousColor = this._sensors.color;
if (BoostColorIdByIndex[firstSample]) { this._sensors.color = this.boostColorForIndex(this._colorSamples[0]);
this._sensors.previousColor = this._sensors.color;
this._sensors.color = BoostColorIdByIndex[firstSample];
}
} else { } else {
this._sensors.color = COLOR_ID_NONE; this._sensors.color = BoostColor.NONE;
} }
} else { } else {
this._sensors.color = COLOR_ID_NONE; this._sensors.color = BoostColor.NONE;
} }
break; break;
case BoostIO.MOTOREXT: case BoostIO.MOTOREXT:
@ -1074,7 +1085,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 = COLOR_ID_NONE; this._sensors.color = BoostColor.NONE;
} }
this._ports[portID] = 'none'; this._ports[portID] = 'none';
this._motors[portID] = null; this._motors[portID] = null;
@ -1304,7 +1315,7 @@ class Scratch3BoostBlocks {
COLOR: { COLOR: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'COLOR', menu: 'COLOR',
defaultValue: COLOR_ID_ANY defaultValue: BoostColor.ANY
} }
} }
}, },
@ -1320,7 +1331,7 @@ class Scratch3BoostBlocks {
COLOR: { COLOR: {
type: ArgumentType.STRING, type: ArgumentType.STRING,
menu: 'COLOR', menu: 'COLOR',
defaultValue: COLOR_ID_ANY defaultValue: BoostColor.ANY
} }
} }
}, },
@ -1563,7 +1574,7 @@ class Scratch3BoostBlocks {
default: 'red', default: 'red',
description: 'the color red' description: 'the color red'
}), }),
value: COLOR_ID_RED value: BoostColor.RED
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -1571,7 +1582,7 @@ class Scratch3BoostBlocks {
default: 'blue', default: 'blue',
description: 'the color blue' description: 'the color blue'
}), }),
value: COLOR_ID_BLUE value: BoostColor.BLUE
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -1579,7 +1590,7 @@ class Scratch3BoostBlocks {
default: 'green', default: 'green',
description: 'the color green' description: 'the color green'
}), }),
value: COLOR_ID_GREEN value: BoostColor.GREEN
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -1587,7 +1598,7 @@ class Scratch3BoostBlocks {
default: 'yellow', default: 'yellow',
description: 'the color yellow' description: 'the color yellow'
}), }),
value: COLOR_ID_YELLOW value: BoostColor.YELLOW
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -1595,7 +1606,7 @@ class Scratch3BoostBlocks {
default: 'white', default: 'white',
desription: 'the color white' desription: 'the color white'
}), }),
value: COLOR_ID_WHITE value: BoostColor.WHITE
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -1603,7 +1614,7 @@ class Scratch3BoostBlocks {
default: 'black', default: 'black',
description: 'the color black' description: 'the color black'
}), }),
value: COLOR_ID_BLACK value: BoostColor.BLACK
}, },
{ {
text: formatMessage({ text: formatMessage({
@ -1611,7 +1622,7 @@ class Scratch3BoostBlocks {
default: 'any color', default: 'any color',
description: 'any color' description: 'any color'
}), }),
value: COLOR_ID_ANY value: BoostColor.ANY
} }
] ]
} }
@ -1939,12 +1950,12 @@ class Scratch3BoostBlocks {
* @return {boolean} - true when the color sensor senses the specified color. * @return {boolean} - true when the color sensor senses the specified color.
*/ */
whenColor (args) { whenColor (args) {
if (args.COLOR === COLOR_ID_ANY) { if (args.COLOR === BoostColor.ANY) {
// For "any" color, return true if the color is not "none", and // For "any" color, return true if the color is not "none", and
// the color is different from the previous color detected. This // the color is different from the previous color detected. This
// allows the hat to trigger when the color changes from one color // allows the hat to trigger when the color changes from one color
// to another. // to another.
return this._peripheral.color !== COLOR_ID_NONE && return this._peripheral.color !== BoostColor.NONE &&
this._peripheral.color !== this._peripheral.previousColor; this._peripheral.color !== this._peripheral.previousColor;
} }
@ -1958,8 +1969,8 @@ class Scratch3BoostBlocks {
* @return {boolean} - true when the color sensor senses the specified color. * @return {boolean} - true when the color sensor senses the specified color.
*/ */
seeingColor (args) { seeingColor (args) {
if (args.COLOR === COLOR_ID_ANY) { if (args.COLOR === BoostColor.ANY) {
return this._peripheral.color !== COLOR_ID_NONE; return this._peripheral.color !== BoostColor.NONE;
} }
return args.COLOR === this._peripheral.color; return args.COLOR === this._peripheral.color;