Port AB supported on PUPHub, setName on LPF2 hubs

This commit is contained in:
Nathan Kunicki 2018-08-02 11:41:21 +01:00
parent 02b2215ca8
commit 2a5b5a47b9
5 changed files with 142 additions and 9 deletions

41
DOCS.md
View File

@ -379,6 +379,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [.setLEDColor(color)](#BoostMoveHub+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#BoostMoveHub+setMotorSpeed) ⇒ <code>Promise</code>
* [.setMotorAngle(port, angle, [speed])](#BoostMoveHub+setMotorAngle) ⇒ <code>Promise</code>
* [.setName(name)](#LPF2Hub+setName) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
@ -493,6 +494,18 @@ Rotate a motor by a given angle.
| angle | <code>number</code> | | How much the motor should be rotated (in degrees). |
| [speed] | <code>number</code> | <code>100</code> | How fast the motor should be rotated. |
<a name="LPF2Hub+setName"></a>
### boostMoveHub.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>BoostMoveHub</code>](#BoostMoveHub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | New name of the hub (ASCII characters only). |
<a name="Hub+connect"></a>
### boostMoveHub.connect() ⇒ <code>Promise</code>
@ -660,6 +673,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [.batteryLevel](#Hub+batteryLevel)
* [.setLEDColor(color)](#PUPHub+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#PUPHub+setMotorSpeed) ⇒ <code>Promise</code>
* [.setName(name)](#LPF2Hub+setName) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
@ -756,9 +770,21 @@ Set the motor speed on a given port.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| speed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| speed | <code>number</code> \| <code>Array.&lt;number&gt;</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. |
| [time] | <code>number</code> | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. |
<a name="LPF2Hub+setName"></a>
### pupHub.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>PUPHub</code>](#PUPHub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | New name of the hub (ASCII characters only). |
<a name="Hub+connect"></a>
### pupHub.connect() ⇒ <code>Promise</code>
@ -913,6 +939,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [.rssi](#Hub+rssi)
* [.batteryLevel](#Hub+batteryLevel)
* [.setLEDColor(color)](#PUPRemote+setLEDColor) ⇒ <code>Promise</code>
* [.setName(name)](#LPF2Hub+setName) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
@ -993,6 +1020,18 @@ Set the color of the LED on the Remote via a color value.
| --- | --- | --- |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="LPF2Hub+setName"></a>
### pupRemote.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>PUPRemote</code>](#PUPRemote)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | New name of the hub (ASCII characters only). |
<a name="Hub+connect"></a>
### pupRemote.connect() ⇒ <code>Promise</code>

View File

@ -429,6 +429,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [.setLEDColor(color)](#BoostMoveHub+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#BoostMoveHub+setMotorSpeed) ⇒ <code>Promise</code>
* [.setMotorAngle(port, angle, [speed])](#BoostMoveHub+setMotorAngle) ⇒ <code>Promise</code>
* [.setName(name)](#LPF2Hub+setName) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
@ -543,6 +544,18 @@ Rotate a motor by a given angle.
| angle | <code>number</code> | | How much the motor should be rotated (in degrees). |
| [speed] | <code>number</code> | <code>100</code> | How fast the motor should be rotated. |
<a name="LPF2Hub+setName"></a>
### boostMoveHub.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>BoostMoveHub</code>](#BoostMoveHub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | New name of the hub (ASCII characters only). |
<a name="Hub+connect"></a>
### boostMoveHub.connect() ⇒ <code>Promise</code>
@ -710,6 +723,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [.batteryLevel](#Hub+batteryLevel)
* [.setLEDColor(color)](#PUPHub+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#PUPHub+setMotorSpeed) ⇒ <code>Promise</code>
* [.setName(name)](#LPF2Hub+setName) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
@ -806,9 +820,21 @@ Set the motor speed on a given port.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| speed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| speed | <code>number</code> \| <code>Array.&lt;number&gt;</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. |
| [time] | <code>number</code> | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. |
<a name="LPF2Hub+setName"></a>
### pupHub.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>PUPHub</code>](#PUPHub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | New name of the hub (ASCII characters only). |
<a name="Hub+connect"></a>
### pupHub.connect() ⇒ <code>Promise</code>
@ -963,6 +989,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [.rssi](#Hub+rssi)
* [.batteryLevel](#Hub+batteryLevel)
* [.setLEDColor(color)](#PUPRemote+setLEDColor) ⇒ <code>Promise</code>
* [.setName(name)](#LPF2Hub+setName) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
@ -1043,6 +1070,18 @@ Set the color of the LED on the Remote via a color value.
| --- | --- | --- |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="LPF2Hub+setName"></a>
### pupRemote.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>PUPRemote</code>](#PUPRemote)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | New name of the hub (ASCII characters only). |
<a name="Hub+connect"></a>
### pupRemote.connect() ⇒ <code>Promise</code>

View File

@ -47,6 +47,28 @@ export class LPF2Hub extends Hub {
}
/**
* Set the name of the Hub.
* @method LPF2Hub#setName
* @param {string} name New name of the hub (ASCII characters only).
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setName (name: string) {
if (name.length > 14) {
throw new Error("Name must be 14 characters or less");
}
return new Promise((resolve, reject) => {
let data = Buffer.from([0x00, 0x00, 0x01, 0x01, 0x01]);
data = Buffer.concat([data, Buffer.from(name, "ascii")]);
data[0] = data.length;
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
this._name = name;
return resolve();
});
}
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback);
}

View File

@ -36,7 +36,8 @@ export class PUPHub extends LPF2Hub {
this.type = Consts.Hubs.POWERED_UP_HUB;
this._ports = {
"A": new Port("A", 0),
"B": new Port("B", 1)
"B": new Port("B", 1),
"AB": new Port("AB", 57)
};
debug("Discovered Powered Up Hub");
}
@ -74,23 +75,51 @@ export class PUPHub extends LPF2Hub {
* Set the motor speed on a given port.
* @method PUPHub#setMotorSpeed
* @param {string} port
* @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @param {number | Array<number>} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.
* @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
*/
public setMotorSpeed (port: string, speed: number, time?: number) {
return new Promise((resolve, reject) => {
public setMotorSpeed (port: string, speed: number | [number, number], time?: number) {
const portObj = this._portLookup(port);
if (portObj.id !== "AB" && speed instanceof Array) {
throw new Error(`Port ${portObj.id} can only accept a single speed`);
}
return new Promise((resolve, reject) => {
if (time) {
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
let data = null;
if (portObj.id === "AB") {
if (speed instanceof Array) {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]);
} else {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]);
}
} else {
// @ts-ignore: The type of speed is properly checked at the start
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
}
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
setTimeout(() => {
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]);
let data = null;
if (portObj.id === "AB") {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, 0x00]);
} else {
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]);
}
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();
}, time);
} else {
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
let data = null;
if (portObj.id === "AB") {
if (speed instanceof Array) {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]);
} else {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]);
}
} else {
// @ts-ignore: The type of speed is properly checked at the start
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
}
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();
}

View File

@ -60,9 +60,13 @@ export class WeDo2SmartHub extends Hub {
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setName (name: string) {
if (name.length > 14) {
throw new Error("Name must be 14 characters or less");
}
return new Promise((resolve, reject) => {
const data = Buffer.from(name, "ascii");
this._writeMessage(Consts.BLECharacteristics.WEDO2_NAME_ID, data);
this._writeMessage(Consts.BLECharacteristics.WEDO2_NAME_ID, data);
this._name = name;
return resolve();
});