First pass at Duplo train support

This commit is contained in:
Nathan Kellenicki 2018-08-23 16:15:36 +01:00
parent 7a23697685
commit 9a3077866c
9 changed files with 642 additions and 6 deletions

242
DOCS.md
View File

@ -11,6 +11,8 @@
<dd></dd>
<dt><a href="#PUPRemote">PUPRemote</a><code>LPF2Hub</code></dt>
<dd></dd>
<dt><a href="#DuploTrainHub">DuploTrainHub</a><code>LPF2Hub</code></dt>
<dd></dd>
</dl>
<a name="PoweredUP"></a>
@ -1206,3 +1208,243 @@ Emits when a button is pressed.
| button | <code>string</code> | |
| state | <code>number</code> | A number representing one of the button state consts. |
<a name="DuploTrainHub"></a>
## DuploTrainHub ⇐ <code>LPF2Hub</code>
**Kind**: global class
**Extends**: <code>LPF2Hub</code>, <code>Hub</code>
* [DuploTrainHub](#DuploTrainHub) ⇐ <code>LPF2Hub</code>
* [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) ⇒ <code>Promise</code>
* [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainHub+rampMotorSpeed) ⇒ <code>Promise</code>
* [.setName(name)](#LPF2Hub+setName) ⇒ <code>Promise</code>
* [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
* [.unsubscribe(port)](#Hub+unsubscribe) ⇒ <code>Promise</code>
* [.sleep(delay)](#Hub+sleep) ⇒ <code>Promise</code>
* [.wait(commands)](#Hub+wait) ⇒ <code>Promise</code>
* ["button" (button, state)](#LPF2Hub+event_button)
* ["color" (port, color)](#LPF2Hub+event_color)
<a name="new_DuploTrainHub_new"></a>
### new DuploTrainHub()
The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="LPF2Hub+current"></a>
### duploTrainHub.current
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| current | <code>number</code> | Current usage of the hub (Amps) |
<a name="Hub+name"></a>
### duploTrainHub.name
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>name</code>](#Hub+name)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | Name of the hub |
<a name="Hub+uuid"></a>
### duploTrainHub.uuid
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>uuid</code>](#Hub+uuid)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| uuid | <code>string</code> | UUID of the hub |
<a name="Hub+rssi"></a>
### duploTrainHub.rssi
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>rssi</code>](#Hub+rssi)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| rssi | <code>number</code> | Signal strength of the hub |
<a name="Hub+batteryLevel"></a>
### duploTrainHub.batteryLevel
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>batteryLevel</code>](#Hub+batteryLevel)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| batteryLevel | <code>number</code> | Battery level of the hub (Percentage between 0-100) |
<a name="DuploTrainHub+setMotorSpeed"></a>
### duploTrainHub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| 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="DuploTrainHub+rampMotorSpeed"></a>
### duploTrainHub.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ <code>Promise</code>
Ramp the motor speed on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| fromSpeed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| toSpeed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| time | <code>number</code> | How long the ramp should last (in milliseconds). |
<a name="LPF2Hub+setName"></a>
### duploTrainHub.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | New name of the hub (14 characters or less, ASCII only). |
<a name="LPF2Hub+setLEDColor"></a>
### duploTrainHub.setLEDColor(color) ⇒ <code>Promise</code>
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="Hub+connect"></a>
### duploTrainHub.connect() ⇒ <code>Promise</code>
Connect to the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>connect</code>](#Hub+connect)
**Returns**: <code>Promise</code> - Resolved upon successful connect.
<a name="Hub+disconnect"></a>
### duploTrainHub.disconnect() ⇒ <code>Promise</code>
Disconnect the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>disconnect</code>](#Hub+disconnect)
**Returns**: <code>Promise</code> - Resolved upon successful disconnect.
<a name="Hub+subscribe"></a>
### duploTrainHub.subscribe(port, [mode]) ⇒ <code>Promise</code>
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>subscribe</code>](#Hub+subscribe)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| [mode] | <code>number</code> | The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
<a name="Hub+unsubscribe"></a>
### duploTrainHub.unsubscribe(port) ⇒ <code>Promise</code>
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>unsubscribe</code>](#Hub+unsubscribe)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type |
| --- | --- |
| port | <code>string</code> |
<a name="Hub+sleep"></a>
### duploTrainHub.sleep(delay) ⇒ <code>Promise</code>
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 [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>sleep</code>](#Hub+sleep)
**Returns**: <code>Promise</code> - Resolved after the delay is finished.
| Param | Type | Description |
| --- | --- | --- |
| delay | <code>number</code> | How long to sleep (in milliseconds). |
<a name="Hub+wait"></a>
### duploTrainHub.wait(commands) ⇒ <code>Promise</code>
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 [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>wait</code>](#Hub+wait)
**Returns**: <code>Promise</code> - Resolved after the commands are finished.
| Param | Type | Description |
| --- | --- | --- |
| commands | <code>Array.&lt;Promise.&lt;any&gt;&gt;</code> | Array of executing commands. |
<a name="LPF2Hub+event_button"></a>
### "button" (button, state)
Emits when a button is pressed.
**Kind**: event emitted by [<code>DuploTrainHub</code>](#DuploTrainHub)
| Param | Type | Description |
| --- | --- | --- |
| button | <code>string</code> | |
| state | <code>number</code> | A number representing one of the button state consts. |
<a name="LPF2Hub+event_color"></a>
### "color" (port, color)
Emits when a color sensor is activated.
**Kind**: event emitted by [<code>DuploTrainHub</code>](#DuploTrainHub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| color | <code>number</code> | A number representing one of the LED color consts. |

242
README.md
View File

@ -90,6 +90,8 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R
<dd></dd>
<dt><a href="#PUPRemote">PUPRemote</a><code>LPF2Hub</code></dt>
<dd></dd>
<dt><a href="#DuploTrainHub">DuploTrainHub</a><code>LPF2Hub</code></dt>
<dd></dd>
</dl>
<a name="PoweredUP"></a>
@ -1285,3 +1287,243 @@ Emits when a button is pressed.
| button | <code>string</code> | |
| state | <code>number</code> | A number representing one of the button state consts. |
<a name="DuploTrainHub"></a>
## DuploTrainHub ⇐ <code>LPF2Hub</code>
**Kind**: global class
**Extends**: <code>LPF2Hub</code>, <code>Hub</code>
* [DuploTrainHub](#DuploTrainHub) ⇐ <code>LPF2Hub</code>
* [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) ⇒ <code>Promise</code>
* [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainHub+rampMotorSpeed) ⇒ <code>Promise</code>
* [.setName(name)](#LPF2Hub+setName) ⇒ <code>Promise</code>
* [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
* [.unsubscribe(port)](#Hub+unsubscribe) ⇒ <code>Promise</code>
* [.sleep(delay)](#Hub+sleep) ⇒ <code>Promise</code>
* [.wait(commands)](#Hub+wait) ⇒ <code>Promise</code>
* ["button" (button, state)](#LPF2Hub+event_button)
* ["color" (port, color)](#LPF2Hub+event_color)
<a name="new_DuploTrainHub_new"></a>
### new DuploTrainHub()
The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="LPF2Hub+current"></a>
### duploTrainHub.current
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| current | <code>number</code> | Current usage of the hub (Amps) |
<a name="Hub+name"></a>
### duploTrainHub.name
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>name</code>](#Hub+name)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | Name of the hub |
<a name="Hub+uuid"></a>
### duploTrainHub.uuid
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>uuid</code>](#Hub+uuid)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| uuid | <code>string</code> | UUID of the hub |
<a name="Hub+rssi"></a>
### duploTrainHub.rssi
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>rssi</code>](#Hub+rssi)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| rssi | <code>number</code> | Signal strength of the hub |
<a name="Hub+batteryLevel"></a>
### duploTrainHub.batteryLevel
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>batteryLevel</code>](#Hub+batteryLevel)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| batteryLevel | <code>number</code> | Battery level of the hub (Percentage between 0-100) |
<a name="DuploTrainHub+setMotorSpeed"></a>
### duploTrainHub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| 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="DuploTrainHub+rampMotorSpeed"></a>
### duploTrainHub.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ <code>Promise</code>
Ramp the motor speed on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| fromSpeed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| toSpeed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| time | <code>number</code> | How long the ramp should last (in milliseconds). |
<a name="LPF2Hub+setName"></a>
### duploTrainHub.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | New name of the hub (14 characters or less, ASCII only). |
<a name="LPF2Hub+setLEDColor"></a>
### duploTrainHub.setLEDColor(color) ⇒ <code>Promise</code>
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="Hub+connect"></a>
### duploTrainHub.connect() ⇒ <code>Promise</code>
Connect to the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>connect</code>](#Hub+connect)
**Returns**: <code>Promise</code> - Resolved upon successful connect.
<a name="Hub+disconnect"></a>
### duploTrainHub.disconnect() ⇒ <code>Promise</code>
Disconnect the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>disconnect</code>](#Hub+disconnect)
**Returns**: <code>Promise</code> - Resolved upon successful disconnect.
<a name="Hub+subscribe"></a>
### duploTrainHub.subscribe(port, [mode]) ⇒ <code>Promise</code>
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>subscribe</code>](#Hub+subscribe)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| [mode] | <code>number</code> | The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
<a name="Hub+unsubscribe"></a>
### duploTrainHub.unsubscribe(port) ⇒ <code>Promise</code>
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>unsubscribe</code>](#Hub+unsubscribe)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type |
| --- | --- |
| port | <code>string</code> |
<a name="Hub+sleep"></a>
### duploTrainHub.sleep(delay) ⇒ <code>Promise</code>
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 [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>sleep</code>](#Hub+sleep)
**Returns**: <code>Promise</code> - Resolved after the delay is finished.
| Param | Type | Description |
| --- | --- | --- |
| delay | <code>number</code> | How long to sleep (in milliseconds). |
<a name="Hub+wait"></a>
### duploTrainHub.wait(commands) ⇒ <code>Promise</code>
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 [<code>DuploTrainHub</code>](#DuploTrainHub)
**Overrides**: [<code>wait</code>](#Hub+wait)
**Returns**: <code>Promise</code> - Resolved after the commands are finished.
| Param | Type | Description |
| --- | --- | --- |
| commands | <code>Array.&lt;Promise.&lt;any&gt;&gt;</code> | Array of executing commands. |
<a name="LPF2Hub+event_button"></a>
### "button" (button, state)
Emits when a button is pressed.
**Kind**: event emitted by [<code>DuploTrainHub</code>](#DuploTrainHub)
| Param | Type | Description |
| --- | --- | --- |
| button | <code>string</code> | |
| state | <code>number</code> | A number representing one of the button state consts. |
<a name="LPF2Hub+event_color"></a>
### "color" (port, color)
Emits when a color sensor is activated.
**Kind**: event emitted by [<code>DuploTrainHub</code>](#DuploTrainHub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| color | <code>number</code> | A number representing one of the LED color consts. |

View File

@ -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 {

150
duplotrainhub.ts Normal file
View File

@ -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.<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 | [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`);
}
if (portObj.id === "AB") {
const portObjA = this._portLookup("A");
const portObjB = this._portLookup("B");
if (portObjA.type !== portObjB.type) {
throw new Error(`Port ${portObj.id} requires both motors be of the same type`);
}
}
return new Promise((resolve, reject) => {
if (time) {
let data = null;
if (portObj.id === "AB") {
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed)]);
} else {
// @ts-ignore: The type of speed is properly checked at the start
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
}
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
setTimeout(() => {
let data = null;
if (portObj.id === "AB") {
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, 0x00, 0x00]);
} else {
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]);
}
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();
}, time);
} else {
let data = null;
if (portObj.id === "AB") {
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed)]);
} else {
// @ts-ignore: The type of speed is properly checked at the start
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
}
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();
}
});
}
/**
* Ramp the motor speed on a given port.
* @method DuploTrainHub#rampMotorSpeed
* @param {string} port
* @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @param {number} time How long the ramp should last (in milliseconds).
* @returns {Promise} Resolved upon successful completion of command.
*/
public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) {
return new Promise((resolve, reject) => {
this._calculateRamp(fromSpeed, toSpeed, time)
.on("changeSpeed", (speed) => {
this.setMotorSpeed(port, speed);
})
.on("finished", resolve);
});
}
}

1
hub.ts
View File

@ -6,7 +6,6 @@ import { Port } from "./port";
import * as Consts from "./consts";
import Debug = require("debug");
import { resolve } from "path";
const debug = Debug("hub");

View File

@ -7,7 +7,7 @@
"types": "dist/poweredup.d.ts",
"scripts": {
"build": "tslint -c tslint.json \"*.ts\" && tsc",
"docs": "jsdoc2md dist/poweredup.js dist/lpf2hub.js dist/wedo2smarthub.js dist/boostmovehub.js dist/puphub.js dist/pupremote.js dist/hub.js dist/consts.js > DOCS.md",
"docs": "jsdoc2md dist/poweredup.js dist/lpf2hub.js dist/wedo2smarthub.js dist/boostmovehub.js dist/puphub.js dist/pupremote.js dist/duplotrainhub.js dist/hub.js dist/consts.js > DOCS.md",
"readme": "cat ABOUT.md DOCS.md > README.md",
"all": "npm run build && npm run docs && npm run readme",
"prepublishOnly": "npm run build"

View File

@ -1,6 +1,7 @@
import { Peripheral } from "noble";
import { BoostMoveHub } from "./boostmovehub";
import { DuploTrainHub } from "./duplotrainhub";
import { Hub } from "./hub";
import { PUPHub } from "./puphub";
import { PUPRemote } from "./pupremote";
@ -66,6 +67,8 @@ export class PoweredUP extends EventEmitter {
hub = new PUPHub(peripheral, this.autoSubscribe);
} else if (PUPRemote.IsPUPRemote(peripheral)) {
hub = new PUPRemote(peripheral, this.autoSubscribe);
} else if (DuploTrainHub.IsDuploTrainHub(peripheral)) {
hub = new DuploTrainHub(peripheral, this.autoSubscribe);
} else {
return;
}

View File

@ -6,7 +6,6 @@ import { Port } from "./port";
import * as Consts from "./consts";
import Debug = require("debug");
import { resolve } from "path";
const debug = Debug("puphub");

View File

@ -6,7 +6,6 @@ import { Port } from "./port";
import * as Consts from "./consts";
import Debug = require("debug");
import { resolve } from "path";
const debug = Debug("wedo2smarthub");