BoostHub now LPF2Hub

This commit is contained in:
Nathan Kunicki 2018-06-27 16:35:09 +01:00
parent b4cd5fd40b
commit e0a6694bd9
7 changed files with 153 additions and 159 deletions

88
DOCS.md
View File

@ -3,7 +3,7 @@
<dl>
<dt><a href="#LPF2">LPF2</a><code>EventEmitter</code></dt>
<dd></dd>
<dt><a href="#BoostHub">BoostHub</a><code><a href="#Hub">Hub</a></code></dt>
<dt><a href="#LPF2Hub">LPF2Hub</a><code><a href="#Hub">Hub</a></code></dt>
<dd></dd>
<dt><a href="#WeDo2Hub">WeDo2Hub</a><code><a href="#Hub">Hub</a></code></dt>
<dd></dd>
@ -62,48 +62,48 @@ Emits when a LPF2 Hub device is found.
| Param | Type |
| --- | --- |
| hub | [<code>WeDo2Hub</code>](#WeDo2Hub) \| [<code>BoostHub</code>](#BoostHub) |
| hub | [<code>WeDo2Hub</code>](#WeDo2Hub) \| [<code>LPF2Hub</code>](#LPF2Hub) |
<a name="BoostHub"></a>
<a name="LPF2Hub"></a>
## BoostHub ⇐ [<code>Hub</code>](#Hub)
## LPF2Hub ⇐ [<code>Hub</code>](#Hub)
**Kind**: global class
**Extends**: [<code>Hub</code>](#Hub)
* [BoostHub](#BoostHub) ⇐ [<code>Hub</code>](#Hub)
* [.setLEDColor(color)](#BoostHub+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#BoostHub+setMotorSpeed) ⇒ <code>Promise</code>
* [.setMotorAngle(port, angle, [speed])](#BoostHub+setMotorAngle) ⇒ <code>Promise</code>
* [LPF2Hub](#LPF2Hub) ⇐ [<code>Hub</code>](#Hub)
* [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#LPF2Hub+setMotorSpeed) ⇒ <code>Promise</code>
* [.setMotorAngle(port, angle, [speed])](#LPF2Hub+setMotorAngle) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
* [.unsubscribe(port)](#Hub+unsubscribe) ⇒ <code>Promise</code>
* [.sleep(delay)](#Hub+sleep) ⇒ <code>Promise</code>
* [.wait(commands)](#Hub+wait) ⇒ <code>Promise</code>
* ["button" (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)
* ["button" (button, state)](#LPF2Hub+event_button)
* ["distance" (port, distance)](#LPF2Hub+event_distance)
* ["color" (port, color)](#LPF2Hub+event_color)
* ["tilt" (port, x, y)](#LPF2Hub+event_tilt)
* ["rotate" (port, rotation)](#LPF2Hub+event_rotate)
<a name="BoostHub+setLEDColor"></a>
<a name="LPF2Hub+setLEDColor"></a>
### boostHub.setLEDColor(color) ⇒ <code>Promise</code>
### lpF2Hub.setLEDColor(color) ⇒ <code>Promise</code>
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="BoostHub+setMotorSpeed"></a>
<a name="LPF2Hub+setMotorSpeed"></a>
### boostHub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
### lpF2Hub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
| Param | Type | Description |
@ -112,12 +112,12 @@ Set the motor speed on a given port.
| speed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| [time] | <code>number</code> | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. |
<a name="BoostHub+setMotorAngle"></a>
<a name="LPF2Hub+setMotorAngle"></a>
### boostHub.setMotorAngle(port, angle, [speed]) ⇒ <code>Promise</code>
### lpF2Hub.setMotorAngle(port, angle, [speed]) ⇒ <code>Promise</code>
Rotate a motor by a given angle.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command (ie. once the motor is finished).
| Param | Type | Default | Description |
@ -128,25 +128,25 @@ Rotate a motor by a given angle.
<a name="Hub+connect"></a>
### boostHub.connect() ⇒ <code>Promise</code>
### lpF2Hub.connect() ⇒ <code>Promise</code>
Connect to the Hub.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Overrides**: [<code>connect</code>](#Hub+connect)
**Returns**: <code>Promise</code> - Resolved upon successful connect.
<a name="Hub+disconnect"></a>
### boostHub.disconnect() ⇒ <code>Promise</code>
### lpF2Hub.disconnect() ⇒ <code>Promise</code>
Disconnect the Hub.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful disconnect.
<a name="Hub+subscribe"></a>
### boostHub.subscribe(port, [mode]) ⇒ <code>Promise</code>
### lpF2Hub.subscribe(port, [mode]) ⇒ <code>Promise</code>
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
@ -156,10 +156,10 @@ Subscribe to sensor notifications on a given port.
<a name="Hub+unsubscribe"></a>
### boostHub.unsubscribe(port) ⇒ <code>Promise</code>
### lpF2Hub.unsubscribe(port) ⇒ <code>Promise</code>
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type |
@ -168,12 +168,12 @@ Unsubscribe to sensor notifications on a given port.
<a name="Hub+sleep"></a>
### boostHub.sleep(delay) ⇒ <code>Promise</code>
### lpF2Hub.sleep(delay) ⇒ <code>Promise</code>
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 [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved after the delay is finished.
| Param | Type | Description |
@ -182,60 +182,60 @@ This is a helper method to make it easier to add delays into a chain of commands
<a name="Hub+wait"></a>
### boostHub.wait(commands) ⇒ <code>Promise</code>
### lpF2Hub.wait(commands) ⇒ <code>Promise</code>
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 [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved after the commands are finished.
| Param | Type | Description |
| --- | --- | --- |
| commands | <code>Array.&lt;Promise.&lt;any&gt;&gt;</code> | Array of executing commands. |
<a name="BoostHub+event_button"></a>
<a name="LPF2Hub+event_button"></a>
### "button" (button, state)
Emits when a button is pressed.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
| button | <code>string</code> | |
| state | <code>number</code> | A number representing one of the button state consts. |
<a name="BoostHub+event_distance"></a>
<a name="LPF2Hub+event_distance"></a>
### "distance" (port, distance)
Emits when a distance sensor is activated.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| distance | <code>number</code> | Distance, in millimeters. |
<a name="BoostHub+event_color"></a>
<a name="LPF2Hub+event_color"></a>
### "color" (port, color)
Emits when a color sensor is activated.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="BoostHub+event_tilt"></a>
<a name="LPF2Hub+event_tilt"></a>
### "tilt" (port, x, y)
Emits when a tilt sensor is activated.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
@ -243,12 +243,12 @@ Emits when a tilt sensor is activated.
| x | <code>number</code> | |
| y | <code>number</code> | |
<a name="BoostHub+event_rotate"></a>
<a name="LPF2Hub+event_rotate"></a>
### "rotate" (port, rotation)
Emits when a rotation sensor is activated.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type |
| --- | --- |

View File

@ -44,7 +44,7 @@ Examples are available in the "examples" directory.
<dl>
<dt><a href="#LPF2">LPF2</a><code>EventEmitter</code></dt>
<dd></dd>
<dt><a href="#BoostHub">BoostHub</a><code><a href="#Hub">Hub</a></code></dt>
<dt><a href="#LPF2Hub">LPF2Hub</a><code><a href="#Hub">Hub</a></code></dt>
<dd></dd>
<dt><a href="#WeDo2Hub">WeDo2Hub</a><code><a href="#Hub">Hub</a></code></dt>
<dd></dd>
@ -103,48 +103,48 @@ Emits when a LPF2 Hub device is found.
| Param | Type |
| --- | --- |
| hub | [<code>WeDo2Hub</code>](#WeDo2Hub) \| [<code>BoostHub</code>](#BoostHub) |
| hub | [<code>WeDo2Hub</code>](#WeDo2Hub) \| [<code>LPF2Hub</code>](#LPF2Hub) |
<a name="BoostHub"></a>
<a name="LPF2Hub"></a>
## BoostHub ⇐ [<code>Hub</code>](#Hub)
## LPF2Hub ⇐ [<code>Hub</code>](#Hub)
**Kind**: global class
**Extends**: [<code>Hub</code>](#Hub)
* [BoostHub](#BoostHub) ⇐ [<code>Hub</code>](#Hub)
* [.setLEDColor(color)](#BoostHub+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#BoostHub+setMotorSpeed) ⇒ <code>Promise</code>
* [.setMotorAngle(port, angle, [speed])](#BoostHub+setMotorAngle) ⇒ <code>Promise</code>
* [LPF2Hub](#LPF2Hub) ⇐ [<code>Hub</code>](#Hub)
* [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#LPF2Hub+setMotorSpeed) ⇒ <code>Promise</code>
* [.setMotorAngle(port, angle, [speed])](#LPF2Hub+setMotorAngle) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
* [.unsubscribe(port)](#Hub+unsubscribe) ⇒ <code>Promise</code>
* [.sleep(delay)](#Hub+sleep) ⇒ <code>Promise</code>
* [.wait(commands)](#Hub+wait) ⇒ <code>Promise</code>
* ["button" (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)
* ["button" (button, state)](#LPF2Hub+event_button)
* ["distance" (port, distance)](#LPF2Hub+event_distance)
* ["color" (port, color)](#LPF2Hub+event_color)
* ["tilt" (port, x, y)](#LPF2Hub+event_tilt)
* ["rotate" (port, rotation)](#LPF2Hub+event_rotate)
<a name="BoostHub+setLEDColor"></a>
<a name="LPF2Hub+setLEDColor"></a>
### boostHub.setLEDColor(color) ⇒ <code>Promise</code>
### lpF2Hub.setLEDColor(color) ⇒ <code>Promise</code>
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="BoostHub+setMotorSpeed"></a>
<a name="LPF2Hub+setMotorSpeed"></a>
### boostHub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
### lpF2Hub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
| Param | Type | Description |
@ -153,12 +153,12 @@ Set the motor speed on a given port.
| speed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| [time] | <code>number</code> | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. |
<a name="BoostHub+setMotorAngle"></a>
<a name="LPF2Hub+setMotorAngle"></a>
### boostHub.setMotorAngle(port, angle, [speed]) ⇒ <code>Promise</code>
### lpF2Hub.setMotorAngle(port, angle, [speed]) ⇒ <code>Promise</code>
Rotate a motor by a given angle.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command (ie. once the motor is finished).
| Param | Type | Default | Description |
@ -169,25 +169,25 @@ Rotate a motor by a given angle.
<a name="Hub+connect"></a>
### boostHub.connect() ⇒ <code>Promise</code>
### lpF2Hub.connect() ⇒ <code>Promise</code>
Connect to the Hub.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Overrides**: [<code>connect</code>](#Hub+connect)
**Returns**: <code>Promise</code> - Resolved upon successful connect.
<a name="Hub+disconnect"></a>
### boostHub.disconnect() ⇒ <code>Promise</code>
### lpF2Hub.disconnect() ⇒ <code>Promise</code>
Disconnect the Hub.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful disconnect.
<a name="Hub+subscribe"></a>
### boostHub.subscribe(port, [mode]) ⇒ <code>Promise</code>
### lpF2Hub.subscribe(port, [mode]) ⇒ <code>Promise</code>
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
@ -197,10 +197,10 @@ Subscribe to sensor notifications on a given port.
<a name="Hub+unsubscribe"></a>
### boostHub.unsubscribe(port) ⇒ <code>Promise</code>
### lpF2Hub.unsubscribe(port) ⇒ <code>Promise</code>
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type |
@ -209,12 +209,12 @@ Unsubscribe to sensor notifications on a given port.
<a name="Hub+sleep"></a>
### boostHub.sleep(delay) ⇒ <code>Promise</code>
### lpF2Hub.sleep(delay) ⇒ <code>Promise</code>
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 [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved after the delay is finished.
| Param | Type | Description |
@ -223,60 +223,60 @@ This is a helper method to make it easier to add delays into a chain of commands
<a name="Hub+wait"></a>
### boostHub.wait(commands) ⇒ <code>Promise</code>
### lpF2Hub.wait(commands) ⇒ <code>Promise</code>
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 [<code>BoostHub</code>](#BoostHub)
**Kind**: instance method of [<code>LPF2Hub</code>](#LPF2Hub)
**Returns**: <code>Promise</code> - Resolved after the commands are finished.
| Param | Type | Description |
| --- | --- | --- |
| commands | <code>Array.&lt;Promise.&lt;any&gt;&gt;</code> | Array of executing commands. |
<a name="BoostHub+event_button"></a>
<a name="LPF2Hub+event_button"></a>
### "button" (button, state)
Emits when a button is pressed.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
| button | <code>string</code> | |
| state | <code>number</code> | A number representing one of the button state consts. |
<a name="BoostHub+event_distance"></a>
<a name="LPF2Hub+event_distance"></a>
### "distance" (port, distance)
Emits when a distance sensor is activated.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| distance | <code>number</code> | Distance, in millimeters. |
<a name="BoostHub+event_color"></a>
<a name="LPF2Hub+event_color"></a>
### "color" (port, color)
Emits when a color sensor is activated.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="BoostHub+event_tilt"></a>
<a name="LPF2Hub+event_tilt"></a>
### "tilt" (port, x, y)
Emits when a tilt sensor is activated.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
@ -284,12 +284,12 @@ Emits when a tilt sensor is activated.
| x | <code>number</code> | |
| y | <code>number</code> | |
<a name="BoostHub+event_rotate"></a>
<a name="LPF2Hub+event_rotate"></a>
### "rotate" (port, rotation)
Emits when a rotation sensor is activated.
**Kind**: event emitted by [<code>BoostHub</code>](#BoostHub)
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type |
| --- | --- |

13
hub.ts
View File

@ -227,6 +227,19 @@ export class Hub extends EventEmitter {
}
protected _getPortForPortNumber (num: number) {
for (const key of Object.keys(this._ports)) {
if (this._ports[key].value === num) {
return this._ports[key];
}
}
return false;
}
protected _mapSpeed (speed: number) { // Speed range of -100 to 100 is supported unless speed mapping is turned off, in which case, you're on your own!
if (!this.useSpeedMap) {
return speed;

10
lpf2.ts
View File

@ -1,7 +1,7 @@
import { Peripheral } from "noble";
import { BoostHub } from "./boosthub";
import { Hub } from "./hub";
import { LPF2Hub } from "./lpf2hub";
import { WeDo2Hub } from "./wedo2hub";
import { EventEmitter} from "events";
@ -56,8 +56,8 @@ export class LPF2 extends EventEmitter {
if (WeDo2Hub.IsWeDo2Hub(peripheral)) {
hub = new WeDo2Hub(peripheral, this.autoSubscribe);
} else if (BoostHub.IsBoostHub(peripheral)) {
hub = new BoostHub(peripheral, this.autoSubscribe);
} else if (LPF2Hub.IsLPF2Hub(peripheral)) {
hub = new LPF2Hub(peripheral, this.autoSubscribe);
} else {
return;
}
@ -86,7 +86,7 @@ export class LPF2 extends EventEmitter {
/**
* Emits when a LPF2 Hub device is found.
* @event LPF2#discover
* @param {WeDo2Hub | BoostHub} hub
* @param {WeDo2Hub | LPF2Hub} hub
*/
this.emit("discover", hub);
@ -135,4 +135,4 @@ export class LPF2 extends EventEmitter {
}
export default LPF2;
export { Hub, WeDo2Hub, BoostHub };
export { Hub, WeDo2Hub, LPF2Hub };

View File

@ -6,18 +6,18 @@ import { Port } from "./port.js";
import * as Consts from "./consts";
import Debug = require("debug");
const debug = Debug("boosthub");
const debug = Debug("lpf2hub");
/**
* @class BoostHub
* @class LPF2Hub
* @extends Hub
*/
export class BoostHub extends Hub {
export class LPF2Hub extends Hub {
public static IsBoostHub (peripheral: Peripheral) {
return (peripheral.advertisement.localName === Consts.BLENames.BOOST_MOVE_HUB_NAME && peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.BOOST_MOVE_HUB) >= 0);
public static IsLPF2Hub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.BOOST_MOVE_HUB) >= 0);
}
@ -27,6 +27,26 @@ export class BoostHub extends Hub {
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) {
super(peripheral, autoSubscribe);
switch (peripheral.advertisement.localName) {
case Consts.BLENames.POWERED_UP_HUB_NAME:
{
this.type = Consts.Hubs.POWERED_UP_HUB;
this._ports = {
"A": new Port("A", 55),
"B": new Port("B", 56),
"AB": new Port("AB", 57)
};
debug("Discovered Powered Up Hub");
break;
}
case Consts.BLENames.POWERED_UP_REMOTE_NAME:
{
this.type = Consts.Hubs.POWERED_UP_REMOTE;
debug("Discovered Powered Up Remote");
break;
}
default:
{
this.type = Consts.Hubs.BOOST_MOVE_HUB;
this._ports = {
"A": new Port("A", 55),
@ -37,6 +57,9 @@ export class BoostHub extends Hub {
"D": new Port("D", 2)
};
debug("Discovered Boost Move Hub");
break;
}
}
}
@ -55,7 +78,7 @@ export class BoostHub extends Hub {
/**
* Set the color of the LED on the Hub via a color value.
* @method BoostHub#setLEDColor
* @method LPF2Hub#setLEDColor
* @param {number} color A number representing one of the LED color consts.
* @returns {Promise} Resolved upon successful issuance of command.
*/
@ -89,7 +112,7 @@ export class BoostHub extends Hub {
/**
* Set the motor speed on a given port.
* @method BoostHub#setMotorSpeed
* @method LPF2Hub#setMotorSpeed
* @param {string} port
* @param {number} speed 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 to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
@ -120,7 +143,7 @@ export class BoostHub extends Hub {
/**
* Rotate a motor by a given angle.
* @method BoostHub#setMotorAngle
* @method LPF2Hub#setMotorAngle
* @param {string} port
* @param {number} angle How much the motor should be rotated (in degrees).
* @param {number} [speed=100] How fast the motor should be rotated.
@ -160,31 +183,6 @@ export class BoostHub extends Hub {
}
private _getPortForPortNumber (num: number) {
let port = null;
if (num === 1) {
port = this._ports["C"];
} else if (num === 2) {
port = this._ports["D"];
} else if (num === 55) {
port = this._ports["A"];
} else if (num === 56) {
port = this._ports["B"];
} else if (num === 57) {
port = this._ports["AB"];
} else if (num === 58) {
port = this._ports["TILT"];
} else {
return false;
}
return port;
}
private _parseMessage (data: Buffer) {
switch (data[2]) {
@ -218,7 +216,7 @@ export class BoostHub extends Hub {
if (data[5] === 1) {
/**
* Emits when a button is pressed.
* @event BoostHub#button
* @event LPF2Hub#button
* @param {string} button
* @param {number} state A number representing one of the button state consts.
*/
@ -284,7 +282,7 @@ export class BoostHub extends Hub {
}
/**
* Emits when a distance sensor is activated.
* @event BoostHub#distance
* @event LPF2Hub#distance
* @param {string} port
* @param {number} distance Distance, in millimeters.
*/
@ -296,7 +294,7 @@ export class BoostHub extends Hub {
/**
* Emits when a color sensor is activated.
* @event BoostHub#color
* @event LPF2Hub#color
* @param {string} port
* @param {number} color A number representing one of the LED color consts.
*/
@ -322,7 +320,7 @@ export class BoostHub extends Hub {
this._lastTiltY = tiltY;
/**
* Emits when a tilt sensor is activated.
* @event BoostHub#tilt
* @event LPF2Hub#tilt
* @param {string} port If the event is fired from the Move Hub's in-built tilt sensor, the special port "TILT" is used.
* @param {number} x
* @param {number} y
@ -335,7 +333,7 @@ export class BoostHub extends Hub {
const rotation = data.readInt32LE(2);
/**
* Emits when a rotation sensor is activated.
* @event BoostHub#rotate
* @event LPF2Hub#rotate
* @param {string} port
* @param {number} rotation
*/

View File

@ -6,7 +6,7 @@
"types": "dist/lpf2.d.ts",
"scripts": {
"build": "tslint -c tslint.json \"*.ts\" && tsc",
"docs": "jsdoc2md dist/lpf2.js dist/boosthub.js dist/wedo2hub.js dist/hub.js dist/consts.js > DOCS.md",
"docs": "jsdoc2md dist/lpf2.js dist/lpf2hub.js dist/wedo2hub.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"

View File

@ -130,23 +130,6 @@ export class WeDo2Hub extends Hub {
}
private _getPortForPortNumber (num: number) {
let port = null;
if (num === 1) {
port = this._ports["A"];
} else if (num === 2) {
port = this._ports["B"];
} else {
return;
}
return port;
}
private _parsePortMessage (data: Buffer) {
const port = this._getPortForPortNumber(data[0]);