From ffd3cceab8d8348bda2b1c33184fa35254b44956 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 17 Dec 2019 14:44:48 -0800 Subject: [PATCH] Exported hub PortMap --- src/hubs/boostmovehub.ts | 21 +++++++++++++-------- src/hubs/controlplushub.ts | 25 +++++++++++++++---------- src/hubs/duplotrainbase.ts | 17 +++++++++++------ src/hubs/hub.ts | 21 +++++++++++++++------ src/hubs/puphub.ts | 15 ++++++++++----- src/hubs/pupremote.ts | 15 ++++++++++----- src/hubs/wedo2smarthub.ts | 15 ++++++++++----- 7 files changed, 84 insertions(+), 45 deletions(-) diff --git a/src/hubs/boostmovehub.ts b/src/hubs/boostmovehub.ts index 0870f87..1ad7572 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/boostmovehub.ts @@ -35,14 +35,7 @@ export class BoostMoveHub extends LPF2Hub { protected _voltagePort = 0x3c; constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.BOOST_MOVE_HUB); - this._portNames = { - "A": 0, - "B": 1, - "C": 2, - "D": 3, - "TILT": 58 - }; + super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB); debug("Discovered Boost Move Hub"); } @@ -65,3 +58,15 @@ export class BoostMoveHub extends LPF2Hub { } + +export namespace BoostMoveHub { + + export const PortMap: {[port: string]: number} = { + "A": 0, + "B": 1, + "C": 2, + "D": 3, + "TILT": 58 + } + +} \ No newline at end of file diff --git a/src/hubs/controlplushub.ts b/src/hubs/controlplushub.ts index 9eb3c7e..54a849b 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/controlplushub.ts @@ -37,16 +37,7 @@ export class ControlPlusHub extends LPF2Hub { protected _voltageMaxV = 9.615; constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.CONTROL_PLUS_HUB); - this._portNames = { - "A": 0, - "B": 1, - "C": 2, - "D": 3, - "ACCEL": 97, - "GYRO": 98, - "TILT": 99 - }; + super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB); debug("Discovered Control+ Hub"); } @@ -122,3 +113,17 @@ export class ControlPlusHub extends LPF2Hub { } + +export namespace ControlPlusHub { + + export const PortMap: {[port: string]: number} = { + "A": 0, + "B": 1, + "C": 2, + "D": 3, + "ACCEL": 97, + "GYRO": 98, + "TILT": 99 + } + +} \ No newline at end of file diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index c8a01fb..3d3399b 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -36,12 +36,7 @@ export class DuploTrainBase extends LPF2Hub { protected _voltageMaxRaw = 3047; constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.DUPLO_TRAIN_HUB); - this._portNames = { - "MOTOR": 0, - "COLOR": 18, - "SPEEDOMETER": 19 - }; + super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_HUB); debug("Discovered Duplo Train Base"); } @@ -73,3 +68,13 @@ export class DuploTrainBase extends LPF2Hub { } + +export namespace DuploTrainBase { + + export const PortMap: {[port: string]: number} = { + "MOTOR": 0, + "COLOR": 18, + "SPEEDOMETER": 19 + } + +} \ No newline at end of file diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index fb72352..04d7d44 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -27,8 +27,6 @@ const debug = Debug("hub"); export class Hub extends EventEmitter { protected _attachedDevices: {[portId: number]: Device} = {}; - - protected _portNames: {[port: string]: number} = {}; // protected _virtualPorts: {[port: string]: Port} = {}; protected _name: string = ""; @@ -43,11 +41,13 @@ export class Hub extends EventEmitter { protected _bleDevice: IBLEAbstraction; private _type: Consts.HubType; + private _portMap: {[port: string]: number} = {}; - constructor (device: IBLEAbstraction, type: Consts.HubType = Consts.HubType.UNKNOWN) { + constructor (device: IBLEAbstraction, portMap: {[port: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { super(); this._type = type; this._bleDevice = device; + this._portMap = portMap; device.on("disconnect", () => { /** * Emits when the hub is disconnected. @@ -76,6 +76,15 @@ export class Hub extends EventEmitter { } + /** + * @readonly + * @property {string[]} ports Array of port names + */ + public get ports () { + return Object.keys(this._portMap); + } + + /** * @readonly * @property {string} firmwareVersion Firmware version of the hub @@ -178,7 +187,7 @@ export class Hub extends EventEmitter { public getDeviceAtPort (portName: string) { - const portId = this._portNames[portName]; + const portId = this._portMap[portName]; if (portId) { return this._attachedDevices[portId]; } else { @@ -198,8 +207,8 @@ export class Hub extends EventEmitter { public getPortNameForPortId (portId: number) { - for (const port of Object.keys(this._portNames)) { - if (this._portNames[port] === portId) { + for (const port of Object.keys(this._portMap)) { + if (this._portMap[port] === portId) { return port; } } diff --git a/src/hubs/puphub.ts b/src/hubs/puphub.ts index 43dd145..dfb4673 100644 --- a/src/hubs/puphub.ts +++ b/src/hubs/puphub.ts @@ -35,11 +35,7 @@ export class PUPHub extends LPF2Hub { protected _voltagePort = 0x3c; constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.POWERED_UP_HUB); - this._portNames = { - "A": 0, - "B": 1 - }; + super(device, PUPHub.PortMap, Consts.HubType.POWERED_UP_HUB); debug("Discovered Powered UP Hub"); } @@ -62,3 +58,12 @@ export class PUPHub extends LPF2Hub { } + +export namespace PUPHub { + + export const PortMap: {[port: string]: number} = { + "A": 0, + "B": 1 + } + +} \ No newline at end of file diff --git a/src/hubs/pupremote.ts b/src/hubs/pupremote.ts index 2ce066e..64b9550 100644 --- a/src/hubs/pupremote.ts +++ b/src/hubs/pupremote.ts @@ -38,11 +38,7 @@ export class PUPRemote extends LPF2Hub { constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.POWERED_UP_REMOTE); - this._portNames = { - "LEFT": 0, - "RIGHT": 1 - }; + super(device, PUPRemote.PortMap, Consts.HubType.POWERED_UP_REMOTE); debug("Discovered Powered UP Remote"); } @@ -58,3 +54,12 @@ export class PUPRemote extends LPF2Hub { } + +export namespace PUPRemote { + + export const PortMap: {[port: string]: number} = { + "LEFT": 0, + "RIGHT": 1 + } + +} \ No newline at end of file diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index 4f8560c..ce085e7 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -34,11 +34,7 @@ export class WeDo2SmartHub extends Hub { constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.WEDO2_SMART_HUB); - this._portNames = { - "A": 1, - "B": 2 - }; + super(device, WeDo2SmartHub.PortMap, Consts.HubType.WEDO2_SMART_HUB); debug("Discovered WeDo 2.0 Smart Hub"); } @@ -353,3 +349,12 @@ export class WeDo2SmartHub extends Hub { } + +export namespace WeDo2SmartHub { + + export const PortMap: {[port: string]: number} = { + "A": 1, + "B": 2 + } + +} \ No newline at end of file