- Cleaned up TODOs!

- Added and made use of more enums rather than hardcoded bytes
This commit is contained in:
Kevin Andersen 2019-02-21 14:47:30 -05:00
parent a3dcdd01dd
commit 9767d72188

View file

@ -66,7 +66,6 @@ const BoostIO = {
* @enum {number} * @enum {number}
*/ */
const BoostOutputCommandFeedback = { const BoostOutputCommandFeedback = {
// TODO: Figure out if this enum is necessary or if we're always just sending 0x81
BUFFER_EMPTY_COMMAND_IN_PROGRESS: 0x01, BUFFER_EMPTY_COMMAND_IN_PROGRESS: 0x01,
BUFFER_EMPTY_COMMAND_COMPLETED: 0x02, BUFFER_EMPTY_COMMAND_COMPLETED: 0x02,
CURRENT_COMMAND_DISCARDED: 0x04, CURRENT_COMMAND_DISCARDED: 0x04,
@ -109,6 +108,8 @@ const BoostMessage = {
HUB_ALERTS: 0x03, HUB_ALERTS: 0x03,
HUB_ATTACHED_IO: 0x04, HUB_ATTACHED_IO: 0x04,
ERROR: 0x05, ERROR: 0x05,
PORT_INPUT_FORMAT_SETUP_SINGLE: 0x41,
PORT_INPUT_FORMAT_SETUP_COMBINED: 0x42,
PORT_INFORMATION: 0x43, PORT_INFORMATION: 0x43,
PORT_MODEINFORMATION: 0x44, PORT_MODEINFORMATION: 0x44,
PORT_VALUE: 0x45, PORT_VALUE: 0x45,
@ -141,6 +142,51 @@ const BoostOutputSubCommand = {
WRITE_DIRECT_MODE_DATA: 0x51, WRITE_DIRECT_MODE_DATA: 0x51,
} }
/**
* Enum for Startup/Completion information for an output command.
* Startup and completion bytes must be OR'ed to be combined to a single byte.
* @readonly
* @enum {number}
*/
const BoostOutputExecution = {
// Startup information
BUFFER_IF_NECESSARY: 0x00,
EXECUTE_IMMEDIATELY: 0x10,
// Completion information
NO_ACTION: 0x00,
COMMAND_FEEDBACK: 0x01,
}
/**
* Enum for Boost Motor end states
* @readonly
* @enum {number}
*/
const BoostMotorEndState = {
FLOAT: 0,
HOLD: 126,
BRAKE: 127
}
/**
* Enum for Boost Motor Acceleration/Deceleration profiles
* @readyonly
* @enum {number}
*/
const BoostMotorProfile = {
DO_NOT_USE: 0x00,
ACCELERATION: 0x01,
DECELERATION: 0x02
}
/** /**
* Enum for when Boost IO's are attached/detached * Enum for when Boost IO's are attached/detached
* @readonly * @readonly
@ -161,7 +207,8 @@ const BoostMode = {
TILT: 0, // angle (pitch/yaw) TILT: 0, // angle (pitch/yaw)
LED: 1, // Set LED to accept RGB values LED: 1, // Set LED to accept RGB values
COLOR: 0, // Read indexed colors from Vision Sensor COLOR: 0, // Read indexed colors from Vision Sensor
MOTOR: 2, // Set motors to report their position MOTOR_SENSOR: 2, // Set motors to report their position
MOTOR_OUTPUT: 0,
UNKNOWN: 0 // Anything else will use the default mode (mode 0) UNKNOWN: 0 // Anything else will use the default mode (mode 0)
}; };
@ -401,7 +448,7 @@ class BoostMotor {
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
this._index, this._index,
BoostOutputSubCommand.WRITE_DIRECT_MODE_DATA, BoostOutputSubCommand.WRITE_DIRECT_MODE_DATA,
0x00, BoostMode.MOTOR_OUTPUT,
[this._power * this._direction] // power in range 0-100 [this._power * this._direction] // power in range 0-100
); );
@ -433,14 +480,13 @@ class BoostMotor {
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
this._index, this._index,
0x0B, BoostOutputSubCommand.START_SPEED_FOR_DEGREES,
null, null,
numberToInt32Array(degrees).concat( numberToInt32Array(degrees).concat(
[ [this._power * this._direction, // power in range 0-100
this._power * this._direction, // power in range 0-100 0xFF, // max speed
0xff, // max speed BoostMotorEndState.FLOAT,
0x00, BoostMotorProfile.DO_NOT_USE]) // byte for using acceleration/braking profile
0x03])
); );
this._status = BoostOutputCommandFeedback.BUFFER_EMPTY_COMMAND_IN_PROGRESS; this._status = BoostOutputCommandFeedback.BUFFER_EMPTY_COMMAND_IN_PROGRESS;
@ -458,8 +504,8 @@ class BoostMotor {
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
this._index, this._index,
BoostMessage.MOTOR_POWER, BoostMessage.MOTOR_POWER,
0x00, BoostMode.MOTOR_OUTPUT,
[127] // 127 = break [BoostMotorEndState.BRAKE]
); );
this._parent.send(BLECharacteristic, cmd); this._parent.send(BLECharacteristic, cmd);
@ -478,8 +524,8 @@ class BoostMotor {
const cmd = this._parent.generateOutputCommand( const cmd = this._parent.generateOutputCommand(
this._index, this._index,
BoostMessage.MOTOR_POWER, BoostMessage.MOTOR_POWER,
0x00, BoostMode.MOTOR_OUTPUT,
[0] // 0 = stop [BoostMotorEndState.FLOAT]
); );
this._parent.send(BLECharacteristic, cmd, useLimiter); this._parent.send(BLECharacteristic, cmd, useLimiter);
@ -802,12 +848,11 @@ class Boost {
* @return {array} - a generated output command. * @return {array} - a generated output command.
*/ */
generateOutputCommand (portID, subCommandID = BoostOutputSubCommand.WRITE_DIRECT_MODE_DATA, mode=null, values = null) { generateOutputCommand (portID, subCommandID = BoostOutputSubCommand.WRITE_DIRECT_MODE_DATA, mode=null, values = null) {
let command = [0x00, BoostMessage.OUTPUT]; let command = [0x00 /* Hub ID (always 0 for now) */, BoostMessage.OUTPUT];
if (values) { if (values) {
command = command.concat( command = command.concat(portID).concat(
portID BoostOutputExecution.EXECUTE_IMMEDIATELY ^
).concat( BoostOutputExecution.COMMAND_FEEDBACK
0x11 // Execute immediately TODO: Use enum!
); );
if(subCommandID) { if(subCommandID) {
@ -841,7 +886,7 @@ class Boost {
generateInputCommand (portID, mode, delta, enableNotifications) { generateInputCommand (portID, mode, delta, enableNotifications) {
var command = [ var command = [
0x00, // Hub ID 0x00, // Hub ID
0x41, // Message Type (Port Input Format Setup (Single)) TODO: Use enum BoostMessage.PORT_INPUT_FORMAT_SETUP_SINGLE,
portID, portID,
mode, mode,
].concat(numberToInt32Array(delta)).concat([ ].concat(numberToInt32Array(delta)).concat([
@ -985,7 +1030,7 @@ class Boost {
switch(type) { switch(type) {
case BoostIO.MOTORINT: case BoostIO.MOTORINT:
case BoostIO.MOTOREXT: case BoostIO.MOTOREXT:
typeString = 'MOTOR' typeString = 'MOTOR_SENSOR'
break; break;
case BoostIO.COLOR: case BoostIO.COLOR:
typeString = 'COLOR' typeString = 'COLOR'
@ -1896,7 +1941,6 @@ class Scratch3BoostBlocks {
*/ */
getMotorPosition (args) { getMotorPosition (args) {
switch(args.MOTOR_REPORTER_ID) { switch(args.MOTOR_REPORTER_ID) {
// TODO: Handle negative rotation.
case BoostMotorLabel.A: case BoostMotorLabel.A:
return this._peripheral._motors[BoostPort.A].position - this._peripheral._motors[BoostPort.A].positionZero return this._peripheral._motors[BoostPort.A].position - this._peripheral._motors[BoostPort.A].positionZero
case BoostMotorLabel.B: case BoostMotorLabel.B: