diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 24f6e1c..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,54 +0,0 @@ -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/.drone.yml b/.drone.yml new file mode 100644 index 0000000..1aef699 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,22 @@ +pipeline: + + install: + image: node:10.15.1 + commands: + - apt-get update + - apt-get install -y bluetooth bluez libbluetooth-dev libudev-dev + - npm install + + build: + image: node:10.15.1 + commands: + - npm run all + + publish: + image: plugins/npm + username: nathankellenicki + token: + from_secret: NPM_TOKEN + when: + ref: + - refs/tags/v* \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1dd31ce..27243c4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ dist/ .vscode/ *.tgz +**/.DS_store diff --git a/README.md b/README.md index ef956f1..a7e182c 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,15 @@ -[![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) +[![Drone](https://drone.kellenicki.com/api/badges/nkellenicki/node-poweredup/status.svg)](https://drone.kellenicki.com/nkellenicki/node-poweredup) +[![NPM Version](https://img.shields.io/npm/v/node-poweredup.svg?style=flat)](https://www.npmjs.com/package/node-poweredup) -# **node-poweredup** - A Node.js module to interface with LEGO Powered UP components. +# **node-poweredup** - A Javascript module to interface with LEGO Powered Up components. ### Introduction -LEGO Powered UP is the successor to Power Functions, the system for adding electronics to LEGO models. Powered UP is a collection of ranges - starting with LEGO WeDo 2.0 released in 2016, LEGO Boost released in 2017, and LEGO Powered UP released in 2018. It also includes the 2018 Duplo App-Controlled Train sets. +LEGO Powered Up is the successor to Power Functions, the system for adding electronics to LEGO models. Powered Up is a collection of ranges - starting with LEGO WeDo 2.0 released in 2016, LEGO Boost released in 2017, LEGO Powered Up released in 2018, and LEGO Technic CONTROL+ released in 2019. It also includes the 2018 Duplo App-Controlled Train sets. -Powered UP has a few improvements over Power Functions: +This library allows communication and control of Powered Up devices and peripherals via Javascript, both from Node.js and from the browser using Web Bluetooth. -1. The use of Bluetooth Low Energy makes it easy to control from a computer, and even write code for. - -2. The ability to use sensors to react to events happening in the real world opens up a whole new world of possibilities. - -3. As Powered UP hubs and remotes pair with each other, the system allows for a near unlimited number of independently controlled models in the same room. Power Functions was limited to 8 due to the use of infra-red for communication. - -### Installation +### Node.js Installation Node.js v8.0 required. @@ -30,17 +23,19 @@ Note: node-poweredup has been tested on macOS 10.13 and Debian/Raspbian on the R ### Compatibility -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. +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. -| 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 | +| Device Name | Product Code | Type | WeDo 2.0 Smart Hub | Boost Move Hub | Powered Up Hub | Control+ Hub | Availability | +| ------------------------------- | ------------ | ------------- | ------------------ | -------------- | -------------- | ------------ | ----- | +| WeDo 2.0 Tilt Sensor | 45305 | Sensor | Yes | Yes | Yes | Yes | 45300 | +| WeDo 2.0 Motion Sensor | 45304 | Sensor | Yes | Yes | Yes | Yes | 45300 | +| WeDo 2.0 Medium Motor | 45303 | Motor | Yes | Yes | Yes | Yes | 45300
76112 | +| Boost Color and Distance Sensor | 88007 | Sensor | *Partial* | Yes | Yes | Yes | 17101 | +| Boost Tacho Motor | 88008 | Motor/Sensor | *Partial* | Yes | Yes | Yes | 17101 | +| Powered Up Train Motor | 88011 | Motor | Yes | Yes | Yes | Yes | 60197
60198 | +| Powered Up LED Lights | 88005 | Light | Yes | Yes | Yes | Yes | 88005 | +| Control+ Large Motor | 22169 | Motor/Sensor | *Partial* | No | Yes | Yes | 42099 | +| Control+ XLarge Motor | 22172 | Motor/Sensor | *Partial* | No | Yes | Yes | 42099
42100 | In addition, the Hubs themselves have certain built-in features which this library exposes. @@ -48,50 +43,54 @@ In addition, the Hubs themselves have certain built-in features which this libra | ------------------ | ------------ | ---------------------- | ------------ | | 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 | +| 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 | - +| Control+ Hub | 22127 | RGB LED
Button
Tilt Sensor
Accelerometer | 42099
42100 | ### Known Issues and Limitations * The Boost Color and Distance sensor only works in color mode with the WeDo 2.0 Smart Hub. -* When used with the WeDo 2.0 Smart Hub, the Boost Tacho Motor does not support rotating the motor by angle. +* When used with the WeDo 2.0 Smart Hub, the Boost Tacho Motor and Control+ Motors do not support rotating the motor by angle. -* When used with the Powered UP Hub, the Boost Tacho Motor does not support rotating the motor by angle. It also does not support rotation detection. - -* Plugging two Boost Tacho Motors into the Powered UP Hub will crash the Hub (This requires a firmware update from LEGO to fix). +* When used with the Boost Move Hub, the Control+ Motors do not currently accept commands. ### Documentation [Full documentation is available here.](https://nathankellenicki.github.io/node-poweredup/) -### Sample Usage +### Node.js Sample Usage ```javascript const PoweredUP = require("node-poweredup"); const poweredUP = new PoweredUP.PoweredUP(); poweredUP.on("discover", async (hub) => { // Wait to discover a Hub + console.log(`Discovered ${hub.name}!`); await hub.connect(); // Connect to the Hub + console.log("Connected"); await hub.sleep(3000); // Sleep for 3 seconds before starting while (true) { // Repeat indefinitely + console.log("Running motor B at speed 75"); hub.setMotorSpeed("B", 75); // Start a motor attached to port B to run a 3/4 speed (75) indefinitely + console.log("Running motor A at speed 100 for 2 seconds"); await hub.setMotorSpeed("A", 100, 2000); // Run a motor attached to port A for 2 seconds at maximum speed (100) then stop await hub.sleep(1000); // Do nothing for 1 second + console.log("Running motor A at speed -50 for 1 seconds"); await hub.setMotorSpeed("A", -50, 1000); // Run a motor attached to port A for 1 second at 1/2 speed in reverse (-50) then stop await hub.sleep(1000); // Do nothing for 1 second } }); poweredUP.scan(); // Start scanning for Hubs +console.log("Scanning for Hubs..."); ``` More examples are available in the "examples" directory. ### Credits -Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian Raff ([@hobbyquaker](https://github.com/hobbyquaker)), Valentin Heun ([@vheun](https://github.com/vheun)), Johan Korten ([@jakorten](https://github.com/jakorten)), and Andrey Pokhilko ([@undera](https://github.com/undera)) for their various works, contributions, and assistance on figuring out the LEGO Boost, WeDo 2.0, and Powered UP protocols. +Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian Raff ([@hobbyquaker](https://github.com/hobbyquaker)), Valentin Heun ([@vheun](https://github.com/vheun)), Johan Korten ([@jakorten](https://github.com/jakorten)), and Andrey Pokhilko ([@undera](https://github.com/undera)) for their various works, contributions, and assistance on figuring out the LEGO Boost, WeDo 2.0, and Powered Up protocols. diff --git a/docs/BoostMoveHub.html b/docs/BoostMoveHub.html index f10abce..e7b07b8 100644 --- a/docs/BoostMoveHub.html +++ b/docs/BoostMoveHub.html @@ -4,7 +4,7 @@ - DocStrap Class: BoostMoveHub + node-poweredup Class: BoostMoveHub + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Class: ControlPlusHub

+
+ +
+ +

+ ControlPlusHub +

+ + +
+ + +
+
+ + +
+
+

new ControlPlusHub()

+ + +
+
+ + +
+

The ControlPlusHub is emitted if the discovered device is a Control+ Hub.

+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + + + + + + + + + + + + + + + +

Members

+ +
+ +
+
+

<readonly> batteryLevel

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
batteryLevel + + +number + + + + +

Battery level of the hub (Percentage between 0-100)

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> current

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
current + + +number + + + + +

Current usage of the hub (Milliamps)

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> firmwareVersion

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firmwareVersion + + +string + + + + +

Firmware version of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> name

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

Name of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> rssi

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
rssi + + +number + + + + +

Signal strength of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> uuid

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
uuid + + +string + + + + +

UUID of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> voltage

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
voltage + + +number + + + + +

Voltage of the hub (Volts)

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+
+

brakeMotor(port)

+ + +
+
+ + +
+

Fully (hard) stop the motor on a given port.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

connect()

+ + +
+
+ + +
+

Connect to the Hub.

+
+ + + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful connect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

disconnect()

+ + +
+
+ + +
+

Disconnect the Hub.

+
+ + + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful disconnect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

getHubType()

+ + +
+
+ + +
+

Get the hub type.

+
+ + + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +HubType + + + +
+
+ + + + + +
+ + + +
+
+

getPortDeviceType(port)

+ + +
+
+ + +
+

Get the device type for a given port.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +DeviceType + + + +
+
+ + + + + +
+ + + +
+
+

rampMotorSpeed(port, fromSpeed, toSpeed, time)

+ + +
+
+ + +
+

Ramp the motor speed on a given port.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
fromSpeed + + +number + + + + +

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

toSpeed + + +number + + + + +

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

time + + +number + + + + +

How long the ramp should last (in milliseconds).

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

resetAbsolutePosition(port)

+ + +
+
+ + +
+

Reset the current motor position as absolute position zero

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command (ie. once the motor is finished).

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

setAbsolutePosition(port, pos [, speed])

+ + +
+
+ + +
+

Tell motor to goto an absolute position

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDefaultDescription
port + + +string + + + + + + + + + + + + +
pos + + +number + + + + + + + + + + + + +

The position of the motor to go to

speed + + +number +| + +Array.<number> + + + + + + + <optional>
+ + + + + +
+ + 100 + +

A value between 1 - 100 should be set (Direction does not apply when going to absolute position)

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command (ie. once the motor is finished).

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

setLEDColor(color)

+ + +
+
+ + +
+

Set the color of the LED on the Hub via a color value.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
color + + +Color + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

setLEDRGB(red, green, blue)

+ + +
+
+ + +
+

Set the color of the LED on the Hub via RGB values.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
red + + +number + + + + +
green + + +number + + + + +
blue + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

setLightBrightness(port, brightness [, time])

+ + +
+
+ + +
+

Set the light brightness on a given port.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
port + + +string + + + + + + + + + + +
brightness + + +number + + + + + + + + + + +

Brightness value between 0-100 (0 is off)

time + + +number + + + + + + + <optional>
+ + + + + +

How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely.

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command. If time is specified, this is once the light is turned off.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

setMotorAngle(port, angle [, speed])

+ + +
+
+ + +
+

Rotate a motor by a given angle.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDefaultDescription
port + + +string + + + + + + + + + + + + +
angle + + +number + + + + + + + + + + + + +

How much the motor should be rotated (in degrees).

speed + + +number +| + +Array.<number> + + + + + + + <optional>
+ + + + + +
+ + 100 + +

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command (ie. once the motor is finished).

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

setMotorSpeed(port, speed [, time])

+ + +
+
+ + +
+

Set the motor speed on a given port.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
port + + +string + + + + + + + + + + +
speed + + +number +| + +Array.<number> + + + + + + + + + + +

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.

time + + +number + + + + + + + <optional>
+ + + + + +

How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command. If time is specified, this is once the motor is finished.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

setName(name)

+ + +
+
+ + +
+

Set the name of the Hub.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

New name of the hub (14 characters or less, ASCII only).

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

shutdown()

+ + +
+
+ + +
+

Shutdown the Hub.

+
+ + + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful disconnect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

sleep(delay)

+ + +
+
+ + +
+

Sleep a given amount of time.

+

This is a helper method to make it easier to add delays into a chain of commands.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
delay + + +number + + + + +

How long to sleep (in milliseconds).

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved after the delay is finished.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

subscribe(port [, mode])

+ + +
+
+ + +
+

Subscribe to sensor notifications on a given port.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
port + + +string + + + + + + + + + + +
mode + + +number + + + + + + + <optional>
+ + + + + +

The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

unsubscribe(port)

+ + +
+
+ + +
+

Unsubscribe to sensor notifications on a given port.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

wait(commands)

+ + +
+
+ + +
+

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.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
commands + + +Array.<Promise.<any>> + + + + +

Array of executing commands.

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved after the commands are finished.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ +
+ + + + + +

Events

+ +
+ +
+
+

accel

+ + +
+
+ + +
+

Emits when accelerometer detects movement. Measured in DPS - degrees per second.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
x + + +number + + + + +
y + + +number + + + + +
z + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

attach

+ + +
+
+ + +
+

Emits when a motor or sensor is attached to the Hub.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
type + + +DeviceType + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

button

+ + +
+
+ + +
+

Emits when a button is pressed.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
button + + +string + + + + +
state + + +ButtonState + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

color

+ + +
+
+ + +
+

Emits when a color sensor is activated.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
color + + +Color + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

colorAndDistance

+ + +
+
+ + +
+

A combined color and distance event, emits when the sensor is activated.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
color + + +Color + + + + +
distance + + +number + + + + +

Distance, in millimeters.

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

detach

+ + +
+
+ + +
+

Emits when an attached motor or sensor is detached from the Hub.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

distance

+ + +
+
+ + +
+

Emits when a distance sensor is activated.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
distance + + +number + + + + +

Distance, in millimeters.

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

rotate

+ + +
+
+ + +
+

Emits when a rotation sensor is activated.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
rotation + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

speed

+ + +
+
+ + +
+

Emits on a speed change.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
speed + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

temp

+ + +
+
+ + +
+

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

temp + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

tilt

+ + +
+
+ + +
+

Emits when a tilt sensor is activated.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

x + + +number + + + + +
y + + +number + + + + +
z + + +number + + + + +

(Only available when using a Control+ Hub)

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/DuploTrainBase.html b/docs/DuploTrainBase.html index 6027706..385ade6 100644 --- a/docs/DuploTrainBase.html +++ b/docs/DuploTrainBase.html @@ -4,7 +4,7 @@ - DocStrap Class: DuploTrainBase + node-poweredup Class: DuploTrainBase + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: controlplushub.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const lpf2hub_1 = require("./lpf2hub");
+const port_1 = require("./port");
+const Consts = __importStar(require("./consts"));
+const Debug = require("debug");
+const debug = Debug("ControlPlusHub");
+/**
+ * The ControlPlusHub is emitted if the discovered device is a Control+ Hub.
+ * @class ControlPlusHub
+ * @extends LPF2Hub
+ * @extends Hub
+ */
+class ControlPlusHub extends lpf2hub_1.LPF2Hub {
+    static IsControlPlusHub(peripheral) {
+        return (peripheral.advertisement &&
+            peripheral.advertisement.serviceUuids &&
+            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.CONTROL_PLUS_LARGE_HUB);
+    }
+    constructor(device, autoSubscribe = true) {
+        super(device, autoSubscribe);
+        this.type = Consts.HubType.CONTROL_PLUS_HUB;
+        this._ports = {
+            "A": new port_1.Port("A", 0),
+            "B": new port_1.Port("B", 1),
+            "C": new port_1.Port("C", 2),
+            "D": new port_1.Port("D", 3),
+        };
+        this.on("attach", (port, type) => {
+            this._combinePorts(port, type);
+        });
+        debug("Discovered Control+ Hub");
+    }
+    connect() {
+        return new Promise(async (resolve, reject) => {
+            debug("Connecting to Control+ Hub");
+            await super.connect();
+            this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x62, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01])); // Accelerometer
+            this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x63, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Gyro/Tilt
+            this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01])); // Temperature
+            debug("Connect completed");
+            return resolve();
+        });
+    }
+    /**
+     * Set the motor speed on a given port.
+     * @method ControlPlusHub#setMotorSpeed
+     * @param {string} port
+     * @param {number | Array.<number>} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.
+     * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
+     * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
+     */
+    setMotorSpeed(port, speed, time) {
+        const portObj = this._portLookup(port);
+        if (!this._virtualPorts[portObj.id] && speed instanceof Array) {
+            throw new Error(`Port ${portObj.id} can only accept a single speed`);
+        }
+        let cancelEventTimer = true;
+        if (typeof time === "boolean") {
+            if (time === true) {
+                cancelEventTimer = false;
+            }
+            time = undefined;
+        }
+        if (cancelEventTimer) {
+            portObj.cancelEventTimer();
+        }
+        return new Promise((resolve, reject) => {
+            if (time && typeof time === "number") {
+                if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR ||
+                    portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR ||
+                    portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR ||
+                    portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR) {
+                    portObj.busy = true;
+                    let data = null;
+                    if (this._virtualPorts[portObj.id]) {
+                        data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]);
+                    }
+                    else {
+                        // @ts-ignore: The type of speed is properly checked at the start
+                        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.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.BLECharacteristic.LPF2_ALL, data);
+                    const timeout = global.setTimeout(() => {
+                        const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
+                        this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
+                        return resolve();
+                        // @ts-ignore: The type of time is properly checked at the start
+                    }, time);
+                    portObj.setEventTimer(timeout);
+                }
+            }
+            else {
+                if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) {
+                    portObj.busy = true;
+                    let data = null;
+                    if (this._virtualPorts[portObj.id]) {
+                        data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]);
+                    }
+                    else {
+                        // @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.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.BLECharacteristic.LPF2_ALL, data);
+                }
+            }
+        });
+    }
+    /**
+     * Ramp the motor speed on a given port.
+     * @method ControlPlusHub#rampMotorSpeed
+     * @param {string} port
+     * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
+     * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
+     * @param {number} time How long the ramp should last (in milliseconds).
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    rampMotorSpeed(port, fromSpeed, toSpeed, time) {
+        const portObj = this._portLookup(port);
+        portObj.cancelEventTimer();
+        return new Promise((resolve, reject) => {
+            this._calculateRamp(fromSpeed, toSpeed, time, portObj)
+                .on("changeSpeed", (speed) => {
+                this.setMotorSpeed(port, speed, true);
+            })
+                .on("finished", resolve);
+        });
+    }
+    /**
+     * Rotate a motor by a given angle.
+     * @method ControlPlusHub#setMotorAngle
+     * @param {string} port
+     * @param {number} angle How much the motor should be rotated (in degrees).
+     * @param {number | Array.<number>} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.
+     * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
+     */
+    setMotorAngle(port, angle, speed = 100) {
+        const portObj = this._portLookup(port);
+        if (!(portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR ||
+            portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR ||
+            portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR ||
+            portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR)) {
+            throw new Error("Angle rotation is only available when using a Boost Tacho Motor, Boost Move Hub Motor, Control+ Medium Motor, or Control+ Large Motor");
+        }
+        if (!this._virtualPorts[portObj.id] && speed instanceof Array) {
+            throw new Error(`Port ${portObj.id} can only accept a single speed`);
+        }
+        portObj.cancelEventTimer();
+        return new Promise((resolve, reject) => {
+            portObj.busy = true;
+            let data = null;
+            if (this._virtualPorts[portObj.id]) {
+                data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]);
+            }
+            else {
+                // @ts-ignore: The type of speed is properly checked at the start
+                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.BLECharacteristic.LPF2_ALL, data);
+            portObj.finished = () => {
+                return resolve();
+            };
+        });
+    }
+    /**
+     * Tell motor to goto an absolute position
+     * @method ControlPlusHub#setAbsolutePosition
+     * @param {string} port
+     * @param {number} pos The position of the motor to go to
+     * @param {number | Array.<number>} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position)
+     * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
+     */
+    setAbsolutePosition(port, pos, speed = 100) {
+        const portObj = this._portLookup(port);
+        if (!(portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR ||
+            portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR)) {
+            throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor");
+        }
+        portObj.cancelEventTimer();
+        return new Promise((resolve, reject) => {
+            portObj.busy = true;
+            let data = null;
+            if (this._virtualPorts[portObj.id]) {
+                data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
+                data.writeInt32LE(pos, 4);
+                data.writeInt32LE(pos, 8);
+            }
+            else {
+                // @ts-ignore: The type of speed is properly checked at the start
+                data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
+                data.writeInt32LE(pos, 4);
+            }
+            this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
+            portObj.finished = () => {
+                return resolve();
+            };
+        });
+    }
+    /**
+     * Reset the current motor position as absolute position zero
+     * @method ControlPlusHub#resetAbsolutePosition
+     * @param {string} port
+     * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
+     */
+    resetAbsolutePosition(port) {
+        const portObj = this._portLookup(port);
+        if (!(portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR ||
+            portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR)) {
+            throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor");
+        }
+        return new Promise((resolve) => {
+            const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]);
+            this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
+            return resolve();
+        });
+    }
+    /**
+     * Fully (hard) stop the motor on a given port.
+     * @method ControlPlusHub#brakeMotor
+     * @param {string} port
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    brakeMotor(port) {
+        return this.setMotorSpeed(port, 127);
+    }
+    /**
+     * Set the light brightness on a given port.
+     * @method ControlPlusHub#setLightBrightness
+     * @param {string} port
+     * @param {number} brightness Brightness value between 0-100 (0 is off)
+     * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely.
+     * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off.
+     */
+    setLightBrightness(port, brightness, time) {
+        const portObj = this._portLookup(port);
+        portObj.cancelEventTimer();
+        return new Promise((resolve, reject) => {
+            const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]);
+            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.BLECharacteristic.LPF2_ALL, data);
+                    return resolve();
+                }, time);
+                portObj.setEventTimer(timeout);
+            }
+            else {
+                return resolve();
+            }
+        });
+    }
+}
+exports.ControlPlusHub = ControlPlusHub;
+//# sourceMappingURL=controlplushub.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/duplotrainbase.js.html b/docs/duplotrainbase.js.html index 617adcd..ce08cc2 100644 --- a/docs/duplotrainbase.js.html +++ b/docs/duplotrainbase.js.html @@ -4,7 +4,7 @@ - DocStrap Source: duplotrainbase.js + node-poweredup Source: duplotrainbase.js + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: poweredup-node.js

+ +
+
+
"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+const boostmovehub_1 = require("./boostmovehub");
+const controlplushub_1 = require("./controlplushub");
+const duplotrainbase_1 = require("./duplotrainbase");
+const nobledevice_1 = require("./nobledevice");
+const puphub_1 = require("./puphub");
+const pupremote_1 = require("./pupremote");
+const wedo2smarthub_1 = require("./wedo2smarthub");
+const events_1 = require("events");
+const Debug = require("debug");
+const debug = Debug("poweredup");
+const noble = require("noble-mac");
+let ready = false;
+let wantScan = false;
+let discoveryEventAttached = false;
+const startScanning = () => {
+    noble.startScanning();
+};
+noble.on("stateChange", (state) => {
+    ready = (state === "poweredOn");
+    if (ready) {
+        if (wantScan) {
+            debug("Scanning started");
+            startScanning();
+        }
+    }
+    else {
+        noble.stopScanning();
+    }
+});
+/**
+ * @class PoweredUP
+ * @extends EventEmitter
+ */
+class PoweredUP extends events_1.EventEmitter {
+    constructor() {
+        super();
+        this.autoSubscribe = true;
+        this._connectedHubs = {};
+        this._discoveryEventHandler = this._discoveryEventHandler.bind(this);
+    }
+    /**
+     * Begin scanning for Powered UP Hub devices.
+     * @method PoweredUP#scan
+     */
+    async scan() {
+        wantScan = true;
+        if (!discoveryEventAttached) {
+            noble.on("discover", this._discoveryEventHandler);
+            discoveryEventAttached = true;
+        }
+        if (ready) {
+            debug("Scanning started");
+            startScanning();
+        }
+        return true;
+    }
+    /**
+     * Stop scanning for Powered UP Hub devices.
+     * @method PoweredUP#stop
+     */
+    stop() {
+        wantScan = false;
+        if (discoveryEventAttached) {
+            noble.removeListener("discover", this._discoveryEventHandler);
+            discoveryEventAttached = false;
+        }
+        noble.stopScanning();
+    }
+    /**
+     * Retrieve a list of Powered UP Hubs.
+     * @method PoweredUP#getConnectedHubs
+     * @returns {Hub[]}
+     */
+    getConnectedHubs() {
+        return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]);
+    }
+    /**
+     * Retrieve a Powered UP Hub by UUID.
+     * @method PoweredUP#getConnectedHubByUUID
+     * @param {string} uuid
+     * @returns {Hub | null}
+     */
+    getConnectedHubByUUID(uuid) {
+        return this._connectedHubs[uuid];
+    }
+    /**
+     * Retrieve a list of Powered UP Hub by name.
+     * @method PoweredUP#getConnectedHubsByName
+     * @param {string} name
+     * @returns {Hub[]}
+     */
+    getConnectedHubsByName(name) {
+        return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.name === name);
+    }
+    async _discoveryEventHandler(peripheral) {
+        peripheral.removeAllListeners();
+        const device = new nobledevice_1.NobleDevice(peripheral);
+        let hub;
+        if (await wedo2smarthub_1.WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) {
+            hub = new wedo2smarthub_1.WeDo2SmartHub(device, this.autoSubscribe);
+        }
+        else if (await boostmovehub_1.BoostMoveHub.IsBoostMoveHub(peripheral)) {
+            hub = new boostmovehub_1.BoostMoveHub(device, this.autoSubscribe);
+        }
+        else if (await puphub_1.PUPHub.IsPUPHub(peripheral)) {
+            hub = new puphub_1.PUPHub(device, this.autoSubscribe);
+        }
+        else if (await pupremote_1.PUPRemote.IsPUPRemote(peripheral)) {
+            hub = new pupremote_1.PUPRemote(device, this.autoSubscribe);
+        }
+        else if (await duplotrainbase_1.DuploTrainBase.IsDuploTrainBase(peripheral)) {
+            hub = new duplotrainbase_1.DuploTrainBase(device, this.autoSubscribe);
+        }
+        else if (await controlplushub_1.ControlPlusHub.IsControlPlusHub(peripheral)) {
+            hub = new controlplushub_1.ControlPlusHub(device, this.autoSubscribe);
+        }
+        else {
+            return;
+        }
+        device.on("discoverComplete", () => {
+            hub.on("connect", () => {
+                debug(`Hub ${hub.uuid} connected`);
+                this._connectedHubs[hub.uuid] = hub;
+            });
+            hub.on("disconnect", () => {
+                debug(`Hub ${hub.uuid} disconnected`);
+                delete this._connectedHubs[hub.uuid];
+                if (wantScan) {
+                    startScanning();
+                }
+            });
+            debug(`Hub ${hub.uuid} discovered`);
+            /**
+             * Emits when a Powered UP Hub device is found.
+             * @event PoweredUP#discover
+             * @param {WeDo2SmartHub | BoostMoveHub | ControlPlusHub | PUPHub | PUPRemote | DuploTrainBase} hub
+             */
+            this.emit("discover", hub);
+        });
+    }
+}
+exports.PoweredUP = PoweredUP;
+//# sourceMappingURL=poweredup-node.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/poweredup.js.html b/docs/poweredup.js.html index c0f8e66..ecc71a3 100644 --- a/docs/poweredup.js.html +++ b/docs/poweredup.js.html @@ -93,6 +93,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", { value: true }); +const bledevice_1 = require("./bledevice"); const boostmovehub_1 = require("./boostmovehub"); const duplotrainbase_1 = require("./duplotrainbase"); const puphub_1 = require("./puphub"); @@ -192,21 +193,22 @@ class PoweredUP extends events_1.EventEmitter { return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.name === name); } async _discoveryEventHandler(peripheral) { + const device = new bledevice_1.BLEDevice(peripheral); let hub; if (await wedo2smarthub_1.WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) { - hub = new wedo2smarthub_1.WeDo2SmartHub(peripheral, this.autoSubscribe); + hub = new wedo2smarthub_1.WeDo2SmartHub(device, this.autoSubscribe); } else if (await boostmovehub_1.BoostMoveHub.IsBoostMoveHub(peripheral)) { - hub = new boostmovehub_1.BoostMoveHub(peripheral, this.autoSubscribe); + hub = new boostmovehub_1.BoostMoveHub(device, this.autoSubscribe); } else if (await puphub_1.PUPHub.IsPUPHub(peripheral)) { - hub = new puphub_1.PUPHub(peripheral, this.autoSubscribe); + hub = new puphub_1.PUPHub(device, this.autoSubscribe); } else if (await pupremote_1.PUPRemote.IsPUPRemote(peripheral)) { - hub = new pupremote_1.PUPRemote(peripheral, this.autoSubscribe); + hub = new pupremote_1.PUPRemote(device, this.autoSubscribe); } else if (await duplotrainbase_1.DuploTrainBase.IsDuploTrainBase(peripheral)) { - hub = new duplotrainbase_1.DuploTrainBase(peripheral, this.autoSubscribe); + hub = new duplotrainbase_1.DuploTrainBase(device, this.autoSubscribe); } else { return; @@ -216,7 +218,7 @@ class PoweredUP extends events_1.EventEmitter { // if (!isBrowserContext) { // startScanning(); // } - hub.on("discoverComplete", () => { + device.on("discoverComplete", () => { hub.on("connect", () => { debug(`Hub ${hub.uuid} connected`); this._connectedHubs[hub.uuid] = hub; @@ -284,7 +286,7 @@ exports.PoweredUP = PoweredUP; Documentation generated by JSDoc 3.5.5 - on Mon Feb 4th 2019 + on Wed Feb 13th 2019 using the DocStrap template. diff --git a/docs/puphub.js.html b/docs/puphub.js.html index 094618a..17bcef1 100644 --- a/docs/puphub.js.html +++ b/docs/puphub.js.html @@ -4,7 +4,7 @@ - DocStrap Source: puphub.js + node-poweredup Source: puphub.js