diff --git a/DOCS.md b/DOCS.md index 9ff14b0..f60fa32 100644 --- a/DOCS.md +++ b/DOCS.md @@ -11,7 +11,7 @@
PUPRemoteLPF2Hub
-
DuploTrainHubLPF2Hub
+
DuploTrainBaseLPF2Hub
@@ -66,7 +66,7 @@ Emits when a Powered UP Hub device is found. | Param | Type | | --- | --- | -| hub | [WeDo2SmartHub](#WeDo2SmartHub) \| [BoostMoveHub](#BoostMoveHub) \| [PUPHub](#PUPHub) \| [PUPRemote](#PUPRemote) \| [DuploTrainHub](#DuploTrainHub) | +| hub | [WeDo2SmartHub](#WeDo2SmartHub) \| [BoostMoveHub](#BoostMoveHub) \| [PUPHub](#PUPHub) \| [PUPRemote](#PUPRemote) \| [DuploTrainBase](#DuploTrainBase) | @@ -85,7 +85,7 @@ Emits when a Powered UP Hub device is found. * [.setLEDRGB(red, green, blue)](#WeDo2SmartHub+setLEDRGB) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#WeDo2SmartHub+setMotorSpeed) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#WeDo2SmartHub+rampMotorSpeed) ⇒ Promise - * [.playSound(frequency, time)](#WeDo2SmartHub+playSound) ⇒ Promise + * [.playTone(frequency, time)](#WeDo2SmartHub+playTone) ⇒ Promise * [.setLightBrightness(port, brightness, [time])](#WeDo2SmartHub+setLightBrightness) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise * [.disconnect()](#Hub+disconnect) ⇒ Promise @@ -217,18 +217,18 @@ Ramp the motor speed on a given port. | 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). | - + -### weDo2SmartHub.playSound(frequency, time) ⇒ Promise -Play a sound on the Hub's in-built buzzer +### weDo2SmartHub.playTone(frequency, time) ⇒ Promise +Play a tone on the Hub's in-built buzzer **Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) -**Returns**: Promise - Resolved upon successful completion of command (ie. once the sound has finished playing). +**Returns**: Promise - Resolved upon successful completion of command (ie. once the tone has finished playing). | Param | Type | Description | | --- | --- | --- | | frequency | number | | -| time | number | How long the sound should play for (in milliseconds). | +| time | number | How long the tone should play for (in milliseconds). | @@ -1208,41 +1208,42 @@ Emits when a button is pressed. | button | string | | | state | number | A number representing one of the button state consts. | - + -## DuploTrainHub ⇐ LPF2Hub +## DuploTrainBase ⇐ LPF2Hub **Kind**: global class **Extends**: LPF2Hub, Hub -* [DuploTrainHub](#DuploTrainHub) ⇐ LPF2Hub - * [new DuploTrainHub()](#new_DuploTrainHub_new) +* [DuploTrainBase](#DuploTrainBase) ⇐ LPF2Hub + * [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) ⇒ Promise - * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainHub+rampMotorSpeed) ⇒ Promise + * [.setLEDColor(color)](#DuploTrainBase+setLEDColor) ⇒ Promise + * [.setMotorSpeed(port, speed, [time])](#DuploTrainBase+setMotorSpeed) ⇒ Promise + * [.playSound(sound)](#DuploTrainBase+playSound) ⇒ Promise + * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainBase+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) + * ["speed" (port, speed)](#LPF2Hub+event_speed) - + -### 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. -### duploTrainHub.current -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.current +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Read only**: true **Properties** @@ -1252,8 +1253,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. -### duploTrainHub.name -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.name +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Overrides**: [name](#Hub+name) **Read only**: true **Properties** @@ -1264,8 +1265,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. -### duploTrainHub.uuid -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.uuid +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Overrides**: [uuid](#Hub+uuid) **Read only**: true **Properties** @@ -1276,8 +1277,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. -### duploTrainHub.rssi -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.rssi +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Overrides**: [rssi](#Hub+rssi) **Read only**: true **Properties** @@ -1288,8 +1289,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. -### duploTrainHub.batteryLevel -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.batteryLevel +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Overrides**: [batteryLevel](#Hub+batteryLevel) **Read only**: true **Properties** @@ -1298,12 +1299,25 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. | --- | --- | --- | | batteryLevel | number | Battery level of the hub (Percentage between 0-100) | - + -### duploTrainHub.setMotorSpeed(port, speed, [time]) ⇒ Promise +### duploTrainBase.setLEDColor(color) ⇒ Promise +Set the color of the LED on the train via a color value. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [setLEDColor](#LPF2Hub+setLEDColor) +**Returns**: Promise - Resolved upon successful issuance of command. + +| Param | Type | Description | +| --- | --- | --- | +| color | number | A number representing one of the LED color consts. | + + + +### duploTrainBase.setMotorSpeed(port, speed, [time]) ⇒ Promise Set the motor speed on a given port. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Returns**: Promise - 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 | 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 +### duploTrainBase.playSound(sound) ⇒ Promise +Play a built-in train sound. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Returns**: Promise - Resolved upon successful issuance of command. + +| Param | Type | Description | +| --- | --- | --- | +| sound | number | A number representing one of the Train Base sound consts. | + + + +### duploTrainBase.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ Promise Ramp the motor speed on a given port. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Returns**: Promise - Resolved upon successful completion of command. | Param | Type | Description | @@ -1329,50 +1355,38 @@ Ramp the motor speed on a given port. -### duploTrainHub.setName(name) ⇒ Promise +### duploTrainBase.setName(name) ⇒ Promise Set the name of the Hub. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **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 +### duploTrainBase.connect() ⇒ Promise Connect to the Hub. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [connect](#Hub+connect) **Returns**: Promise - Resolved upon successful connect. -### duploTrainHub.disconnect() ⇒ Promise +### duploTrainBase.disconnect() ⇒ Promise Disconnect the Hub. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [disconnect](#Hub+disconnect) **Returns**: Promise - Resolved upon successful disconnect. -### duploTrainHub.subscribe(port, [mode]) ⇒ Promise +### duploTrainBase.subscribe(port, [mode]) ⇒ Promise Subscribe to sensor notifications on a given port. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [subscribe](#Hub+subscribe) **Returns**: Promise - Resolved upon successful issuance of command. @@ -1383,10 +1397,10 @@ Subscribe to sensor notifications on a given port. -### duploTrainHub.unsubscribe(port) ⇒ Promise +### duploTrainBase.unsubscribe(port) ⇒ Promise Unsubscribe to sensor notifications on a given port. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [unsubscribe](#Hub+unsubscribe) **Returns**: Promise - Resolved upon successful issuance of command. @@ -1396,12 +1410,12 @@ Unsubscribe to sensor notifications on a given port. -### duploTrainHub.sleep(delay) ⇒ Promise +### duploTrainBase.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) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [sleep](#Hub+sleep) **Returns**: Promise - 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 -### duploTrainHub.wait(commands) ⇒ Promise +### duploTrainBase.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) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [wait](#Hub+wait) **Returns**: Promise - 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 | 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) +**Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) | Param | Type | Description | | --- | --- | --- | | port | string | | | color | number | A number representing one of the LED color consts. | + + +### "speed" (port, speed) +Emits on a speed change. + +**Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) + +| Param | Type | +| --- | --- | +| port | string | +| speed | number | + diff --git a/README.md b/README.md index 1d5f9c1..f13baec 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R
PUPRemoteLPF2Hub
-
DuploTrainHubLPF2Hub
+
DuploTrainBaseLPF2Hub
@@ -145,7 +145,7 @@ Emits when a Powered UP Hub device is found. | Param | Type | | --- | --- | -| hub | [WeDo2SmartHub](#WeDo2SmartHub) \| [BoostMoveHub](#BoostMoveHub) \| [PUPHub](#PUPHub) \| [PUPRemote](#PUPRemote) \| [DuploTrainHub](#DuploTrainHub) | +| hub | [WeDo2SmartHub](#WeDo2SmartHub) \| [BoostMoveHub](#BoostMoveHub) \| [PUPHub](#PUPHub) \| [PUPRemote](#PUPRemote) \| [DuploTrainBase](#DuploTrainBase) | @@ -164,7 +164,7 @@ Emits when a Powered UP Hub device is found. * [.setLEDRGB(red, green, blue)](#WeDo2SmartHub+setLEDRGB) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#WeDo2SmartHub+setMotorSpeed) ⇒ Promise * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#WeDo2SmartHub+rampMotorSpeed) ⇒ Promise - * [.playSound(frequency, time)](#WeDo2SmartHub+playSound) ⇒ Promise + * [.playTone(frequency, time)](#WeDo2SmartHub+playTone) ⇒ Promise * [.setLightBrightness(port, brightness, [time])](#WeDo2SmartHub+setLightBrightness) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise * [.disconnect()](#Hub+disconnect) ⇒ Promise @@ -296,18 +296,18 @@ Ramp the motor speed on a given port. | 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). | - + -### weDo2SmartHub.playSound(frequency, time) ⇒ Promise -Play a sound on the Hub's in-built buzzer +### weDo2SmartHub.playTone(frequency, time) ⇒ Promise +Play a tone on the Hub's in-built buzzer **Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) -**Returns**: Promise - Resolved upon successful completion of command (ie. once the sound has finished playing). +**Returns**: Promise - Resolved upon successful completion of command (ie. once the tone has finished playing). | Param | Type | Description | | --- | --- | --- | | frequency | number | | -| time | number | How long the sound should play for (in milliseconds). | +| time | number | How long the tone should play for (in milliseconds). | @@ -1287,41 +1287,42 @@ Emits when a button is pressed. | button | string | | | state | number | A number representing one of the button state consts. | - + -## DuploTrainHub ⇐ LPF2Hub +## DuploTrainBase ⇐ LPF2Hub **Kind**: global class **Extends**: LPF2Hub, Hub -* [DuploTrainHub](#DuploTrainHub) ⇐ LPF2Hub - * [new DuploTrainHub()](#new_DuploTrainHub_new) +* [DuploTrainBase](#DuploTrainBase) ⇐ LPF2Hub + * [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) ⇒ Promise - * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainHub+rampMotorSpeed) ⇒ Promise + * [.setLEDColor(color)](#DuploTrainBase+setLEDColor) ⇒ Promise + * [.setMotorSpeed(port, speed, [time])](#DuploTrainBase+setMotorSpeed) ⇒ Promise + * [.playSound(sound)](#DuploTrainBase+playSound) ⇒ Promise + * [.rampMotorSpeed(port, fromSpeed, toSpeed, time)](#DuploTrainBase+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) + * ["speed" (port, speed)](#LPF2Hub+event_speed) - + -### 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. -### duploTrainHub.current -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.current +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Read only**: true **Properties** @@ -1331,8 +1332,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. -### duploTrainHub.name -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.name +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Overrides**: [name](#Hub+name) **Read only**: true **Properties** @@ -1343,8 +1344,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. -### duploTrainHub.uuid -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.uuid +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Overrides**: [uuid](#Hub+uuid) **Read only**: true **Properties** @@ -1355,8 +1356,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. -### duploTrainHub.rssi -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.rssi +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Overrides**: [rssi](#Hub+rssi) **Read only**: true **Properties** @@ -1367,8 +1368,8 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. -### duploTrainHub.batteryLevel -**Kind**: instance property of [DuploTrainHub](#DuploTrainHub) +### duploTrainBase.batteryLevel +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) **Overrides**: [batteryLevel](#Hub+batteryLevel) **Read only**: true **Properties** @@ -1377,12 +1378,25 @@ The DuploTrainHub is emitted if the discovered device is a Duplo Train Hub. | --- | --- | --- | | batteryLevel | number | Battery level of the hub (Percentage between 0-100) | - + -### duploTrainHub.setMotorSpeed(port, speed, [time]) ⇒ Promise +### duploTrainBase.setLEDColor(color) ⇒ Promise +Set the color of the LED on the train via a color value. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [setLEDColor](#LPF2Hub+setLEDColor) +**Returns**: Promise - Resolved upon successful issuance of command. + +| Param | Type | Description | +| --- | --- | --- | +| color | number | A number representing one of the LED color consts. | + + + +### duploTrainBase.setMotorSpeed(port, speed, [time]) ⇒ Promise Set the motor speed on a given port. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Returns**: Promise - 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 | 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 +### duploTrainBase.playSound(sound) ⇒ Promise +Play a built-in train sound. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Returns**: Promise - Resolved upon successful issuance of command. + +| Param | Type | Description | +| --- | --- | --- | +| sound | number | A number representing one of the Train Base sound consts. | + + + +### duploTrainBase.rampMotorSpeed(port, fromSpeed, toSpeed, time) ⇒ Promise Ramp the motor speed on a given port. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Returns**: Promise - Resolved upon successful completion of command. | Param | Type | Description | @@ -1408,50 +1434,38 @@ Ramp the motor speed on a given port. -### duploTrainHub.setName(name) ⇒ Promise +### duploTrainBase.setName(name) ⇒ Promise Set the name of the Hub. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **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 +### duploTrainBase.connect() ⇒ Promise Connect to the Hub. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [connect](#Hub+connect) **Returns**: Promise - Resolved upon successful connect. -### duploTrainHub.disconnect() ⇒ Promise +### duploTrainBase.disconnect() ⇒ Promise Disconnect the Hub. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [disconnect](#Hub+disconnect) **Returns**: Promise - Resolved upon successful disconnect. -### duploTrainHub.subscribe(port, [mode]) ⇒ Promise +### duploTrainBase.subscribe(port, [mode]) ⇒ Promise Subscribe to sensor notifications on a given port. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [subscribe](#Hub+subscribe) **Returns**: Promise - Resolved upon successful issuance of command. @@ -1462,10 +1476,10 @@ Subscribe to sensor notifications on a given port. -### duploTrainHub.unsubscribe(port) ⇒ Promise +### duploTrainBase.unsubscribe(port) ⇒ Promise Unsubscribe to sensor notifications on a given port. -**Kind**: instance method of [DuploTrainHub](#DuploTrainHub) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [unsubscribe](#Hub+unsubscribe) **Returns**: Promise - Resolved upon successful issuance of command. @@ -1475,12 +1489,12 @@ Unsubscribe to sensor notifications on a given port. -### duploTrainHub.sleep(delay) ⇒ Promise +### duploTrainBase.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) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [sleep](#Hub+sleep) **Returns**: Promise - 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 -### duploTrainHub.wait(commands) ⇒ Promise +### duploTrainBase.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) +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Overrides**: [wait](#Hub+wait) **Returns**: Promise - 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 | 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) +**Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) | Param | Type | Description | | --- | --- | --- | | port | string | | | color | number | A number representing one of the LED color consts. | + + +### "speed" (port, speed) +Emits on a speed change. + +**Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) + +| Param | Type | +| --- | --- | +| port | string | +| speed | number | + diff --git a/boostmovehub.ts b/boostmovehub.ts index 49012ad..27df0e8 100644 --- a/boostmovehub.ts +++ b/boostmovehub.ts @@ -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); } diff --git a/consts.ts b/consts.ts index de220c3..8972fe9 100644 --- a/consts.ts +++ b/consts.ts @@ -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" diff --git a/duplotrainhub.ts b/duplotrainbase.ts similarity index 55% rename from duplotrainhub.ts rename to duplotrainbase.ts index 2072451..7a4a117 100644 --- a/duplotrainhub.ts +++ b/duplotrainbase.ts @@ -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.} 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); }); diff --git a/hub.ts b/hub.ts index 51c4065..611b6e4 100644 --- a/hub.ts +++ b/hub.ts @@ -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; } diff --git a/lpf2hub.ts b/lpf2hub.ts index 9e09ba6..4f3fd92 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -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; + } } } diff --git a/package.json b/package.json index 4399d49..7b9fa29 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/poweredup.ts b/poweredup.ts index d9bcba4..2db8705 100644 --- a/poweredup.ts +++ b/poweredup.ts @@ -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); diff --git a/puphub.ts b/puphub.ts index 1cb18c6..30e6a4b 100644 --- a/puphub.ts +++ b/puphub.ts @@ -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); diff --git a/pupremote.ts b/pupremote.ts index eda110b..1e7d837 100644 --- a/pupremote.ts +++ b/pupremote.ts @@ -40,6 +40,11 @@ export class PUPRemote extends LPF2Hub { * @ignore */ + /** + * @event PUPRemote#speed + * @ignore + */ + /** * @event PUPRemote#attach * @ignore diff --git a/wedo2smarthub.ts b/wedo2smarthub.ts index 0e3d724..0aed878 100644 --- a/wedo2smarthub.ts +++ b/wedo2smarthub.ts @@ -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);