diff --git a/DOCS.md b/DOCS.md index f3f59f0..5c38cde 100644 --- a/DOCS.md +++ b/DOCS.md @@ -18,6 +18,8 @@ ## Typedefs
+
HubType
+
Device
Color
@@ -106,6 +108,8 @@ Emits when a Powered UP Hub device is found. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["button" (button, state)](#WeDo2SmartHub+event_button) * ["distance" (port, distance)](#WeDo2SmartHub+event_distance) * ["color" (port, color)](#WeDo2SmartHub+event_color) @@ -325,6 +329,23 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### weDo2SmartHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) + + +### weDo2SmartHub.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -396,7 +417,7 @@ Emits when a motor or sensor is attached to the Hub. | Param | Type | | --- | --- | | port | string | -| type | [Device](#Device) | +| type | DeviceType | @@ -435,6 +456,8 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) @@ -676,6 +699,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### boostMoveHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [getHubType](#Hub+getHubType) + + +### boostMoveHub.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -761,7 +803,7 @@ Emits when a motor or sensor is attached to the Hub. | Param | Type | | --- | --- | | port | string | -| type | [Device](#Device) | +| type | DeviceType | @@ -800,6 +842,8 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) @@ -1026,6 +1070,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### pupHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [getHubType](#Hub+getHubType) + + +### pupHub.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -1099,7 +1162,7 @@ Emits when a motor or sensor is attached to the Hub. | Param | Type | | --- | --- | | port | string | -| type | [Device](#Device) | +| type | DeviceType | @@ -1135,6 +1198,8 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["button" (button, state)](#LPF2Hub+event_button) * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) @@ -1315,6 +1380,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### pupRemote.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [getHubType](#Hub+getHubType) + + +### pupRemote.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -1365,6 +1449,8 @@ A combined color and distance event, emits when the sensor is activated. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["color" (port, color)](#LPF2Hub+event_color) * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["speed" (port, speed)](#LPF2Hub+event_speed) @@ -1586,6 +1672,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### duploTrainBase.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [getHubType](#Hub+getHubType) + + +### duploTrainBase.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "color" (port, color) @@ -1623,6 +1728,21 @@ Emits on a speed change. | port | string | | speed | number | + + +## HubType +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| UNKNOWN | number | 0 | +| WEDO2_SMART_HUB | number | 1 | +| BOOST_MOVE_HUB | number | 2 | +| POWERED_UP_HUB | number | 3 | +| POWERED_UP_REMOTE | number | 4 | +| DUPLO_TRAIN_HUB | number | 5 | + ## Device diff --git a/README.md b/README.md index d050856..a693fd5 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,8 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R ## Typedefs
+
HubType
+
Device
Color
@@ -189,6 +191,8 @@ Emits when a Powered UP Hub device is found. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["button" (button, state)](#WeDo2SmartHub+event_button) * ["distance" (port, distance)](#WeDo2SmartHub+event_distance) * ["color" (port, color)](#WeDo2SmartHub+event_color) @@ -408,6 +412,23 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### weDo2SmartHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) + + +### weDo2SmartHub.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [WeDo2SmartHub](#WeDo2SmartHub) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -479,7 +500,7 @@ Emits when a motor or sensor is attached to the Hub. | Param | Type | | --- | --- | | port | string | -| type | [Device](#Device) | +| type | DeviceType | @@ -518,6 +539,8 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) @@ -759,6 +782,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### boostMoveHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [getHubType](#Hub+getHubType) + + +### boostMoveHub.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [BoostMoveHub](#BoostMoveHub) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -844,7 +886,7 @@ Emits when a motor or sensor is attached to the Hub. | Param | Type | | --- | --- | | port | string | -| type | [Device](#Device) | +| type | DeviceType | @@ -883,6 +925,8 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["button" (button, state)](#LPF2Hub+event_button) * ["distance" (port, distance)](#LPF2Hub+event_distance) * ["color" (port, color)](#LPF2Hub+event_color) @@ -1109,6 +1153,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### pupHub.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [getHubType](#Hub+getHubType) + + +### pupHub.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [PUPHub](#PUPHub) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -1182,7 +1245,7 @@ Emits when a motor or sensor is attached to the Hub. | Param | Type | | --- | --- | | port | string | -| type | [Device](#Device) | +| type | DeviceType | @@ -1218,6 +1281,8 @@ Emits when an attached motor or sensor is detached from the Hub. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["button" (button, state)](#LPF2Hub+event_button) * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) @@ -1398,6 +1463,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### pupRemote.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [getHubType](#Hub+getHubType) + + +### pupRemote.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [PUPRemote](#PUPRemote) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "button" (button, state) @@ -1448,6 +1532,8 @@ A combined color and distance event, emits when the sensor is activated. * [.unsubscribe(port)](#Hub+unsubscribe) ⇒ Promise * [.sleep(delay)](#Hub+sleep) ⇒ Promise * [.wait(commands)](#Hub+wait) ⇒ Promise + * [.getHubType()](#Hub+getHubType) ⇒ [HubType](#HubType) + * [.getPortDeviceType(port)](#Hub+getPortDeviceType) ⇒ DeviceType * ["color" (port, color)](#LPF2Hub+event_color) * ["colorAndDistance" (port, color, distance)](#LPF2Hub+event_colorAndDistance) * ["speed" (port, speed)](#LPF2Hub+event_speed) @@ -1669,6 +1755,25 @@ This is a helper method to make it easier to wait for concurrent commands to com | --- | --- | --- | | commands | Array.<Promise.<any>> | Array of executing commands. | + + +### duploTrainBase.getHubType() ⇒ [HubType](#HubType) +Get the hub type. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [getHubType](#Hub+getHubType) + + +### duploTrainBase.getPortDeviceType(port) ⇒ DeviceType +Get the device type for a given port. + +**Kind**: instance method of [DuploTrainBase](#DuploTrainBase) +**Overrides**: [getPortDeviceType](#Hub+getPortDeviceType) + +| Param | Type | +| --- | --- | +| port | string | + ### "color" (port, color) @@ -1706,6 +1811,21 @@ Emits on a speed change. | port | string | | speed | number | + + +## HubType +**Kind**: global typedef +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| UNKNOWN | number | 0 | +| WEDO2_SMART_HUB | number | 1 | +| BOOST_MOVE_HUB | number | 2 | +| POWERED_UP_HUB | number | 3 | +| POWERED_UP_REMOTE | number | 4 | +| DUPLO_TRAIN_HUB | number | 5 | + ## Device diff --git a/boostmovehub.ts b/boostmovehub.ts index 8d0ad09..c013dd4 100644 --- a/boostmovehub.ts +++ b/boostmovehub.ts @@ -33,7 +33,7 @@ export class BoostMoveHub extends LPF2Hub { constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hub.BOOST_MOVE_HUB; + this.type = Consts.HubType.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.Device.BOOST_TACHO_MOTOR || portObj.type === Consts.Device.BOOST_MOVE_HUB_MOTOR) { + if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { portObj.busy = true; let data = null; if (portObj.id === "AB") { @@ -111,7 +111,7 @@ export class BoostMoveHub extends LPF2Hub { } else { - if (portObj.type === Consts.Device.BOOST_TACHO_MOTOR || portObj.type === Consts.Device.BOOST_MOVE_HUB_MOTOR) { + if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { portObj.busy = true; let data = null; if (portObj.id === "AB") { @@ -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.Device.BOOST_TACHO_MOTOR || portObj.type === Consts.Device.BOOST_MOVE_HUB_MOTOR)) { + if (!(portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.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) { diff --git a/consts.ts b/consts.ts index dc48c68..c57367b 100644 --- a/consts.ts +++ b/consts.ts @@ -1,4 +1,13 @@ -export enum Hub { +/** + * @typedef HubType + * @property {number} UNKNOWN 0 + * @property {number} WEDO2_SMART_HUB 1 + * @property {number} BOOST_MOVE_HUB 2 + * @property {number} POWERED_UP_HUB 3 + * @property {number} POWERED_UP_REMOTE 4 + * @property {number} DUPLO_TRAIN_HUB 5 + */ +export enum HubType { UNKNOWN = 0, WEDO2_SMART_HUB = 1, BOOST_MOVE_HUB = 2, @@ -27,7 +36,7 @@ export enum Hub { * @property {number} DUPLO_TRAIN_BASE_SPEEDOMETER 44 * @property {number} POWERED_UP_REMOTE_BUTTON 55 */ -export enum Device { +export enum DeviceType { UNKNOWN = 0, BASIC_MOTOR = 1, TRAIN_MOTOR = 2, diff --git a/duplotrainbase.ts b/duplotrainbase.ts index 8581b92..f8a6ab6 100644 --- a/duplotrainbase.ts +++ b/duplotrainbase.ts @@ -58,7 +58,7 @@ export class DuploTrainBase extends LPF2Hub { constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hub.DUPLO_TRAIN_HUB; + this.type = Consts.HubType.DUPLO_TRAIN_HUB; this._ports = { "MOTOR": new Port("MOTOR", 0), "COLOR": new Port("COLOR", 18), diff --git a/hub.ts b/hub.ts index 6763015..51beb19 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.Hub = Consts.Hub.UNKNOWN; + public type: Consts.HubType = Consts.HubType.UNKNOWN; protected _ports: {[port: string]: Port} = {}; protected _characteristics: {[uuid: string]: Characteristic} = {}; @@ -216,6 +216,27 @@ export class Hub extends EventEmitter { } + /** + * Get the hub type. + * @method Hub#getHubType + * @returns {HubType} + */ + public getHubType () { + return this.type; + } + + + /** + * Get the device type for a given port. + * @method Hub#getPortDeviceType + * @param {string} port + * @returns {DeviceType} + */ + public getPortDeviceType (port: string) { + return this._portLookup(port).type; + } + + protected _getCharacteristic (uuid: string) { return this._characteristics[uuid.replace(/-/g, "")]; } @@ -257,12 +278,12 @@ export class Hub extends EventEmitter { * Emits when a motor or sensor is attached to the Hub. * @event Hub#attach * @param {string} port - * @param {Device} type + * @param {DeviceType} type */ this.emit("attach", port.id, type); } } else { - port.type = Consts.Device.UNKNOWN; + port.type = Consts.DeviceType.UNKNOWN; debug(`Port ${port.id} disconnected`); /** * Emits when an attached motor or sensor is detached from the Hub. @@ -349,19 +370,19 @@ export class Hub extends EventEmitter { } - private _getModeForDeviceType (type: Consts.Device) { + private _getModeForDeviceType (type: Consts.DeviceType) { switch (type) { - case Consts.Device.BASIC_MOTOR: + case Consts.DeviceType.BASIC_MOTOR: return 0x02; - case Consts.Device.TRAIN_MOTOR: + case Consts.DeviceType.TRAIN_MOTOR: return 0x02; - case Consts.Device.BOOST_TACHO_MOTOR: + case Consts.DeviceType.BOOST_TACHO_MOTOR: return 0x02; - case Consts.Device.BOOST_MOVE_HUB_MOTOR: + case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: return 0x02; - case Consts.Device.BOOST_DISTANCE: - return (this.type === Consts.Hub.WEDO2_SMART_HUB ? 0x00 : 0x08); - case Consts.Device.BOOST_TILT: + case Consts.DeviceType.BOOST_DISTANCE: + return (this.type === Consts.HubType.WEDO2_SMART_HUB ? 0x00 : 0x08); + case Consts.DeviceType.BOOST_TILT: return 0x04; default: return 0x00; diff --git a/lpf2hub.ts b/lpf2hub.ts index 1d19681..9cf1ed1 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -42,7 +42,7 @@ export class LPF2Hub extends Hub { 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) { + if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); } return resolve(); @@ -242,12 +242,12 @@ export class LPF2Hub extends Hub { private _parseSensorMessage (data: Buffer) { - if ((data[3] === 0x3b && this.type === Consts.Hub.POWERED_UP_REMOTE) || (data[3] === 0x3c && this.type !== Consts.Hub.POWERED_UP_REMOTE)) { // Voltage + if ((data[3] === 0x3b && this.type === Consts.HubType.POWERED_UP_REMOTE) || (data[3] === 0x3c && this.type !== Consts.HubType.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.Hub.POWERED_UP_REMOTE) { // Current (Non-PUP Remote) + } else if (data[3] === 0x3b && this.type !== Consts.HubType.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.Device.WEDO2_DISTANCE: + case Consts.DeviceType.WEDO2_DISTANCE: { let distance = data[4]; if (data[5] === 1) { @@ -282,7 +282,7 @@ export class LPF2Hub extends Hub { this.emit("distance", port.id, distance * 10); break; } - case Consts.Device.BOOST_DISTANCE: + case Consts.DeviceType.BOOST_DISTANCE: { /** @@ -318,7 +318,7 @@ export class LPF2Hub extends Hub { } break; } - case Consts.Device.WEDO2_TILT: + case Consts.DeviceType.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.Device.BOOST_TACHO_MOTOR: + case Consts.DeviceType.BOOST_TACHO_MOTOR: { const rotation = data.readInt32LE(4); /** @@ -346,20 +346,20 @@ export class LPF2Hub extends Hub { this.emit("rotate", port.id, rotation); break; } - case Consts.Device.BOOST_MOVE_HUB_MOTOR: + case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: { const rotation = data.readInt32LE(4); this.emit("rotate", port.id, rotation); break; } - case Consts.Device.BOOST_TILT: + case Consts.DeviceType.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.Device.POWERED_UP_REMOTE_BUTTON: + case Consts.DeviceType.POWERED_UP_REMOTE_BUTTON: { switch (data[4]) { case 0x01: @@ -385,14 +385,14 @@ export class LPF2Hub extends Hub { } break; } - case Consts.Device.DUPLO_TRAIN_BASE_COLOR: + case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: { if (data[4] <= 10) { this.emit("color", port.id, data[4]); } break; } - case Consts.Device.DUPLO_TRAIN_BASE_SPEEDOMETER: + case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER: { /** * Emits on a speed change. diff --git a/package.json b/package.json index 5e16a40..4a85221 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-poweredup", - "version": "1.3.0", + "version": "1.4.0", "description": "A Node.js module to interface with LEGO Powered UP components.", "homepage": "https://github.com/nathankellenicki/node-poweredup/", "main": "dist/index.js", diff --git a/port.ts b/port.ts index 20d1274..0a279cb 100644 --- a/port.ts +++ b/port.ts @@ -6,7 +6,7 @@ export class Port { public id: string; public value: number; - public type: Consts.Device; + public type: Consts.DeviceType; 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.Device.UNKNOWN; + this.type = Consts.DeviceType.UNKNOWN; } public cancelEventTimer () { diff --git a/puphub.ts b/puphub.ts index 360b770..e0885ee 100644 --- a/puphub.ts +++ b/puphub.ts @@ -38,7 +38,7 @@ export class PUPHub extends LPF2Hub { constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hub.POWERED_UP_HUB; + this.type = Consts.HubType.POWERED_UP_HUB; this._ports = { "A": new Port("A", 0), "B": new Port("B", 1), diff --git a/pupremote.ts b/pupremote.ts index 4659648..b5adf1b 100644 --- a/pupremote.ts +++ b/pupremote.ts @@ -63,7 +63,7 @@ export class PUPRemote extends LPF2Hub { constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hub.POWERED_UP_REMOTE; + this.type = Consts.HubType.POWERED_UP_REMOTE; this._ports = { "LEFT": new Port("LEFT", 0), "RIGHT": new Port("RIGHT", 1) diff --git a/wedo2smarthub.ts b/wedo2smarthub.ts index da4c262..22d1b68 100644 --- a/wedo2smarthub.ts +++ b/wedo2smarthub.ts @@ -36,7 +36,7 @@ export class WeDo2SmartHub extends Hub { constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(peripheral, autoSubscribe); - this.type = Consts.Hub.WEDO2_SMART_HUB; + this.type = Consts.HubType.WEDO2_SMART_HUB; this._ports = { "A": new Port("A", 1), "B": new Port("B", 2) @@ -293,7 +293,7 @@ export class WeDo2SmartHub extends Hub { if (port && port.connected) { switch (port.type) { - case Consts.Device.WEDO2_DISTANCE: + case Consts.DeviceType.WEDO2_DISTANCE: { let distance = data[2]; if (data[3] === 1) { @@ -308,7 +308,7 @@ export class WeDo2SmartHub extends Hub { this.emit("distance", port.id, distance * 10); break; } - case Consts.Device.BOOST_DISTANCE: + case Consts.DeviceType.BOOST_DISTANCE: { const distance = data[2]; /** @@ -320,7 +320,7 @@ export class WeDo2SmartHub extends Hub { this.emit("color", port.id, distance); break; } - case Consts.Device.WEDO2_TILT: + case Consts.DeviceType.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.Device.BOOST_TACHO_MOTOR: + case Consts.DeviceType.BOOST_TACHO_MOTOR: { const rotation = data.readInt32LE(2); /**