Renamed module to node-poweredup
This commit is contained in:
parent
56ebc21db8
commit
f838acfe8b
16
ABOUT.md
16
ABOUT.md
@ -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
|
||||
|
||||
Node.js v8.0+ required.
|
||||
|
||||
```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
|
||||
|
||||
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 |
|
||||
| ------------------------------- | ------------- | ------------------ | -------------- | -------------- | ------------ |
|
||||
@ -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 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 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 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
|
||||
|
||||
```javascript
|
||||
const LPF2 = require("node-lpf2").LPF2;
|
||||
const lpf2 = new LPF2();
|
||||
const PoweredUP = require("node-poweredup");
|
||||
const pup = new PoweredUP.PoweredUP();
|
||||
```
|
||||
|
||||
Examples are available in the "examples" directory.
|
||||
|
56
DOCS.md
56
DOCS.md
@ -1,7 +1,7 @@
|
||||
## Classes
|
||||
|
||||
<dl>
|
||||
<dt><a href="#LPF2">LPF2</a> ⇐ <code>EventEmitter</code></dt>
|
||||
<dt><a href="#PoweredUP">PoweredUP</a> ⇐ <code>EventEmitter</code></dt>
|
||||
<dd></dd>
|
||||
<dt><a href="#WeDo2SmartHub">WeDo2SmartHub</a> ⇐ <code>Hub</code></dt>
|
||||
<dd></dd>
|
||||
@ -13,54 +13,54 @@
|
||||
<dd></dd>
|
||||
</dl>
|
||||
|
||||
<a name="LPF2"></a>
|
||||
<a name="PoweredUP"></a>
|
||||
|
||||
## LPF2 ⇐ <code>EventEmitter</code>
|
||||
## PoweredUP ⇐ <code>EventEmitter</code>
|
||||
**Kind**: global class
|
||||
**Extends**: <code>EventEmitter</code>
|
||||
|
||||
* [LPF2](#LPF2) ⇐ <code>EventEmitter</code>
|
||||
* [.scan()](#LPF2+scan)
|
||||
* [.stop()](#LPF2+stop)
|
||||
* [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ <code>Hub</code> \| <code>null</code>
|
||||
* [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ <code>Array.<Hub></code>
|
||||
* ["discover" (hub)](#LPF2+event_discover)
|
||||
* [PoweredUP](#PoweredUP) ⇐ <code>EventEmitter</code>
|
||||
* [.scan()](#PoweredUP+scan)
|
||||
* [.stop()](#PoweredUP+stop)
|
||||
* [.getConnectedHubByUUID(uuid)](#PoweredUP+getConnectedHubByUUID) ⇒ <code>Hub</code> \| <code>null</code>
|
||||
* [.getConnectedHubs()](#PoweredUP+getConnectedHubs) ⇒ <code>Array.<Hub></code>
|
||||
* ["discover" (hub)](#PoweredUP+event_discover)
|
||||
|
||||
<a name="LPF2+scan"></a>
|
||||
<a name="PoweredUP+scan"></a>
|
||||
|
||||
### lpF2.scan()
|
||||
Begin scanning for LPF2 Hub devices.
|
||||
### poweredUP.scan()
|
||||
Begin scanning for Powered UP Hub devices.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+stop"></a>
|
||||
**Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
|
||||
<a name="PoweredUP+stop"></a>
|
||||
|
||||
### lpF2.stop()
|
||||
Stop scanning for LPF2 Hub devices.
|
||||
### poweredUP.stop()
|
||||
Stop scanning for Powered UP Hub devices.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+getConnectedHubByUUID"></a>
|
||||
**Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
|
||||
<a name="PoweredUP+getConnectedHubByUUID"></a>
|
||||
|
||||
### lpF2.getConnectedHubByUUID(uuid) ⇒ <code>Hub</code> \| <code>null</code>
|
||||
Retrieve a LPF2 Hub by UUID.
|
||||
### poweredUP.getConnectedHubByUUID(uuid) ⇒ <code>Hub</code> \| <code>null</code>
|
||||
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 |
|
||||
| --- | --- |
|
||||
| uuid | <code>string</code> |
|
||||
|
||||
<a name="LPF2+getConnectedHubs"></a>
|
||||
<a name="PoweredUP+getConnectedHubs"></a>
|
||||
|
||||
### lpF2.getConnectedHubs() ⇒ <code>Array.<Hub></code>
|
||||
Retrieve a list of LPF2 Hubs.
|
||||
### poweredUP.getConnectedHubs() ⇒ <code>Array.<Hub></code>
|
||||
Retrieve a list of Powered UP Hubs.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+event_discover"></a>
|
||||
**Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
|
||||
<a name="PoweredUP+event_discover"></a>
|
||||
|
||||
### "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 |
|
||||
| --- | --- |
|
||||
|
72
README.md
72
README.md
@ -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
|
||||
|
||||
Node.js v8.0+ required.
|
||||
|
||||
```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
|
||||
|
||||
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 |
|
||||
| ------------------------------- | ------------- | ------------------ | -------------- | -------------- | ------------ |
|
||||
@ -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 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 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 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
|
||||
|
||||
```javascript
|
||||
const LPF2 = require("node-lpf2").LPF2;
|
||||
const lpf2 = new LPF2();
|
||||
const PoweredUP = require("node-poweredup");
|
||||
const pup = new PoweredUP.PoweredUP();
|
||||
```
|
||||
|
||||
Examples are available in the "examples" directory.
|
||||
@ -53,7 +53,7 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R
|
||||
## Classes
|
||||
|
||||
<dl>
|
||||
<dt><a href="#LPF2">LPF2</a> ⇐ <code>EventEmitter</code></dt>
|
||||
<dt><a href="#PoweredUP">PoweredUP</a> ⇐ <code>EventEmitter</code></dt>
|
||||
<dd></dd>
|
||||
<dt><a href="#WeDo2SmartHub">WeDo2SmartHub</a> ⇐ <code>Hub</code></dt>
|
||||
<dd></dd>
|
||||
@ -65,54 +65,54 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R
|
||||
<dd></dd>
|
||||
</dl>
|
||||
|
||||
<a name="LPF2"></a>
|
||||
<a name="PoweredUP"></a>
|
||||
|
||||
## LPF2 ⇐ <code>EventEmitter</code>
|
||||
## PoweredUP ⇐ <code>EventEmitter</code>
|
||||
**Kind**: global class
|
||||
**Extends**: <code>EventEmitter</code>
|
||||
|
||||
* [LPF2](#LPF2) ⇐ <code>EventEmitter</code>
|
||||
* [.scan()](#LPF2+scan)
|
||||
* [.stop()](#LPF2+stop)
|
||||
* [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ <code>Hub</code> \| <code>null</code>
|
||||
* [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ <code>Array.<Hub></code>
|
||||
* ["discover" (hub)](#LPF2+event_discover)
|
||||
* [PoweredUP](#PoweredUP) ⇐ <code>EventEmitter</code>
|
||||
* [.scan()](#PoweredUP+scan)
|
||||
* [.stop()](#PoweredUP+stop)
|
||||
* [.getConnectedHubByUUID(uuid)](#PoweredUP+getConnectedHubByUUID) ⇒ <code>Hub</code> \| <code>null</code>
|
||||
* [.getConnectedHubs()](#PoweredUP+getConnectedHubs) ⇒ <code>Array.<Hub></code>
|
||||
* ["discover" (hub)](#PoweredUP+event_discover)
|
||||
|
||||
<a name="LPF2+scan"></a>
|
||||
<a name="PoweredUP+scan"></a>
|
||||
|
||||
### lpF2.scan()
|
||||
Begin scanning for LPF2 Hub devices.
|
||||
### poweredUP.scan()
|
||||
Begin scanning for Powered UP Hub devices.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+stop"></a>
|
||||
**Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
|
||||
<a name="PoweredUP+stop"></a>
|
||||
|
||||
### lpF2.stop()
|
||||
Stop scanning for LPF2 Hub devices.
|
||||
### poweredUP.stop()
|
||||
Stop scanning for Powered UP Hub devices.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+getConnectedHubByUUID"></a>
|
||||
**Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
|
||||
<a name="PoweredUP+getConnectedHubByUUID"></a>
|
||||
|
||||
### lpF2.getConnectedHubByUUID(uuid) ⇒ <code>Hub</code> \| <code>null</code>
|
||||
Retrieve a LPF2 Hub by UUID.
|
||||
### poweredUP.getConnectedHubByUUID(uuid) ⇒ <code>Hub</code> \| <code>null</code>
|
||||
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 |
|
||||
| --- | --- |
|
||||
| uuid | <code>string</code> |
|
||||
|
||||
<a name="LPF2+getConnectedHubs"></a>
|
||||
<a name="PoweredUP+getConnectedHubs"></a>
|
||||
|
||||
### lpF2.getConnectedHubs() ⇒ <code>Array.<Hub></code>
|
||||
Retrieve a list of LPF2 Hubs.
|
||||
### poweredUP.getConnectedHubs() ⇒ <code>Array.<Hub></code>
|
||||
Retrieve a list of Powered UP Hubs.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+event_discover"></a>
|
||||
**Kind**: instance method of [<code>PoweredUP</code>](#PoweredUP)
|
||||
<a name="PoweredUP+event_discover"></a>
|
||||
|
||||
### "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 |
|
||||
| --- | --- |
|
||||
|
@ -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
|
||||
// It is included here as an example.
|
||||
|
||||
const LPF2 = require(".").LPF2;
|
||||
const PoweredUP = require("..").PoweredUP;
|
||||
|
||||
const lpf2 = new LPF2();
|
||||
lpf2.scan(); // Start scanning for hubs
|
||||
const pup = new PoweredUP();
|
||||
pup.scan(); // Start scanning for hubs
|
||||
|
||||
console.log("Looking for Hubs...");
|
||||
|
||||
@ -24,7 +24,7 @@ let state = 0;
|
||||
|
||||
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);
|
||||
|
||||
|
@ -4,10 +4,10 @@
|
||||
*
|
||||
*/
|
||||
|
||||
const LPF2 = require("..");
|
||||
const PoweredUP = require("..");
|
||||
|
||||
const lpf2 = new LPF2.LPF2();
|
||||
lpf2.scan(); // Start scanning
|
||||
const pup = new PoweredUP.PoweredUP();
|
||||
pup.scan(); // Start scanning
|
||||
|
||||
console.log("Looking for Batmobile and Remote...");
|
||||
|
||||
@ -16,40 +16,40 @@ let remote = 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;
|
||||
await batmobile.connect();
|
||||
console.log("Connected to Batmobile!");
|
||||
|
||||
} else if (hub instanceof LPF2.PUPRemote) {
|
||||
} else if (hub instanceof PoweredUP.PUPRemote) {
|
||||
|
||||
remote = hub;
|
||||
remote.on("button", async (button, state) => {
|
||||
if (batmobile) {
|
||||
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;
|
||||
batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", button === "LEFT" ? -100 : 100);
|
||||
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;
|
||||
batmobile.setMotorSpeed(button === "LEFT" ? "B" : "A", button === "LEFT" ? 100 : -100);
|
||||
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);
|
||||
}
|
||||
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;
|
||||
if (button === "LEFT") {
|
||||
@ -66,7 +66,7 @@ lpf2.on("discover", async (hub) => { // Wait to Batmobile and Remote
|
||||
}
|
||||
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;
|
||||
if (button === "GREEN") {
|
||||
@ -85,8 +85,8 @@ lpf2.on("discover", async (hub) => { // Wait to Batmobile and Remote
|
||||
}
|
||||
|
||||
if (batmobile && remote) {
|
||||
batmobile.setLEDColor(LPF2.Consts.Colors.WHITE);
|
||||
remote.setLEDColor(LPF2.Consts.Colors.RED);
|
||||
batmobile.setLEDColor(PoweredUP.Consts.Colors.WHITE);
|
||||
remote.setLEDColor(PoweredUP.Consts.Colors.RED);
|
||||
console.log("You're now ready to go!");
|
||||
}
|
||||
|
||||
|
@ -4,14 +4,14 @@
|
||||
*
|
||||
*/
|
||||
|
||||
const LPF2 = require("..").LPF2;
|
||||
const PoweredUP = require("..").PoweredUP;
|
||||
|
||||
const lpf2 = new LPF2();
|
||||
lpf2.scan(); // Start scanning for hubs
|
||||
const pup = new PoweredUP();
|
||||
pup.scan(); // Start scanning 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
|
||||
console.log(`Connected to ${hub.name}!`);
|
||||
@ -25,7 +25,7 @@ lpf2.on("discover", async (hub) => { // Wait to discover hubs
|
||||
let color = 1;
|
||||
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) => {
|
||||
hub.setLEDColor(color); // Set the color
|
||||
})
|
||||
|
@ -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
|
||||
|
||||
console.log("Looking for Hubs...");
|
||||
@ -8,11 +8,32 @@ console.log("Looking for Hubs...");
|
||||
lpf2.on("discover", async (hub) => { // Wait to discover hubs
|
||||
|
||||
await hub.connect(); // Connect to hub
|
||||
console.log("Connected to Hub!");
|
||||
console.log(`Connected to ${hub.name}!`);
|
||||
|
||||
setTimeout(() => {
|
||||
hub.setMotorSpeed("A", 50, 500);
|
||||
//hub.setMotorSpeed("B", 50);
|
||||
}, 3000);
|
||||
await hub.sleep(4000);
|
||||
|
||||
hub.setMotorAngle("A", 90);
|
||||
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);
|
||||
|
||||
// }
|
||||
|
||||
});
|
@ -1,18 +1,18 @@
|
||||
const LPF2 = require("..");
|
||||
const PoweredUP = require("..");
|
||||
|
||||
const lpf2 = new LPF2.LPF2();
|
||||
lpf2.scan(); // Start scanning for trains
|
||||
const pup = new PoweredUP.PoweredUP();
|
||||
pup.scan(); // Start scanning for trains
|
||||
|
||||
// Change these to make the train behave as you want
|
||||
const FORWARD_DIRECTION_COLOR = LPF2.Consts.Colors.YELLOW;
|
||||
const BACKWARDS_DIRECTION_COLOR = LPF2.Consts.Colors.RED;
|
||||
const FORWARD_DIRECTION_COLOR = PoweredUP.Consts.Colors.YELLOW;
|
||||
const BACKWARDS_DIRECTION_COLOR = PoweredUP.Consts.Colors.RED;
|
||||
const TRAIN_SPEED = 40;
|
||||
const STOP_DELAY = 2000;
|
||||
const TRAIN_MOTOR_PORT = "A";
|
||||
|
||||
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;
|
||||
|
||||
|
@ -4,53 +4,53 @@
|
||||
*
|
||||
*/
|
||||
|
||||
const LPF2 = require("..");
|
||||
const PoweredUP = require("..");
|
||||
|
||||
const lpf2 = new LPF2.LPF2();
|
||||
lpf2.scan(); // Start scanning
|
||||
const pup = new PoweredUP.PoweredUP();
|
||||
pup.scan(); // Start scanning
|
||||
|
||||
console.log("Looking for Vernie and Remote...");
|
||||
|
||||
let vernie = 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;
|
||||
await vernie.connect();
|
||||
console.log("Connected to Vernie!");
|
||||
|
||||
} else if (hub instanceof LPF2.PUPRemote) {
|
||||
} else if (hub instanceof PoweredUP.PUPRemote) {
|
||||
remote = hub;
|
||||
|
||||
remote.on("button", async (button, state) => {
|
||||
if (vernie) {
|
||||
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);
|
||||
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);
|
||||
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") {
|
||||
vernie.setMotorSpeed(button === "LEFT" ? "A" : "B", 0);
|
||||
}
|
||||
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);
|
||||
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") {
|
||||
await vernie.setMotorAngle("D", 80, 20);
|
||||
@ -67,8 +67,8 @@ lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote
|
||||
}
|
||||
|
||||
if (vernie && remote) {
|
||||
vernie.setLEDColor(LPF2.Consts.Colors.BLUE);
|
||||
remote.setLEDColor(LPF2.Consts.Colors.BLUE);
|
||||
vernie.setLEDColor(PoweredUP.Consts.Colors.BLUE);
|
||||
remote.setLEDColor(PoweredUP.Consts.Colors.BLUE);
|
||||
console.log("You're now ready to go!");
|
||||
}
|
||||
|
||||
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "node-lpf2",
|
||||
"name": "node-poweredup",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
|
10
package.json
10
package.json
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "node-lpf2",
|
||||
"name": "node-poweredup",
|
||||
"version": "1.0.0",
|
||||
"description": "A Node.js module to interface with Lego Power Functions 2.0 components.",
|
||||
"main": "dist/lpf2.js",
|
||||
"types": "dist/lpf2.d.ts",
|
||||
"description": "A Node.js module to interface with LEGO Powered UP components.",
|
||||
"main": "dist/poweredup.js",
|
||||
"types": "dist/poweredup.d.ts",
|
||||
"scripts": {
|
||||
"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",
|
||||
"all": "npm run build && npm run docs && npm run readme",
|
||||
"prepublishOnly": "tsc"
|
||||
|
@ -11,7 +11,7 @@ import * as Consts from "./consts";
|
||||
import { EventEmitter} from "events";
|
||||
|
||||
import Debug = require("debug");
|
||||
const debug = Debug("lpf2");
|
||||
const debug = Debug("PoweredUP");
|
||||
import noble = require("noble");
|
||||
|
||||
let ready = false;
|
||||
@ -30,10 +30,10 @@ noble.on("stateChange", (state: string) => {
|
||||
});
|
||||
|
||||
/**
|
||||
* @class LPF2
|
||||
* @class PoweredUP
|
||||
* @extends EventEmitter
|
||||
*/
|
||||
export class LPF2 extends EventEmitter {
|
||||
export class PoweredUP extends EventEmitter {
|
||||
|
||||
|
||||
public autoSubscribe: boolean = true;
|
||||
@ -48,8 +48,8 @@ export class LPF2 extends EventEmitter {
|
||||
|
||||
|
||||
/**
|
||||
* Begin scanning for LPF2 Hub devices.
|
||||
* @method LPF2#scan
|
||||
* Begin scanning for Powered UP Hub devices.
|
||||
* @method PoweredUP#scan
|
||||
*/
|
||||
public scan () {
|
||||
wantScan = true;
|
||||
@ -90,8 +90,8 @@ export class LPF2 extends EventEmitter {
|
||||
|
||||
debug(`Hub ${hub.uuid} discovered`);
|
||||
/**
|
||||
* Emits when a LPF2 Hub device is found.
|
||||
* @event LPF2#discover
|
||||
* Emits when a Powered UP Hub device is found.
|
||||
* @event PoweredUP#discover
|
||||
* @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote} hub
|
||||
*/
|
||||
this.emit("discover", hub);
|
||||
@ -106,8 +106,8 @@ export class LPF2 extends EventEmitter {
|
||||
|
||||
|
||||
/**
|
||||
* Stop scanning for LPF2 Hub devices.
|
||||
* @method LPF2#stop
|
||||
* Stop scanning for Powered UP Hub devices.
|
||||
* @method PoweredUP#stop
|
||||
*/
|
||||
public stop () {
|
||||
wantScan = false;
|
||||
@ -116,8 +116,8 @@ export class LPF2 extends EventEmitter {
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve a LPF2 Hub by UUID.
|
||||
* @method LPF2#getConnectedHubByUUID
|
||||
* Retrieve a Powered UP Hub by UUID.
|
||||
* @method PoweredUP#getConnectedHubByUUID
|
||||
* @param {string} uuid
|
||||
* @returns {Hub | null}
|
||||
*/
|
||||
@ -127,8 +127,8 @@ export class LPF2 extends EventEmitter {
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve a list of LPF2 Hubs.
|
||||
* @method LPF2#getConnectedHubs
|
||||
* Retrieve a list of Powered UP Hubs.
|
||||
* @method PoweredUP#getConnectedHubs
|
||||
* @returns {Hub[]}
|
||||
*/
|
||||
public getConnectedHubs () {
|
||||
@ -140,5 +140,5 @@ export class LPF2 extends EventEmitter {
|
||||
|
||||
}
|
||||
|
||||
export default LPF2;
|
||||
export default PoweredUP;
|
||||
export { Hub, WeDo2SmartHub, BoostMoveHub, PUPHub, PUPRemote, Consts };
|
Loading…
x
Reference in New Issue
Block a user