Duplo Train Base motor, speaker, color sensor, and speedometer

This commit is contained in:
Nathan Kellenicki 2018-08-31 20:38:32 -07:00
parent bec809ceaf
commit d9d1e8f72b
12 changed files with 312 additions and 210 deletions

162
DOCS.md
View File

@ -11,7 +11,7 @@
<dd></dd>
<dt><a href="#PUPRemote">PUPRemote</a><code>LPF2Hub</code></dt>
<dd></dd>
<dt><a href="#DuploTrainHub">DuploTrainHub</a><code>LPF2Hub</code></dt>
<dt><a href="#DuploTrainBase">DuploTrainBase</a><code>LPF2Hub</code></dt>
<dd></dd>
</dl>
@ -66,7 +66,7 @@ Emits when a Powered UP Hub device is found.
| Param | Type |
| --- | --- |
| hub | [<code>WeDo2SmartHub</code>](#WeDo2SmartHub) \| [<code>BoostMoveHub</code>](#BoostMoveHub) \| [<code>PUPHub</code>](#PUPHub) \| [<code>PUPRemote</code>](#PUPRemote) \| [<code>DuploTrainHub</code>](#DuploTrainHub) |
| hub | [<code>WeDo2SmartHub</code>](#WeDo2SmartHub) \| [<code>BoostMoveHub</code>](#BoostMoveHub) \| [<code>PUPHub</code>](#PUPHub) \| [<code>PUPRemote</code>](#PUPRemote) \| [<code>DuploTrainBase</code>](#DuploTrainBase) |
<a name="WeDo2SmartHub"></a>
@ -85,7 +85,7 @@ Emits when a Powered UP Hub device is found.
* [.setLEDRGB(red, green, blue)](#WeDo2SmartHub+setLEDRGB) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#WeDo2SmartHub+setMotorSpeed) ⇒ <code>Promise</code>
* [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#WeDo2SmartHub+rampMotorSpeed) ⇒ <code>Promise</code>
* [.playSound(frequency, time)](#WeDo2SmartHub+playSound) ⇒ <code>Promise</code>
* [.playTone(frequency, time)](#WeDo2SmartHub+playTone) ⇒ <code>Promise</code>
* [.setLightBrightness(port, brightness, [time])](#WeDo2SmartHub+setLightBrightness) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
@ -217,18 +217,18 @@ Ramp the motor speed on a given port.
| 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="WeDo2SmartHub+playSound"></a>
<a name="WeDo2SmartHub+playTone"></a>
### weDo2SmartHub.playSound(frequency, time) ⇒ <code>Promise</code>
Play a sound on the Hub's in-built buzzer
### weDo2SmartHub.playTone(frequency, time) ⇒ <code>Promise</code>
Play a tone on the Hub's in-built buzzer
**Kind**: instance method of [<code>WeDo2SmartHub</code>](#WeDo2SmartHub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command (ie. once the sound has finished playing).
**Returns**: <code>Promise</code> - Resolved upon successful completion of command (ie. once the tone has finished playing).
| Param | Type | Description |
| --- | --- | --- |
| frequency | <code>number</code> | |
| time | <code>number</code> | How long the sound should play for (in milliseconds). |
| time | <code>number</code> | How long the tone should play for (in milliseconds). |
<a name="WeDo2SmartHub+setLightBrightness"></a>
@ -1208,41 +1208,42 @@ 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>
<a name="DuploTrainBase"></a>
## DuploTrainHub<code>LPF2Hub</code>
## DuploTrainBase<code>LPF2Hub</code>
**Kind**: global class
**Extends**: <code>LPF2Hub</code>, <code>Hub</code>
* [DuploTrainHub](#DuploTrainHub) ⇐ <code>LPF2Hub</code>
* [new DuploTrainHub()](#new_DuploTrainHub_new)
* [DuploTrainBase](#DuploTrainBase) ⇐ <code>LPF2Hub</code>
* [new DuploTrainBase()](#new_DuploTrainBase_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>
* [.setLEDColor(color)](#DuploTrainBase+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#DuploTrainBase+setMotorSpeed) ⇒ <code>Promise</code>
* [.playSound(sound)](#DuploTrainBase+playSound) ⇒ <code>Promise</code>
* [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainBase+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)
* ["speed" (port, speed)](#LPF2Hub+event_speed)
<a name="new_DuploTrainHub_new"></a>
<a name="new_DuploTrainBase_new"></a>
### new DuploTrainHub()
The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
### new DuploTrainBase()
The DuploTrainBase is emitted if the discovered device is a Duplo Train Base.
<a name="LPF2Hub+current"></a>
### duploTrainHub.current
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.current
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Read only**: true
**Properties**
@ -1252,8 +1253,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="Hub+name"></a>
### duploTrainHub.name
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.name
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>name</code>](#Hub+name)
**Read only**: true
**Properties**
@ -1264,8 +1265,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="Hub+uuid"></a>
### duploTrainHub.uuid
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.uuid
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>uuid</code>](#Hub+uuid)
**Read only**: true
**Properties**
@ -1276,8 +1277,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="Hub+rssi"></a>
### duploTrainHub.rssi
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.rssi
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>rssi</code>](#Hub+rssi)
**Read only**: true
**Properties**
@ -1288,8 +1289,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="Hub+batteryLevel"></a>
### duploTrainHub.batteryLevel
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.batteryLevel
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>batteryLevel</code>](#Hub+batteryLevel)
**Read only**: true
**Properties**
@ -1298,12 +1299,25 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
| --- | --- | --- |
| batteryLevel | <code>number</code> | Battery level of the hub (Percentage between 0-100) |
<a name="DuploTrainHub+setMotorSpeed"></a>
<a name="DuploTrainBase+setLEDColor"></a>
### duploTrainHub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
### duploTrainBase.setLEDColor(color) ⇒ <code>Promise</code>
Set the color of the LED on the train via a color value.
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>setLEDColor</code>](#LPF2Hub+setLEDColor)
**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="DuploTrainBase+setMotorSpeed"></a>
### duploTrainBase.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
| Param | Type | Description |
@ -1312,12 +1326,24 @@ Set the motor speed on a given port.
| 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>
<a name="DuploTrainBase+playSound"></a>
### duploTrainHub.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ <code>Promise</code>
### duploTrainBase.playSound(sound) ⇒ <code>Promise</code>
Play a built-in train sound.
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| sound | <code>number</code> | A number representing one of the Train Base sound consts. |
<a name="DuploTrainBase+rampMotorSpeed"></a>
### duploTrainBase.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ <code>Promise</code>
Ramp the motor speed on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
| Param | Type | Description |
@ -1329,50 +1355,38 @@ Ramp the motor speed on a given port.
<a name="LPF2Hub+setName"></a>
### duploTrainHub.setName(name) ⇒ <code>Promise</code>
### duploTrainBase.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**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>
### duploTrainBase.connect() ⇒ <code>Promise</code>
Connect to the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>connect</code>](#Hub+connect)
**Returns**: <code>Promise</code> - Resolved upon successful connect.
<a name="Hub+disconnect"></a>
### duploTrainHub.disconnect() ⇒ <code>Promise</code>
### duploTrainBase.disconnect() ⇒ <code>Promise</code>
Disconnect the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**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>
### duploTrainBase.subscribe(port, [mode]) ⇒ <code>Promise</code>
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>subscribe</code>](#Hub+subscribe)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
@ -1383,10 +1397,10 @@ Subscribe to sensor notifications on a given port.
<a name="Hub+unsubscribe"></a>
### duploTrainHub.unsubscribe(port) ⇒ <code>Promise</code>
### duploTrainBase.unsubscribe(port) ⇒ <code>Promise</code>
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>unsubscribe</code>](#Hub+unsubscribe)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
@ -1396,12 +1410,12 @@ Unsubscribe to sensor notifications on a given port.
<a name="Hub+sleep"></a>
### duploTrainHub.sleep(delay) ⇒ <code>Promise</code>
### duploTrainBase.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)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>sleep</code>](#Hub+sleep)
**Returns**: <code>Promise</code> - Resolved after the delay is finished.
@ -1411,12 +1425,12 @@ This is a helper method to make it easier to add delays into a chain of commands
<a name="Hub+wait"></a>
### duploTrainHub.wait(commands) ⇒ <code>Promise</code>
### duploTrainBase.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)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>wait</code>](#Hub+wait)
**Returns**: <code>Promise</code> - Resolved after the commands are finished.
@ -1424,27 +1438,27 @@ This is a helper method to make it easier to wait for concurrent commands to com
| --- | --- | --- |
| 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)
**Kind**: event emitted by [<code>DuploTrainBase</code>](#DuploTrainBase)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="LPF2Hub+event_speed"></a>
### "speed" (port, speed)
Emits on a speed change.
**Kind**: event emitted by [<code>DuploTrainBase</code>](#DuploTrainBase)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
| speed | <code>number</code> |

162
README.md
View File

@ -90,7 +90,7 @@ 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>
<dt><a href="#DuploTrainBase">DuploTrainBase</a><code>LPF2Hub</code></dt>
<dd></dd>
</dl>
@ -145,7 +145,7 @@ Emits when a Powered UP Hub device is found.
| Param | Type |
| --- | --- |
| hub | [<code>WeDo2SmartHub</code>](#WeDo2SmartHub) \| [<code>BoostMoveHub</code>](#BoostMoveHub) \| [<code>PUPHub</code>](#PUPHub) \| [<code>PUPRemote</code>](#PUPRemote) \| [<code>DuploTrainHub</code>](#DuploTrainHub) |
| hub | [<code>WeDo2SmartHub</code>](#WeDo2SmartHub) \| [<code>BoostMoveHub</code>](#BoostMoveHub) \| [<code>PUPHub</code>](#PUPHub) \| [<code>PUPRemote</code>](#PUPRemote) \| [<code>DuploTrainBase</code>](#DuploTrainBase) |
<a name="WeDo2SmartHub"></a>
@ -164,7 +164,7 @@ Emits when a Powered UP Hub device is found.
* [.setLEDRGB(red, green, blue)](#WeDo2SmartHub+setLEDRGB) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#WeDo2SmartHub+setMotorSpeed) ⇒ <code>Promise</code>
* [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#WeDo2SmartHub+rampMotorSpeed) ⇒ <code>Promise</code>
* [.playSound(frequency, time)](#WeDo2SmartHub+playSound) ⇒ <code>Promise</code>
* [.playTone(frequency, time)](#WeDo2SmartHub+playTone) ⇒ <code>Promise</code>
* [.setLightBrightness(port, brightness, [time])](#WeDo2SmartHub+setLightBrightness) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
@ -296,18 +296,18 @@ Ramp the motor speed on a given port.
| 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="WeDo2SmartHub+playSound"></a>
<a name="WeDo2SmartHub+playTone"></a>
### weDo2SmartHub.playSound(frequency, time) ⇒ <code>Promise</code>
Play a sound on the Hub's in-built buzzer
### weDo2SmartHub.playTone(frequency, time) ⇒ <code>Promise</code>
Play a tone on the Hub's in-built buzzer
**Kind**: instance method of [<code>WeDo2SmartHub</code>](#WeDo2SmartHub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command (ie. once the sound has finished playing).
**Returns**: <code>Promise</code> - Resolved upon successful completion of command (ie. once the tone has finished playing).
| Param | Type | Description |
| --- | --- | --- |
| frequency | <code>number</code> | |
| time | <code>number</code> | How long the sound should play for (in milliseconds). |
| time | <code>number</code> | How long the tone should play for (in milliseconds). |
<a name="WeDo2SmartHub+setLightBrightness"></a>
@ -1287,41 +1287,42 @@ 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>
<a name="DuploTrainBase"></a>
## DuploTrainHub<code>LPF2Hub</code>
## DuploTrainBase<code>LPF2Hub</code>
**Kind**: global class
**Extends**: <code>LPF2Hub</code>, <code>Hub</code>
* [DuploTrainHub](#DuploTrainHub) ⇐ <code>LPF2Hub</code>
* [new DuploTrainHub()](#new_DuploTrainHub_new)
* [DuploTrainBase](#DuploTrainBase) ⇐ <code>LPF2Hub</code>
* [new DuploTrainBase()](#new_DuploTrainBase_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>
* [.setLEDColor(color)](#DuploTrainBase+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#DuploTrainBase+setMotorSpeed) ⇒ <code>Promise</code>
* [.playSound(sound)](#DuploTrainBase+playSound) ⇒ <code>Promise</code>
* [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainBase+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)
* ["speed" (port, speed)](#LPF2Hub+event_speed)
<a name="new_DuploTrainHub_new"></a>
<a name="new_DuploTrainBase_new"></a>
### new DuploTrainHub()
The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
### new DuploTrainBase()
The DuploTrainBase is emitted if the discovered device is a Duplo Train Base.
<a name="LPF2Hub+current"></a>
### duploTrainHub.current
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.current
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Read only**: true
**Properties**
@ -1331,8 +1332,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="Hub+name"></a>
### duploTrainHub.name
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.name
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>name</code>](#Hub+name)
**Read only**: true
**Properties**
@ -1343,8 +1344,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="Hub+uuid"></a>
### duploTrainHub.uuid
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.uuid
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>uuid</code>](#Hub+uuid)
**Read only**: true
**Properties**
@ -1355,8 +1356,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="Hub+rssi"></a>
### duploTrainHub.rssi
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.rssi
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>rssi</code>](#Hub+rssi)
**Read only**: true
**Properties**
@ -1367,8 +1368,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
<a name="Hub+batteryLevel"></a>
### duploTrainHub.batteryLevel
**Kind**: instance property of [<code>DuploTrainHub</code>](#DuploTrainHub)
### duploTrainBase.batteryLevel
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>batteryLevel</code>](#Hub+batteryLevel)
**Read only**: true
**Properties**
@ -1377,12 +1378,25 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
| --- | --- | --- |
| batteryLevel | <code>number</code> | Battery level of the hub (Percentage between 0-100) |
<a name="DuploTrainHub+setMotorSpeed"></a>
<a name="DuploTrainBase+setLEDColor"></a>
### duploTrainHub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
### duploTrainBase.setLEDColor(color) ⇒ <code>Promise</code>
Set the color of the LED on the train via a color value.
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>setLEDColor</code>](#LPF2Hub+setLEDColor)
**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="DuploTrainBase+setMotorSpeed"></a>
### duploTrainBase.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
| Param | Type | Description |
@ -1391,12 +1405,24 @@ Set the motor speed on a given port.
| 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>
<a name="DuploTrainBase+playSound"></a>
### duploTrainHub.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ <code>Promise</code>
### duploTrainBase.playSound(sound) ⇒ <code>Promise</code>
Play a built-in train sound.
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| sound | <code>number</code> | A number representing one of the Train Base sound consts. |
<a name="DuploTrainBase+rampMotorSpeed"></a>
### duploTrainBase.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ <code>Promise</code>
Ramp the motor speed on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
| Param | Type | Description |
@ -1408,50 +1434,38 @@ Ramp the motor speed on a given port.
<a name="LPF2Hub+setName"></a>
### duploTrainHub.setName(name) ⇒ <code>Promise</code>
### duploTrainBase.setName(name) ⇒ <code>Promise</code>
Set the name of the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**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>
### duploTrainBase.connect() ⇒ <code>Promise</code>
Connect to the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>connect</code>](#Hub+connect)
**Returns**: <code>Promise</code> - Resolved upon successful connect.
<a name="Hub+disconnect"></a>
### duploTrainHub.disconnect() ⇒ <code>Promise</code>
### duploTrainBase.disconnect() ⇒ <code>Promise</code>
Disconnect the Hub.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**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>
### duploTrainBase.subscribe(port, [mode]) ⇒ <code>Promise</code>
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>subscribe</code>](#Hub+subscribe)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
@ -1462,10 +1476,10 @@ Subscribe to sensor notifications on a given port.
<a name="Hub+unsubscribe"></a>
### duploTrainHub.unsubscribe(port) ⇒ <code>Promise</code>
### duploTrainBase.unsubscribe(port) ⇒ <code>Promise</code>
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>DuploTrainHub</code>](#DuploTrainHub)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>unsubscribe</code>](#Hub+unsubscribe)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
@ -1475,12 +1489,12 @@ Unsubscribe to sensor notifications on a given port.
<a name="Hub+sleep"></a>
### duploTrainHub.sleep(delay) ⇒ <code>Promise</code>
### duploTrainBase.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)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>sleep</code>](#Hub+sleep)
**Returns**: <code>Promise</code> - Resolved after the delay is finished.
@ -1490,12 +1504,12 @@ This is a helper method to make it easier to add delays into a chain of commands
<a name="Hub+wait"></a>
### duploTrainHub.wait(commands) ⇒ <code>Promise</code>
### duploTrainBase.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)
**Kind**: instance method of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Overrides**: [<code>wait</code>](#Hub+wait)
**Returns**: <code>Promise</code> - Resolved after the commands are finished.
@ -1503,27 +1517,27 @@ This is a helper method to make it easier to wait for concurrent commands to com
| --- | --- | --- |
| 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)
**Kind**: event emitted by [<code>DuploTrainBase</code>](#DuploTrainBase)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="LPF2Hub+event_speed"></a>
### "speed" (port, speed)
Emits on a speed change.
**Kind**: event emitted by [<code>DuploTrainBase</code>](#DuploTrainBase)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
| speed | <code>number</code> |

View File

@ -18,6 +18,14 @@ const debug = Debug("boostmovehub");
export class BoostMoveHub extends LPF2Hub {
// We set JSDoc to ignore these events as a Boost Move Hub will never emit them.
/**
* @event BoostMoveHub#speed
* @ignore
*/
public static IsBoostMoveHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID);
}

View File

@ -7,6 +7,7 @@ export enum Hubs {
DUPLO_TRAIN_HUB = 5
}
export enum Devices {
UNKNOWN = 0,
BASIC_MOTOR = 1,
@ -19,6 +20,10 @@ export enum Devices {
BOOST_TACHO_MOTOR = 38,
BOOST_MOVE_HUB_MOTOR = 39,
BOOST_TILT = 40,
DUPLO_TRAIN_BASE_MOTOR = 41,
DUPLO_TRAIN_BASE_SPEAKER = 42,
DUPLO_TRAIN_BASE_COLOR = 43,
DUPLO_TRAIN_BASE_SPEEDOMETER = 44,
POWERED_UP_REMOTE_BUTTON = 55
}
@ -38,6 +43,7 @@ export enum Colors {
NONE = 255
}
export enum ButtonStates {
PRESSED = 0,
RELEASED = 1,
@ -46,13 +52,22 @@ export enum ButtonStates {
STOP = 4
}
export enum DuploTrainBaseSounds {
STATION_DEPARTURE = 5,
WATER_REFILL = 7,
HORN = 9
}
export enum BLEManufacturerData {
BOOST_MOVE_HUB_ID = 64,
POWERED_UP_HUB_ID = 65,
POWERED_UP_REMOTE_ID = 66,
DUPLO_TRAIN_HUB_ID = 66
DUPLO_TRAIN_HUB_ID = 32
}
export enum BLEServices {
WEDO2_SMART_HUB = "000015231212efde1523785feabcd123",
LPF2_HUB = "000016231212efde1623785feabcd123"

View File

@ -6,47 +6,52 @@ import { Port } from "./port";
import * as Consts from "./consts";
import Debug = require("debug");
const debug = Debug("duplotrainhub");
const debug = Debug("duploTrainBase");
/**
* The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub.
* @class DuploTrainHub
* The DuploTrainBase is emitted if the discovered device is a Duplo Train Base.
* @class DuploTrainBase
* @extends LPF2Hub
* @extends Hub
*/
export class DuploTrainHub extends LPF2Hub {
export class DuploTrainBase extends LPF2Hub {
// We set JSDoc to ignore these events as a Duplo Train Hub will never emit them.
// We set JSDoc to ignore these events as a Duplo Train Base will never emit them.
/**
* @event DuploTrainHub#distance
* @event DuploTrainBase#distance
* @ignore
*/
/**
* @event DuploTrainHub#tilt
* @event DuploTrainBase#tilt
* @ignore
*/
/**
* @event DuploTrainHub#rotate
* @event DuploTrainBase#rotate
* @ignore
*/
/**
* @event DuploTrainHub#attach
* @event DuploTrainBase#button
* @ignore
*/
/**
* @event DuploTrainHub#detach
* @event DuploTrainBase#attach
* @ignore
*/
/**
* @event DuploTrainBase#detach
* @ignore
*/
public static IsDuploTrainHub (peripheral: Peripheral) {
public static IsDuploTrainBase (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID);
}
@ -55,15 +60,17 @@ export class DuploTrainHub extends LPF2Hub {
super(peripheral, autoSubscribe);
this.type = Consts.Hubs.DUPLO_TRAIN_HUB;
this._ports = {
"MOTOR": new Port("MOTOR", 0)
"MOTOR": new Port("MOTOR", 0),
"COLOR": new Port("COLOR", 18),
"SPEEDOMETER": new Port("SPEEDOMETER", 19)
};
debug("Discovered Duplo Train Hub");
debug("Discovered Duplo Train Base");
}
public connect () {
return new Promise(async (resolve, reject) => {
debug("Connecting to Duplo Train Hub");
debug("Connecting to Duplo Train Base");
await super.connect();
debug("Connect completed");
return resolve();
@ -71,26 +78,34 @@ export class DuploTrainHub extends LPF2Hub {
}
/**
* Set the color of the LED on the train via a color value.
* @method DuploTrainBase#setLEDColor
* @param {number} color A number representing one of the LED color consts.
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setLEDColor (color: number | boolean) {
return new Promise((resolve, reject) => {
if (color === false) {
color = 0;
}
const data = Buffer.from([0x81, 0x11, 0x11, 0x51, 0x00, color]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();
});
}
/**
* Set the motor speed on a given port.
* @method DuploTrainHub#setMotorSpeed
* @method DuploTrainBase#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) {
public setMotorSpeed (port: string, speed: number, time?: number | boolean) {
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`);
}
}
let cancelEventTimer = true;
if (typeof time === "boolean") {
if (time === true) {
@ -102,34 +117,17 @@ export class DuploTrainHub extends LPF2Hub {
portObj.cancelEventTimer();
}
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]);
}
if (time && typeof time === "number") {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
const timeout = global.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]);
}
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();
}, time);
portObj.setEventTimer(timeout);
} 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]);
}
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();
}
@ -137,9 +135,24 @@ export class DuploTrainHub extends LPF2Hub {
}
/**
* Play a built-in train sound.
* @method DuploTrainBase#playSound
* @param {number} sound A number representing one of the Train Base sound consts.
* @returns {Promise} Resolved upon successful issuance of command.
*/
public playSound (sound: number) {
return new Promise((resolve, reject) => {
const data = Buffer.from([0x81, 0x01, 0x11, 0x51, 0x01, sound]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();
});
}
/**
* Ramp the motor speed on a given port.
* @method DuploTrainHub#rampMotorSpeed
* @method DuploTrainBase#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.
@ -152,7 +165,7 @@ export class DuploTrainHub extends LPF2Hub {
return new Promise((resolve, reject) => {
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
.on("changeSpeed", (speed) => {
this.setMotorSpeed(port, speed);
this.setMotorSpeed(port, speed, true);
})
.on("finished", resolve);
});

2
hub.ts
View File

@ -356,8 +356,6 @@ export class Hub extends EventEmitter {
return (this.type === Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08);
case Consts.Devices.BOOST_TILT:
return 0x04;
case Consts.Devices.POWERED_UP_REMOTE_BUTTON:
return 0x00;
default:
return 0x00;
}

View File

@ -42,6 +42,9 @@ export class LPF2Hub extends Hub {
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports
if (this.type === Consts.Hubs.DUPLO_TRAIN_HUB) {
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]));
}
return resolve();
});
}
@ -348,6 +351,25 @@ export class LPF2Hub extends Hub {
}
break;
}
case Consts.Devices.DUPLO_TRAIN_BASE_COLOR:
{
if (data[4] <= 10) {
this.emit("color", port.id, data[4]);
}
break;
}
case Consts.Devices.DUPLO_TRAIN_BASE_SPEEDOMETER:
{
/**
* Emits on a speed change.
* @event LPF2Hub#speed
* @param {string} port
* @param {number} speed
*/
const speed = data.readInt16LE(4);
this.emit("speed", port.id, speed);
break;
}
}
}

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/duplotrainhub.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/duplotrainbase.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,7 +1,7 @@
import { Peripheral } from "noble";
import { BoostMoveHub } from "./boostmovehub";
import { DuploTrainHub } from "./duplotrainhub";
import { DuploTrainBase } from "./duplotrainbase";
import { Hub } from "./hub";
import { PUPHub } from "./puphub";
import { PUPRemote } from "./pupremote";
@ -67,8 +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 if (DuploTrainBase.IsDuploTrainBase(peripheral)) {
hub = new DuploTrainBase(peripheral, this.autoSubscribe);
} else {
return;
}
@ -95,7 +95,7 @@ export class PoweredUP extends EventEmitter {
/**
* Emits when a Powered UP Hub device is found.
* @event PoweredUP#discover
* @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote | DuploTrainHub} hub
* @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote | DuploTrainBase} hub
*/
this.emit("discover", hub);

View File

@ -25,6 +25,11 @@ export class PUPHub extends LPF2Hub {
* @ignore
*/
/**
* @event PUPHub#speed
* @ignore
*/
public static IsPUPHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID);

View File

@ -40,6 +40,11 @@ export class PUPRemote extends LPF2Hub {
* @ignore
*/
/**
* @event PUPRemote#speed
* @ignore
*/
/**
* @event PUPRemote#attach
* @ignore

View File

@ -17,6 +17,14 @@ const debug = Debug("wedo2smarthub");
export class WeDo2SmartHub extends Hub {
// We set JSDoc to ignore these events as a WeDo 2.0 Smart Hub will never emit them.
/**
* @event WeDo2SmartHub#speed
* @ignore
*/
public static IsWeDo2SmartHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.WEDO2_SMART_HUB) >= 0);
}
@ -173,13 +181,13 @@ export class WeDo2SmartHub extends Hub {
/**
* Play a sound on the Hub's in-built buzzer
* @method WeDo2SmartHub#playSound
* Play a tone on the Hub's in-built buzzer
* @method WeDo2SmartHub#playTone
* @param {number} frequency
* @param {number} time How long the sound should play for (in milliseconds).
* @returns {Promise} Resolved upon successful completion of command (ie. once the sound has finished playing).
* @param {number} time How long the tone should play for (in milliseconds).
* @returns {Promise} Resolved upon successful completion of command (ie. once the tone has finished playing).
*/
public playSound (frequency: number, time: number) {
public playTone (frequency: number, time: number) {
return new Promise((resolve, reject) => {
const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]);
data.writeUInt16LE(frequency, 3);