Renamed module to node-poweredup

This commit is contained in:
Nathan Kunicki 2018-08-07 10:59:43 +01:00
parent 56ebc21db8
commit f838acfe8b
12 changed files with 162 additions and 141 deletions

View File

@ -1,20 +1,20 @@
# **node-lpf2** - A Node.js module to interface with LEGO Power Functions 2.0 components. # **node-poweredup** - A Node.js module to interface with LEGO Powered UP components.
### Installation ### Installation
Node.js v8.0+ required. Node.js v8.0+ required.
```javascript ```javascript
npm install node-lpf2 --save npm install node-poweredup --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). node-poweredup 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. Note: node-poweredup has been tested on macOS 10.13 and Debian/Raspbian on the Raspberry Pi 3 Model B.
### Compatibility ### 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. 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 | | Name | Type | WeDo 2.0 Smart Hub | Boost Move Hub | Powered Up Hub | Availability |
| ------------------------------- | ------------- | ------------------ | -------------- | -------------- | ------------ | | ------------------------------- | ------------- | ------------------ | -------------- | -------------- | ------------ |
@ -22,7 +22,7 @@ While most LPF2 components and Hubs are compatible with each other, there are ex
| WeDo 2.0 Motion Sensor | Sensor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a> | | WeDo 2.0 Motion Sensor | Sensor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a> |
| WeDo 2.0 Medium Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a><br /> <a href="https://brickset.com/sets/76112-1/">76112</a> | | WeDo 2.0 Medium Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a><br /> <a href="https://brickset.com/sets/76112-1/">76112</a> |
| Boost Color and Distance Sensor | Sensor | *Partial* | Yes | Yes | <a href="https://brickset.com/sets/17101-1/">17101</a> | | Boost Color and Distance Sensor | Sensor | *Partial* | Yes | Yes | <a href="https://brickset.com/sets/17101-1/">17101</a> |
| Boost Interactive Motor | Motor/Sensor | *Partial* | Yes | *Partial* | <a href="https://brickset.com/sets/17101-1/">17101</a> | | Boost Tacho Motor | Motor/Sensor | *Partial* | Yes | *Partial* | <a href="https://brickset.com/sets/17101-1/">17101</a> |
| Powered Up Train Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a> | | Powered Up Train Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a> |
| Powered Up LED Lights | Light | Unknown | Unknown | Unknown | <a href="https://brickset.com/sets/88005-1/">88005</a> | | Powered Up LED Lights | Light | Unknown | Unknown | Unknown | <a href="https://brickset.com/sets/88005-1/">88005</a> |
@ -40,8 +40,8 @@ While most LPF2 components and Hubs are compatible with each other, there are ex
### Usage ### Usage
```javascript ```javascript
const LPF2 = require("node-lpf2").LPF2; const PoweredUP = require("node-poweredup");
const lpf2 = new LPF2(); const pup = new PoweredUP.PoweredUP();
``` ```
Examples are available in the "examples" directory. Examples are available in the "examples" directory.

56
DOCS.md
View File

@ -1,7 +1,7 @@
## Classes ## Classes
<dl> <dl>
<dt><a href="#LPF2">LPF2</a><code>EventEmitter</code></dt> <dt><a href="#PoweredUP">PoweredUP</a><code>EventEmitter</code></dt>
<dd></dd> <dd></dd>
<dt><a href="#WeDo2SmartHub">WeDo2SmartHub</a><code>Hub</code></dt> <dt><a href="#WeDo2SmartHub">WeDo2SmartHub</a><code>Hub</code></dt>
<dd></dd> <dd></dd>
@ -13,54 +13,54 @@
<dd></dd> <dd></dd>
</dl> </dl>
<a name="LPF2"></a> <a name="PoweredUP"></a>
## LPF2<code>EventEmitter</code> ## PoweredUP<code>EventEmitter</code>
**Kind**: global class **Kind**: global class
**Extends**: <code>EventEmitter</code> **Extends**: <code>EventEmitter</code>
* [LPF2](#LPF2) ⇐ <code>EventEmitter</code> * [PoweredUP](#PoweredUP) ⇐ <code>EventEmitter</code>
* [.scan()](#LPF2+scan) * [.scan()](#PoweredUP+scan)
* [.stop()](#LPF2+stop) * [.stop()](#PoweredUP+stop)
* [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ <code>Hub</code> \| <code>null</code> * [.getConnectedHubByUUID(uuid)](#PoweredUP+getConnectedHubByUUID) ⇒ <code>Hub</code> \| <code>null</code>
* [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ <code>Array.&lt;Hub&gt;</code> * [.getConnectedHubs()](#PoweredUP+getConnectedHubs) ⇒ <code>Array.&lt;Hub&gt;</code>
* ["discover" (hub)](#LPF2+event_discover) * ["discover" (hub)](#PoweredUP+event_discover)
<a name="LPF2+scan"></a> <a name="PoweredUP+scan"></a>
### lpF2.scan() ### poweredUP.scan()
Begin scanning for LPF2 Hub devices. Begin scanning for Powered UP Hub devices.
**Kind**: instance method of [<code>LPF2</code>](#LPF2) **Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
<a name="LPF2+stop"></a> <a name="PoweredUP+stop"></a>
### lpF2.stop() ### poweredUP.stop()
Stop scanning for LPF2 Hub devices. Stop scanning for Powered UP Hub devices.
**Kind**: instance method of [<code>LPF2</code>](#LPF2) **Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
<a name="LPF2+getConnectedHubByUUID"></a> <a name="PoweredUP+getConnectedHubByUUID"></a>
### lpF2.getConnectedHubByUUID(uuid) ⇒ <code>Hub</code> \| <code>null</code> ### poweredUP.getConnectedHubByUUID(uuid) ⇒ <code>Hub</code> \| <code>null</code>
Retrieve a LPF2 Hub by UUID. Retrieve a Powered UP Hub by UUID.
**Kind**: instance method of [<code>LPF2</code>](#LPF2) **Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
| Param | Type | | Param | Type |
| --- | --- | | --- | --- |
| uuid | <code>string</code> | | uuid | <code>string</code> |
<a name="LPF2+getConnectedHubs"></a> <a name="PoweredUP+getConnectedHubs"></a>
### lpF2.getConnectedHubs() ⇒ <code>Array.&lt;Hub&gt;</code> ### poweredUP.getConnectedHubs() ⇒ <code>Array.&lt;Hub&gt;</code>
Retrieve a list of LPF2 Hubs. Retrieve a list of Powered UP Hubs.
**Kind**: instance method of [<code>LPF2</code>](#LPF2) **Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
<a name="LPF2+event_discover"></a> <a name="PoweredUP+event_discover"></a>
### "discover" (hub) ### "discover" (hub)
Emits when a LPF2 Hub device is found. Emits when a Powered UP Hub device is found.
**Kind**: event emitted by [<code>LPF2</code>](#LPF2) **Kind**: event emitted by [<code>PoweredUP</code>](#PoweredUP)
| Param | Type | | Param | Type |
| --- | --- | | --- | --- |

View File

@ -1,20 +1,20 @@
# **node-lpf2** - A Node.js module to interface with LEGO Power Functions 2.0 components. # **node-poweredup** - A Node.js module to interface with LEGO Powered UP components.
### Installation ### Installation
Node.js v8.0+ required. Node.js v8.0+ required.
```javascript ```javascript
npm install node-lpf2 --save npm install node-poweredup --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). node-poweredup 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. Note: node-poweredup has been tested on macOS 10.13 and Debian/Raspbian on the Raspberry Pi 3 Model B.
### Compatibility ### 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. 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 | | Name | Type | WeDo 2.0 Smart Hub | Boost Move Hub | Powered Up Hub | Availability |
| ------------------------------- | ------------- | ------------------ | -------------- | -------------- | ------------ | | ------------------------------- | ------------- | ------------------ | -------------- | -------------- | ------------ |
@ -22,7 +22,7 @@ While most LPF2 components and Hubs are compatible with each other, there are ex
| WeDo 2.0 Motion Sensor | Sensor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a> | | WeDo 2.0 Motion Sensor | Sensor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a> |
| WeDo 2.0 Medium Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a><br /> <a href="https://brickset.com/sets/76112-1/">76112</a> | | WeDo 2.0 Medium Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a><br /> <a href="https://brickset.com/sets/76112-1/">76112</a> |
| Boost Color and Distance Sensor | Sensor | *Partial* | Yes | Yes | <a href="https://brickset.com/sets/17101-1/">17101</a> | | Boost Color and Distance Sensor | Sensor | *Partial* | Yes | Yes | <a href="https://brickset.com/sets/17101-1/">17101</a> |
| Boost Interactive Motor | Motor/Sensor | *Partial* | Yes | *Partial* | <a href="https://brickset.com/sets/17101-1/">17101</a> | | Boost Tacho Motor | Motor/Sensor | *Partial* | Yes | *Partial* | <a href="https://brickset.com/sets/17101-1/">17101</a> |
| Powered Up Train Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a> | | Powered Up Train Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a> |
| Powered Up LED Lights | Light | Unknown | Unknown | Unknown | <a href="https://brickset.com/sets/88005-1/">88005</a> | | Powered Up LED Lights | Light | Unknown | Unknown | Unknown | <a href="https://brickset.com/sets/88005-1/">88005</a> |
@ -40,8 +40,8 @@ While most LPF2 components and Hubs are compatible with each other, there are ex
### Usage ### Usage
```javascript ```javascript
const LPF2 = require("node-lpf2").LPF2; const PoweredUP = require("node-poweredup");
const lpf2 = new LPF2(); const pup = new PoweredUP.PoweredUP();
``` ```
Examples are available in the "examples" directory. Examples are available in the "examples" directory.
@ -53,7 +53,7 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R
## Classes ## Classes
<dl> <dl>
<dt><a href="#LPF2">LPF2</a><code>EventEmitter</code></dt> <dt><a href="#PoweredUP">PoweredUP</a><code>EventEmitter</code></dt>
<dd></dd> <dd></dd>
<dt><a href="#WeDo2SmartHub">WeDo2SmartHub</a><code>Hub</code></dt> <dt><a href="#WeDo2SmartHub">WeDo2SmartHub</a><code>Hub</code></dt>
<dd></dd> <dd></dd>
@ -65,54 +65,54 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R
<dd></dd> <dd></dd>
</dl> </dl>
<a name="LPF2"></a> <a name="PoweredUP"></a>
## LPF2<code>EventEmitter</code> ## PoweredUP<code>EventEmitter</code>
**Kind**: global class **Kind**: global class
**Extends**: <code>EventEmitter</code> **Extends**: <code>EventEmitter</code>
* [LPF2](#LPF2) ⇐ <code>EventEmitter</code> * [PoweredUP](#PoweredUP) ⇐ <code>EventEmitter</code>
* [.scan()](#LPF2+scan) * [.scan()](#PoweredUP+scan)
* [.stop()](#LPF2+stop) * [.stop()](#PoweredUP+stop)
* [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ <code>Hub</code> \| <code>null</code> * [.getConnectedHubByUUID(uuid)](#PoweredUP+getConnectedHubByUUID) ⇒ <code>Hub</code> \| <code>null</code>
* [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ <code>Array.&lt;Hub&gt;</code> * [.getConnectedHubs()](#PoweredUP+getConnectedHubs) ⇒ <code>Array.&lt;Hub&gt;</code>
* ["discover" (hub)](#LPF2+event_discover) * ["discover" (hub)](#PoweredUP+event_discover)
<a name="LPF2+scan"></a> <a name="PoweredUP+scan"></a>
### lpF2.scan() ### poweredUP.scan()
Begin scanning for LPF2 Hub devices. Begin scanning for Powered UP Hub devices.
**Kind**: instance method of [<code>LPF2</code>](#LPF2) **Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
<a name="LPF2+stop"></a> <a name="PoweredUP+stop"></a>
### lpF2.stop() ### poweredUP.stop()
Stop scanning for LPF2 Hub devices. Stop scanning for Powered UP Hub devices.
**Kind**: instance method of [<code>LPF2</code>](#LPF2) **Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
<a name="LPF2+getConnectedHubByUUID"></a> <a name="PoweredUP+getConnectedHubByUUID"></a>
### lpF2.getConnectedHubByUUID(uuid) ⇒ <code>Hub</code> \| <code>null</code> ### poweredUP.getConnectedHubByUUID(uuid) ⇒ <code>Hub</code> \| <code>null</code>
Retrieve a LPF2 Hub by UUID. Retrieve a Powered UP Hub by UUID.
**Kind**: instance method of [<code>LPF2</code>](#LPF2) **Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
| Param | Type | | Param | Type |
| --- | --- | | --- | --- |
| uuid | <code>string</code> | | uuid | <code>string</code> |
<a name="LPF2+getConnectedHubs"></a> <a name="PoweredUP+getConnectedHubs"></a>
### lpF2.getConnectedHubs() ⇒ <code>Array.&lt;Hub&gt;</code> ### poweredUP.getConnectedHubs() ⇒ <code>Array.&lt;Hub&gt;</code>
Retrieve a list of LPF2 Hubs. Retrieve a list of Powered UP Hubs.
**Kind**: instance method of [<code>LPF2</code>](#LPF2) **Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
<a name="LPF2+event_discover"></a> <a name="PoweredUP+event_discover"></a>
### "discover" (hub) ### "discover" (hub)
Emits when a LPF2 Hub device is found. Emits when a Powered UP Hub device is found.
**Kind**: event emitted by [<code>LPF2</code>](#LPF2) **Kind**: event emitted by [<code>PoweredUP</code>](#PoweredUP)
| Param | Type | | Param | Type |
| --- | --- | | --- | --- |

View File

@ -1,10 +1,10 @@
// This example won't work by itself, this is the code used to automate this train layout: https://www.youtube.com/watch?v=Tyzo_hHFiUc // This example won't work by itself, this is the code used to automate this train layout: https://www.youtube.com/watch?v=Tyzo_hHFiUc
// It is included here as an example. // It is included here as an example.
const LPF2 = require(".").LPF2; const PoweredUP = require("..").PoweredUP;
const lpf2 = new LPF2(); const pup = new PoweredUP();
lpf2.scan(); // Start scanning for hubs pup.scan(); // Start scanning for hubs
console.log("Looking for Hubs..."); console.log("Looking for Hubs...");
@ -24,7 +24,7 @@ let state = 0;
let moving = true; let moving = true;
lpf2.on("discover", async (hub) => { // Wait to discover hubs pup.on("discover", async (hub) => { // Wait to discover hubs
console.log(hub.uuid); console.log(hub.uuid);

View File

@ -4,10 +4,10 @@
* *
*/ */
const LPF2 = require(".."); const PoweredUP = require("..");
const lpf2 = new LPF2.LPF2(); const pup = new PoweredUP.PoweredUP();
lpf2.scan(); // Start scanning pup.scan(); // Start scanning
console.log("Looking for Batmobile and Remote..."); console.log("Looking for Batmobile and Remote...");
@ -16,40 +16,40 @@ let remote = null;
let lastButton = null; let lastButton = null;
lpf2.on("discover", async (hub) => { // Wait to Batmobile and Remote pup.on("discover", async (hub) => { // Wait to Batmobile and Remote
if (hub instanceof LPF2.PUPHub) { if (hub instanceof PoweredUP.PUPHub) {
batmobile = hub; batmobile = hub;
await batmobile.connect(); await batmobile.connect();
console.log("Connected to Batmobile!"); console.log("Connected to Batmobile!");
} else if (hub instanceof LPF2.PUPRemote) { } else if (hub instanceof PoweredUP.PUPRemote) {
remote = hub; remote = hub;
remote.on("button", async (button, state) => { remote.on("button", async (button, state) => {
if (batmobile) { if (batmobile) {
switch (state) { switch (state) {
case LPF2.Consts.ButtonStates.UP: // If up is pressed, move the wheels forward case PoweredUP.Consts.ButtonStates.UP: // If up is pressed, move the wheels forward
{ {
lastButton = state; lastButton = state;
batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", button === "LEFT" ? -100 : 100); batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", button === "LEFT" ? -100 : 100);
break; break;
} }
case LPF2.Consts.ButtonStates.DOWN: // If down is pressed, move the wheels backwards case PoweredUP.Consts.ButtonStates.DOWN: // If down is pressed, move the wheels backwards
{ {
lastButton = state; lastButton = state;
batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", button === "LEFT" ? 100 : -100); batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", button === "LEFT" ? 100 : -100);
break; break;
} }
case LPF2.Consts.ButtonStates.RELEASED: // Stop the wheels when the button is released case PoweredUP.Consts.ButtonStates.RELEASED: // Stop the wheels when the button is released
{ {
if (lastButton === LPF2.Consts.ButtonStates.UP || lastButton === LPF2.Consts.ButtonStates.DOWN) { if (lastButton === PoweredUP.Consts.ButtonStates.UP || lastButton === PoweredUP.Consts.ButtonStates.DOWN) {
batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", 0); batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", 0);
} }
break; break;
} }
case LPF2.Consts.ButtonStates.STOP: // When left red button is pressed, do a retreat. When right red button is pressed, scan the area. case PoweredUP.Consts.ButtonStates.STOP: // When left red button is pressed, do a retreat. When right red button is pressed, scan the area.
{ {
lastButton = state; lastButton = state;
if (button === "LEFT") { if (button === "LEFT") {
@ -66,7 +66,7 @@ lpf2.on("discover", async (hub) => { // Wait to Batmobile and Remote
} }
break; break;
} }
case LPF2.Consts.ButtonStates.PRESSED: // Do a wheelie when the green button is pressed case PoweredUP.Consts.ButtonStates.PRESSED: // Do a wheelie when the green button is pressed
{ {
lastButton = state; lastButton = state;
if (button === "GREEN") { if (button === "GREEN") {
@ -85,8 +85,8 @@ lpf2.on("discover", async (hub) => { // Wait to Batmobile and Remote
} }
if (batmobile && remote) { if (batmobile && remote) {
batmobile.setLEDColor(LPF2.Consts.Colors.WHITE); batmobile.setLEDColor(PoweredUP.Consts.Colors.WHITE);
remote.setLEDColor(LPF2.Consts.Colors.RED); remote.setLEDColor(PoweredUP.Consts.Colors.RED);
console.log("You're now ready to go!"); console.log("You're now ready to go!");
} }

View File

@ -4,14 +4,14 @@
* *
*/ */
const LPF2 = require("..").LPF2; const PoweredUP = require("..").PoweredUP;
const lpf2 = new LPF2(); const pup = new PoweredUP();
lpf2.scan(); // Start scanning for hubs pup.scan(); // Start scanning for hubs
console.log("Looking for Hubs..."); console.log("Looking for Hubs...");
lpf2.on("discover", async (hub) => { // Wait to discover hubs pup.on("discover", async (hub) => { // Wait to discover hubs
await hub.connect(); // Connect to hub await hub.connect(); // Connect to hub
console.log(`Connected to ${hub.name}!`); console.log(`Connected to ${hub.name}!`);
@ -25,7 +25,7 @@ lpf2.on("discover", async (hub) => { // Wait to discover hubs
let color = 1; let color = 1;
setInterval(() => { setInterval(() => {
const hubs = lpf2.getConnectedHubs(); // Get an array of all connected hubs const hubs = pup.getConnectedHubs(); // Get an array of all connected hubs
hubs.forEach((hub) => { hubs.forEach((hub) => {
hub.setLEDColor(color); // Set the color hub.setLEDColor(color); // Set the color
}) })

View File

@ -1,6 +1,6 @@
const LPF2 = require("..").LPF2; const LPF2 = require("..");
const lpf2 = new LPF2(); const lpf2 = new LPF2.LPF2();
lpf2.scan(); // Start scanning for hubs lpf2.scan(); // Start scanning for hubs
console.log("Looking for Hubs..."); console.log("Looking for Hubs...");
@ -8,11 +8,32 @@ console.log("Looking for Hubs...");
lpf2.on("discover", async (hub) => { // Wait to discover hubs lpf2.on("discover", async (hub) => { // Wait to discover hubs
await hub.connect(); // Connect to hub await hub.connect(); // Connect to hub
console.log("Connected to Hub!"); console.log(`Connected to ${hub.name}!`);
setTimeout(() => { await hub.sleep(4000);
hub.setMotorSpeed("A", 50, 500);
//hub.setMotorSpeed("B", 50); hub.setMotorAngle("A", 90);
}, 3000); console.log("Done");
// while (true) {
// await hub.wait([
// hub.setMotorSpeed("A", 10, 1000),
// hub.setMotorSpeed("B", 10, 1000)
// ]);
// await hub.wait([
// hub.setMotorSpeed("A", -10, 2000),
// hub.setMotorSpeed("B", -10, 2000)
// ]);
// await hub.wait([
// hub.setMotorSpeed("A", 10, 1000),
// hub.setMotorSpeed("B", 10, 1000)
// ]);
// await hub.sleep(4000);
// }
}); });

View File

@ -1,18 +1,18 @@
const LPF2 = require(".."); const PoweredUP = require("..");
const lpf2 = new LPF2.LPF2(); const pup = new PoweredUP.PoweredUP();
lpf2.scan(); // Start scanning for trains pup.scan(); // Start scanning for trains
// Change these to make the train behave as you want // Change these to make the train behave as you want
const FORWARD_DIRECTION_COLOR = LPF2.Consts.Colors.YELLOW; const FORWARD_DIRECTION_COLOR = PoweredUP.Consts.Colors.YELLOW;
const BACKWARDS_DIRECTION_COLOR = LPF2.Consts.Colors.RED; const BACKWARDS_DIRECTION_COLOR = PoweredUP.Consts.Colors.RED;
const TRAIN_SPEED = 40; const TRAIN_SPEED = 40;
const STOP_DELAY = 2000; const STOP_DELAY = 2000;
const TRAIN_MOTOR_PORT = "A"; const TRAIN_MOTOR_PORT = "A";
console.log("Looking for trains..."); console.log("Looking for trains...");
lpf2.on("discover", async (hub) => { // Wait to discover a train pup.on("discover", async (hub) => { // Wait to discover a train
let moving = true; let moving = true;

View File

@ -4,53 +4,53 @@
* *
*/ */
const LPF2 = require(".."); const PoweredUP = require("..");
const lpf2 = new LPF2.LPF2(); const pup = new PoweredUP.PoweredUP();
lpf2.scan(); // Start scanning pup.scan(); // Start scanning
console.log("Looking for Vernie and Remote..."); console.log("Looking for Vernie and Remote...");
let vernie = null; let vernie = null;
let remote = null; let remote = null;
lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote pup.on("discover", async (hub) => { // Wait to discover Vernie and Remote
if (hub instanceof LPF2.BoostMoveHub) { if (hub instanceof PoweredUP.BoostMoveHub) {
vernie = hub; vernie = hub;
await vernie.connect(); await vernie.connect();
console.log("Connected to Vernie!"); console.log("Connected to Vernie!");
} else if (hub instanceof LPF2.PUPRemote) { } else if (hub instanceof PoweredUP.PUPRemote) {
remote = hub; remote = hub;
remote.on("button", async (button, state) => { remote.on("button", async (button, state) => {
if (vernie) { if (vernie) {
switch (state) { switch (state) {
case LPF2.Consts.ButtonStates.UP: // If up is pressed, move the track forward case PoweredUP.Consts.ButtonStates.UP: // If up is pressed, move the track forward
{ {
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 50); vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 50);
break; break;
} }
case LPF2.Consts.ButtonStates.DOWN: // If down is pressed, move the track backwards case PoweredUP.Consts.ButtonStates.DOWN: // If down is pressed, move the track backwards
{ {
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", -50); vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", -50);
break; break;
} }
case LPF2.Consts.ButtonStates.RELEASED: // Stop the track when the button is released case PoweredUP.Consts.ButtonStates.RELEASED: // Stop the track when the button is released
{ {
if (button !== "GREEN") { if (button !== "GREEN") {
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 0); vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 0);
} }
break; break;
} }
case LPF2.Consts.ButtonStates.STOP: // Move the head left or right when a red button is pressed case PoweredUP.Consts.ButtonStates.STOP: // Move the head left or right when a red button is pressed
{ {
await vernie.setMotorAngle("D", 35, button === "LEFT" ? -20 : 20); await vernie.setMotorAngle("D", 35, button === "LEFT" ? -20 : 20);
break; break;
} }
case LPF2.Consts.ButtonStates.PRESSED: // Fire when the green button is pressed case PoweredUP.Consts.ButtonStates.PRESSED: // Fire when the green button is pressed
{ {
if (button === "GREEN") { if (button === "GREEN") {
await vernie.setMotorAngle("D", 80, 20); await vernie.setMotorAngle("D", 80, 20);
@ -67,8 +67,8 @@ lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote
} }
if (vernie && remote) { if (vernie && remote) {
vernie.setLEDColor(LPF2.Consts.Colors.BLUE); vernie.setLEDColor(PoweredUP.Consts.Colors.BLUE);
remote.setLEDColor(LPF2.Consts.Colors.BLUE); remote.setLEDColor(PoweredUP.Consts.Colors.BLUE);
console.log("You're now ready to go!"); console.log("You're now ready to go!");
} }

2
package-lock.json generated
View File

@ -1,5 +1,5 @@
{ {
"name": "node-lpf2", "name": "node-poweredup",
"version": "1.0.0", "version": "1.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,

View File

@ -1,12 +1,12 @@
{ {
"name": "node-lpf2", "name": "node-poweredup",
"version": "1.0.0", "version": "1.0.0",
"description": "A Node.js module to interface with Lego Power Functions 2.0 components.", "description": "A Node.js module to interface with LEGO Powered UP components.",
"main": "dist/lpf2.js", "main": "dist/poweredup.js",
"types": "dist/lpf2.d.ts", "types": "dist/poweredup.d.ts",
"scripts": { "scripts": {
"build": "tslint -c tslint.json \"*.ts\" && tsc", "build": "tslint -c tslint.json \"*.ts\" && tsc",
"docs": "jsdoc2md dist/lpf2.js dist/lpf2hub.js dist/wedo2smarthub.js dist/boostmovehub.js dist/puphub.js dist/pupremote.js dist/hub.js dist/consts.js > DOCS.md", "docs": "jsdoc2md dist/poweredup.js dist/lpf2hub.js dist/wedo2smarthub.js dist/boostmovehub.js dist/puphub.js dist/pupremote.js dist/hub.js dist/consts.js > DOCS.md",
"readme": "cat ABOUT.md DOCS.md > README.md", "readme": "cat ABOUT.md DOCS.md > README.md",
"all": "npm run build && npm run docs && npm run readme", "all": "npm run build && npm run docs && npm run readme",
"prepublishOnly": "tsc" "prepublishOnly": "tsc"

View File

@ -11,7 +11,7 @@ import * as Consts from "./consts";
import { EventEmitter} from "events"; import { EventEmitter} from "events";
import Debug = require("debug"); import Debug = require("debug");
const debug = Debug("lpf2"); const debug = Debug("PoweredUP");
import noble = require("noble"); import noble = require("noble");
let ready = false; let ready = false;
@ -30,10 +30,10 @@ noble.on("stateChange", (state: string) => {
}); });
/** /**
* @class LPF2 * @class PoweredUP
* @extends EventEmitter * @extends EventEmitter
*/ */
export class LPF2 extends EventEmitter { export class PoweredUP extends EventEmitter {
public autoSubscribe: boolean = true; public autoSubscribe: boolean = true;
@ -48,8 +48,8 @@ export class LPF2 extends EventEmitter {
/** /**
* Begin scanning for LPF2 Hub devices. * Begin scanning for Powered UP Hub devices.
* @method LPF2#scan * @method PoweredUP#scan
*/ */
public scan () { public scan () {
wantScan = true; wantScan = true;
@ -90,8 +90,8 @@ export class LPF2 extends EventEmitter {
debug(`Hub ${hub.uuid} discovered`); debug(`Hub ${hub.uuid} discovered`);
/** /**
* Emits when a LPF2 Hub device is found. * Emits when a Powered UP Hub device is found.
* @event LPF2#discover * @event PoweredUP#discover
* @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote} hub * @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote} hub
*/ */
this.emit("discover", hub); this.emit("discover", hub);
@ -106,8 +106,8 @@ export class LPF2 extends EventEmitter {
/** /**
* Stop scanning for LPF2 Hub devices. * Stop scanning for Powered UP Hub devices.
* @method LPF2#stop * @method PoweredUP#stop
*/ */
public stop () { public stop () {
wantScan = false; wantScan = false;
@ -116,8 +116,8 @@ export class LPF2 extends EventEmitter {
/** /**
* Retrieve a LPF2 Hub by UUID. * Retrieve a Powered UP Hub by UUID.
* @method LPF2#getConnectedHubByUUID * @method PoweredUP#getConnectedHubByUUID
* @param {string} uuid * @param {string} uuid
* @returns {Hub | null} * @returns {Hub | null}
*/ */
@ -127,8 +127,8 @@ export class LPF2 extends EventEmitter {
/** /**
* Retrieve a list of LPF2 Hubs. * Retrieve a list of Powered UP Hubs.
* @method LPF2#getConnectedHubs * @method PoweredUP#getConnectedHubs
* @returns {Hub[]} * @returns {Hub[]}
*/ */
public getConnectedHubs () { public getConnectedHubs () {
@ -140,5 +140,5 @@ export class LPF2 extends EventEmitter {
} }
export default LPF2; export default PoweredUP;
export { Hub, WeDo2SmartHub, BoostMoveHub, PUPHub, PUPRemote, Consts }; export { Hub, WeDo2SmartHub, BoostMoveHub, PUPHub, PUPRemote, Consts };