23 KiB
node-lpf2 - A Node.js module to interface with LEGO Power Functions 2.0 components.
Installation
Node.js v8.0+ required.
npm install node-lpf2 --save
node-lpf2 uses the Noble BLE library by Sandeep Mistry. On macOS everything should function out of the box. On Linux and Windows there are certain dependencies which may need installed first.
Note: node-lpf2 has been tested on macOS 10.13 and Debian/Raspbian on the Raspberry Pi 3 Model B.
Compatibility
While most LPF2 components and Hubs are compatible with each other, there are exceptions. 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 (1) | Yes | Yes | 17101 |
Boost Interactive Motor | Motor/Sensor | Partial (2) | Yes | Yes | 17101 |
Powered Up Train Motor | Motor | No | Yes | Yes | 60197 60198 |
Powered Up LED Lights | Light | Unknown | Unknown | Unknown | 88005 |
(1) Only color mode is supported on the WeDo 2.0 Smart Hub at this point.
(2) When used with the WeDo 2.0 Smart Hub, the Boost Interactive Medium Motor functions like the WeDo 2.0 Medium Motor - that is, only basic constant speed is possible. However the rotation sensing functionality is supported.
Usage
const LPF2 = require("node-lpf2").LPF2;
const lpf2 = new LPF2();
Examples are available in the "examples" directory.
Credits
Thanks go to Jorge Pereira (@JorgePe), Sebastian Raff (@hobbyquaker), Valentin Heun (@vheun), and Johan Korten (@jakorten) for their various works and assistance on figuring out the Boost, WeDo 2.0, and Powered Up protocols.
Classes
LPF2 ⇐ EventEmitter
Kind: global class
Extends: EventEmitter
- LPF2 ⇐
EventEmitter
lpF2.scan()
Begin scanning for LPF2 Hub devices.
Kind: instance method of LPF2
lpF2.stop()
Stop scanning for LPF2 Hub devices.
Kind: instance method of LPF2
lpF2.getConnectedDeviceByUUID(uuid) ⇒ Hub
| null
Retrieve a LPF2 Hub device by UUID.
Kind: instance method of LPF2
Param | Type |
---|---|
uuid | string |
lpF2.getConnectedDevices() ⇒ Array.<Hub>
Retrieve a list of LPF2 Hub devices.
Kind: instance method of LPF2
"discover" (hub)
Emits when a LPF2 Hub device is found.
Kind: event emitted by LPF2
Param | Type |
---|---|
hub | WeDo2Hub | LPF2Hub |
LPF2Hub ⇐ Hub
Kind: global class
Extends: Hub
- LPF2Hub ⇐
Hub
- new LPF2Hub()
- .setLEDColor(color) ⇒
Promise
- .setMotorSpeed(port, speed, [time]) ⇒
Promise
- .setMotorAngle(port, angle, [speed]) ⇒
Promise
- .connect() ⇒
Promise
- .disconnect() ⇒
Promise
- .subscribe(port, [mode]) ⇒
Promise
- .unsubscribe(port) ⇒
Promise
- .sleep(delay) ⇒
Promise
- .wait(commands) ⇒
Promise
- "button" (button, state)
- "distance" (port, distance)
- "color" (port, color)
- "tilt" (port, x, y)
- "rotate" (port, rotation)
- "attach" (port, type)
- "detach" (port)
new LPF2Hub()
The LPF2Hub is emitted if the discovered device is either a Boost Move Hub, Powered Up Hub, or Powered Up Remote.
lpF2Hub.setLEDColor(color) ⇒ Promise
Set the color of the LED on the Hub via a color value.
Kind: instance method of LPF2Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type | Description |
---|---|---|
color | number |
A number representing one of the LED color consts. |
lpF2Hub.setMotorSpeed(port, speed, [time]) ⇒ Promise
Set the motor speed on a given port.
Kind: instance method of LPF2Hub
Returns: Promise
- Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
Param | Type | Description |
---|---|---|
port | string |
|
speed | 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 to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. |
lpF2Hub.setMotorAngle(port, angle, [speed]) ⇒ Promise
Rotate a motor by a given angle.
Kind: instance method of LPF2Hub
Returns: Promise
- Resolved upon successful completion of command (ie. once the motor is finished).
Param | Type | Default | Description |
---|---|---|---|
port | string |
||
angle | number |
How much the motor should be rotated (in degrees). | |
[speed] | number |
100 |
How fast the motor should be rotated. |
lpF2Hub.connect() ⇒ Promise
Connect to the Hub.
Kind: instance method of LPF2Hub
Overrides: connect
Returns: Promise
- Resolved upon successful connect.
lpF2Hub.disconnect() ⇒ Promise
Disconnect the Hub.
Kind: instance method of LPF2Hub
Returns: Promise
- Resolved upon successful disconnect.
lpF2Hub.subscribe(port, [mode]) ⇒ Promise
Subscribe to sensor notifications on a given port.
Kind: instance method of LPF2Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type | Description |
---|---|---|
port | string |
|
[mode] | number |
The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
lpF2Hub.unsubscribe(port) ⇒ Promise
Unsubscribe to sensor notifications on a given port.
Kind: instance method of LPF2Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type |
---|---|
port | string |
lpF2Hub.sleep(delay) ⇒ Promise
Sleep a given amount of time.
This is a helper method to make it easier to add delays into a chain of commands.
Kind: instance method of LPF2Hub
Returns: Promise
- Resolved after the delay is finished.
Param | Type | Description |
---|---|---|
delay | number |
How long to sleep (in milliseconds). |
lpF2Hub.wait(commands) ⇒ Promise
Wait until a given list of concurrently running commands are complete.
This is a helper method to make it easier to wait for concurrent commands to complete.
Kind: instance method of LPF2Hub
Returns: Promise
- Resolved after the commands are finished.
Param | Type | Description |
---|---|---|
commands | Array.<Promise.<any>> |
Array of executing commands. |
"button" (button, state)
Emits when a button is pressed.
Kind: event emitted by LPF2Hub
Param | Type | Description |
---|---|---|
button | string |
|
state | number |
A number representing one of the button state consts. |
"distance" (port, distance)
Emits when a distance sensor is activated.
Kind: event emitted by LPF2Hub
Param | Type | Description |
---|---|---|
port | string |
|
distance | number |
Distance, in millimeters. |
"color" (port, color)
Emits when a color sensor is activated.
Kind: event emitted by LPF2Hub
Param | Type | Description |
---|---|---|
port | string |
|
color | number |
A number representing one of the LED color consts. |
"tilt" (port, x, y)
Emits when a tilt sensor is activated.
Kind: event emitted by LPF2Hub
Param | Type | Description |
---|---|---|
port | string |
If the event is fired from the Move Hub's in-built tilt sensor, the special port "TILT" is used. |
x | number |
|
y | number |
"rotate" (port, rotation)
Emits when a rotation sensor is activated.
Kind: event emitted by LPF2Hub
Param | Type |
---|---|
port | string |
rotation | number |
"attach" (port, type)
Emits when a motor or sensor is attached to the Hub.
Kind: event emitted by LPF2Hub
Param | Type | Description |
---|---|---|
port | string |
|
type | number |
A number representing one of the peripheral consts. |
"detach" (port)
Emits when an attached motor or sensor is detached from the Hub.
Kind: event emitted by LPF2Hub
Param | Type |
---|---|
port | string |
WeDo2Hub ⇐ Hub
Kind: global class
Extends: Hub
- WeDo2Hub ⇐
Hub
- new WeDo2Hub()
- .setLEDColor(color) ⇒
Promise
- .setLEDRGB(red, green, blue) ⇒
Promise
- .setMotorSpeed(port, speed) ⇒
Promise
- .connect() ⇒
Promise
- .disconnect() ⇒
Promise
- .subscribe(port, [mode]) ⇒
Promise
- .unsubscribe(port) ⇒
Promise
- .sleep(delay) ⇒
Promise
- .wait(commands) ⇒
Promise
- "button" (button, state)
- "distance" (port, distance)
- "color" (port, color)
- "tilt" (port, x, y)
- "rotate" (port, rotation)
- "attach" (port, type)
- "detach" (port)
new WeDo2Hub()
The WeDo2Hub is emitted if the discovered device is a WeDo 2.0 Smart Hub.
weDo2Hub.setLEDColor(color) ⇒ Promise
Set the color of the LED on the Hub via a color value.
Kind: instance method of WeDo2Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type | Description |
---|---|---|
color | number |
A number representing one of the LED color consts. |
weDo2Hub.setLEDRGB(red, green, blue) ⇒ Promise
Set the color of the LED on the Hub via RGB values.
Kind: instance method of WeDo2Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type |
---|---|
red | number |
green | number |
blue | number |
weDo2Hub.setMotorSpeed(port, speed) ⇒ Promise
Set the motor speed on a given port.
Kind: instance method of WeDo2Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type | Description |
---|---|---|
port | string |
|
speed | number |
For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
weDo2Hub.connect() ⇒ Promise
Connect to the Hub.
Kind: instance method of WeDo2Hub
Overrides: connect
Returns: Promise
- Resolved upon successful connect.
weDo2Hub.disconnect() ⇒ Promise
Disconnect the Hub.
Kind: instance method of WeDo2Hub
Returns: Promise
- Resolved upon successful disconnect.
weDo2Hub.subscribe(port, [mode]) ⇒ Promise
Subscribe to sensor notifications on a given port.
Kind: instance method of WeDo2Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type | Description |
---|---|---|
port | string |
|
[mode] | number |
The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
weDo2Hub.unsubscribe(port) ⇒ Promise
Unsubscribe to sensor notifications on a given port.
Kind: instance method of WeDo2Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type |
---|---|
port | string |
weDo2Hub.sleep(delay) ⇒ Promise
Sleep a given amount of time.
This is a helper method to make it easier to add delays into a chain of commands.
Kind: instance method of WeDo2Hub
Returns: Promise
- Resolved after the delay is finished.
Param | Type | Description |
---|---|---|
delay | number |
How long to sleep (in milliseconds). |
weDo2Hub.wait(commands) ⇒ Promise
Wait until a given list of concurrently running commands are complete.
This is a helper method to make it easier to wait for concurrent commands to complete.
Kind: instance method of WeDo2Hub
Returns: Promise
- Resolved after the commands are finished.
Param | Type | Description |
---|---|---|
commands | Array.<Promise.<any>> |
Array of executing commands. |
"button" (button, state)
Emits when a button is pressed.
Kind: event emitted by WeDo2Hub
Param | Type | Description |
---|---|---|
button | string |
|
state | number |
A number representing one of the button state consts. |
"distance" (port, distance)
Emits when a distance sensor is activated.
Kind: event emitted by WeDo2Hub
Param | Type | Description |
---|---|---|
port | string |
|
distance | number |
Distance, in millimeters. |
"color" (port, color)
Emits when a color sensor is activated.
Kind: event emitted by WeDo2Hub
Param | Type | Description |
---|---|---|
port | string |
|
color | number |
A number representing one of the LED color consts. |
"tilt" (port, x, y)
Emits when a tilt sensor is activated.
Kind: event emitted by WeDo2Hub
Param | Type |
---|---|
port | string |
x | number |
y | number |
"rotate" (port, rotation)
Emits when a rotation sensor is activated.
Kind: event emitted by WeDo2Hub
Param | Type |
---|---|
port | string |
rotation | number |
"attach" (port, type)
Emits when a motor or sensor is attached to the Hub.
Kind: event emitted by WeDo2Hub
Param | Type | Description |
---|---|---|
port | string |
|
type | number |
A number representing one of the peripheral consts. |
"detach" (port)
Emits when an attached motor or sensor is detached from the Hub.
Kind: event emitted by WeDo2Hub
Param | Type |
---|---|
port | string |
Hub ⇐ EventEmitter
Kind: global class
Extends: EventEmitter
- Hub ⇐
EventEmitter
- .connect() ⇒
Promise
- .disconnect() ⇒
Promise
- .subscribe(port, [mode]) ⇒
Promise
- .unsubscribe(port) ⇒
Promise
- .sleep(delay) ⇒
Promise
- .wait(commands) ⇒
Promise
- "attach" (port, type)
- "detach" (port)
- .connect() ⇒
hub.connect() ⇒ Promise
Connect to the Hub.
Kind: instance method of Hub
Returns: Promise
- Resolved upon successful connect.
hub.disconnect() ⇒ Promise
Disconnect the Hub.
Kind: instance method of Hub
Returns: Promise
- Resolved upon successful disconnect.
hub.subscribe(port, [mode]) ⇒ Promise
Subscribe to sensor notifications on a given port.
Kind: instance method of Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type | Description |
---|---|---|
port | string |
|
[mode] | number |
The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
hub.unsubscribe(port) ⇒ Promise
Unsubscribe to sensor notifications on a given port.
Kind: instance method of Hub
Returns: Promise
- Resolved upon successful issuance of command.
Param | Type |
---|---|
port | string |
hub.sleep(delay) ⇒ Promise
Sleep a given amount of time.
This is a helper method to make it easier to add delays into a chain of commands.
Kind: instance method of Hub
Returns: Promise
- Resolved after the delay is finished.
Param | Type | Description |
---|---|---|
delay | number |
How long to sleep (in milliseconds). |
hub.wait(commands) ⇒ Promise
Wait until a given list of concurrently running commands are complete.
This is a helper method to make it easier to wait for concurrent commands to complete.
Kind: instance method of Hub
Returns: Promise
- Resolved after the commands are finished.
Param | Type | Description |
---|---|---|
commands | Array.<Promise.<any>> |
Array of executing commands. |
"attach" (port, type)
Emits when a motor or sensor is attached to the Hub.
Kind: event emitted by Hub
Param | Type | Description |
---|---|---|
port | string |
|
type | number |
A number representing one of the peripheral consts. |
"detach" (port)
Emits when an attached motor or sensor is detached from the Hub.
Kind: event emitted by Hub
Param | Type |
---|---|
port | string |