diff --git a/DOCS.md b/DOCS.md index 39f00ae..ee96efa 100644 --- a/DOCS.md +++ b/DOCS.md @@ -11,6 +11,8 @@
LPF2Hub
LPF2Hub
string
| |
| state | number
| A number representing one of the button state consts. |
+
+
+## DuploTrainHub ⇐ LPF2Hub
+**Kind**: global class
+**Extends**: LPF2Hub
, Hub
+
+* [DuploTrainHub](#DuploTrainHub) ⇐ LPF2Hub
+ * [new DuploTrainHub()](#new_DuploTrainHub_new)
+ * [.current](#LPF2Hub+current)
+ * [.name](#Hub+name)
+ * [.uuid](#Hub+uuid)
+ * [.rssi](#Hub+rssi)
+ * [.batteryLevel](#Hub+batteryLevel)
+ * [.setMotorSpeed(port, speed, [time])](#DuploTrainHub+setMotorSpeed) ⇒ Promise
+ * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainHub+rampMotorSpeed) ⇒ Promise
+ * [.setName(name)](#LPF2Hub+setName) ⇒ Promise
+ * [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ Promise
+ * [.connect()](#Hub+connect) ⇒ Promise
+ * [.disconnect()](#Hub+disconnect) ⇒ Promise
+ * [.subscribe(port, [mode])](#Hub+subscribe) ⇒ Promise
+ * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise
+ * [.sleep(delay)](#Hub+sleep) ⇒ Promise
+ * [.wait(commands)](#Hub+wait) ⇒ Promise
+ * ["button" (button, state)](#LPF2Hub+event_button)
+ * ["color" (port, color)](#LPF2Hub+event_color)
+
+
+
+### new DuploTrainHub()
+The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
+
+
+
+### duploTrainHub.current
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| current | number
| Current usage of the hub (Amps) |
+
+
+
+### duploTrainHub.name
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [name
](#Hub+name)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| name | string
| Name of the hub |
+
+
+
+### duploTrainHub.uuid
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [uuid
](#Hub+uuid)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| uuid | string
| UUID of the hub |
+
+
+
+### duploTrainHub.rssi
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [rssi
](#Hub+rssi)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| rssi | number
| Signal strength of the hub |
+
+
+
+### duploTrainHub.batteryLevel
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [batteryLevel
](#Hub+batteryLevel)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| batteryLevel | number
| Battery level of the hub (Percentage between 0-100) |
+
+
+
+### duploTrainHub.setMotorSpeed(port, speed, [time]) ⇒ Promise
+Set the motor speed on a given port.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Returns**: Promise
- Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| speed | number
\| Array.<number>
| 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] | number
| How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. |
+
+
+
+### duploTrainHub.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ Promise
+Ramp the motor speed on a given port.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Returns**: Promise
- Resolved upon successful completion of command.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| fromSpeed | number
| For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
+| toSpeed | number
| For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
+| time | number
| How long the ramp should last (in milliseconds). |
+
+
+
+### duploTrainHub.setName(name) ⇒ Promise
+Set the name of the Hub.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Returns**: Promise
- Resolved upon successful issuance of command.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| name | string
| New name of the hub (14 characters or less, ASCII only). |
+
+
+
+### duploTrainHub.setLEDColor(color) ⇒ Promise
+Set the color of the LED on the Hub via a color value.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Returns**: Promise
- Resolved upon successful issuance of command.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| color | number
| A number representing one of the LED color consts. |
+
+
+
+### duploTrainHub.connect() ⇒ Promise
+Connect to the Hub.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [connect
](#Hub+connect)
+**Returns**: Promise
- Resolved upon successful connect.
+
+
+### duploTrainHub.disconnect() ⇒ Promise
+Disconnect the Hub.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [disconnect
](#Hub+disconnect)
+**Returns**: Promise
- Resolved upon successful disconnect.
+
+
+### duploTrainHub.subscribe(port, [mode]) ⇒ Promise
+Subscribe to sensor notifications on a given port.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [subscribe
](#Hub+subscribe)
+**Returns**: Promise
- Resolved upon successful issuance of command.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| [mode] | number
| The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
+
+
+
+### duploTrainHub.unsubscribe(port) ⇒ Promise
+Unsubscribe to sensor notifications on a given port.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [unsubscribe
](#Hub+unsubscribe)
+**Returns**: Promise
- Resolved upon successful issuance of command.
+
+| Param | Type |
+| --- | --- |
+| port | string
|
+
+
+
+### duploTrainHub.sleep(delay) ⇒ Promise
+Sleep a given amount of time.
+
+This is a helper method to make it easier to add delays into a chain of commands.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [sleep
](#Hub+sleep)
+**Returns**: Promise
- Resolved after the delay is finished.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| delay | number
| How long to sleep (in milliseconds). |
+
+
+
+### duploTrainHub.wait(commands) ⇒ Promise
+Wait until a given list of concurrently running commands are complete.
+
+This is a helper method to make it easier to wait for concurrent commands to complete.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [wait
](#Hub+wait)
+**Returns**: Promise
- Resolved after the commands are finished.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| commands | Array.<Promise.<any>>
| Array of executing commands. |
+
+
+
+### "button" (button, state)
+Emits when a button is pressed.
+
+**Kind**: event emitted by [DuploTrainHub
](#DuploTrainHub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| button | string
| |
+| state | number
| A number representing one of the button state consts. |
+
+
+
+### "color" (port, color)
+Emits when a color sensor is activated.
+
+**Kind**: event emitted by [DuploTrainHub
](#DuploTrainHub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| color | number
| A number representing one of the LED color consts. |
+
diff --git a/README.md b/README.md
index ea9e36d..4836dc1 100644
--- a/README.md
+++ b/README.md
@@ -90,6 +90,8 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R
LPF2Hub
LPF2Hub
string
| |
| state | number
| A number representing one of the button state consts. |
+
+
+## DuploTrainHub ⇐ LPF2Hub
+**Kind**: global class
+**Extends**: LPF2Hub
, Hub
+
+* [DuploTrainHub](#DuploTrainHub) ⇐ LPF2Hub
+ * [new DuploTrainHub()](#new_DuploTrainHub_new)
+ * [.current](#LPF2Hub+current)
+ * [.name](#Hub+name)
+ * [.uuid](#Hub+uuid)
+ * [.rssi](#Hub+rssi)
+ * [.batteryLevel](#Hub+batteryLevel)
+ * [.setMotorSpeed(port, speed, [time])](#DuploTrainHub+setMotorSpeed) ⇒ Promise
+ * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainHub+rampMotorSpeed) ⇒ Promise
+ * [.setName(name)](#LPF2Hub+setName) ⇒ Promise
+ * [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ Promise
+ * [.connect()](#Hub+connect) ⇒ Promise
+ * [.disconnect()](#Hub+disconnect) ⇒ Promise
+ * [.subscribe(port, [mode])](#Hub+subscribe) ⇒ Promise
+ * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise
+ * [.sleep(delay)](#Hub+sleep) ⇒ Promise
+ * [.wait(commands)](#Hub+wait) ⇒ Promise
+ * ["button" (button, state)](#LPF2Hub+event_button)
+ * ["color" (port, color)](#LPF2Hub+event_color)
+
+
+
+### new DuploTrainHub()
+The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
+
+
+
+### duploTrainHub.current
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| current | number
| Current usage of the hub (Amps) |
+
+
+
+### duploTrainHub.name
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [name
](#Hub+name)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| name | string
| Name of the hub |
+
+
+
+### duploTrainHub.uuid
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [uuid
](#Hub+uuid)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| uuid | string
| UUID of the hub |
+
+
+
+### duploTrainHub.rssi
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [rssi
](#Hub+rssi)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| rssi | number
| Signal strength of the hub |
+
+
+
+### duploTrainHub.batteryLevel
+**Kind**: instance property of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [batteryLevel
](#Hub+batteryLevel)
+**Read only**: true
+**Properties**
+
+| Name | Type | Description |
+| --- | --- | --- |
+| batteryLevel | number
| Battery level of the hub (Percentage between 0-100) |
+
+
+
+### duploTrainHub.setMotorSpeed(port, speed, [time]) ⇒ Promise
+Set the motor speed on a given port.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Returns**: Promise
- Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| speed | number
\| Array.<number>
| 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] | number
| How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. |
+
+
+
+### duploTrainHub.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ Promise
+Ramp the motor speed on a given port.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Returns**: Promise
- Resolved upon successful completion of command.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| fromSpeed | number
| For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
+| toSpeed | number
| For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
+| time | number
| How long the ramp should last (in milliseconds). |
+
+
+
+### duploTrainHub.setName(name) ⇒ Promise
+Set the name of the Hub.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Returns**: Promise
- Resolved upon successful issuance of command.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| name | string
| New name of the hub (14 characters or less, ASCII only). |
+
+
+
+### duploTrainHub.setLEDColor(color) ⇒ Promise
+Set the color of the LED on the Hub via a color value.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Returns**: Promise
- Resolved upon successful issuance of command.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| color | number
| A number representing one of the LED color consts. |
+
+
+
+### duploTrainHub.connect() ⇒ Promise
+Connect to the Hub.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [connect
](#Hub+connect)
+**Returns**: Promise
- Resolved upon successful connect.
+
+
+### duploTrainHub.disconnect() ⇒ Promise
+Disconnect the Hub.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [disconnect
](#Hub+disconnect)
+**Returns**: Promise
- Resolved upon successful disconnect.
+
+
+### duploTrainHub.subscribe(port, [mode]) ⇒ Promise
+Subscribe to sensor notifications on a given port.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [subscribe
](#Hub+subscribe)
+**Returns**: Promise
- Resolved upon successful issuance of command.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| [mode] | number
| The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
+
+
+
+### duploTrainHub.unsubscribe(port) ⇒ Promise
+Unsubscribe to sensor notifications on a given port.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [unsubscribe
](#Hub+unsubscribe)
+**Returns**: Promise
- Resolved upon successful issuance of command.
+
+| Param | Type |
+| --- | --- |
+| port | string
|
+
+
+
+### duploTrainHub.sleep(delay) ⇒ Promise
+Sleep a given amount of time.
+
+This is a helper method to make it easier to add delays into a chain of commands.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [sleep
](#Hub+sleep)
+**Returns**: Promise
- Resolved after the delay is finished.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| delay | number
| How long to sleep (in milliseconds). |
+
+
+
+### duploTrainHub.wait(commands) ⇒ Promise
+Wait until a given list of concurrently running commands are complete.
+
+This is a helper method to make it easier to wait for concurrent commands to complete.
+
+**Kind**: instance method of [DuploTrainHub
](#DuploTrainHub)
+**Overrides**: [wait
](#Hub+wait)
+**Returns**: Promise
- Resolved after the commands are finished.
+
+| Param | Type | Description |
+| --- | --- | --- |
+| commands | Array.<Promise.<any>>
| Array of executing commands. |
+
+
+
+### "button" (button, state)
+Emits when a button is pressed.
+
+**Kind**: event emitted by [DuploTrainHub
](#DuploTrainHub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| button | string
| |
+| state | number
| A number representing one of the button state consts. |
+
+
+
+### "color" (port, color)
+Emits when a color sensor is activated.
+
+**Kind**: event emitted by [DuploTrainHub
](#DuploTrainHub)
+
+| Param | Type | Description |
+| --- | --- | --- |
+| port | string
| |
+| color | number
| A number representing one of the LED color consts. |
+
diff --git a/consts.ts b/consts.ts
index 2ed1d74..de220c3 100644
--- a/consts.ts
+++ b/consts.ts
@@ -3,7 +3,8 @@ export enum Hubs {
WEDO2_SMART_HUB = 1,
BOOST_MOVE_HUB = 2,
POWERED_UP_HUB = 3,
- POWERED_UP_REMOTE = 4
+ POWERED_UP_REMOTE = 4,
+ DUPLO_TRAIN_HUB = 5
}
export enum Devices {
@@ -48,7 +49,8 @@ export enum ButtonStates {
export enum BLEManufacturerData {
BOOST_MOVE_HUB_ID = 64,
POWERED_UP_HUB_ID = 65,
- POWERED_UP_REMOTE_ID = 66
+ POWERED_UP_REMOTE_ID = 66,
+ DUPLO_TRAIN_HUB_ID = 66
}
export enum BLEServices {
diff --git a/duplotrainhub.ts b/duplotrainhub.ts
new file mode 100644
index 0000000..064ca6d
--- /dev/null
+++ b/duplotrainhub.ts
@@ -0,0 +1,150 @@
+import { Peripheral } from "noble";
+
+import { LPF2Hub } from "./lpf2hub";
+import { Port } from "./port";
+
+import * as Consts from "./consts";
+
+import Debug = require("debug");
+const debug = Debug("duplotrainhub");
+
+
+/**
+ * The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
+ * @class DuploTrainHub
+ * @extends LPF2Hub
+ * @extends Hub
+ */
+export class DuploTrainHub extends LPF2Hub {
+
+
+ // We set JSDoc to ignore these events as a Duplo Train Hub will never emit them.
+
+ /**
+ * @event DuploTrainHub#distance
+ * @ignore
+ */
+
+ /**
+ * @event DuploTrainHub#tilt
+ * @ignore
+ */
+
+ /**
+ * @event DuploTrainHub#rotate
+ * @ignore
+ */
+
+ /**
+ * @event DuploTrainHub#attach
+ * @ignore
+ */
+
+ /**
+ * @event DuploTrainHub#detach
+ * @ignore
+ */
+
+
+ public static IsDuploTrainHub (peripheral: Peripheral) {
+ return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID);
+ }
+
+
+ constructor (peripheral: Peripheral, autoSubscribe: boolean = true) {
+ super(peripheral, autoSubscribe);
+ this.type = Consts.Hubs.DUPLO_TRAIN_HUB;
+ this._ports = {
+ "MOTOR": new Port("MOTOR", 0),
+ "LIGHT": new Port("LIGHT", 1)
+ };
+ debug("Discovered Duplo Train Hub");
+ }
+
+
+ public connect () {
+ return new Promise(async (resolve, reject) => {
+ debug("Connecting to Duplo Train Hub");
+ await super.connect();
+ debug("Connect completed");
+ return resolve();
+ });
+ }
+
+
+ /**
+ * Set the motor speed on a given port.
+ * @method DuploTrainHub#setMotorSpeed
+ * @param {string} port
+ * @param {number | Array.