Better consts

This commit is contained in:
Nathan Kunicki 2018-06-20 14:29:53 +01:00
parent fe14307159
commit 12fde4c513
6 changed files with 247 additions and 247 deletions

304
README.md
View File

@ -5,10 +5,10 @@
<dd></dd> <dd></dd>
<dt><a href="#Hub">Hub</a><code>EventEmitter</code></dt> <dt><a href="#Hub">Hub</a><code>EventEmitter</code></dt>
<dd></dd> <dd></dd>
<dt><a href="#WeDo2Hub">WeDo2Hub</a><code><a href="#Hub">Hub</a></code></dt>
<dd></dd>
<dt><a href="#BoostHub">BoostHub</a><code><a href="#Hub">Hub</a></code></dt> <dt><a href="#BoostHub">BoostHub</a><code><a href="#Hub">Hub</a></code></dt>
<dd></dd> <dd></dd>
<dt><a href="#WeDo2Hub">WeDo2Hub</a><code><a href="#Hub">Hub</a></code></dt>
<dd></dd>
</dl> </dl>
<a name="LPF2"></a> <a name="LPF2"></a>
@ -83,156 +83,6 @@ Unsubscribe to sensor notifications on a given port.
| --- | --- | | --- | --- |
| port | <code>string</code> | | port | <code>string</code> |
<a name="WeDo2Hub"></a>
## WeDo2Hub ⇐ [<code>Hub</code>](#Hub)
**Kind**: global class
**Extends**: [<code>Hub</code>](#Hub)
* [WeDo2Hub](#WeDo2Hub) ⇐ [<code>Hub</code>](#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)
<a name="WeDo2Hub+setLEDColor"></a>
### weDo2Hub.setLEDColor(color)
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="WeDo2Hub+setLEDRGB"></a>
### weDo2Hub.setLEDRGB(red, green, blue)
Set the color of the LED on the Hub via RGB values.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| red | <code>number</code> |
| green | <code>number</code> |
| blue | <code>number</code> |
<a name="WeDo2Hub+setMotorSpeed"></a>
### weDo2Hub.setMotorSpeed(port, speed)
Set the motor speed on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| 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. |
<a name="Hub+connect"></a>
### weDo2Hub.connect([callback])
Connect to the Hub.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Overrides**: [<code>connect</code>](#Hub+connect)
| Param | Type |
| --- | --- |
| [callback] | <code>function</code> |
<a name="Hub+subscribe"></a>
### weDo2Hub.subscribe(port, [mode])
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| port | <code>string</code> | | |
| [mode] | <code>number</code> \| <code>boolean</code> | <code>false</code> | The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
<a name="Hub+unsubscribe"></a>
### weDo2Hub.unsubscribe(port)
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
<a name="WeDo2Hub+event_button"></a>
### "button" (state)
Emits when a button is pressed.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| state | <code>number</code> | A number representing one of the button state consts. |
<a name="WeDo2Hub+event_distance"></a>
### "distance" (port, distance)
Emits when a distance sensor is activated.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| distance | <code>number</code> | Distance, in millimeters. |
<a name="WeDo2Hub+event_color"></a>
### "color" (port, color)
Emits when a color sensor is activated.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="WeDo2Hub+event_tilt"></a>
### "tilt" (port, x, y)
Emits when a tilt sensor is activated.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
| x | <code>number</code> |
| y | <code>number</code> |
<a name="WeDo2Hub+event_rotate"></a>
### "rotate" (port, rotation)
Emits when a rotation sensor is activated.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
| rotation | <code>number</code> |
<a name="BoostHub"></a> <a name="BoostHub"></a>
## BoostHub ⇐ [<code>Hub</code>](#Hub) ## BoostHub ⇐ [<code>Hub</code>](#Hub)
@ -384,3 +234,153 @@ Emits when a rotation sensor is activated.
| port | <code>string</code> | | port | <code>string</code> |
| rotation | <code>number</code> | | rotation | <code>number</code> |
<a name="WeDo2Hub"></a>
## WeDo2Hub ⇐ [<code>Hub</code>](#Hub)
**Kind**: global class
**Extends**: [<code>Hub</code>](#Hub)
* [WeDo2Hub](#WeDo2Hub) ⇐ [<code>Hub</code>](#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)
<a name="WeDo2Hub+setLEDColor"></a>
### weDo2Hub.setLEDColor(color)
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="WeDo2Hub+setLEDRGB"></a>
### weDo2Hub.setLEDRGB(red, green, blue)
Set the color of the LED on the Hub via RGB values.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| red | <code>number</code> |
| green | <code>number</code> |
| blue | <code>number</code> |
<a name="WeDo2Hub+setMotorSpeed"></a>
### weDo2Hub.setMotorSpeed(port, speed)
Set the motor speed on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| 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. |
<a name="Hub+connect"></a>
### weDo2Hub.connect([callback])
Connect to the Hub.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Overrides**: [<code>connect</code>](#Hub+connect)
| Param | Type |
| --- | --- |
| [callback] | <code>function</code> |
<a name="Hub+subscribe"></a>
### weDo2Hub.subscribe(port, [mode])
Subscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| port | <code>string</code> | | |
| [mode] | <code>number</code> \| <code>boolean</code> | <code>false</code> | The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. |
<a name="Hub+unsubscribe"></a>
### weDo2Hub.unsubscribe(port)
Unsubscribe to sensor notifications on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
<a name="WeDo2Hub+event_button"></a>
### "button" (state)
Emits when a button is pressed.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| state | <code>number</code> | A number representing one of the button state consts. |
<a name="WeDo2Hub+event_distance"></a>
### "distance" (port, distance)
Emits when a distance sensor is activated.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| distance | <code>number</code> | Distance, in millimeters. |
<a name="WeDo2Hub+event_color"></a>
### "color" (port, color)
Emits when a color sensor is activated.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| color | <code>number</code> | A number representing one of the LED color consts. |
<a name="WeDo2Hub+event_tilt"></a>
### "tilt" (port, x, y)
Emits when a tilt sensor is activated.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
| x | <code>number</code> |
| y | <code>number</code> |
<a name="WeDo2Hub+event_rotate"></a>
### "rotate" (port, rotation)
Emits when a rotation sensor is activated.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
| rotation | <code>number</code> |

View File

@ -3,9 +3,10 @@ import { Peripheral } from "noble";
import { Hub } from "./hub.js"; import { Hub } from "./hub.js";
import { Port } from "./port.js"; import { Port } from "./port.js";
import * as Consts from "./consts";
import Debug = require("debug"); import Debug = require("debug");
const debug = Debug("lpf2"); const debug = Debug("lpf2");
import { Consts } from "./consts.js";
/** /**
@ -16,7 +17,7 @@ export class BoostHub extends Hub {
public static IsBoostHub (peripheral: Peripheral) { 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) { public connect (callback: () => void) {
debug("Connecting to Boost Move Hub"); debug("Connecting to Boost Move Hub");
super.connect(() => { 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)); this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]));
debug("Connect completed"); debug("Connect completed");
@ -59,7 +60,7 @@ export class BoostHub extends Hub {
* @param {number} color - A number representing one of the LED color consts. * @param {number} color - A number representing one of the LED color consts.
*/ */
public setLEDColor (color: number | boolean) { public setLEDColor (color: number | boolean) {
const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL]; const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (characteristic) { if (characteristic) {
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]); let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
characteristic.write(data); 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. * @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) { 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 (characteristic) {
if (time) { if (time) {
const data = Buffer.from([0x0c, 0x00, 0x81, this._ports[port].value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]); 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. * @param {number} [speed=100] - How fast the motor should be rotated.
*/ */
public setMotorAngle (port: string, angle: number, speed: number = 100) { 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) { if (characteristic) {
const data = Buffer.from([0x0e, 0x00, 0x81, this._ports[port].value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]); 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); 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) { 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) { if (characteristic) {
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); 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) { 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) { if (characteristic) {
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); 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 * @event BoostHub#button
* @param {number} state - A number representing one of the button state consts. * @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; return;
} else if (data[5] === 0) { } else if (data[5] === 0) {
this.emit("button", Consts.Button.RELEASED); this.emit("button", Consts.ButtonStates.RELEASED);
return; return;
} }
} }

123
consts.ts
View File

@ -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 = { export enum Devices {
Hubs: { BASIC_MOTOR = 1,
UNKNOWN: 0, BOOST_LED = 22,
WEDO2_SMART_HUB: 1, WEDO2_TILT = 34,
BOOST_MOVE_HUB: 2, WEDO2_DISTANCE = 35,
POWERED_UP_HUB: 3, BOOST_DISTANCE = 37,
POWERED_UP_REMOTE: 4 BOOST_INTERACTIVE_MOTOR = 38,
}, BOOST_MOVE_HUB_MOTOR = 39,
Devices: { BOOST_TILT = 40
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"
} }
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"
} }

7
hub.ts
View File

@ -3,9 +3,10 @@ import { EventEmitter } from "events";
import { Characteristic, Peripheral, Service } from "noble"; import { Characteristic, Peripheral, Service } from "noble";
import { Port } from "./port"; import { Port } from "./port";
import * as Consts from "./consts";
import Debug = require("debug"); import Debug = require("debug");
const debug = Debug("lpf2"); const debug = Debug("lpf2");
import { Consts } from "./consts.js";
/** /**
@ -16,7 +17,7 @@ export class Hub extends EventEmitter {
public autoSubscribe: boolean; public autoSubscribe: boolean;
public type: number = Consts.Hubs.UNKNOWN; public type: Consts.Hubs = Consts.Hubs.UNKNOWN;
public uuid: string; public uuid: string;
protected _ports: any = {}; protected _ports: any = {};
@ -171,7 +172,7 @@ export class Hub extends EventEmitter {
} }
private _getModeForDeviceType (type: number) { private _getModeForDeviceType (type: Consts.Devices) {
switch (type) { switch (type) {
case Consts.Devices.BASIC_MOTOR: case Consts.Devices.BASIC_MOTOR:
return 0x02; return 0x02;

View File

@ -6,7 +6,7 @@
"types": "dist/lpf2.d.ts", "types": "dist/lpf2.d.ts",
"scripts": { "scripts": {
"build": "tslint -c tslint.json \"*.ts\" && tsc", "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 <me@nathankunicki.com>", "author": "Nathan Kunicki <me@nathankunicki.com>",
"license": "MIT", "license": "MIT",

View File

@ -3,9 +3,10 @@ import { Peripheral } from "noble";
import { Hub } from "./hub.js"; import { Hub } from "./hub.js";
import { Port } from "./port.js"; import { Port } from "./port.js";
import * as Consts from "./consts";
import Debug = require("debug"); import Debug = require("debug");
const debug = Debug("lpf2"); const debug = Debug("lpf2");
import { Consts } from "./consts.js";
/** /**
@ -16,7 +17,7 @@ export class WeDo2Hub extends Hub {
public static IsWeDo2Hub (peripheral: Peripheral) { public static IsWeDo2Hub (peripheral: Peripheral) {
return (peripheral.advertisement.localName === Consts.BLE.Name.WEDO2_SMART_HUB_NAME && peripheral.advertisement.serviceUuids.indexOf(Consts.BLE.Services.WEDO2_SMART_HUB) >= 0); return (peripheral.advertisement.localName === Consts.BLENames.WEDO2_SMART_HUB_NAME && peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.WEDO2_SMART_HUB) >= 0);
} }
@ -38,9 +39,9 @@ export class WeDo2Hub extends Hub {
public connect (callback: () => void) { public connect (callback: () => void) {
debug("Connecting to WeDo 2.0 Smart Hub"); debug("Connecting to WeDo 2.0 Smart Hub");
super.connect(() => { super.connect(() => {
this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE], this._parsePortMessage.bind(this)); this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE], this._parsePortMessage.bind(this));
this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.SENSOR_VALUE], this._parseSensorMessage.bind(this)); this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_SENSOR_VALUE], this._parseSensorMessage.bind(this));
this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.BUTTON], this._parseSensorMessage.bind(this)); this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_BUTTON], this._parseSensorMessage.bind(this));
debug("Connect completed"); debug("Connect completed");
if (callback) { if (callback) {
callback(); callback();
@ -55,8 +56,8 @@ export class WeDo2Hub extends Hub {
* @param {number} color - A number representing one of the LED color consts. * @param {number} color - A number representing one of the LED color consts.
*/ */
public setLEDColor (color: number | boolean) { public setLEDColor (color: number | boolean) {
const motorCharacteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.MOTOR_VALUE_WRITE]; const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
const portCharacteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE_WRITE]; const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
if (motorCharacteristic && portCharacteristic) { if (motorCharacteristic && portCharacteristic) {
let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); let data = Buffer.from([0x06, 0x17, 0x01, 0x01]);
portCharacteristic.write(data); portCharacteristic.write(data);
@ -77,8 +78,8 @@ export class WeDo2Hub extends Hub {
* @param {number} blue * @param {number} blue
*/ */
public setLEDRGB (red: number, green: number, blue: number) { public setLEDRGB (red: number, green: number, blue: number) {
const motorCharacteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.MOTOR_VALUE_WRITE]; const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
const portCharacteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE_WRITE]; const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
if (motorCharacteristic && portCharacteristic) { if (motorCharacteristic && portCharacteristic) {
const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]); const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]);
portCharacteristic.write(data1); portCharacteristic.write(data1);
@ -95,7 +96,7 @@ export class WeDo2Hub extends Hub {
* @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} speed - For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
*/ */
public setMotorSpeed (port: string, speed: number) { public setMotorSpeed (port: string, speed: number) {
const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.MOTOR_VALUE_WRITE]; const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
if (characteristic) { if (characteristic) {
characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed])); characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]));
} }
@ -103,7 +104,7 @@ export class WeDo2Hub extends Hub {
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE_WRITE]; const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
if (characteristic) { if (characteristic) {
characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback);
} }
@ -111,7 +112,7 @@ export class WeDo2Hub extends Hub {
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE_WRITE]; const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
if (characteristic) { if (characteristic) {
characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback);
} }
@ -158,10 +159,10 @@ export class WeDo2Hub extends Hub {
* @event WeDo2Hub#button * @event WeDo2Hub#button
* @param {number} state - A number representing one of the button state consts. * @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; return;
} else if (data[0] === 0) { } else if (data[0] === 0) {
this.emit("button", Consts.Button.RELEASED); this.emit("button", Consts.ButtonStates.RELEASED);
return; return;
} }