# **node-lpf2** - A Node.js module to interface with LEGO Power Functions 2.0 components. ### Installation Node.js v8.0+ required. ```javascript 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](https://github.com/noble/noble#prerequisites). 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. | | Type | WeDo 2.0 Smart Hub | Boost Move Hub | Powered Up Hub | | ------------------------------- | ------ | ------------------ | -------------- | -------------- | | WeDo 2.0 Tilt Sensor | Sensor | Yes | Yes | Yes | | WeDo 2.0 Distance Sensor | Sensor | Yes | Yes | Yes | | WeDo 2.0 Medium Motor | Motor | Yes | Yes | Yes | | Boost Color and Distance Sensor | Sensor | *Some (1)* | Yes | Yes | | Boost Interactive Medium Motor | Motor | *Some (2)* | Yes | Yes | | Powered Up Train Motor | Motor | Yes | Yes | Yes | (1) Only color mode is supported on the WeDo 2.0 Smart Hub at this point. (2) Only basic motor functionality is supported on the WeDo 2.0 Smart Hub at this point. No interactive functionality. In short, it functions like the WeDo 2.0 Medium Motor. ### Usage ```javascript const LPF2 = require("node-lpf2").LPF2; const lpf2 = new LPF2(); ``` Examples are available in the "examples" directory. ## Classes
EventEmitter
Hub
Hub
EventEmitter
EventEmitter
**Kind**: global class
**Extends**: EventEmitter
* [LPF2](#LPF2) ⇐ EventEmitter
* [.scan()](#LPF2+scan)
* ["discover" (hub)](#LPF2+event_discover)
### lpF2.scan()
Begin scanning for LPF2 Hub devices.
**Kind**: instance method of [LPF2
](#LPF2)
### "discover" (hub)
Emits when a LPF2 Hub device is found.
**Kind**: event emitted by [LPF2
](#LPF2)
| Param | Type |
| --- | --- |
| hub | [Hub
](#Hub) |
## BoostHub ⇐ [Hub
](#Hub)
**Kind**: global class
**Extends**: [Hub
](#Hub)
* [BoostHub](#BoostHub) ⇐ [Hub
](#Hub)
* [.setLEDColor(color)](#BoostHub+setLEDColor)
* [.setMotorSpeed(port, speed, [time])](#BoostHub+setMotorSpeed)
* [.setMotorAngle(port, angle, [speed])](#BoostHub+setMotorAngle)
* [.connect([callback])](#Hub+connect)
* [.subscribe(port, [mode])](#Hub+subscribe)
* [.unsubscribe(port)](#Hub+unsubscribe)
* ["button" (state)](#BoostHub+event_button)
* ["distance" (port, distance)](#BoostHub+event_distance)
* ["color" (port, color)](#BoostHub+event_color)
* ["tilt" (port, x, y)](#BoostHub+event_tilt)
* ["rotate" (port, rotation)](#BoostHub+event_rotate)
### boostHub.setLEDColor(color)
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [BoostHub
](#BoostHub)
| Param | Type | Description |
| --- | --- | --- |
| color | number
| A number representing one of the LED color consts. |
### boostHub.setMotorSpeed(port, speed, [time])
Set the motor speed on a given port.
**Kind**: instance method of [BoostHub
](#BoostHub)
| 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. |
### boostHub.setMotorAngle(port, angle, [speed])
Rotate a motor by a given angle.
**Kind**: instance method of [BoostHub
](#BoostHub)
| 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. |
### boostHub.connect([callback])
Connect to the Hub.
**Kind**: instance method of [BoostHub
](#BoostHub)
**Overrides**: [connect
](#Hub+connect)
| Param | Type |
| --- | --- |
| [callback] | function
|
### boostHub.subscribe(port, [mode])
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [BoostHub
](#BoostHub)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| port | string
| | |
| [mode] | number
\| boolean
| false
| The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
### boostHub.unsubscribe(port)
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [BoostHub
](#BoostHub)
| Param | Type |
| --- | --- |
| port | string
|
### "button" (state)
Emits when a button is pressed.
**Kind**: event emitted by [BoostHub
](#BoostHub)
| Param | Type | Description |
| --- | --- | --- |
| 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 [BoostHub
](#BoostHub)
| Param | Type | Description |
| --- | --- | --- |
| port | string
| |
| distance | number
| Distance, in millimeters. |
### "color" (port, color)
Emits when a color sensor is activated.
**Kind**: event emitted by [BoostHub
](#BoostHub)
| 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 [BoostHub
](#BoostHub)
| Param | Type |
| --- | --- |
| port | string
|
| x | number
|
| y | number
|
### "rotate" (port, rotation)
Emits when a rotation sensor is activated.
**Kind**: event emitted by [BoostHub
](#BoostHub)
| Param | Type |
| --- | --- |
| port | string
|
| rotation | number
|
## WeDo2Hub ⇐ [Hub
](#Hub)
**Kind**: global class
**Extends**: [Hub
](#Hub)
* [WeDo2Hub](#WeDo2Hub) ⇐ [Hub
](#Hub)
* [.setLEDColor(color)](#WeDo2Hub+setLEDColor)
* [.setLEDRGB(red, green, blue)](#WeDo2Hub+setLEDRGB)
* [.setMotorSpeed(port, speed)](#WeDo2Hub+setMotorSpeed)
* [.connect([callback])](#Hub+connect)
* [.subscribe(port, [mode])](#Hub+subscribe)
* [.unsubscribe(port)](#Hub+unsubscribe)
* ["button" (state)](#WeDo2Hub+event_button)
* ["distance" (port, distance)](#WeDo2Hub+event_distance)
* ["color" (port, color)](#WeDo2Hub+event_color)
* ["tilt" (port, x, y)](#WeDo2Hub+event_tilt)
* ["rotate" (port, rotation)](#WeDo2Hub+event_rotate)
### weDo2Hub.setLEDColor(color)
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [WeDo2Hub
](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| color | number
| A number representing one of the LED color consts. |
### weDo2Hub.setLEDRGB(red, green, blue)
Set the color of the LED on the Hub via RGB values.
**Kind**: instance method of [WeDo2Hub
](#WeDo2Hub)
| Param | Type |
| --- | --- |
| red | number
|
| green | number
|
| blue | number
|
### weDo2Hub.setMotorSpeed(port, speed)
Set the motor speed on a given port.
**Kind**: instance method of [WeDo2Hub
](#WeDo2Hub)
| 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([callback])
Connect to the Hub.
**Kind**: instance method of [WeDo2Hub
](#WeDo2Hub)
**Overrides**: [connect
](#Hub+connect)
| Param | Type |
| --- | --- |
| [callback] | function
|
### weDo2Hub.subscribe(port, [mode])
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [WeDo2Hub
](#WeDo2Hub)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| port | string
| | |
| [mode] | number
\| boolean
| false
| The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
### weDo2Hub.unsubscribe(port)
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [WeDo2Hub
](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | string
|
### "button" (state)
Emits when a button is pressed.
**Kind**: event emitted by [WeDo2Hub
](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| 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
](#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
](#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
](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | string
|
| x | number
|
| y | number
|
### "rotate" (port, rotation)
Emits when a rotation sensor is activated.
**Kind**: event emitted by [WeDo2Hub
](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | string
|
| rotation | number
|
## Hub ⇐ EventEmitter
**Kind**: global class
**Extends**: EventEmitter
* [Hub](#Hub) ⇐ EventEmitter
* [.connect([callback])](#Hub+connect)
* [.subscribe(port, [mode])](#Hub+subscribe)
* [.unsubscribe(port)](#Hub+unsubscribe)
### hub.connect([callback])
Connect to the Hub.
**Kind**: instance method of [Hub
](#Hub)
| Param | Type |
| --- | --- |
| [callback] | function
|
### hub.subscribe(port, [mode])
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [Hub
](#Hub)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| port | string
| | |
| [mode] | number
\| boolean
| false
| The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
### hub.unsubscribe(port)
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [Hub
](#Hub)
| Param | Type |
| --- | --- |
| port | string
|