diff --git a/DOCS.md b/DOCS.md index 8b443d5..f3f59f0 100644 --- a/DOCS.md +++ b/DOCS.md @@ -18,13 +18,13 @@ ## Typedefs
-
Devices
+
Device
-
Colors
+
Color
-
ButtonStates
+
ButtonState
-
DuploTrainBaseSounds
+
DuploTrainBaseSound
@@ -183,9 +183,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -332,10 +332,10 @@ Emits when a button is pressed. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the ButtonStates. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -356,10 +356,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -393,10 +393,10 @@ Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the Devices.. | +| Param | Type | +| --- | --- | +| port | string | +| type | [Device](#Device) | @@ -585,9 +585,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -683,10 +683,10 @@ Emits when a button is pressed. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the ButtonStates. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -707,10 +707,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED Colors | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -722,7 +722,7 @@ A combined color and distance event, emits when the sensor is activated. | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED Colors | +| color | [Color](#Color) | | | distance | number | Distance, in millimeters. | @@ -758,10 +758,10 @@ Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) **Overrides**: [attach](#Hub+event_attach) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the Devices.. | +| Param | Type | +| --- | --- | +| port | string | +| type | [Device](#Device) | @@ -935,9 +935,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [PUPHub](#PUPHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1033,10 +1033,10 @@ Emits when a button is pressed. **Kind**: event emitted by [PUPHub](#PUPHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the ButtonStates. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -1057,10 +1057,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [PUPHub](#PUPHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED Colors | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -1072,7 +1072,7 @@ A combined color and distance event, emits when the sensor is activated. | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED Colors | +| color | [Color](#Color) | | | distance | number | Distance, in millimeters. | @@ -1096,10 +1096,10 @@ Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [PUPHub](#PUPHub) **Overrides**: [attach](#Hub+event_attach) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the Devices.. | +| Param | Type | +| --- | --- | +| port | string | +| type | [Device](#Device) | @@ -1211,9 +1211,9 @@ Set the color of the LED on the Remote via a color value. **Overrides**: [setLEDColor](#LPF2Hub+setLEDColor) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1322,10 +1322,10 @@ Emits when a button is pressed. **Kind**: event emitted by [PUPRemote](#PUPRemote) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the ButtonStates. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -1337,7 +1337,7 @@ A combined color and distance event, emits when the sensor is activated. | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED Colors | +| color | [Color](#Color) | | | distance | number | Distance, in millimeters. | @@ -1442,9 +1442,9 @@ Set the color of the LED on the train via a color value. **Overrides**: [setLEDColor](#LPF2Hub+setLEDColor) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1468,9 +1468,9 @@ Play a built-in train sound. **Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| sound | number | A number representing one of the DuploTrainBaseSounds. | +| Param | Type | +| --- | --- | +| sound | [DuploTrainBaseSound](#DuploTrainBaseSound) | @@ -1593,10 +1593,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED Colors | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -1608,7 +1608,7 @@ A combined color and distance event, emits when the sensor is activated. | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED Colors | +| color | [Color](#Color) | | | distance | number | Distance, in millimeters. | @@ -1623,9 +1623,9 @@ Emits on a speed change. | port | string | | speed | number | - + -## Devices +## Device **Kind**: global typedef **Properties** @@ -1648,9 +1648,9 @@ Emits on a speed change. | DUPLO_TRAIN_BASE_SPEEDOMETER | number | 44 | | POWERED_UP_REMOTE_BUTTON | number | 55 | - + -## Colors +## Color **Kind**: global typedef **Properties** @@ -1669,9 +1669,9 @@ Emits on a speed change. | WHITE | number | 10 | | NONE | number | 255 | - + -## ButtonStates +## ButtonState **Kind**: global typedef **Properties** @@ -1683,9 +1683,9 @@ Emits on a speed change. | DOWN | number | 3 | | STOP | number | 4 | - + -## DuploTrainBaseSounds +## DuploTrainBaseSound **Kind**: global typedef **Properties** diff --git a/README.md b/README.md index 70d8355..d050856 100644 --- a/README.md +++ b/README.md @@ -101,13 +101,13 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R ## Typedefs
-
Devices
+
Device
-
Colors
+
Color
-
ButtonStates
+
ButtonState
-
DuploTrainBaseSounds
+
DuploTrainBaseSound
@@ -266,9 +266,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -415,10 +415,10 @@ Emits when a button is pressed. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the ButtonStates. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -439,10 +439,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -476,10 +476,10 @@ Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [WeDo2SmartHub](#WeDo2SmartHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the Devices.. | +| Param | Type | +| --- | --- | +| port | string | +| type | [Device](#Device) | @@ -668,9 +668,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [BoostMoveHub](#BoostMoveHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -766,10 +766,10 @@ Emits when a button is pressed. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the ButtonStates. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -790,10 +790,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED Colors | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -805,7 +805,7 @@ A combined color and distance event, emits when the sensor is activated. | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED Colors | +| color | [Color](#Color) | | | distance | number | Distance, in millimeters. | @@ -841,10 +841,10 @@ Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [BoostMoveHub](#BoostMoveHub) **Overrides**: [attach](#Hub+event_attach) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the Devices.. | +| Param | Type | +| --- | --- | +| port | string | +| type | [Device](#Device) | @@ -1018,9 +1018,9 @@ Set the color of the LED on the Hub via a color value. **Kind**: instance method of [PUPHub](#PUPHub) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1116,10 +1116,10 @@ Emits when a button is pressed. **Kind**: event emitted by [PUPHub](#PUPHub) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the ButtonStates. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -1140,10 +1140,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [PUPHub](#PUPHub) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED Colors | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -1155,7 +1155,7 @@ A combined color and distance event, emits when the sensor is activated. | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED Colors | +| color | [Color](#Color) | | | distance | number | Distance, in millimeters. | @@ -1179,10 +1179,10 @@ Emits when a motor or sensor is attached to the Hub. **Kind**: event emitted by [PUPHub](#PUPHub) **Overrides**: [attach](#Hub+event_attach) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| type | number | A number representing one of the Devices.. | +| Param | Type | +| --- | --- | +| port | string | +| type | [Device](#Device) | @@ -1294,9 +1294,9 @@ Set the color of the LED on the Remote via a color value. **Overrides**: [setLEDColor](#LPF2Hub+setLEDColor) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1405,10 +1405,10 @@ Emits when a button is pressed. **Kind**: event emitted by [PUPRemote](#PUPRemote) -| Param | Type | Description | -| --- | --- | --- | -| button | string | | -| state | number | A number representing one of the ButtonStates. | +| Param | Type | +| --- | --- | +| button | string | +| state | [ButtonState](#ButtonState) | @@ -1420,7 +1420,7 @@ A combined color and distance event, emits when the sensor is activated. | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED Colors | +| color | [Color](#Color) | | | distance | number | Distance, in millimeters. | @@ -1525,9 +1525,9 @@ Set the color of the LED on the train via a color value. **Overrides**: [setLEDColor](#LPF2Hub+setLEDColor) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| color | number | A number representing one of the LED Colors. | +| Param | Type | +| --- | --- | +| color | [Color](#Color) | @@ -1551,9 +1551,9 @@ Play a built-in train sound. **Kind**: instance method of [DuploTrainBase](#DuploTrainBase) **Returns**: Promise - Resolved upon successful issuance of command. -| Param | Type | Description | -| --- | --- | --- | -| sound | number | A number representing one of the DuploTrainBaseSounds. | +| Param | Type | +| --- | --- | +| sound | [DuploTrainBaseSound](#DuploTrainBaseSound) | @@ -1676,10 +1676,10 @@ Emits when a color sensor is activated. **Kind**: event emitted by [DuploTrainBase](#DuploTrainBase) -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| color | number | A number representing one of the LED Colors | +| Param | Type | +| --- | --- | +| port | string | +| color | [Color](#Color) | @@ -1691,7 +1691,7 @@ A combined color and distance event, emits when the sensor is activated. | Param | Type | Description | | --- | --- | --- | | port | string | | -| color | number | A number representing one of the LED Colors | +| color | [Color](#Color) | | | distance | number | Distance, in millimeters. | @@ -1706,9 +1706,9 @@ Emits on a speed change. | port | string | | speed | number | - + -## Devices +## Device **Kind**: global typedef **Properties** @@ -1731,9 +1731,9 @@ Emits on a speed change. | DUPLO_TRAIN_BASE_SPEEDOMETER | number | 44 | | POWERED_UP_REMOTE_BUTTON | number | 55 | - + -## Colors +## Color **Kind**: global typedef **Properties** @@ -1752,9 +1752,9 @@ Emits on a speed change. | WHITE | number | 10 | | NONE | number | 255 | - + -## ButtonStates +## ButtonState **Kind**: global typedef **Properties** @@ -1766,9 +1766,9 @@ Emits on a speed change. | DOWN | number | 3 | | STOP | number | 4 | - + -## DuploTrainBaseSounds +## DuploTrainBaseSound **Kind**: global typedef **Properties** diff --git a/boostmovehub.ts b/boostmovehub.ts index 3a04fd8..8d0ad09 100644 --- a/boostmovehub.ts +++ b/boostmovehub.ts @@ -27,13 +27,13 @@ export class BoostMoveHub extends LPF2Hub { public static IsBoostMoveHub (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID); } constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hubs.BOOST_MOVE_HUB; + this.type = Consts.Hub.BOOST_MOVE_HUB; this._ports = { "A": new Port("A", 55), "B": new Port("B", 56), @@ -82,7 +82,7 @@ export class BoostMoveHub extends LPF2Hub { return new Promise((resolve, reject) => { if (time && typeof time === "number") { - if (portObj.type === Consts.Devices.BOOST_TACHO_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) { + if (portObj.type === Consts.Device.BOOST_TACHO_MOTOR || portObj.type === Consts.Device.BOOST_MOVE_HUB_MOTOR) { portObj.busy = true; let data = null; if (portObj.id === "AB") { @@ -92,17 +92,17 @@ export class BoostMoveHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); // @ts-ignore: The type of time is properly checked at the start }, time); @@ -111,7 +111,7 @@ export class BoostMoveHub extends LPF2Hub { } else { - if (portObj.type === Consts.Devices.BOOST_TACHO_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) { + if (portObj.type === Consts.Device.BOOST_TACHO_MOTOR || portObj.type === Consts.Device.BOOST_MOVE_HUB_MOTOR) { portObj.busy = true; let data = null; if (portObj.id === "AB") { @@ -120,14 +120,14 @@ export class BoostMoveHub extends LPF2Hub { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); } } @@ -167,7 +167,7 @@ export class BoostMoveHub extends LPF2Hub { */ public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) { const portObj = this._portLookup(port); - if (!(portObj.type === Consts.Devices.BOOST_TACHO_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR)) { + if (!(portObj.type === Consts.Device.BOOST_TACHO_MOTOR || portObj.type === Consts.Device.BOOST_MOVE_HUB_MOTOR)) { throw new Error("Angle rotation is only available when using a Boost Tacho Motor or Boost Move Hub Motor"); } if (portObj.id !== "AB" && speed instanceof Array) { @@ -184,7 +184,7 @@ export class BoostMoveHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt32LE(angle, 4); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; @@ -205,11 +205,11 @@ export class BoostMoveHub extends LPF2Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); diff --git a/consts.ts b/consts.ts index 478eade..dc48c68 100644 --- a/consts.ts +++ b/consts.ts @@ -1,4 +1,4 @@ -export enum Hubs { +export enum Hub { UNKNOWN = 0, WEDO2_SMART_HUB = 1, BOOST_MOVE_HUB = 2, @@ -9,7 +9,7 @@ export enum Hubs { /** - * @typedef Devices + * @typedef Device * @property {number} UNKNOWN 0 * @property {number} BASIC_MOTOR 1 * @property {number} TRAIN_MOTOR 2 @@ -27,7 +27,7 @@ export enum Hubs { * @property {number} DUPLO_TRAIN_BASE_SPEEDOMETER 44 * @property {number} POWERED_UP_REMOTE_BUTTON 55 */ -export enum Devices { +export enum Device { UNKNOWN = 0, BASIC_MOTOR = 1, TRAIN_MOTOR = 2, @@ -48,7 +48,7 @@ export enum Devices { /** - * @typedef Colors + * @typedef Color * @property {number} BLACK 0 * @property {number} PINK 1 * @property {number} PURPLE 2 @@ -62,7 +62,7 @@ export enum Devices { * @property {number} WHITE 10 * @property {number} NONE 255 */ -export enum Colors { +export enum Color { BLACK = 0, PINK = 1, PURPLE = 2, @@ -79,14 +79,14 @@ export enum Colors { /** - * @typedef ButtonStates + * @typedef ButtonState * @property {number} PRESSED 0 * @property {number} RELEASED 1 * @property {number} UP 2 * @property {number} DOWN 3 * @property {number} STOP 4 */ -export enum ButtonStates { +export enum ButtonState { PRESSED = 0, RELEASED = 1, UP = 2, @@ -96,14 +96,14 @@ export enum ButtonStates { /** - * @typedef DuploTrainBaseSounds + * @typedef DuploTrainBaseSound * @property {number} BRAKE 3 * @property {number} STATION_DEPARTURE 5 * @property {number} WATER_REFILL 7 * @property {number} HORN 9 * @property {number} STEAM 10 */ -export enum DuploTrainBaseSounds { +export enum DuploTrainBaseSound { BREAK = 3, STATION_DEPARTURE = 5, WATER_REFILL = 7, @@ -120,13 +120,13 @@ export enum BLEManufacturerData { } -export enum BLEServices { +export enum BLEService { WEDO2_SMART_HUB = "00001523-1212-efde-1523-785feabcd123", LPF2_HUB = "00001623-1212-efde-1623-785feabcd123" } -export enum BLECharacteristics { +export enum BLECharacteristic { WEDO2_BATTERY = "2a19", WEDO2_BUTTON = "00001526-1212-efde-1523-785feabcd123", // "1526" WEDO2_PORT_TYPE = "00001527-1212-efde-1523-785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub diff --git a/duplotrainbase.ts b/duplotrainbase.ts index 21ae00f..8581b92 100644 --- a/duplotrainbase.ts +++ b/duplotrainbase.ts @@ -52,13 +52,13 @@ export class DuploTrainBase extends LPF2Hub { public static IsDuploTrainBase (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID); } constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hubs.DUPLO_TRAIN_HUB; + this.type = Consts.Hub.DUPLO_TRAIN_HUB; this._ports = { "MOTOR": new Port("MOTOR", 0), "COLOR": new Port("COLOR", 18), @@ -81,7 +81,7 @@ export class DuploTrainBase extends LPF2Hub { /** * Set the color of the LED on the train via a color value. * @method DuploTrainBase#setLEDColor - * @param {number} color A number representing one of the LED Colors. + * @param {Color} color * @returns {Promise} Resolved upon successful issuance of command. */ public setLEDColor (color: number | boolean) { @@ -90,7 +90,7 @@ export class DuploTrainBase extends LPF2Hub { color = 0; } const data = Buffer.from([0x81, 0x11, 0x11, 0x51, 0x00, color]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -119,16 +119,16 @@ export class DuploTrainBase extends LPF2Hub { return new Promise((resolve, reject) => { if (time && typeof time === "number") { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); } else { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); } }); @@ -138,13 +138,13 @@ export class DuploTrainBase extends LPF2Hub { /** * Play a built-in train sound. * @method DuploTrainBase#playSound - * @param {number} sound A number representing one of the DuploTrainBaseSounds. + * @param {DuploTrainBaseSound} sound * @returns {Promise} Resolved upon successful issuance of command. */ public playSound (sound: number) { return new Promise((resolve, reject) => { const data = Buffer.from([0x81, 0x01, 0x11, 0x51, 0x01, sound]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } diff --git a/hub.ts b/hub.ts index e462875..6763015 100644 --- a/hub.ts +++ b/hub.ts @@ -19,7 +19,7 @@ export class Hub extends EventEmitter { public autoSubscribe: boolean = true; public useSpeedMap: boolean = true; - public type: Consts.Hubs = Consts.Hubs.UNKNOWN; + public type: Consts.Hub = Consts.Hub.UNKNOWN; protected _ports: {[port: string]: Port} = {}; protected _characteristics: {[uuid: string]: Characteristic} = {}; @@ -257,12 +257,12 @@ export class Hub extends EventEmitter { * Emits when a motor or sensor is attached to the Hub. * @event Hub#attach * @param {string} port - * @param {number} type A number representing one of the Devices.. + * @param {Device} type */ this.emit("attach", port.id, type); } } else { - port.type = Consts.Devices.UNKNOWN; + port.type = Consts.Device.UNKNOWN; debug(`Port ${port.id} disconnected`); /** * Emits when an attached motor or sensor is detached from the Hub. @@ -349,19 +349,19 @@ export class Hub extends EventEmitter { } - private _getModeForDeviceType (type: Consts.Devices) { + private _getModeForDeviceType (type: Consts.Device) { switch (type) { - case Consts.Devices.BASIC_MOTOR: + case Consts.Device.BASIC_MOTOR: return 0x02; - case Consts.Devices.TRAIN_MOTOR: + case Consts.Device.TRAIN_MOTOR: return 0x02; - case Consts.Devices.BOOST_TACHO_MOTOR: + case Consts.Device.BOOST_TACHO_MOTOR: return 0x02; - case Consts.Devices.BOOST_MOVE_HUB_MOTOR: + case Consts.Device.BOOST_MOVE_HUB_MOTOR: return 0x02; - case Consts.Devices.BOOST_DISTANCE: - return (this.type === Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08); - case Consts.Devices.BOOST_TILT: + case Consts.Device.BOOST_DISTANCE: + return (this.type === Consts.Hub.WEDO2_SMART_HUB ? 0x00 : 0x08); + case Consts.Device.BOOST_TILT: return 0x04; default: return 0x00; diff --git a/lpf2hub.ts b/lpf2hub.ts index 08d74b8..1d19681 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -37,13 +37,13 @@ export class LPF2Hub extends Hub { public connect () { return new Promise(async (resolve, reject) => { await super.connect(); - const characteristic = this._getCharacteristic(Consts.BLECharacteristics.LPF2_ALL); + const characteristic = this._getCharacteristic(Consts.BLECharacteristic.LPF2_ALL); this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports - if (this.type === Consts.Hubs.DUPLO_TRAIN_HUB) { - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports + if (this.type === Consts.Hub.DUPLO_TRAIN_HUB) { + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); } return resolve(); }); @@ -64,8 +64,8 @@ export class LPF2Hub extends Hub { let data = Buffer.from([0x01, 0x01, 0x01]); data = Buffer.concat([data, Buffer.from(name, "ascii")]); // Send this twice, as sometimes the first time doesn't take - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); this._name = name; return resolve(); }); @@ -75,18 +75,18 @@ export class LPF2Hub extends Hub { /** * Set the color of the LED on the Hub via a color value. * @method LPF2Hub#setLEDColor - * @param {number} color A number representing one of the LED Colors. + * @param {Color} color * @returns {Promise} Resolved upon successful issuance of command. */ public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, 0x32, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); if (color === false) { color = 0; } data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x00, color]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -103,21 +103,21 @@ export class LPF2Hub extends Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, 0x32, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x01, red, green, blue]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); } protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); } @@ -186,12 +186,12 @@ export class LPF2Hub extends Hub { * Emits when a button is pressed. * @event LPF2Hub#button * @param {string} button - * @param {number} state A number representing one of the ButtonStates. + * @param {ButtonState} state */ - this.emit("button", "GREEN", Consts.ButtonStates.PRESSED); + this.emit("button", "GREEN", Consts.ButtonState.PRESSED); return; } else if (data[5] === 0) { - this.emit("button", "GREEN", Consts.ButtonStates.RELEASED); + this.emit("button", "GREEN", Consts.ButtonState.RELEASED); return; } } @@ -242,12 +242,12 @@ export class LPF2Hub extends Hub { private _parseSensorMessage (data: Buffer) { - if ((data[3] === 0x3b && this.type === Consts.Hubs.POWERED_UP_REMOTE) || (data[3] === 0x3c && this.type !== Consts.Hubs.POWERED_UP_REMOTE)) { // Voltage + if ((data[3] === 0x3b && this.type === Consts.Hub.POWERED_UP_REMOTE) || (data[3] === 0x3c && this.type !== Consts.Hub.POWERED_UP_REMOTE)) { // Voltage data = this._padMessage(data, 6); const batteryLevel = (data.readUInt16LE(4) / 4096) * 100; this._batteryLevel = Math.floor(batteryLevel); return; - } else if (data[3] === 0x3b && this.type !== Consts.Hubs.POWERED_UP_REMOTE) { // Current (Non-PUP Remote) + } else if (data[3] === 0x3b && this.type !== Consts.Hub.POWERED_UP_REMOTE) { // Current (Non-PUP Remote) data = this._padMessage(data, 6); const current = data.readUInt16LE(4) / 4096; this._current = current * 100; @@ -267,7 +267,7 @@ export class LPF2Hub extends Hub { if (port && port.connected) { switch (port.type) { - case Consts.Devices.WEDO2_DISTANCE: + case Consts.Device.WEDO2_DISTANCE: { let distance = data[4]; if (data[5] === 1) { @@ -282,14 +282,14 @@ export class LPF2Hub extends Hub { this.emit("distance", port.id, distance * 10); break; } - case Consts.Devices.BOOST_DISTANCE: + case Consts.Device.BOOST_DISTANCE: { /** * Emits when a color sensor is activated. * @event LPF2Hub#color * @param {string} port - * @param {number} color A number representing one of the LED Colors + * @param {Color} color */ if (data[4] <= 10) { this.emit("color", port.id, data[4]); @@ -310,7 +310,7 @@ export class LPF2Hub extends Hub { * A combined color and distance event, emits when the sensor is activated. * @event LPF2Hub#colorAndDistance * @param {string} port - * @param {number} color A number representing one of the LED Colors + * @param {Color} color * @param {number} distance Distance, in millimeters. */ if (data[4] <= 10) { @@ -318,7 +318,7 @@ export class LPF2Hub extends Hub { } break; } - case Consts.Devices.WEDO2_TILT: + case Consts.Device.WEDO2_TILT: { const tiltX = data[4] > 160 ? data[4] - 255 : data[4] - (data[4] * 2); const tiltY = data[5] > 160 ? 255 - data[5] : data[5] - (data[5] * 2); @@ -334,7 +334,7 @@ export class LPF2Hub extends Hub { this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY); break; } - case Consts.Devices.BOOST_TACHO_MOTOR: + case Consts.Device.BOOST_TACHO_MOTOR: { const rotation = data.readInt32LE(4); /** @@ -346,53 +346,53 @@ export class LPF2Hub extends Hub { this.emit("rotate", port.id, rotation); break; } - case Consts.Devices.BOOST_MOVE_HUB_MOTOR: + case Consts.Device.BOOST_MOVE_HUB_MOTOR: { const rotation = data.readInt32LE(4); this.emit("rotate", port.id, rotation); break; } - case Consts.Devices.BOOST_TILT: + case Consts.Device.BOOST_TILT: { const tiltX = data[4] > 160 ? data[4] - 255 : data[4]; const tiltY = data[5] > 160 ? 255 - data[5] : data[5] - (data[5] * 2); this.emit("tilt", port.id, tiltX, tiltY); break; } - case Consts.Devices.POWERED_UP_REMOTE_BUTTON: + case Consts.Device.POWERED_UP_REMOTE_BUTTON: { switch (data[4]) { case 0x01: { - this.emit("button", port.id, Consts.ButtonStates.UP); + this.emit("button", port.id, Consts.ButtonState.UP); break; } case 0xff: { - this.emit("button", port.id, Consts.ButtonStates.DOWN); + this.emit("button", port.id, Consts.ButtonState.DOWN); break; } case 0x7f: { - this.emit("button", port.id, Consts.ButtonStates.STOP); + this.emit("button", port.id, Consts.ButtonState.STOP); break; } case 0x00: { - this.emit("button", port.id, Consts.ButtonStates.RELEASED); + this.emit("button", port.id, Consts.ButtonState.RELEASED); break; } } break; } - case Consts.Devices.DUPLO_TRAIN_BASE_COLOR: + case Consts.Device.DUPLO_TRAIN_BASE_COLOR: { if (data[4] <= 10) { this.emit("color", port.id, data[4]); } break; } - case Consts.Devices.DUPLO_TRAIN_BASE_SPEEDOMETER: + case Consts.Device.DUPLO_TRAIN_BASE_SPEEDOMETER: { /** * Emits on a speed change. diff --git a/port.ts b/port.ts index 508f703..20d1274 100644 --- a/port.ts +++ b/port.ts @@ -6,7 +6,7 @@ export class Port { public id: string; public value: number; - public type: Consts.Devices; + public type: Consts.Device; public connected: boolean = false; public busy: boolean = false; public finished: (() => void) | null = null; @@ -16,7 +16,7 @@ export class Port { constructor (id: string, value: number) { this.id = id; this.value = value; - this.type = Consts.Devices.UNKNOWN; + this.type = Consts.Device.UNKNOWN; } public cancelEventTimer () { diff --git a/poweredup.ts b/poweredup.ts index 16b2058..942d99b 100644 --- a/poweredup.ts +++ b/poweredup.ts @@ -22,7 +22,7 @@ let wantScan = false; const startScanning = () => { if (isBrowserContext) { - noble.startScanning([Consts.BLEServices.WEDO2_SMART_HUB, Consts.BLEServices.LPF2_HUB]); + noble.startScanning([Consts.BLEService.WEDO2_SMART_HUB, Consts.BLEService.LPF2_HUB]); } else { noble.startScanning(); } diff --git a/puphub.ts b/puphub.ts index 1b6c726..360b770 100644 --- a/puphub.ts +++ b/puphub.ts @@ -32,13 +32,13 @@ export class PUPHub extends LPF2Hub { public static IsPUPHub (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID); } constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hubs.POWERED_UP_HUB; + this.type = Consts.Hub.POWERED_UP_HUB; this._ports = { "A": new Port("A", 0), "B": new Port("B", 1), @@ -97,7 +97,7 @@ export class PUPHub extends LPF2Hub { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]); } - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { let data = null; if (portObj.id === "AB") { @@ -105,7 +105,7 @@ export class PUPHub extends LPF2Hub { } else { data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]); } - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); @@ -117,7 +117,7 @@ export class PUPHub extends LPF2Hub { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]); } - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); } }); @@ -159,11 +159,11 @@ export class PUPHub extends LPF2Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); diff --git a/pupremote.ts b/pupremote.ts index 82e790f..4659648 100644 --- a/pupremote.ts +++ b/pupremote.ts @@ -57,13 +57,13 @@ export class PUPRemote extends LPF2Hub { public static IsPUPRemote (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID); } constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hubs.POWERED_UP_REMOTE; + this.type = Consts.Hub.POWERED_UP_REMOTE; this._ports = { "LEFT": new Port("LEFT", 0), "RIGHT": new Port("RIGHT", 1) @@ -85,18 +85,18 @@ export class PUPRemote extends LPF2Hub { /** * Set the color of the LED on the Remote via a color value. * @method PUPRemote#setLEDColor - * @param {number} color A number representing one of the LED Colors. + * @param {Color} color * @returns {Promise} Resolved upon successful issuance of command. */ public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, 0x34, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); if (color === false) { color = 0; } data = Buffer.from([0x81, 0x34, 0x11, 0x51, 0x00, color]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -113,9 +113,9 @@ export class PUPRemote extends LPF2Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, 0x34, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); data = Buffer.from([0x81, 0x34, 0x11, 0x51, 0x01, red, green, blue]); - this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } diff --git a/wedo2smarthub.ts b/wedo2smarthub.ts index ec6193a..da4c262 100644 --- a/wedo2smarthub.ts +++ b/wedo2smarthub.ts @@ -26,7 +26,7 @@ export class WeDo2SmartHub extends Hub { public static IsWeDo2SmartHub (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0); } @@ -36,7 +36,7 @@ export class WeDo2SmartHub extends Hub { constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hubs.WEDO2_SMART_HUB; + this.type = Consts.Hub.WEDO2_SMART_HUB; this._ports = { "A": new Port("A", 1), "B": new Port("B", 2) @@ -49,12 +49,12 @@ export class WeDo2SmartHub extends Hub { return new Promise(async (resolve, reject) => { debug("Connecting to WeDo 2.0 Smart Hub"); await super.connect(); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_PORT_TYPE), this._parsePortMessage.bind(this)); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_SENSOR_VALUE), this._parseSensorMessage.bind(this)); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BUTTON), this._parseSensorMessage.bind(this)); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY), this._parseBatteryMessage.bind(this)); - this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_HIGH_CURRENT_ALERT), this._parseHighCurrentAlert.bind(this)); - this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY).read((err, data) => { + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_PORT_TYPE), this._parsePortMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_SENSOR_VALUE), this._parseSensorMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BUTTON), this._parseSensorMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY), this._parseBatteryMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT), this._parseHighCurrentAlert.bind(this)); + this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY).read((err, data) => { this._parseBatteryMessage(data); }); debug("Connect completed"); @@ -76,8 +76,8 @@ export class WeDo2SmartHub extends Hub { return new Promise((resolve, reject) => { const data = Buffer.from(name, "ascii"); // Send this twice, as sometimes the first time doesn't take - this._writeMessage(Consts.BLECharacteristics.WEDO2_NAME_ID, data); - this._writeMessage(Consts.BLECharacteristics.WEDO2_NAME_ID, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_NAME_ID, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_NAME_ID, data); this._name = name; return resolve(); }); @@ -87,18 +87,18 @@ export class WeDo2SmartHub extends Hub { /** * Set the color of the LED on the Hub via a color value. * @method WeDo2SmartHub#setLEDColor - * @param {number} color A number representing one of the LED Colors. + * @param {Color} color * @returns {Promise} Resolved upon successful issuance of command. */ public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); if (color === false) { color = 0; } data = Buffer.from([0x06, 0x04, 0x01, color]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }); } @@ -115,9 +115,9 @@ export class WeDo2SmartHub extends Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x06, 0x17, 0x01, 0x02]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }); } @@ -144,10 +144,10 @@ export class WeDo2SmartHub extends Hub { portObj.cancelEventTimer(); } return new Promise((resolve, reject) => { - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)])); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)])); if (time && typeof time === "number") { const timeout = global.setTimeout(() => { - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00])); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00])); return resolve(); }, time); portObj.setEventTimer(timeout); @@ -192,7 +192,7 @@ export class WeDo2SmartHub extends Hub { const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]); data.writeUInt16LE(frequency, 3); data.writeUInt16LE(time, 5); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); global.setTimeout(resolve, time); }); } @@ -211,11 +211,11 @@ export class WeDo2SmartHub extends Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]); - this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }, time); portObj.setEventTimer(timeout); @@ -227,12 +227,12 @@ export class WeDo2SmartHub extends Hub { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); + this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); } protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); + this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); } @@ -276,12 +276,12 @@ export class WeDo2SmartHub extends Hub { * Emits when a button is pressed. * @event WeDo2SmartHub#button * @param {string} button - * @param {number} state A number representing one of the ButtonStates. + * @param {ButtonState} state */ - this.emit("button", "GREEN", Consts.ButtonStates.PRESSED); + this.emit("button", "GREEN", Consts.ButtonState.PRESSED); return; } else if (data[0] === 0x00) { - this.emit("button", "GREEN", Consts.ButtonStates.RELEASED); + this.emit("button", "GREEN", Consts.ButtonState.RELEASED); return; } @@ -293,7 +293,7 @@ export class WeDo2SmartHub extends Hub { if (port && port.connected) { switch (port.type) { - case Consts.Devices.WEDO2_DISTANCE: + case Consts.Device.WEDO2_DISTANCE: { let distance = data[2]; if (data[3] === 1) { @@ -308,19 +308,19 @@ export class WeDo2SmartHub extends Hub { this.emit("distance", port.id, distance * 10); break; } - case Consts.Devices.BOOST_DISTANCE: + case Consts.Device.BOOST_DISTANCE: { const distance = data[2]; /** * Emits when a color sensor is activated. * @event WeDo2SmartHub#color * @param {string} port - * @param {number} color A number representing one of the LED Colors. + * @param {Color} color */ this.emit("color", port.id, distance); break; } - case Consts.Devices.WEDO2_TILT: + case Consts.Device.WEDO2_TILT: { this._lastTiltX = data[2]; if (this._lastTiltX > 100) { @@ -340,7 +340,7 @@ export class WeDo2SmartHub extends Hub { this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY); break; } - case Consts.Devices.BOOST_TACHO_MOTOR: + case Consts.Device.BOOST_TACHO_MOTOR: { const rotation = data.readInt32LE(2); /**