Deleted BoostUnit, since the new protocol has prescribed units for a particular mode. Implemented helper-function for turning javascript numbers into a bytearray. Changed generateInputCommand()-function delta-parameter to be int32 according to the new protocol. Fixed issue that whenColor hat-block didn't work until dropdown value changed.

This commit is contained in:
Kevin Andersen 2019-02-01 10:19:59 -05:00
parent 2aaf424279
commit c03dc60feb

View file

@ -134,27 +134,20 @@ const BoostMode = {
UNKNOWN: 0 UNKNOWN: 0
}; };
/**
* Enum for units for input sensors on the Boost.
*
* 0 = raw
* 1 = percent
*
* @enum {number}
*/
const BoostUnit = {
TILT: 0,
DISTANCE: 1,
LED: 0,
MOTOR: 0,
COLOR: 0,
UNKNOWN:0
};
function buf2hex(buffer) { // buffer is an ArrayBuffer function buf2hex(buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(' '); return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(' ');
} }
function number2int32array(number) {
var buffer = new ArrayBuffer(4)
var dataview = new DataView(buffer)
dataview.setInt32(0, number)
return [dataview.getInt8(3),
dataview.getInt8(2),
dataview.getInt8(1),
dataview.getInt8(0)];
}
/** /**
* Manage power, direction, and timers for one Boost motor. * Manage power, direction, and timers for one Boost motor.
*/ */
@ -388,13 +381,11 @@ class BoostMotor {
this._index, this._index,
0x0B, 0x0B,
null, null,
[dataview.getInt8(3), number2int32array(degrees).concat(
dataview.getInt8(2), [
dataview.getInt8(1),
dataview.getInt8(0),
this._power * this._direction, // power in range 0-100 this._power * this._direction, // power in range 0-100
0xff, 0xff,
0x00,0x03] 0x00,0x03])
); );
this._isOn = true; this._isOn = true;
@ -624,7 +615,6 @@ class Boost {
this._ports.indexOf(BoostDevice.LED), this._ports.indexOf(BoostDevice.LED),
BoostMode.LED, BoostMode.LED,
0, 0,
BoostUnit.LED,
false false
); );
@ -778,19 +768,18 @@ class Boost {
* @param {number} connectID - the port (Connect ID) to send a command to. * @param {number} connectID - the port (Connect ID) to send a command to.
* @param {number} mode - the mode of the input sensor. * @param {number} mode - the mode of the input sensor.
* @param {number} delta - the delta change needed to trigger notification. * @param {number} delta - the delta change needed to trigger notification.
* @param {array} units - the unit of the input sensor value.
* @param {boolean} enableNotifications - whether to enable notifications. * @param {boolean} enableNotifications - whether to enable notifications.
* @return {array} - a generated input command. * @return {array} - a generated input command.
*/ */
generateInputCommand (connectID, mode, delta, units, enableNotifications) { generateInputCommand (connectID, mode, delta, enableNotifications) {
var command = [ var command = [
0x00, // Hub ID 0x00, // Hub ID
0x41, // Message Type (Port Input Format Setup (Single)) 0x41, // Message Type (Port Input Format Setup (Single))
connectID, connectID,
mode, mode,
delta, ].concat(number2int32array(delta)).concat([
enableNotifications enableNotifications
]; ]);
command.unshift(command.length+1) // Prepend payload with length byte command.unshift(command.length+1) // Prepend payload with length byte
@ -850,8 +839,9 @@ class Boost {
} }
break; break;
case BoostMessageTypes.PORT_VALUE: case BoostMessageTypes.PORT_VALUE:
//console.log(buf2hex(data))
var type = this._ports[data[3]]; var type = this._ports[data[3]];
var valueFormat = data.length //var valueFormat = data.length
// TODO: Build a proper value-formatting based on the PORT_INPUT_FORMAT-messages instead of hardcoding value-handling // TODO: Build a proper value-formatting based on the PORT_INPUT_FORMAT-messages instead of hardcoding value-handling
switch(type) { switch(type) {
case BoostDevice.TILT: case BoostDevice.TILT:
@ -919,8 +909,6 @@ class Boost {
break; break;
case BoostDevice.LED: case BoostDevice.LED:
typeString = 'LED' typeString = 'LED'
console.log("NOW!")
console.log("LED: " + connectID + ", " + type)
this.setLEDMode(); this.setLEDMode();
this.setLED(0x00FF00); this.setLED(0x00FF00);
break; break;
@ -930,12 +918,11 @@ class Boost {
default: default:
typeString = 'UNKNOWN' typeString = 'UNKNOWN'
} }
//const typeString = type === BoostDevice.DISTANCE ? 'DISTANCE' : 'TILT';
const cmd = this.generateInputCommand( const cmd = this.generateInputCommand(
connectID, connectID,
BoostMode[typeString], BoostMode[typeString],
1, 1,
BoostUnit[typeString],
true true
); );
@ -1216,7 +1203,7 @@ class Scratch3BoostBlocks {
blockType: BlockType.HAT, blockType: BlockType.HAT,
arguments: { arguments: {
COLOR: { COLOR: {
type: ArgumentType.STRING, type: ArgumentType.NUMBER,
menu: 'COLOR', menu: 'COLOR',
defaultValue: BoostColor.RED defaultValue: BoostColor.RED
} }
@ -1856,7 +1843,7 @@ class Scratch3BoostBlocks {
* @private * @private
*/ */
_isColor (color) { _isColor (color) {
return this.getColor() === color; return this.getColor() == color;
} }
/** /**