diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..24f6e1c --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,54 @@ +version: 2 + +defaults: &defaults + working_directory: ~/repo + docker: + - image: circleci/node:8.11.4 + +jobs: + build: + <<: *defaults + steps: + - checkout + - restore_cache: + keys: + - v1-dependencies-{{ checksum "package.json" }} + - v1-dependencies- + - run: sudo apt-get install -y bluetooth bluez libbluetooth-dev libudev-dev + - run: npm install + - save_cache: + paths: + - node_modules + key: v1-dependencies-{{ checksum "package.json" }} + - run: npm run all + - persist_to_workspace: + root: ~/repo + paths: . + deploy: + <<: *defaults + steps: + - attach_workspace: + at: ~/repo + - run: + name: Authenticate with registry + command: echo "//registry.npmjs.org/:_authToken=$npm_TOKEN" > ~/repo/.npmrc + - run: + name: Publish package + command: npm publish + +workflows: + version: 2 + build-deploy: + jobs: + - build: + filters: + tags: + only: /^v.*/ + - deploy: + requires: + - build + filters: + tags: + only: /^v.*/ + branches: + ignore: /.*/ diff --git a/ABOUT.md b/ABOUT.md index 99cd6f6..cc45000 100644 --- a/ABOUT.md +++ b/ABOUT.md @@ -1,3 +1,7 @@ +[![CircleCI](https://circleci.com/gh/nathankellenicki/node-poweredup.svg?style=shield)](https://circleci.com/gh/nathankellenicki/node-poweredup) +[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/node-poweredup?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +![NPM Version](https://img.shields.io/npm/v/node-poweredup.svg?style=flat) + # **node-poweredup** - A Node.js module to interface with LEGO Powered UP components. ### Introduction @@ -28,15 +32,25 @@ Note: node-poweredup has been tested on macOS 10.13 and Debian/Raspbian on the R While most Powered UP components and Hubs are compatible with each other, there are exceptions. For example, there is limited backwards compatibility between newer components and the WeDo 2.0 Smart Hub. However WeDo 2.0 components are fully forwards compatible with newer Hubs. -| Name | Type | WeDo 2.0 Smart Hub | Boost Move Hub | Powered UP Hub | Availability | -| ------------------------------- | ------------- | ------------------ | -------------- | -------------- | ------------ | -| WeDo 2.0 Tilt Sensor | Sensor | Yes | Yes | Yes | 45300 | -| WeDo 2.0 Motion Sensor | Sensor | Yes | Yes | Yes | 45300 | -| WeDo 2.0 Medium Motor | Motor | Yes | Yes | Yes | 45300
76112 | -| Boost Color and Distance Sensor | Sensor | *Partial* | Yes | Yes | 17101 | -| Boost Tacho Motor | Motor/Sensor | *Partial* | Yes | *Partial* | 17101 | -| Powered UP Train Motor | Motor | Yes | Yes | Yes | 60197
60198 | -| Powered UP LED Lights | Light | Yes | Yes | Yes | 88005 | +| Device Name | Product Code | Type | WeDo 2.0 Smart Hub | Boost Move Hub | Powered UP Hub | Availability | +| ------------------------------- | ------------ | ------------- | ------------------ | -------------- | -------------- | ------------ | +| WeDo 2.0 Tilt Sensor | 45305 | Sensor | Yes | Yes | Yes | 45300 | +| WeDo 2.0 Motion Sensor | 45304 | Sensor | Yes | Yes | Yes | 45300 | +| WeDo 2.0 Medium Motor | 45303 | Motor | Yes | Yes | Yes | 45300
76112 | +| Boost Color and Distance Sensor | 88007 | Sensor | *Partial* | Yes | Yes | 17101 | +| Boost Tacho Motor | 88008 | Motor/Sensor | *Partial* | Yes | *Partial* | 17101 | +| Powered UP Train Motor | 88011 | Motor | Yes | Yes | Yes | 60197
60198 | +| Powered UP LED Lights | 88005 | Light | Yes | Yes | Yes | 88005 | + +In addition, the Hubs themselves have certain built-in features which this library exposes. + +| Hub Name | Product Code | Built-In Features | Availability | +| ------------------ | ------------ | ---------------------- | ------------ | +| WeDo 2.0 Smart hub | 45301 | RGB LED
Piezo Buzzer
Button | 45300 | +| Boost Move Hub | 88006 | RGB LED
Tilt Sensor
2x Tacho Motors
Button | 17101 | +| Powered UP Hub | 88009 | RGB LED
Button | 60197
60198
76112 | +| Powered UP Remote | 88010 | RGB LED
Left and Right Control Buttons
Button | 60197
60198 | +| Duplo Train Base | 28743 | RGB LED/Headlights
Speaker
Speedometer
Motor
Color and Distance Sensor
Button | 10874
10875 | ### Known Issues and Limitations diff --git a/DOCS.md b/DOCS.md index 2b5c01f..ee1b98c 100644 --- a/DOCS.md +++ b/DOCS.md @@ -15,6 +15,21 @@
+## Typedefs + +
+
HubType
+
+
DeviceType
+
+
Color
+
+
ButtonState
+
+
DuploTrainBaseSound
+
+
+ ## PoweredUP ⇐ EventEmitter @@ -93,6 +108,8 @@ Emits when a Powered UP Hub device is found. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["button" (button, state)](#WeDo2SmartHub+event_button) * ["distance" (port, distance)](#WeDo2SmartHub+event_distance) * ["color" (port, color)](#WeDo2SmartHub+event_color) @@ -170,9 +187,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED color consts. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -312,6 +329,23 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### weDo2SmartHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) + + +### weDo2SmartHub.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -319,10 +353,10 @@ Emits when a button is pressed. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the button state consts. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -343,10 +377,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED color consts. | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -380,10 +414,10 @@ Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the peripheral consts. | +| Param | Type | +| --- | --- | +| port | string | +| type | [DeviceType](#DeviceType) | @@ -422,9 +456,12 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["tilt" (port, x, y)](#LPF2Hub+event_tilt) * ["rotate" (port, rotation)](#LPF2Hub+event_rotate) * ["attach" (port, type)](#Hub+event_attach) @@ -450,6 +487,7 @@ The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. ### boostMoveHub.name **Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [name](#Hub+name) **Read only**: true **Properties** @@ -461,6 +499,7 @@ The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. ### boostMoveHub.uuid **Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [uuid](#Hub+uuid) **Read only**: true **Properties** @@ -472,6 +511,7 @@ The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. ### boostMoveHub.rssi **Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [rssi](#Hub+rssi) **Read only**: true **Properties** @@ -483,6 +523,7 @@ The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. ### boostMoveHub.batteryLevel **Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [batteryLevel](#Hub+batteryLevel) **Read only**: true **Properties** @@ -567,9 +608,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED color consts. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -599,6 +640,7 @@ Connect to the Hub. Disconnect the Hub. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [disconnect](#Hub+disconnect) **Returns**: Promise - Resolved upon successful disconnect. @@ -606,6 +648,7 @@ Disconnect the Hub. Subscribe to sensor notifications on a given port. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [subscribe](#Hub+subscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | Description | @@ -619,6 +662,7 @@ Subscribe to sensor notifications on a given port. Unsubscribe to sensor notifications on a given port. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [unsubscribe](#Hub+unsubscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | @@ -633,6 +677,7 @@ 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 [BoostMoveHub](#BoostMoveHub) +**Overrides**: [sleep](#Hub+sleep) **Returns**: Promise - Resolved after the delay is finished. | Param | Type | Description | @@ -647,12 +692,32 @@ 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 [BoostMoveHub](#BoostMoveHub) +**Overrides**: [wait](#Hub+wait) **Returns**: Promise - Resolved after the commands are finished. | Param | Type | Description | | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### boostMoveHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [getHubType](#Hub+getHubType) + + +### boostMoveHub.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -660,10 +725,10 @@ Emits when a button is pressed. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the button state consts. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -684,10 +749,23 @@ Emits when a color sensor is activated. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | + + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) + | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED color consts. | +| color | [Color](#Color) | | +| distance | number | Distance, in millimeters. | @@ -720,11 +798,12 @@ Emits when a rotation sensor is activated. Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) +**Overrides**: [attach](#Hub+event_attach) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the peripheral consts. | +| Param | Type | +| --- | --- | +| port | string | +| type | [DeviceType](#DeviceType) | @@ -732,6 +811,7 @@ Emits when a motor or sensor is attached to the Hub. Emits when an attached motor or sensor is detached from the Hub. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) +**Overrides**: [detach](#Hub+event_detach) | Param | Type | | --- | --- | @@ -762,9 +842,12 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["tilt" (port, x, y)](#LPF2Hub+event_tilt) * ["attach" (port, type)](#Hub+event_attach) * ["detach" (port)](#Hub+event_detach) @@ -789,6 +872,7 @@ The PUPHub is emitted if the discovered device is a Powered UP Hub. ### pupHub.name **Kind**: instance property of [PUPHub](#PUPHub) +**Overrides**: [name](#Hub+name) **Read only**: true **Properties** @@ -800,6 +884,7 @@ The PUPHub is emitted if the discovered device is a Powered UP Hub. ### pupHub.uuid **Kind**: instance property of [PUPHub](#PUPHub) +**Overrides**: [uuid](#Hub+uuid) **Read only**: true **Properties** @@ -811,6 +896,7 @@ The PUPHub is emitted if the discovered device is a Powered UP Hub. ### pupHub.rssi **Kind**: instance property of [PUPHub](#PUPHub) +**Overrides**: [rssi](#Hub+rssi) **Read only**: true **Properties** @@ -822,6 +908,7 @@ The PUPHub is emitted if the discovered device is a Powered UP Hub. ### pupHub.batteryLevel **Kind**: instance property of [PUPHub](#PUPHub) +**Overrides**: [batteryLevel](#Hub+batteryLevel) **Read only**: true **Properties** @@ -892,9 +979,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [PUPHub](#PUPHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED color consts. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -924,6 +1011,7 @@ Connect to the Hub. Disconnect the Hub. **Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [disconnect](#Hub+disconnect) **Returns**: Promise - Resolved upon successful disconnect. @@ -931,6 +1019,7 @@ Disconnect the Hub. Subscribe to sensor notifications on a given port. **Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [subscribe](#Hub+subscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | Description | @@ -944,6 +1033,7 @@ Subscribe to sensor notifications on a given port. Unsubscribe to sensor notifications on a given port. **Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [unsubscribe](#Hub+unsubscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | @@ -958,6 +1048,7 @@ 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 [PUPHub](#PUPHub) +**Overrides**: [sleep](#Hub+sleep) **Returns**: Promise - Resolved after the delay is finished. | Param | Type | Description | @@ -972,12 +1063,32 @@ 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 [PUPHub](#PUPHub) +**Overrides**: [wait](#Hub+wait) **Returns**: Promise - Resolved after the commands are finished. | Param | Type | Description | | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### pupHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [getHubType](#Hub+getHubType) + + +### pupHub.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -985,10 +1096,10 @@ Emits when a button is pressed. **Kind**: event emitted by [PUPHub](#PUPHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the button state consts. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -1009,10 +1120,23 @@ Emits when a color sensor is activated. **Kind**: event emitted by [PUPHub](#PUPHub) +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | + + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [PUPHub](#PUPHub) + | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED color consts. | +| color | [Color](#Color) | | +| distance | number | Distance, in millimeters. | @@ -1033,11 +1157,12 @@ Emits when a tilt sensor is activated. Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [PUPHub](#PUPHub) +**Overrides**: [attach](#Hub+event_attach) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the peripheral consts. | +| Param | Type | +| --- | --- | +| port | string | +| type | [DeviceType](#DeviceType) | @@ -1045,6 +1170,7 @@ Emits when a motor or sensor is attached to the Hub. Emits when an attached motor or sensor is detached from the Hub. **Kind**: event emitted by [PUPHub](#PUPHub) +**Overrides**: [detach](#Hub+event_detach) | Param | Type | | --- | --- | @@ -1072,7 +1198,10 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["button" (button, state)](#LPF2Hub+event_button) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) @@ -1094,6 +1223,7 @@ The PUPRemote is emitted if the discovered device is a Powered UP Remote. ### pupRemote.name **Kind**: instance property of [PUPRemote](#PUPRemote) +**Overrides**: [name](#Hub+name) **Read only**: true **Properties** @@ -1105,6 +1235,7 @@ The PUPRemote is emitted if the discovered device is a Powered UP Remote. ### pupRemote.uuid **Kind**: instance property of [PUPRemote](#PUPRemote) +**Overrides**: [uuid](#Hub+uuid) **Read only**: true **Properties** @@ -1116,6 +1247,7 @@ The PUPRemote is emitted if the discovered device is a Powered UP Remote. ### pupRemote.rssi **Kind**: instance property of [PUPRemote](#PUPRemote) +**Overrides**: [rssi](#Hub+rssi) **Read only**: true **Properties** @@ -1127,6 +1259,7 @@ The PUPRemote is emitted if the discovered device is a Powered UP Remote. ### pupRemote.batteryLevel **Kind**: instance property of [PUPRemote](#PUPRemote) +**Overrides**: [batteryLevel](#Hub+batteryLevel) **Read only**: true **Properties** @@ -1143,9 +1276,9 @@ Set the color of the LED on the Remote via a color value. **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. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1188,6 +1321,7 @@ Connect to the Hub. Disconnect the Hub. **Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [disconnect](#Hub+disconnect) **Returns**: Promise - Resolved upon successful disconnect. @@ -1195,6 +1329,7 @@ Disconnect the Hub. Subscribe to sensor notifications on a given port. **Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [subscribe](#Hub+subscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | Description | @@ -1208,6 +1343,7 @@ Subscribe to sensor notifications on a given port. Unsubscribe to sensor notifications on a given port. **Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [unsubscribe](#Hub+unsubscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | @@ -1222,6 +1358,7 @@ 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 [PUPRemote](#PUPRemote) +**Overrides**: [sleep](#Hub+sleep) **Returns**: Promise - Resolved after the delay is finished. | Param | Type | Description | @@ -1236,12 +1373,32 @@ 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 [PUPRemote](#PUPRemote) +**Overrides**: [wait](#Hub+wait) **Returns**: Promise - Resolved after the commands are finished. | Param | Type | Description | | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### pupRemote.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [getHubType](#Hub+getHubType) + + +### pupRemote.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -1249,10 +1406,23 @@ Emits when a button is pressed. **Kind**: event emitted by [PUPRemote](#PUPRemote) +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | + + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [PUPRemote](#PUPRemote) + | Param | Type | Description | | --- | --- | --- | -| button | string | | -| state | number | A number representing one of the button state consts. | +| port | string | | +| color | [Color](#Color) | | +| distance | number | Distance, in millimeters. | @@ -1279,7 +1449,10 @@ Emits when a button is pressed. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["speed" (port, speed)](#LPF2Hub+event_speed) @@ -1355,9 +1528,9 @@ Set the color of the LED on the train via a color value. **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. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1381,9 +1554,9 @@ 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. | +| Param | Type | +| --- | --- | +| sound | [DuploTrainBaseSound](#DuploTrainBaseSound) | @@ -1499,6 +1672,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### duploTrainBase.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [getHubType](#Hub+getHubType) + + +### duploTrainBase.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "color" (port, color) @@ -1506,10 +1698,23 @@ Emits when a color sensor is activated. **Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | + + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) + | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED color consts. | +| color | [Color](#Color) | | +| distance | number | Distance, in millimeters. | @@ -1523,3 +1728,92 @@ Emits on a speed change. | port | string | | speed | number | + + +## HubType +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| UNKNOWN | number | 0 | +| WEDO2_SMART_HUB | number | 1 | +| BOOST_MOVE_HUB | number | 2 | +| POWERED_UP_HUB | number | 3 | +| POWERED_UP_REMOTE | number | 4 | +| DUPLO_TRAIN_HUB | number | 5 | + + + +## DeviceType +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| UNKNOWN | number | 0 | +| BASIC_MOTOR | number | 1 | +| TRAIN_MOTOR | number | 2 | +| LED_LIGHTS | number | 8 | +| BOOST_LED | number | 22 | +| WEDO2_TILT | number | 34 | +| WEDO2_DISTANCE | number | 35 | +| BOOST_DISTANCE | number | 37 | +| BOOST_TACHO_MOTOR | number | 38 | +| BOOST_MOVE_HUB_MOTOR | number | 39 | +| BOOST_TILT | number | 40 | +| DUPLO_TRAIN_BASE_MOTOR | number | 41 | +| DUPLO_TRAIN_BASE_SPEAKER | number | 42 | +| DUPLO_TRAIN_BASE_COLOR | number | 43 | +| DUPLO_TRAIN_BASE_SPEEDOMETER | number | 44 | +| POWERED_UP_REMOTE_BUTTON | number | 55 | + + + +## Color +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| BLACK | number | 0 | +| PINK | number | 1 | +| PURPLE | number | 2 | +| BLUE | number | 3 | +| LIGHT_BLUE | number | 4 | +| CYAN | number | 5 | +| GREEN | number | 6 | +| YELLOW | number | 7 | +| ORANGE | number | 8 | +| RED | number | 9 | +| WHITE | number | 10 | +| NONE | number | 255 | + + + +## ButtonState +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| PRESSED | number | 0 | +| RELEASED | number | 1 | +| UP | number | 2 | +| DOWN | number | 3 | +| STOP | number | 4 | + + + +## DuploTrainBaseSound +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| BRAKE | number | 3 | +| STATION_DEPARTURE | number | 5 | +| WATER_REFILL | number | 7 | +| HORN | number | 9 | +| STEAM | number | 10 | + diff --git a/README.md b/README.md index 4ddb31f..2dadfb8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +[![CircleCI](https://circleci.com/gh/nathankellenicki/node-poweredup.svg?style=shield)](https://circleci.com/gh/nathankellenicki/node-poweredup) +[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/node-poweredup?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +![NPM Version](https://img.shields.io/npm/v/node-poweredup.svg?style=flat) + # **node-poweredup** - A Node.js module to interface with LEGO Powered UP components. ### Introduction @@ -28,15 +32,25 @@ Note: node-poweredup has been tested on macOS 10.13 and Debian/Raspbian on the R While most Powered UP components and Hubs are compatible with each other, there are exceptions. For example, there is limited backwards compatibility between newer components and the WeDo 2.0 Smart Hub. However WeDo 2.0 components are fully forwards compatible with newer Hubs. -| Name | Type | WeDo 2.0 Smart Hub | Boost Move Hub | Powered UP Hub | Availability | -| ------------------------------- | ------------- | ------------------ | -------------- | -------------- | ------------ | -| WeDo 2.0 Tilt Sensor | Sensor | Yes | Yes | Yes | 45300 | -| WeDo 2.0 Motion Sensor | Sensor | Yes | Yes | Yes | 45300 | -| WeDo 2.0 Medium Motor | Motor | Yes | Yes | Yes | 45300
76112 | -| Boost Color and Distance Sensor | Sensor | *Partial* | Yes | Yes | 17101 | -| Boost Tacho Motor | Motor/Sensor | *Partial* | Yes | *Partial* | 17101 | -| Powered UP Train Motor | Motor | Yes | Yes | Yes | 60197
60198 | -| Powered UP LED Lights | Light | Yes | Yes | Yes | 88005 | +| Device Name | Product Code | Type | WeDo 2.0 Smart Hub | Boost Move Hub | Powered UP Hub | Availability | +| ------------------------------- | ------------ | ------------- | ------------------ | -------------- | -------------- | ------------ | +| WeDo 2.0 Tilt Sensor | 45305 | Sensor | Yes | Yes | Yes | 45300 | +| WeDo 2.0 Motion Sensor | 45304 | Sensor | Yes | Yes | Yes | 45300 | +| WeDo 2.0 Medium Motor | 45303 | Motor | Yes | Yes | Yes | 45300
76112 | +| Boost Color and Distance Sensor | 88007 | Sensor | *Partial* | Yes | Yes | 17101 | +| Boost Tacho Motor | 88008 | Motor/Sensor | *Partial* | Yes | *Partial* | 17101 | +| Powered UP Train Motor | 88011 | Motor | Yes | Yes | Yes | 60197
60198 | +| Powered UP LED Lights | 88005 | Light | Yes | Yes | Yes | 88005 | + +In addition, the Hubs themselves have certain built-in features which this library exposes. + +| Hub Name | Product Code | Built-In Features | Availability | +| ------------------ | ------------ | ---------------------- | ------------ | +| WeDo 2.0 Smart hub | 45301 | RGB LED
Piezo Buzzer
Button | 45300 | +| Boost Move Hub | 88006 | RGB LED
Tilt Sensor
2x Tacho Motors
Button | 17101 | +| Powered UP Hub | 88009 | RGB LED
Button | 60197
60198
76112 | +| Powered UP Remote | 88010 | RGB LED
Left and Right Control Buttons
Button | 60197
60198 | +| Duplo Train Base | 28743 | RGB LED/Headlights
Speaker
Speedometer
Motor
Color and Distance Sensor
Button | 10874
10875 | ### Known Issues and Limitations @@ -94,6 +108,21 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R
+## Typedefs + +
+
HubType
+
+
DeviceType
+
+
Color
+
+
ButtonState
+
+
DuploTrainBaseSound
+
+
+ ## PoweredUP ⇐ EventEmitter @@ -172,6 +201,8 @@ Emits when a Powered UP Hub device is found. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["button" (button, state)](#WeDo2SmartHub+event_button) * ["distance" (port, distance)](#WeDo2SmartHub+event_distance) * ["color" (port, color)](#WeDo2SmartHub+event_color) @@ -249,9 +280,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED color consts. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -391,6 +422,23 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### weDo2SmartHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) + + +### weDo2SmartHub.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -398,10 +446,10 @@ Emits when a button is pressed. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the button state consts. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -422,10 +470,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED color consts. | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -459,10 +507,10 @@ Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the peripheral consts. | +| Param | Type | +| --- | --- | +| port | string | +| type | [DeviceType](#DeviceType) | @@ -501,9 +549,12 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["tilt" (port, x, y)](#LPF2Hub+event_tilt) * ["rotate" (port, rotation)](#LPF2Hub+event_rotate) * ["attach" (port, type)](#Hub+event_attach) @@ -529,6 +580,7 @@ The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. ### boostMoveHub.name **Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [name](#Hub+name) **Read only**: true **Properties** @@ -540,6 +592,7 @@ The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. ### boostMoveHub.uuid **Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [uuid](#Hub+uuid) **Read only**: true **Properties** @@ -551,6 +604,7 @@ The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. ### boostMoveHub.rssi **Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [rssi](#Hub+rssi) **Read only**: true **Properties** @@ -562,6 +616,7 @@ The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. ### boostMoveHub.batteryLevel **Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [batteryLevel](#Hub+batteryLevel) **Read only**: true **Properties** @@ -646,9 +701,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED color consts. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -678,6 +733,7 @@ Connect to the Hub. Disconnect the Hub. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [disconnect](#Hub+disconnect) **Returns**: Promise - Resolved upon successful disconnect. @@ -685,6 +741,7 @@ Disconnect the Hub. Subscribe to sensor notifications on a given port. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [subscribe](#Hub+subscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | Description | @@ -698,6 +755,7 @@ Subscribe to sensor notifications on a given port. Unsubscribe to sensor notifications on a given port. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [unsubscribe](#Hub+unsubscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | @@ -712,6 +770,7 @@ 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 [BoostMoveHub](#BoostMoveHub) +**Overrides**: [sleep](#Hub+sleep) **Returns**: Promise - Resolved after the delay is finished. | Param | Type | Description | @@ -726,12 +785,32 @@ 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 [BoostMoveHub](#BoostMoveHub) +**Overrides**: [wait](#Hub+wait) **Returns**: Promise - Resolved after the commands are finished. | Param | Type | Description | | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### boostMoveHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [getHubType](#Hub+getHubType) + + +### boostMoveHub.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -739,10 +818,10 @@ Emits when a button is pressed. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the button state consts. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -763,10 +842,23 @@ Emits when a color sensor is activated. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | + + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) + | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED color consts. | +| color | [Color](#Color) | | +| distance | number | Distance, in millimeters. | @@ -799,11 +891,12 @@ Emits when a rotation sensor is activated. Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) +**Overrides**: [attach](#Hub+event_attach) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the peripheral consts. | +| Param | Type | +| --- | --- | +| port | string | +| type | [DeviceType](#DeviceType) | @@ -811,6 +904,7 @@ Emits when a motor or sensor is attached to the Hub. Emits when an attached motor or sensor is detached from the Hub. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) +**Overrides**: [detach](#Hub+event_detach) | Param | Type | | --- | --- | @@ -841,9 +935,12 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["tilt" (port, x, y)](#LPF2Hub+event_tilt) * ["attach" (port, type)](#Hub+event_attach) * ["detach" (port)](#Hub+event_detach) @@ -868,6 +965,7 @@ The PUPHub is emitted if the discovered device is a Powered UP Hub. ### pupHub.name **Kind**: instance property of [PUPHub](#PUPHub) +**Overrides**: [name](#Hub+name) **Read only**: true **Properties** @@ -879,6 +977,7 @@ The PUPHub is emitted if the discovered device is a Powered UP Hub. ### pupHub.uuid **Kind**: instance property of [PUPHub](#PUPHub) +**Overrides**: [uuid](#Hub+uuid) **Read only**: true **Properties** @@ -890,6 +989,7 @@ The PUPHub is emitted if the discovered device is a Powered UP Hub. ### pupHub.rssi **Kind**: instance property of [PUPHub](#PUPHub) +**Overrides**: [rssi](#Hub+rssi) **Read only**: true **Properties** @@ -901,6 +1001,7 @@ The PUPHub is emitted if the discovered device is a Powered UP Hub. ### pupHub.batteryLevel **Kind**: instance property of [PUPHub](#PUPHub) +**Overrides**: [batteryLevel](#Hub+batteryLevel) **Read only**: true **Properties** @@ -971,9 +1072,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [PUPHub](#PUPHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED color consts. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1003,6 +1104,7 @@ Connect to the Hub. Disconnect the Hub. **Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [disconnect](#Hub+disconnect) **Returns**: Promise - Resolved upon successful disconnect. @@ -1010,6 +1112,7 @@ Disconnect the Hub. Subscribe to sensor notifications on a given port. **Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [subscribe](#Hub+subscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | Description | @@ -1023,6 +1126,7 @@ Subscribe to sensor notifications on a given port. Unsubscribe to sensor notifications on a given port. **Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [unsubscribe](#Hub+unsubscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | @@ -1037,6 +1141,7 @@ 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 [PUPHub](#PUPHub) +**Overrides**: [sleep](#Hub+sleep) **Returns**: Promise - Resolved after the delay is finished. | Param | Type | Description | @@ -1051,12 +1156,32 @@ 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 [PUPHub](#PUPHub) +**Overrides**: [wait](#Hub+wait) **Returns**: Promise - Resolved after the commands are finished. | Param | Type | Description | | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### pupHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [getHubType](#Hub+getHubType) + + +### pupHub.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -1064,10 +1189,10 @@ Emits when a button is pressed. **Kind**: event emitted by [PUPHub](#PUPHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the button state consts. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -1088,10 +1213,23 @@ Emits when a color sensor is activated. **Kind**: event emitted by [PUPHub](#PUPHub) +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | + + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [PUPHub](#PUPHub) + | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED color consts. | +| color | [Color](#Color) | | +| distance | number | Distance, in millimeters. | @@ -1112,11 +1250,12 @@ Emits when a tilt sensor is activated. Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [PUPHub](#PUPHub) +**Overrides**: [attach](#Hub+event_attach) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the peripheral consts. | +| Param | Type | +| --- | --- | +| port | string | +| type | [DeviceType](#DeviceType) | @@ -1124,6 +1263,7 @@ Emits when a motor or sensor is attached to the Hub. Emits when an attached motor or sensor is detached from the Hub. **Kind**: event emitted by [PUPHub](#PUPHub) +**Overrides**: [detach](#Hub+event_detach) | Param | Type | | --- | --- | @@ -1151,7 +1291,10 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["button" (button, state)](#LPF2Hub+event_button) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) @@ -1173,6 +1316,7 @@ The PUPRemote is emitted if the discovered device is a Powered UP Remote. ### pupRemote.name **Kind**: instance property of [PUPRemote](#PUPRemote) +**Overrides**: [name](#Hub+name) **Read only**: true **Properties** @@ -1184,6 +1328,7 @@ The PUPRemote is emitted if the discovered device is a Powered UP Remote. ### pupRemote.uuid **Kind**: instance property of [PUPRemote](#PUPRemote) +**Overrides**: [uuid](#Hub+uuid) **Read only**: true **Properties** @@ -1195,6 +1340,7 @@ The PUPRemote is emitted if the discovered device is a Powered UP Remote. ### pupRemote.rssi **Kind**: instance property of [PUPRemote](#PUPRemote) +**Overrides**: [rssi](#Hub+rssi) **Read only**: true **Properties** @@ -1206,6 +1352,7 @@ The PUPRemote is emitted if the discovered device is a Powered UP Remote. ### pupRemote.batteryLevel **Kind**: instance property of [PUPRemote](#PUPRemote) +**Overrides**: [batteryLevel](#Hub+batteryLevel) **Read only**: true **Properties** @@ -1222,9 +1369,9 @@ Set the color of the LED on the Remote via a color value. **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. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1267,6 +1414,7 @@ Connect to the Hub. Disconnect the Hub. **Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [disconnect](#Hub+disconnect) **Returns**: Promise - Resolved upon successful disconnect. @@ -1274,6 +1422,7 @@ Disconnect the Hub. Subscribe to sensor notifications on a given port. **Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [subscribe](#Hub+subscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | Description | @@ -1287,6 +1436,7 @@ Subscribe to sensor notifications on a given port. Unsubscribe to sensor notifications on a given port. **Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [unsubscribe](#Hub+unsubscribe) **Returns**: Promise - Resolved upon successful issuance of command. | Param | Type | @@ -1301,6 +1451,7 @@ 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 [PUPRemote](#PUPRemote) +**Overrides**: [sleep](#Hub+sleep) **Returns**: Promise - Resolved after the delay is finished. | Param | Type | Description | @@ -1315,12 +1466,32 @@ 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 [PUPRemote](#PUPRemote) +**Overrides**: [wait](#Hub+wait) **Returns**: Promise - Resolved after the commands are finished. | Param | Type | Description | | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### pupRemote.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [getHubType](#Hub+getHubType) + + +### pupRemote.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -1328,10 +1499,23 @@ Emits when a button is pressed. **Kind**: event emitted by [PUPRemote](#PUPRemote) +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | + + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [PUPRemote](#PUPRemote) + | Param | Type | Description | | --- | --- | --- | -| button | string | | -| state | number | A number representing one of the button state consts. | +| port | string | | +| color | [Color](#Color) | | +| distance | number | Distance, in millimeters. | @@ -1358,7 +1542,10 @@ Emits when a button is pressed. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ [DeviceType](#DeviceType) * ["color" (port, color)](#LPF2Hub+event_color) + * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["speed" (port, speed)](#LPF2Hub+event_speed) @@ -1434,9 +1621,9 @@ Set the color of the LED on the train via a color value. **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. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1460,9 +1647,9 @@ 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. | +| Param | Type | +| --- | --- | +| sound | [DuploTrainBaseSound](#DuploTrainBaseSound) | @@ -1578,6 +1765,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### duploTrainBase.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [getHubType](#Hub+getHubType) + + +### duploTrainBase.getPortDeviceType(port) ⇒ [DeviceType](#DeviceType) +Get the device type for a given port. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "color" (port, color) @@ -1585,10 +1791,23 @@ Emits when a color sensor is activated. **Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | + + + +### "colorAndDistance" (port, color, distance) +A combined color and distance event, emits when the sensor is activated. + +**Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) + | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED color consts. | +| color | [Color](#Color) | | +| distance | number | Distance, in millimeters. | @@ -1602,3 +1821,92 @@ Emits on a speed change. | port | string | | speed | number | + + +## HubType +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| UNKNOWN | number | 0 | +| WEDO2_SMART_HUB | number | 1 | +| BOOST_MOVE_HUB | number | 2 | +| POWERED_UP_HUB | number | 3 | +| POWERED_UP_REMOTE | number | 4 | +| DUPLO_TRAIN_HUB | number | 5 | + + + +## DeviceType +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| UNKNOWN | number | 0 | +| BASIC_MOTOR | number | 1 | +| TRAIN_MOTOR | number | 2 | +| LED_LIGHTS | number | 8 | +| BOOST_LED | number | 22 | +| WEDO2_TILT | number | 34 | +| WEDO2_DISTANCE | number | 35 | +| BOOST_DISTANCE | number | 37 | +| BOOST_TACHO_MOTOR | number | 38 | +| BOOST_MOVE_HUB_MOTOR | number | 39 | +| BOOST_TILT | number | 40 | +| DUPLO_TRAIN_BASE_MOTOR | number | 41 | +| DUPLO_TRAIN_BASE_SPEAKER | number | 42 | +| DUPLO_TRAIN_BASE_COLOR | number | 43 | +| DUPLO_TRAIN_BASE_SPEEDOMETER | number | 44 | +| POWERED_UP_REMOTE_BUTTON | number | 55 | + + + +## Color +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| BLACK | number | 0 | +| PINK | number | 1 | +| PURPLE | number | 2 | +| BLUE | number | 3 | +| LIGHT_BLUE | number | 4 | +| CYAN | number | 5 | +| GREEN | number | 6 | +| YELLOW | number | 7 | +| ORANGE | number | 8 | +| RED | number | 9 | +| WHITE | number | 10 | +| NONE | number | 255 | + + + +## ButtonState +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| PRESSED | number | 0 | +| RELEASED | number | 1 | +| UP | number | 2 | +| DOWN | number | 3 | +| STOP | number | 4 | + + + +## DuploTrainBaseSound +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| BRAKE | number | 3 | +| STATION_DEPARTURE | number | 5 | +| WATER_REFILL | number | 7 | +| HORN | number | 9 | +| STEAM | number | 10 | + diff --git a/boostmovehub.ts b/boostmovehub.ts index 3a04fd8..c013dd4 100644 --- a/boostmovehub.ts +++ b/boostmovehub.ts @@ -27,13 +27,13 @@ export class BoostMoveHub extends LPF2Hub { public static IsBoostMoveHub (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID); } constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hubs.BOOST_MOVE_HUB; + this.type = Consts.HubType.BOOST_MOVE_HUB; this._ports = { "A": new Port("A", 55), "B": new Port("B", 56), @@ -82,7 +82,7 @@ export class BoostMoveHub extends LPF2Hub { return new Promise((resolve, reject) => { if (time && typeof time === "number") { - if (portObj.type === Consts.Devices.BOOST_TACHO_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) { + if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { portObj.busy = true; let data = null; if (portObj.id === "AB") { @@ -92,17 +92,17 @@ export class BoostMoveHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); // @ts-ignore: The type of time is properly checked at the start }, time); @@ -111,7 +111,7 @@ export class BoostMoveHub extends LPF2Hub { } else { - if (portObj.type === Consts.Devices.BOOST_TACHO_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) { + if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { portObj.busy = true; let data = null; if (portObj.id === "AB") { @@ -120,14 +120,14 @@ export class BoostMoveHub extends LPF2Hub { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); } } @@ -167,7 +167,7 @@ export class BoostMoveHub extends LPF2Hub { */ public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) { const portObj = this._portLookup(port); - if (!(portObj.type === Consts.Devices.BOOST_TACHO_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR)) { + if (!(portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR)) { throw new Error("Angle rotation is only available when using a Boost Tacho Motor or Boost Move Hub Motor"); } if (portObj.id !== "AB" && speed instanceof Array) { @@ -184,7 +184,7 @@ export class BoostMoveHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt32LE(angle, 4); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; @@ -205,11 +205,11 @@ export class BoostMoveHub extends LPF2Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); diff --git a/consts.ts b/consts.ts index f300b9b..4798705 100644 --- a/consts.ts +++ b/consts.ts @@ -1,4 +1,13 @@ -export enum Hubs { +/** + * @typedef HubType + * @property {number} UNKNOWN 0 + * @property {number} WEDO2_SMART_HUB 1 + * @property {number} BOOST_MOVE_HUB 2 + * @property {number} POWERED_UP_HUB 3 + * @property {number} POWERED_UP_REMOTE 4 + * @property {number} DUPLO_TRAIN_HUB 5 + */ +export enum HubType { UNKNOWN = 0, WEDO2_SMART_HUB = 1, BOOST_MOVE_HUB = 2, @@ -8,7 +17,26 @@ export enum Hubs { } -export enum Devices { +/** + * @typedef DeviceType + * @property {number} UNKNOWN 0 + * @property {number} BASIC_MOTOR 1 + * @property {number} TRAIN_MOTOR 2 + * @property {number} LED_LIGHTS 8 + * @property {number} BOOST_LED 22 + * @property {number} WEDO2_TILT 34 + * @property {number} WEDO2_DISTANCE 35 + * @property {number} BOOST_DISTANCE 37 + * @property {number} BOOST_TACHO_MOTOR 38 + * @property {number} BOOST_MOVE_HUB_MOTOR 39 + * @property {number} BOOST_TILT 40 + * @property {number} DUPLO_TRAIN_BASE_MOTOR 41 + * @property {number} DUPLO_TRAIN_BASE_SPEAKER 42 + * @property {number} DUPLO_TRAIN_BASE_COLOR 43 + * @property {number} DUPLO_TRAIN_BASE_SPEEDOMETER 44 + * @property {number} POWERED_UP_REMOTE_BUTTON 55 + */ +export enum DeviceType { UNKNOWN = 0, BASIC_MOTOR = 1, TRAIN_MOTOR = 2, @@ -28,7 +56,22 @@ export enum Devices { } -export enum Colors { +/** + * @typedef Color + * @property {number} BLACK 0 + * @property {number} PINK 1 + * @property {number} PURPLE 2 + * @property {number} BLUE 3 + * @property {number} LIGHT_BLUE 4 + * @property {number} CYAN 5 + * @property {number} GREEN 6 + * @property {number} YELLOW 7 + * @property {number} ORANGE 8 + * @property {number} RED 9 + * @property {number} WHITE 10 + * @property {number} NONE 255 + */ +export enum Color { BLACK = 0, PINK = 1, PURPLE = 2, @@ -44,7 +87,15 @@ export enum Colors { } -export enum ButtonStates { +/** + * @typedef ButtonState + * @property {number} PRESSED 0 + * @property {number} RELEASED 1 + * @property {number} UP 2 + * @property {number} DOWN 3 + * @property {number} STOP 4 + */ +export enum ButtonState { PRESSED = 0, RELEASED = 1, UP = 2, @@ -53,10 +104,20 @@ export enum ButtonStates { } -export enum DuploTrainBaseSounds { +/** + * @typedef DuploTrainBaseSound + * @property {number} BRAKE 3 + * @property {number} STATION_DEPARTURE 5 + * @property {number} WATER_REFILL 7 + * @property {number} HORN 9 + * @property {number} STEAM 10 + */ +export enum DuploTrainBaseSound { + BREAK = 3, STATION_DEPARTURE = 5, WATER_REFILL = 7, - HORN = 9 + HORN = 9, + STEAM = 10 } @@ -68,13 +129,13 @@ export enum BLEManufacturerData { } -export enum BLEServices { +export enum BLEService { WEDO2_SMART_HUB = "00001523-1212-efde-1523-785feabcd123", LPF2_HUB = "00001623-1212-efde-1623-785feabcd123" } -export enum BLECharacteristics { +export enum BLECharacteristic { WEDO2_BATTERY = "2a19", WEDO2_BUTTON = "00001526-1212-efde-1523-785feabcd123", // "1526" WEDO2_PORT_TYPE = "00001527-1212-efde-1523-785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub diff --git a/duplotrainbase.ts b/duplotrainbase.ts index 79ede2b..f8a6ab6 100644 --- a/duplotrainbase.ts +++ b/duplotrainbase.ts @@ -52,13 +52,13 @@ export class DuploTrainBase extends LPF2Hub { public static IsDuploTrainBase (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 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.type = Consts.HubType.DUPLO_TRAIN_HUB; this._ports = { "MOTOR": new Port("MOTOR", 0), "COLOR": new Port("COLOR", 18), @@ -81,7 +81,7 @@ export class DuploTrainBase 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. + * @param {Color} color * @returns {Promise} Resolved upon successful issuance of command. */ public setLEDColor (color: number | boolean) { @@ -90,7 +90,7 @@ export class DuploTrainBase extends LPF2Hub { color = 0; } const data = Buffer.from([0x81, 0x11, 0x11, 0x51, 0x00, color]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -119,16 +119,16 @@ export class DuploTrainBase extends LPF2Hub { return new Promise((resolve, reject) => { 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); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); } else { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); } }); @@ -138,13 +138,13 @@ export class DuploTrainBase extends LPF2Hub { /** * Play a built-in train sound. * @method DuploTrainBase#playSound - * @param {number} sound A number representing one of the Train Base sound consts. + * @param {DuploTrainBaseSound} sound * @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); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } diff --git a/hub.ts b/hub.ts index c257a4f..51beb19 100644 --- a/hub.ts +++ b/hub.ts @@ -19,7 +19,7 @@ export class Hub extends EventEmitter { public autoSubscribe: boolean = true; public useSpeedMap: boolean = true; - public type: Consts.Hubs = Consts.Hubs.UNKNOWN; + public type: Consts.HubType = Consts.HubType.UNKNOWN; protected _ports: {[port: string]: Port} = {}; protected _characteristics: {[uuid: string]: Characteristic} = {}; @@ -216,6 +216,27 @@ export class Hub extends EventEmitter { } + /** + * Get the hub type. + * @method Hub#getHubType + * @returns {HubType} + */ + public getHubType () { + return this.type; + } + + + /** + * Get the device type for a given port. + * @method Hub#getPortDeviceType + * @param {string} port + * @returns {DeviceType} + */ + public getPortDeviceType (port: string) { + return this._portLookup(port).type; + } + + protected _getCharacteristic (uuid: string) { return this._characteristics[uuid.replace(/-/g, "")]; } @@ -257,12 +278,12 @@ export class Hub extends EventEmitter { * Emits when a motor or sensor is attached to the Hub. * @event Hub#attach * @param {string} port - * @param {number} type A number representing one of the peripheral consts. + * @param {DeviceType} type */ this.emit("attach", port.id, type); } } else { - port.type = Consts.Devices.UNKNOWN; + port.type = Consts.DeviceType.UNKNOWN; debug(`Port ${port.id} disconnected`); /** * Emits when an attached motor or sensor is detached from the Hub. @@ -349,19 +370,19 @@ export class Hub extends EventEmitter { } - private _getModeForDeviceType (type: Consts.Devices) { + private _getModeForDeviceType (type: Consts.DeviceType) { switch (type) { - case Consts.Devices.BASIC_MOTOR: + case Consts.DeviceType.BASIC_MOTOR: return 0x02; - case Consts.Devices.TRAIN_MOTOR: + case Consts.DeviceType.TRAIN_MOTOR: return 0x02; - case Consts.Devices.BOOST_TACHO_MOTOR: + case Consts.DeviceType.BOOST_TACHO_MOTOR: return 0x02; - case Consts.Devices.BOOST_MOVE_HUB_MOTOR: + case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: return 0x02; - case Consts.Devices.BOOST_DISTANCE: - return (this.type === Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08); - case Consts.Devices.BOOST_TILT: + case Consts.DeviceType.BOOST_DISTANCE: + return (this.type === Consts.HubType.WEDO2_SMART_HUB ? 0x00 : 0x08); + case Consts.DeviceType.BOOST_TILT: return 0x04; default: return 0x00; diff --git a/lpf2hub.ts b/lpf2hub.ts index c241e63..9cf1ed1 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -37,13 +37,13 @@ export class LPF2Hub extends Hub { public connect () { return new Promise(async (resolve, reject) => { await super.connect(); - const characteristic = this._getCharacteristic(Consts.BLECharacteristics.LPF2_ALL); + const characteristic = this._getCharacteristic(Consts.BLECharacteristic.LPF2_ALL); this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); - 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])); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports + if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); } return resolve(); }); @@ -64,8 +64,8 @@ export class LPF2Hub extends Hub { let data = Buffer.from([0x01, 0x01, 0x01]); data = Buffer.concat([data, Buffer.from(name, "ascii")]); // Send this twice, as sometimes the first time doesn't take - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); this._name = name; return resolve(); }); @@ -75,18 +75,18 @@ export class LPF2Hub extends Hub { /** * Set the color of the LED on the Hub via a color value. * @method LPF2Hub#setLEDColor - * @param {number} color A number representing one of the LED color consts. + * @param {Color} color * @returns {Promise} Resolved upon successful issuance of command. */ public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, 0x32, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); if (color === false) { color = 0; } data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x00, color]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -103,21 +103,21 @@ export class LPF2Hub extends Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, 0x32, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x01, red, green, blue]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); } protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); } @@ -186,12 +186,12 @@ export class LPF2Hub extends Hub { * Emits when a button is pressed. * @event LPF2Hub#button * @param {string} button - * @param {number} state A number representing one of the button state consts. + * @param {ButtonState} state */ - this.emit("button", "GREEN", Consts.ButtonStates.PRESSED); + this.emit("button", "GREEN", Consts.ButtonState.PRESSED); return; } else if (data[5] === 0) { - this.emit("button", "GREEN", Consts.ButtonStates.RELEASED); + this.emit("button", "GREEN", Consts.ButtonState.RELEASED); return; } } @@ -242,12 +242,12 @@ export class LPF2Hub extends Hub { private _parseSensorMessage (data: Buffer) { - if ((data[3] === 0x3b && this.type === Consts.Hubs.POWERED_UP_REMOTE) || (data[3] === 0x3c && this.type !== Consts.Hubs.POWERED_UP_REMOTE)) { // Voltage + if ((data[3] === 0x3b && this.type === Consts.HubType.POWERED_UP_REMOTE) || (data[3] === 0x3c && this.type !== Consts.HubType.POWERED_UP_REMOTE)) { // Voltage data = this._padMessage(data, 6); const batteryLevel = (data.readUInt16LE(4) / 4096) * 100; this._batteryLevel = Math.floor(batteryLevel); return; - } else if (data[3] === 0x3b && this.type !== Consts.Hubs.POWERED_UP_REMOTE) { // Current (Non-PUP Remote) + } else if (data[3] === 0x3b && this.type !== Consts.HubType.POWERED_UP_REMOTE) { // Current (Non-PUP Remote) data = this._padMessage(data, 6); const current = data.readUInt16LE(4) / 4096; this._current = current * 100; @@ -267,7 +267,7 @@ export class LPF2Hub extends Hub { if (port && port.connected) { switch (port.type) { - case Consts.Devices.WEDO2_DISTANCE: + case Consts.DeviceType.WEDO2_DISTANCE: { let distance = data[4]; if (data[5] === 1) { @@ -282,14 +282,14 @@ export class LPF2Hub extends Hub { this.emit("distance", port.id, distance * 10); break; } - case Consts.Devices.BOOST_DISTANCE: + case Consts.DeviceType.BOOST_DISTANCE: { /** * Emits when a color sensor is activated. * @event LPF2Hub#color * @param {string} port - * @param {number} color A number representing one of the LED color consts. + * @param {Color} color */ if (data[4] <= 10) { this.emit("color", port.id, data[4]); @@ -299,13 +299,26 @@ export class LPF2Hub extends Hub { const partial = data[7]; if (partial > 0) { - distance += 1 / partial; + distance += 1.0 / partial; } - this.emit("distance", port.id, Math.floor(distance * 25.4) - 20); + distance = Math.floor(distance * 25.4) - 20; + + this.emit("distance", port.id, distance); + + /** + * A combined color and distance event, emits when the sensor is activated. + * @event LPF2Hub#colorAndDistance + * @param {string} port + * @param {Color} color + * @param {number} distance Distance, in millimeters. + */ + if (data[4] <= 10) { + this.emit("colorAndDistance", port.id, data[4], distance); + } break; } - case Consts.Devices.WEDO2_TILT: + case Consts.DeviceType.WEDO2_TILT: { const tiltX = data[4] > 160 ? data[4] - 255 : data[4] - (data[4] * 2); const tiltY = data[5] > 160 ? 255 - data[5] : data[5] - (data[5] * 2); @@ -321,9 +334,9 @@ export class LPF2Hub extends Hub { this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY); break; } - case Consts.Devices.BOOST_TACHO_MOTOR: + case Consts.DeviceType.BOOST_TACHO_MOTOR: { - const rotation = data.readInt32LE(2); + const rotation = data.readInt32LE(4); /** * Emits when a rotation sensor is activated. * @event LPF2Hub#rotate @@ -333,53 +346,53 @@ export class LPF2Hub extends Hub { this.emit("rotate", port.id, rotation); break; } - case Consts.Devices.BOOST_MOVE_HUB_MOTOR: + case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: { - const rotation = data.readInt32LE(2); + const rotation = data.readInt32LE(4); this.emit("rotate", port.id, rotation); break; } - case Consts.Devices.BOOST_TILT: + case Consts.DeviceType.BOOST_TILT: { const tiltX = data[4] > 160 ? data[4] - 255 : data[4]; const tiltY = data[5] > 160 ? 255 - data[5] : data[5] - (data[5] * 2); this.emit("tilt", port.id, tiltX, tiltY); break; } - case Consts.Devices.POWERED_UP_REMOTE_BUTTON: + case Consts.DeviceType.POWERED_UP_REMOTE_BUTTON: { switch (data[4]) { case 0x01: { - this.emit("button", port.id, Consts.ButtonStates.UP); + this.emit("button", port.id, Consts.ButtonState.UP); break; } case 0xff: { - this.emit("button", port.id, Consts.ButtonStates.DOWN); + this.emit("button", port.id, Consts.ButtonState.DOWN); break; } case 0x7f: { - this.emit("button", port.id, Consts.ButtonStates.STOP); + this.emit("button", port.id, Consts.ButtonState.STOP); break; } case 0x00: { - this.emit("button", port.id, Consts.ButtonStates.RELEASED); + this.emit("button", port.id, Consts.ButtonState.RELEASED); break; } } break; } - case Consts.Devices.DUPLO_TRAIN_BASE_COLOR: + case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: { if (data[4] <= 10) { this.emit("color", port.id, data[4]); } break; } - case Consts.Devices.DUPLO_TRAIN_BASE_SPEEDOMETER: + case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER: { /** * Emits on a speed change. diff --git a/package-lock.json b/package-lock.json index 57bf032..9bc1c11 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "@types/debug": { - "version": "0.0.30", - "resolved": "https://registry.npmjs.org/@types/debug/-/debug-0.0.30.tgz", - "integrity": "sha512-orGL5LXERPYsLov6CWs3Fh6203+dXzJkR7OnddIr2514Hsecwc8xRpzCapshBbKFImCsvS/mk6+FWiN5LyZJAQ==", + "version": "0.0.31", + "resolved": "https://npm-registry.rancher.sie.sony.com/@/@types/debug/_attachments/debug-0.0.31.tgz", + "integrity": "sha512-LS1MCPaQKqspg7FvexuhmDbWUhE2yIJ+4AgVIyObfc06/UKZ8REgxGNjZc82wPLWmbeOm7S+gSsLgo75TanG4A==", "dev": true }, "@types/events": { @@ -27,9 +27,9 @@ } }, "@types/node": { - "version": "10.12.3", - "resolved": "https://npm-registry.rancher.sie.sony.com/@/@types/node/_attachments/node-10.12.3.tgz", - "integrity": "sha512-sfGmOtSMSbQ/AKG8V9xD1gmjquC9awIIZ/Kj309pHb2n3bcRAcGMQv5nJ6gCXZVsneGE4+ve8DXKRCsrg3TFzg==", + "version": "10.12.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.9.tgz", + "integrity": "sha512-eajkMXG812/w3w4a1OcBlaTwsFPO5F7fJ/amy+tieQxEMWBlbV1JGSjkFM+zkHNf81Cad+dfIRA+IBkvmvdAeA==", "dev": true }, "abbrev": { @@ -370,7 +370,7 @@ }, "cross-spawn": { "version": "6.0.5", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", + "resolved": "https://npm-registry.rancher.sie.sony.com/c/cross-spawn/_attachments/cross-spawn-6.0.5.tgz", "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", "requires": { "nice-try": "^1.0.4", @@ -381,9 +381,9 @@ } }, "debug": { - "version": "3.2.6", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", - "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "version": "4.1.0", + "resolved": "https://npm-registry.rancher.sie.sony.com/d/debug/_attachments/debug-4.1.0.tgz", + "integrity": "sha512-heNPJUJIqC+xB6ayLAMHaIrmN9HKa7aQO8MGqKpvCA+uJYVcvR6l5kgdrhRuwPFHU7P5/A1w0BjByPHwpfTDKg==", "requires": { "ms": "^2.1.1" } @@ -602,7 +602,7 @@ }, "isexe": { "version": "2.0.0", - "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "resolved": "https://npm-registry.rancher.sie.sony.com/i/isexe/_attachments/isexe-2.0.0.tgz", "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" }, "js-tokens": { @@ -787,7 +787,7 @@ }, "ms": { "version": "2.1.1", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "resolved": "https://npm-registry.rancher.sie.sony.com/m/ms/_attachments/ms-2.1.1.tgz", "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" }, "nan": { @@ -798,7 +798,7 @@ }, "napi-thread-safe-callback": { "version": "0.0.6", - "resolved": "https://registry.npmjs.org/napi-thread-safe-callback/-/napi-thread-safe-callback-0.0.6.tgz", + "resolved": "https://npm-registry.rancher.sie.sony.com/n/napi-thread-safe-callback/_attachments/napi-thread-safe-callback-0.0.6.tgz", "integrity": "sha512-X7uHCOCdY4u0yamDxDrv3jF2NtYc8A1nvPzBQgvpoSX+WB3jAe2cVNsY448V1ucq7Whf9Wdy02HEUoLW5rJKWg==" }, "needle": { @@ -828,7 +828,7 @@ }, "nice-try": { "version": "1.0.5", - "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", + "resolved": "https://npm-registry.rancher.sie.sony.com/n/nice-try/_attachments/nice-try-1.0.5.tgz", "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==" }, "noble": { @@ -858,8 +858,8 @@ } }, "noble-mac": { - "version": "git+https://github.com/Timeular/noble-mac.git#a776e9a5b66abeb45707559321e3007a0b397b48", - "from": "git+https://github.com/Timeular/noble-mac.git#a776e9a", + "version": "git+https://github.com/Timeular/noble-mac.git#af4418ef5fb1ec97a662687f6a11b7f7cc59b2b4", + "from": "git+https://github.com/Timeular/noble-mac.git#af4418e", "requires": { "cross-spawn": "^6.0.5", "napi-thread-safe-callback": "0.0.6", @@ -869,9 +869,9 @@ } }, "node-addon-api": { - "version": "1.4.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/n/node-addon-api/_attachments/node-addon-api-1.4.0.tgz", - "integrity": "sha512-agquHPHnxYGox7Rjz2+TZQeOiH8IVbNFSTyTPA+peMUAP6klgrBH5dcwHsNNChQh7l/dtF0JNmZPbCqd5OXOIQ==" + "version": "1.6.1", + "resolved": "https://npm-registry.rancher.sie.sony.com/n/node-addon-api/_attachments/node-addon-api-1.6.1.tgz", + "integrity": "sha512-GcLOYrG5/enbqH4SMsqXt6GQUQGGnDnE3FLDZzXYkCgQHuZV5UDFR+EboeY8kpG0avroyOjpFQ2qLEBosFcRIA==" }, "node-pre-gyp": { "version": "0.10.3", @@ -990,7 +990,7 @@ }, "path-key": { "version": "2.0.1", - "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", + "resolved": "https://npm-registry.rancher.sie.sony.com/p/path-key/_attachments/path-key-2.0.1.tgz", "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=" }, "path-parse": { @@ -1169,7 +1169,7 @@ }, "shebang-command": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", + "resolved": "https://npm-registry.rancher.sie.sony.com/s/shebang-command/_attachments/shebang-command-1.2.0.tgz", "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", "requires": { "shebang-regex": "^1.0.0" @@ -1177,7 +1177,7 @@ }, "shebang-regex": { "version": "1.0.0", - "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", + "resolved": "https://npm-registry.rancher.sie.sony.com/s/shebang-regex/_attachments/shebang-regex-1.0.0.tgz", "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=" }, "signal-exit": { @@ -1367,9 +1367,9 @@ } }, "typescript": { - "version": "2.9.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.9.2.tgz", - "integrity": "sha512-Gr4p6nFNaoufRIY4NMdpQRNmgxVIGMs4Fcu/ujdYk3nAZqk7supzBE9idmvfZIlH/Cuj//dvi+019qEue9lV0w==", + "version": "3.1.6", + "resolved": "https://npm-registry.rancher.sie.sony.com/t/typescript/_attachments/typescript-3.1.6.tgz", + "integrity": "sha512-tDMYfVtvpb96msS1lDX9MEdHrW4yOuZ4Kdc4Him9oU796XldPYF/t2+uKoX0BBa0hXXwDlqYQbXY5Rzjzc5hBA==", "dev": true }, "typical": { @@ -1435,7 +1435,7 @@ }, "which": { "version": "1.3.1", - "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", + "resolved": "https://npm-registry.rancher.sie.sony.com/w/which/_attachments/which-1.3.1.tgz", "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", "requires": { "isexe": "^2.0.0" diff --git a/package.json b/package.json index 9bb74c9..9c09741 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "node-poweredup", - "version": "1.1.7", + "version": "1.4.5", "description": "A Node.js module to interface with LEGO Powered UP components.", "homepage": "https://github.com/nathankellenicki/node-poweredup/", "main": "dist/index.js", "types": "dist/index.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/duplotrainbase.js dist/hub.js dist/consts.js > DOCS.md", + "docs": "jsdoc2md dist/consts.js 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" @@ -15,17 +15,17 @@ "author": "Nathan Kellenicki ", "license": "MIT", "dependencies": { - "debug": "^3.1.0", + "debug": "^4.1.0", "noble": "1.9.1", - "noble-mac": "https://github.com/Timeular/noble-mac.git#a776e9a" + "noble-mac": "https://github.com/Timeular/noble-mac.git#af4418e" }, "devDependencies": { "jsdoc-to-markdown": "^4.0.1", - "typescript": "^2.9.2", - "tslint": "^5.10.0", - "@types/debug": "0.0.30", + "typescript": "^3.1.6", + "tslint": "^5.11.0", + "@types/debug": "0.0.31", "@types/noble": "0.0.37", - "@types/node": "^10.3.4" + "@types/node": "^10.12.9" }, "resolutions": { "xpc-connection": "sandeepmistry/node-xpc-connection#pull/26/head" diff --git a/port.ts b/port.ts index 508f703..0a279cb 100644 --- a/port.ts +++ b/port.ts @@ -6,7 +6,7 @@ export class Port { public id: string; public value: number; - public type: Consts.Devices; + public type: Consts.DeviceType; public connected: boolean = false; public busy: boolean = false; public finished: (() => void) | null = null; @@ -16,7 +16,7 @@ export class Port { constructor (id: string, value: number) { this.id = id; this.value = value; - this.type = Consts.Devices.UNKNOWN; + this.type = Consts.DeviceType.UNKNOWN; } public cancelEventTimer () { diff --git a/poweredup.ts b/poweredup.ts index 16b2058..942d99b 100644 --- a/poweredup.ts +++ b/poweredup.ts @@ -22,7 +22,7 @@ let wantScan = false; const startScanning = () => { if (isBrowserContext) { - noble.startScanning([Consts.BLEServices.WEDO2_SMART_HUB, Consts.BLEServices.LPF2_HUB]); + noble.startScanning([Consts.BLEService.WEDO2_SMART_HUB, Consts.BLEService.LPF2_HUB]); } else { noble.startScanning(); } diff --git a/puphub.ts b/puphub.ts index 1b6c726..e0885ee 100644 --- a/puphub.ts +++ b/puphub.ts @@ -32,13 +32,13 @@ export class PUPHub extends LPF2Hub { public static IsPUPHub (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID); } constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hubs.POWERED_UP_HUB; + this.type = Consts.HubType.POWERED_UP_HUB; this._ports = { "A": new Port("A", 0), "B": new Port("B", 1), @@ -97,7 +97,7 @@ export class PUPHub extends LPF2Hub { // @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); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { let data = null; if (portObj.id === "AB") { @@ -105,7 +105,7 @@ export class PUPHub extends LPF2Hub { } else { data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]); } - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); @@ -117,7 +117,7 @@ export class PUPHub extends LPF2Hub { // @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); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); } }); @@ -159,11 +159,11 @@ export class PUPHub extends LPF2Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); diff --git a/pupremote.ts b/pupremote.ts index a16b4b0..b5adf1b 100644 --- a/pupremote.ts +++ b/pupremote.ts @@ -57,13 +57,13 @@ export class PUPRemote extends LPF2Hub { public static IsPUPRemote (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID); } constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hubs.POWERED_UP_REMOTE; + this.type = Consts.HubType.POWERED_UP_REMOTE; this._ports = { "LEFT": new Port("LEFT", 0), "RIGHT": new Port("RIGHT", 1) @@ -85,18 +85,18 @@ export class PUPRemote extends LPF2Hub { /** * Set the color of the LED on the Remote via a color value. * @method PUPRemote#setLEDColor - * @param {number} color A number representing one of the LED color consts. + * @param {Color} color * @returns {Promise} Resolved upon successful issuance of command. */ public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, 0x34, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); if (color === false) { color = 0; } data = Buffer.from([0x81, 0x34, 0x11, 0x51, 0x00, color]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -113,9 +113,9 @@ export class PUPRemote extends LPF2Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, 0x34, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); data = Buffer.from([0x81, 0x34, 0x11, 0x51, 0x01, red, green, blue]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } diff --git a/wedo2smarthub.ts b/wedo2smarthub.ts index 99b7923..22d1b68 100644 --- a/wedo2smarthub.ts +++ b/wedo2smarthub.ts @@ -26,7 +26,7 @@ export class WeDo2SmartHub extends Hub { public static IsWeDo2SmartHub (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0); } @@ -36,7 +36,7 @@ export class WeDo2SmartHub extends Hub { constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hubs.WEDO2_SMART_HUB; + this.type = Consts.HubType.WEDO2_SMART_HUB; this._ports = { "A": new Port("A", 1), "B": new Port("B", 2) @@ -49,12 +49,12 @@ export class WeDo2SmartHub extends Hub { return new Promise(async (resolve, reject) => { debug("Connecting to WeDo 2.0 Smart Hub"); await super.connect(); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_PORT_TYPE), this._parsePortMessage.bind(this)); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_SENSOR_VALUE), this._parseSensorMessage.bind(this)); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BUTTON), this._parseSensorMessage.bind(this)); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY), this._parseBatteryMessage.bind(this)); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_HIGH_CURRENT_ALERT), this._parseHighCurrentAlert.bind(this)); - this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY).read((err, data) => { + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_PORT_TYPE), this._parsePortMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_SENSOR_VALUE), this._parseSensorMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BUTTON), this._parseSensorMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY), this._parseBatteryMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT), this._parseHighCurrentAlert.bind(this)); + this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY).read((err, data) => { this._parseBatteryMessage(data); }); debug("Connect completed"); @@ -76,8 +76,8 @@ export class WeDo2SmartHub extends Hub { return new Promise((resolve, reject) => { const data = Buffer.from(name, "ascii"); // Send this twice, as sometimes the first time doesn't take - this._writeMessage(Consts.BLECharacteristics.WEDO2_NAME_ID, data); - this._writeMessage(Consts.BLECharacteristics.WEDO2_NAME_ID, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_NAME_ID, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_NAME_ID, data); this._name = name; return resolve(); }); @@ -87,18 +87,18 @@ export class WeDo2SmartHub extends Hub { /** * Set the color of the LED on the Hub via a color value. * @method WeDo2SmartHub#setLEDColor - * @param {number} color A number representing one of the LED color consts. + * @param {Color} color * @returns {Promise} Resolved upon successful issuance of command. */ public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); if (color === false) { color = 0; } data = Buffer.from([0x06, 0x04, 0x01, color]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }); } @@ -115,9 +115,9 @@ export class WeDo2SmartHub extends Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x06, 0x17, 0x01, 0x02]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }); } @@ -144,10 +144,10 @@ export class WeDo2SmartHub extends Hub { portObj.cancelEventTimer(); } return new Promise((resolve, reject) => { - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)])); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)])); if (time && typeof time === "number") { const timeout = global.setTimeout(() => { - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00])); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00])); return resolve(); }, time); portObj.setEventTimer(timeout); @@ -192,7 +192,7 @@ export class WeDo2SmartHub extends Hub { const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]); data.writeUInt16LE(frequency, 3); data.writeUInt16LE(time, 5); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); global.setTimeout(resolve, time); }); } @@ -211,11 +211,11 @@ export class WeDo2SmartHub extends Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }, time); portObj.setEventTimer(timeout); @@ -227,12 +227,12 @@ export class WeDo2SmartHub extends Hub { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); + this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); } protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); + this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); } @@ -276,12 +276,12 @@ export class WeDo2SmartHub extends Hub { * Emits when a button is pressed. * @event WeDo2SmartHub#button * @param {string} button - * @param {number} state A number representing one of the button state consts. + * @param {ButtonState} state */ - this.emit("button", "GREEN", Consts.ButtonStates.PRESSED); + this.emit("button", "GREEN", Consts.ButtonState.PRESSED); return; } else if (data[0] === 0x00) { - this.emit("button", "GREEN", Consts.ButtonStates.RELEASED); + this.emit("button", "GREEN", Consts.ButtonState.RELEASED); return; } @@ -293,7 +293,7 @@ export class WeDo2SmartHub extends Hub { if (port && port.connected) { switch (port.type) { - case Consts.Devices.WEDO2_DISTANCE: + case Consts.DeviceType.WEDO2_DISTANCE: { let distance = data[2]; if (data[3] === 1) { @@ -308,19 +308,19 @@ export class WeDo2SmartHub extends Hub { this.emit("distance", port.id, distance * 10); break; } - case Consts.Devices.BOOST_DISTANCE: + case Consts.DeviceType.BOOST_DISTANCE: { const distance = data[2]; /** * Emits when a color sensor is activated. * @event WeDo2SmartHub#color * @param {string} port - * @param {number} color A number representing one of the LED color consts. + * @param {Color} color */ this.emit("color", port.id, distance); break; } - case Consts.Devices.WEDO2_TILT: + case Consts.DeviceType.WEDO2_TILT: { this._lastTiltX = data[2]; if (this._lastTiltX > 100) { @@ -340,7 +340,7 @@ export class WeDo2SmartHub extends Hub { this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY); break; } - case Consts.Devices.BOOST_TACHO_MOTOR: + case Consts.DeviceType.BOOST_TACHO_MOTOR: { const rotation = data.readInt32LE(2); /**