diff --git a/README.md b/README.md index ce4152c..150f5d2 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,10 @@
EventEmitter
Hub
Hub
Hub
string
|
-
-
-## 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
|
-
## BoostHub ⇐ [Hub
](#Hub)
@@ -384,3 +234,153 @@ Emits when a rotation sensor is activated.
| 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
|
+
diff --git a/boosthub.ts b/boosthub.ts
index 4f60e89..b1ab0b6 100644
--- a/boosthub.ts
+++ b/boosthub.ts
@@ -3,9 +3,10 @@ import { Peripheral } from "noble";
import { Hub } from "./hub.js";
import { Port } from "./port.js";
+import * as Consts from "./consts";
+
import Debug = require("debug");
const debug = Debug("lpf2");
-import { Consts } from "./consts.js";
/**
@@ -16,7 +17,7 @@ export class BoostHub extends Hub {
public static IsBoostHub (peripheral: Peripheral) {
- return (peripheral.advertisement.localName === Consts.BLE.Name.BOOST_MOVE_HUB_NAME && peripheral.advertisement.serviceUuids.indexOf(Consts.BLE.Services.BOOST_MOVE_HUB) >= 0);
+ return (peripheral.advertisement.localName === Consts.BLENames.BOOST_MOVE_HUB_NAME && peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.BOOST_MOVE_HUB) >= 0);
}
@@ -42,7 +43,7 @@ export class BoostHub extends Hub {
public connect (callback: () => void) {
debug("Connecting to Boost Move Hub");
super.connect(() => {
- const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
+ const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]));
debug("Connect completed");
@@ -59,7 +60,7 @@ export class BoostHub extends Hub {
* @param {number} color - A number representing one of the LED color consts.
*/
public setLEDColor (color: number | boolean) {
- const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
+ const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (characteristic) {
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
characteristic.write(data);
@@ -91,7 +92,7 @@ export class BoostHub extends Hub {
* @param {number} [time] - How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
*/
public setMotorSpeed (port: string, speed: number, time: number) {
- const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
+ const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (characteristic) {
if (time) {
const data = Buffer.from([0x0c, 0x00, 0x81, this._ports[port].value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]);
@@ -113,7 +114,7 @@ export class BoostHub extends Hub {
* @param {number} [speed=100] - How fast the motor should be rotated.
*/
public setMotorAngle (port: string, angle: number, speed: number = 100) {
- const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
+ const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (characteristic) {
const data = Buffer.from([0x0e, 0x00, 0x81, this._ports[port].value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]);
data.writeUInt32LE(angle, 6);
@@ -124,7 +125,7 @@ export class BoostHub extends Hub {
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
- const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
+ const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (characteristic) {
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback);
}
@@ -132,7 +133,7 @@ export class BoostHub extends Hub {
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
- const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
+ const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (characteristic) {
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback);
}
@@ -199,10 +200,10 @@ export class BoostHub extends Hub {
* @event BoostHub#button
* @param {number} state - A number representing one of the button state consts.
*/
- this.emit("button", Consts.Button.PRESSED);
+ this.emit("button", Consts.ButtonStates.PRESSED);
return;
} else if (data[5] === 0) {
- this.emit("button", Consts.Button.RELEASED);
+ this.emit("button", Consts.ButtonStates.RELEASED);
return;
}
}
diff --git a/consts.ts b/consts.ts
index a89d3bd..a1c6dca 100644
--- a/consts.ts
+++ b/consts.ts
@@ -1,68 +1,65 @@
-/* tslint:disable */
+export enum Hubs {
+ UNKNOWN = 0,
+ WEDO2_SMART_HUB = 1,
+ BOOST_MOVE_HUB = 2,
+ POWERED_UP_HUB = 3,
+ POWERED_UP_REMOTE = 4
+}
-export const Consts = {
- Hubs: {
- UNKNOWN: 0,
- WEDO2_SMART_HUB: 1,
- BOOST_MOVE_HUB: 2,
- POWERED_UP_HUB: 3,
- POWERED_UP_REMOTE: 4
- },
- Devices: {
- BASIC_MOTOR: 1,
- BOOST_LED: 22,
- WEDO2_TILT: 34,
- WEDO2_DISTANCE: 35,
- BOOST_DISTANCE: 37,
- BOOST_INTERACTIVE_MOTOR: 38,
- BOOST_MOVE_HUB_MOTOR: 39,
- BOOST_TILT: 40
- },
- Colors: {
- NONE: 0,
- PINK: 1,
- PURPLE: 2,
- BLUE: 3,
- LIGHT_BLUE: 4,
- CYAN: 5,
- GREEN: 6,
- YELLOW: 7,
- ORANGE: 8,
- RED: 9,
- WHITE: 10
- },
- Button: {
- PRESSED: 0,
- RELEASED: 1
- },
- BLE: {
- Name: {
- WEDO2_SMART_HUB_NAME: "LPF2 Smart Hub 2 I/O",
- BOOST_MOVE_HUB_NAME: "LEGO Move Hub",
- POWERED_UP_HUB_NAME: "HUB NO.4",
- POWERED_UP_REMOTE_NAME: "Handset"
- },
- Services: {
- WEDO2_SMART_HUB: "000015231212efde1523785feabcd123",
- BOOST_MOVE_HUB: "000016231212efde1623785feabcd123"
- },
- Characteristics: {
- WeDo2: {
- BATTERY: "2a19",
- BUTTON: "000015261212efde1523785feabcd123", // "1526"
- PORT_TYPE: "000015271212efde1523785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub
- LOW_VOLTAGE_ALERT: "000015281212efde1523785feabcd123", // "1528"
- HIGH_CURRENT_ALERT: "000015291212efde1523785feabcd123", // "1529"
- LOW_SIGNAL_ALERT: "0000152a1212efde1523785feabcd123", // "152a"
- SENSOR_VALUE: "000015601212efde1523785feabcd123", // "1560"
- VALUE_FORMAT: "000015611212efde1523785feabcd123", // "1561"
- PORT_TYPE_WRITE: "000015631212efde1523785feabcd123", // "1563"
- MOTOR_VALUE_WRITE: "000015651212efde1523785feabcd123", // "1565"
- NAME_ID: "000015241212efde1523785feabcd123", // "1524"
- },
- Boost: {
- ALL: "000016241212efde1623785feabcd123"
- }
- }
- }
-}
\ No newline at end of file
+export enum Devices {
+ BASIC_MOTOR = 1,
+ BOOST_LED = 22,
+ WEDO2_TILT = 34,
+ WEDO2_DISTANCE = 35,
+ BOOST_DISTANCE = 37,
+ BOOST_INTERACTIVE_MOTOR = 38,
+ BOOST_MOVE_HUB_MOTOR = 39,
+ BOOST_TILT = 40
+}
+
+export enum Colors {
+ NONE = 0,
+ PINK = 1,
+ PURPLE = 2,
+ BLUE = 3,
+ LIGHT_BLUE = 4,
+ CYAN = 5,
+ GREEN = 6,
+ YELLOW = 7,
+ ORANGE = 8,
+ RED = 9,
+ WHITE = 10
+}
+
+export enum ButtonStates {
+ PRESSED = 0,
+ RELEASED = 1
+}
+
+export enum BLENames {
+ WEDO2_SMART_HUB_NAME = "LPF2 Smart Hub 2 I/O",
+ BOOST_MOVE_HUB_NAME = "LEGO Move Hub",
+ POWERED_UP_HUB_NAME = "HUB NO.4",
+ POWERED_UP_REMOTE_NAME = "Handset"
+}
+
+export enum BLEServices {
+ WEDO2_SMART_HUB = "000015231212efde1523785feabcd123",
+ BOOST_MOVE_HUB = "000016231212efde1623785feabcd123"
+}
+
+
+export enum BLECharacteristics {
+ WEDO2_BATTERY = "2a19",
+ WEDO2_BUTTON = "000015261212efde1523785feabcd123", // "1526"
+ WEDO2_PORT_TYPE = "000015271212efde1523785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub
+ WEDO2_LOW_VOLTAGE_ALERT = "000015281212efde1523785feabcd123", // "1528"
+ WEDO2_HIGH_CURRENT_ALERT = "000015291212efde1523785feabcd123", // "1529"
+ WEDO2_LOW_SIGNAL_ALERT = "0000152a1212efde1523785feabcd123", // "152a"
+ WEDO2_SENSOR_VALUE = "000015601212efde1523785feabcd123", // "1560"
+ WEDO2_VALUE_FORMAT = "000015611212efde1523785feabcd123", // "1561"
+ WEDO2_PORT_TYPE_WRITE = "000015631212efde1523785feabcd123", // "1563"
+ WEDO2_MOTOR_VALUE_WRITE = "000015651212efde1523785feabcd123", // "1565"
+ WEDO2_NAME_ID = "000015241212efde1523785feabcd123", // "1524"
+ BOOST_ALL = "000016241212efde1623785feabcd123"
+}
diff --git a/hub.ts b/hub.ts
index 176c059..5c12e95 100644
--- a/hub.ts
+++ b/hub.ts
@@ -3,9 +3,10 @@ import { EventEmitter } from "events";
import { Characteristic, Peripheral, Service } from "noble";
import { Port } from "./port";
+import * as Consts from "./consts";
+
import Debug = require("debug");
const debug = Debug("lpf2");
-import { Consts } from "./consts.js";
/**
@@ -16,7 +17,7 @@ export class Hub extends EventEmitter {
public autoSubscribe: boolean;
- public type: number = Consts.Hubs.UNKNOWN;
+ public type: Consts.Hubs = Consts.Hubs.UNKNOWN;
public uuid: string;
protected _ports: any = {};
@@ -171,7 +172,7 @@ export class Hub extends EventEmitter {
}
- private _getModeForDeviceType (type: number) {
+ private _getModeForDeviceType (type: Consts.Devices) {
switch (type) {
case Consts.Devices.BASIC_MOTOR:
return 0x02;
diff --git a/package.json b/package.json
index 388b076..2072bc0 100644
--- a/package.json
+++ b/package.json
@@ -6,7 +6,7 @@
"types": "dist/lpf2.d.ts",
"scripts": {
"build": "tslint -c tslint.json \"*.ts\" && tsc",
- "docs": "jsdoc2md dist/*.js > README.md"
+ "docs": "jsdoc2md dist/lpf2.js dist/hub.js dist/boosthub.js dist/wedo2hub.js > README.md"
},
"author": "Nathan Kunicki