mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
Cleaned up some documentation. Consolidated and renamed Boost enums
This commit is contained in:
parent
bf02426a4a
commit
c902bbaa0d
1 changed files with 35 additions and 43 deletions
|
@ -18,13 +18,13 @@ const iconURI = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOE
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Boost BLE service UUID.
|
* Boost BLE service UUID.
|
||||||
* @enum
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
const BLEService = '00001623-1212-efde-1623-785feabcd123';
|
const BLEService = '00001623-1212-efde-1623-785feabcd123';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Boost BLE characteristic UUID.
|
* Boost BLE characteristic UUID.
|
||||||
* @enum
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
const BLECharacteristic = '00001624-1212-efde-1623-785feabcd123';
|
const BLECharacteristic = '00001624-1212-efde-1623-785feabcd123';
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ const BLESendRateMax = 20;
|
||||||
* @readonly
|
* @readonly
|
||||||
* @enum {number}
|
* @enum {number}
|
||||||
*/
|
*/
|
||||||
const BoostDevice = {
|
const BoostIO = {
|
||||||
MOTORINT: 0x27,
|
MOTORINT: 0x27,
|
||||||
MOTOREXT: 0x26,
|
MOTOREXT: 0x26,
|
||||||
LED: 0x17,
|
LED: 0x17,
|
||||||
|
@ -55,16 +55,6 @@ const BoostDevice = {
|
||||||
CURRENT: 0x15,
|
CURRENT: 0x15,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Enum for ids for various output commands on the Boost.
|
|
||||||
* @readonly
|
|
||||||
* @enum {number}
|
|
||||||
*/
|
|
||||||
const BoostCommand = {
|
|
||||||
// TODO: Figure out if this enum is necessary or if we're always just sending 0x81
|
|
||||||
OUTPUT: 0x81,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enum for ids for various output commands on the Boost.
|
* Enum for ids for various output commands on the Boost.
|
||||||
* @readonly
|
* @readonly
|
||||||
|
@ -108,7 +98,7 @@ const BoostColor = {
|
||||||
* @enum {number}
|
* @enum {number}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const BoostMessageTypes = {
|
const BoostMessage = {
|
||||||
HUB_PROPERTIES: 0x01,
|
HUB_PROPERTIES: 0x01,
|
||||||
HUB_ACTIONS: 0x02,
|
HUB_ACTIONS: 0x02,
|
||||||
HUB_ALERTS: 0x03,
|
HUB_ALERTS: 0x03,
|
||||||
|
@ -120,6 +110,8 @@ const BoostMessageTypes = {
|
||||||
PORT_VALUE_COMBINED: 0x46,
|
PORT_VALUE_COMBINED: 0x46,
|
||||||
PORT_INPUT_FORMAT: 0x47,
|
PORT_INPUT_FORMAT: 0x47,
|
||||||
PORT_INPUT_FORMAT_COMBINED: 0x48,
|
PORT_INPUT_FORMAT_COMBINED: 0x48,
|
||||||
|
WRITE_DIRECT_MODE_DATA: 0x51,
|
||||||
|
OUTPUT: 0x81,
|
||||||
PORT_OUTPUT_COMMAND_FEEDBACK: 0x82
|
PORT_OUTPUT_COMMAND_FEEDBACK: 0x82
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -368,7 +360,7 @@ class BoostMotor {
|
||||||
if (this._power === 0) return;
|
if (this._power === 0) return;
|
||||||
const cmd = this._parent.generateOutputCommand(
|
const cmd = this._parent.generateOutputCommand(
|
||||||
this._index,
|
this._index,
|
||||||
0x51,
|
BoostMessage.WRITE_DIRECT_MODE_DATA,
|
||||||
0x00,
|
0x00,
|
||||||
[this._power * this._direction] // power in range 0-100
|
[this._power * this._direction] // power in range 0-100
|
||||||
);
|
);
|
||||||
|
@ -424,7 +416,7 @@ class BoostMotor {
|
||||||
|
|
||||||
const cmd = this._parent.generateOutputCommand(
|
const cmd = this._parent.generateOutputCommand(
|
||||||
this._index,
|
this._index,
|
||||||
BoostCommand.MOTOR_POWER,
|
BoostMessage.MOTOR_POWER,
|
||||||
0x00,
|
0x00,
|
||||||
[127] // 127 = break
|
[127] // 127 = break
|
||||||
);
|
);
|
||||||
|
@ -444,7 +436,7 @@ class BoostMotor {
|
||||||
|
|
||||||
const cmd = this._parent.generateOutputCommand(
|
const cmd = this._parent.generateOutputCommand(
|
||||||
this._index,
|
this._index,
|
||||||
BoostCommand.MOTOR_POWER,
|
BoostMessage.MOTOR_POWER,
|
||||||
0x00,
|
0x00,
|
||||||
[0] // 0 = stop
|
[0] // 0 = stop
|
||||||
);
|
);
|
||||||
|
@ -619,8 +611,8 @@ class Boost {
|
||||||
];
|
];
|
||||||
|
|
||||||
const cmd = this.generateOutputCommand(
|
const cmd = this.generateOutputCommand(
|
||||||
this._ports.indexOf(BoostDevice.LED),
|
this._ports.indexOf(BoostIO.LED),
|
||||||
0x51,
|
BoostMessage.WRITE_DIRECT_MODE_DATA,
|
||||||
BoostMode.LED,
|
BoostMode.LED,
|
||||||
rgb
|
rgb
|
||||||
);
|
);
|
||||||
|
@ -634,7 +626,7 @@ class Boost {
|
||||||
*/
|
*/
|
||||||
setLEDMode () {
|
setLEDMode () {
|
||||||
const cmd = this.generateInputCommand(
|
const cmd = this.generateInputCommand(
|
||||||
this._ports.indexOf(BoostDevice.LED),
|
this._ports.indexOf(BoostIO.LED),
|
||||||
BoostMode.LED,
|
BoostMode.LED,
|
||||||
0,
|
0,
|
||||||
false
|
false
|
||||||
|
@ -649,8 +641,8 @@ class Boost {
|
||||||
*/
|
*/
|
||||||
stopLED () {
|
stopLED () {
|
||||||
const cmd = this.generateOutputCommand(
|
const cmd = this.generateOutputCommand(
|
||||||
this._ports.indexOf(BoostDevice.LED),
|
this._ports.indexOf(BoostIO.LED),
|
||||||
0x51,
|
BoostMessage.WRITE_DIRECT_MODE_DATA,
|
||||||
BoostUnit.LED,
|
BoostUnit.LED,
|
||||||
[0, 0, 0]
|
[0, 0, 0]
|
||||||
);
|
);
|
||||||
|
@ -761,8 +753,8 @@ class Boost {
|
||||||
* @param {array} values - the list of values to write to the command.
|
* @param {array} values - the list of values to write to the command.
|
||||||
* @return {array} - a generated output command.
|
* @return {array} - a generated output command.
|
||||||
*/
|
*/
|
||||||
generateOutputCommand (portID, subCommandID = 0x51, mode=null, values = null) {
|
generateOutputCommand (portID, subCommandID = BoostMessage.WRITE_DIRECT_MODE_DATA, mode=null, values = null) {
|
||||||
let command = [0x00, BoostCommand.OUTPUT];
|
let command = [0x00, BoostMessage.OUTPUT];
|
||||||
if (values) {
|
if (values) {
|
||||||
command = command.concat(
|
command = command.concat(
|
||||||
portID
|
portID
|
||||||
|
@ -852,7 +844,7 @@ class Boost {
|
||||||
* 5: IO Type ID
|
* 5: IO Type ID
|
||||||
*/
|
*/
|
||||||
|
|
||||||
case BoostMessageTypes.HUB_ATTACHED_IO: // IO Attach/Detach events
|
case BoostMessage.HUB_ATTACHED_IO: // IO Attach/Detach events
|
||||||
|
|
||||||
const event = data[4]
|
const event = data[4]
|
||||||
const typeId = data[5]
|
const typeId = data[5]
|
||||||
|
@ -869,19 +861,19 @@ class Boost {
|
||||||
console.log("No I/O Event case found!")
|
console.log("No I/O Event case found!")
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BoostMessageTypes.PORT_VALUE:
|
case BoostMessage.PORT_VALUE:
|
||||||
var type = this._ports[portID];
|
var type = this._ports[portID];
|
||||||
// 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 BoostIO.TILT:
|
||||||
this._sensors.tiltX = data[4]
|
this._sensors.tiltX = data[4]
|
||||||
this._sensors.tiltY = data[5]
|
this._sensors.tiltY = data[5]
|
||||||
break;
|
break;
|
||||||
case BoostDevice.COLOR:
|
case BoostIO.COLOR:
|
||||||
this._sensors.color = data[4];
|
this._sensors.color = data[4];
|
||||||
break;
|
break;
|
||||||
case BoostDevice.MOTOREXT:
|
case BoostIO.MOTOREXT:
|
||||||
case BoostDevice.MOTORINT:
|
case BoostIO.MOTORINT:
|
||||||
// Taken from EV3 extension tacho motor calculation
|
// Taken from EV3 extension tacho motor calculation
|
||||||
let value = data[4] + (data[5] * 256) + (data[6] * 256 * 256) + (data[7] * 256 * 256 * 256);
|
let value = data[4] + (data[5] * 256) + (data[6] * 256 * 256) + (data[7] * 256 * 256 * 256);
|
||||||
if (value > 0x7fffffff) {
|
if (value > 0x7fffffff) {
|
||||||
|
@ -889,14 +881,14 @@ class Boost {
|
||||||
}
|
}
|
||||||
this._motors[portID]._position = value
|
this._motors[portID]._position = value
|
||||||
break;
|
break;
|
||||||
case BoostDevice.CURRENT:
|
case BoostIO.CURRENT:
|
||||||
case BoostDevice.VOLTAGE:
|
case BoostIO.VOLTAGE:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log("Unknown sensor value! Type: " + type)
|
console.log("Unknown sensor value! Type: " + type)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BoostMessageTypes.PORT_OUTPUT_COMMAND_FEEDBACK:
|
case BoostMessage.PORT_OUTPUT_COMMAND_FEEDBACK:
|
||||||
//TODO: Handle messages that contain feedback from more than one port.
|
//TODO: Handle messages that contain feedback from more than one port.
|
||||||
var feedback = data[4];
|
var feedback = data[4];
|
||||||
switch(feedback) {
|
switch(feedback) {
|
||||||
|
@ -907,8 +899,8 @@ class Boost {
|
||||||
console.log("Got it but didn't find a motor on: " + portID)
|
console.log("Got it but didn't find a motor on: " + portID)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BoostMessageTypes.PORT_INPUT_FORMAT:
|
case BoostMessage.PORT_INPUT_FORMAT:
|
||||||
case BoostMessageTypes.ERROR:
|
case BoostMessage.ERROR:
|
||||||
//DEBUG
|
//DEBUG
|
||||||
console.log(buf2hex(data))
|
console.log(buf2hex(data))
|
||||||
break;
|
break;
|
||||||
|
@ -931,26 +923,26 @@ class Boost {
|
||||||
this._ports[portID] = type;
|
this._ports[portID] = type;
|
||||||
|
|
||||||
// Record motor port
|
// Record motor port
|
||||||
if (type === BoostDevice.MOTORINT || type === BoostDevice.MOTOREXT) {
|
if (type === BoostIO.MOTORINT || type === BoostIO.MOTOREXT) {
|
||||||
this._motors[portID] = new BoostMotor(this, portID);
|
this._motors[portID] = new BoostMotor(this, portID);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set input format for tilt or distance sensor
|
// Set input format for tilt or distance sensor
|
||||||
var typeString = ''
|
var typeString = ''
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case BoostDevice.MOTORINT:
|
case BoostIO.MOTORINT:
|
||||||
case BoostDevice.MOTOREXT:
|
case BoostIO.MOTOREXT:
|
||||||
typeString = 'MOTOR'
|
typeString = 'MOTOR'
|
||||||
break;
|
break;
|
||||||
case BoostDevice.COLOR:
|
case BoostIO.COLOR:
|
||||||
typeString = 'COLOR'
|
typeString = 'COLOR'
|
||||||
break;
|
break;
|
||||||
case BoostDevice.LED:
|
case BoostIO.LED:
|
||||||
typeString = 'LED'
|
typeString = 'LED'
|
||||||
this.setLEDMode();
|
this.setLEDMode();
|
||||||
this.setLED(0x00FF00);
|
this.setLED(0x00FF00);
|
||||||
break;
|
break;
|
||||||
case BoostDevice.TILT:
|
case BoostIO.TILT:
|
||||||
typeString = 'TILT'
|
typeString = 'TILT'
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -974,10 +966,10 @@ class Boost {
|
||||||
*/
|
*/
|
||||||
_clearPort (portID) {
|
_clearPort (portID) {
|
||||||
const type = this._ports[portID];
|
const type = this._ports[portID];
|
||||||
if (type === BoostDevice.TILT) {
|
if (type === BoostIO.TILT) {
|
||||||
this._sensors.tiltX = this._sensors.tiltY = 0;
|
this._sensors.tiltX = this._sensors.tiltY = 0;
|
||||||
}
|
}
|
||||||
if (type === BoostDevice.DISTANCE) {
|
if (type === BoostIO.DISTANCE) {
|
||||||
this._sensors.distance = 0;
|
this._sensors.distance = 0;
|
||||||
}
|
}
|
||||||
this._ports[portID] = 'none';
|
this._ports[portID] = 'none';
|
||||||
|
|
Loading…
Reference in a new issue