From d2f71aa9931c50c59d03773ea614e44869dcd44f Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Wed, 6 Nov 2019 07:45:11 +0100 Subject: [PATCH 1/4] Voltage readout added for duplo train and fixed for PUP remote --- src/boostmovehub.ts | 2 ++ src/controlplushub.ts | 4 ++++ src/duplotrainbase.ts | 4 +++- src/hub.ts | 10 ---------- src/lpf2hub.ts | 45 +++++++++++++++++-------------------------- src/puphub.ts | 3 +++ src/pupremote.ts | 3 +++ 7 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/boostmovehub.ts b/src/boostmovehub.ts index 3c1c9f1..93c1222 100644 --- a/src/boostmovehub.ts +++ b/src/boostmovehub.ts @@ -31,6 +31,8 @@ export class BoostMoveHub extends LPF2Hub { ); } + protected _currentPort = 0x3b; + protected _voltagePort = 0x3c; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { super(device, autoSubscribe); diff --git a/src/controlplushub.ts b/src/controlplushub.ts index 533ea84..64accd7 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -30,6 +30,10 @@ export class ControlPlusHub extends LPF2Hub { ); } + protected _currentPort = 0x3b; + protected _voltagePort = 0x3c; + protected _voltageMaxV = 9.612; + protected _voltageMaxRaw = 4095; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { super(device, autoSubscribe); diff --git a/src/duplotrainbase.ts b/src/duplotrainbase.ts index 1503d1d..c52ca0d 100644 --- a/src/duplotrainbase.ts +++ b/src/duplotrainbase.ts @@ -32,7 +32,9 @@ export class DuploTrainBase extends LPF2Hub { protected _ledPort = 0x11; - + protected _voltagePort = 0x14; + protected _voltageMaxV = 6.4; + protected _voltageMaxRaw = 3047; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { super(device, autoSubscribe); diff --git a/src/hub.ts b/src/hub.ts index 339a63e..5dcd873 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -31,7 +31,6 @@ export class Hub extends EventEmitter { protected _current: number = 0; protected _bleDevice: IBLEDevice; - private _rssi: number = -100; private _isConnecting = false; private _isConnected = false; @@ -82,15 +81,6 @@ export class Hub extends EventEmitter { } - /** - * @readonly - * @property {number} rssi Signal strength of the hub - */ - public get rssi () { - return this._rssi; - } - - /** * @readonly * @property {number} batteryLevel Battery level of the hub (Percentage between 0-100) diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 35c7f0f..4cd8218 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -21,6 +21,12 @@ export class LPF2Hub extends Hub { } protected _ledPort: number = 0x32; + protected _voltagePort: number | undefined; + protected _voltageMaxV: number = 9.6; + protected _voltageMaxRaw: number = 3893; + protected _currentPort: number | undefined; + protected _currentMaxMA: number = 2444; + protected _currentMaxRaw: number = 4095; private _lastTiltX: number = 0; private _lastTiltY: number = 0; @@ -38,8 +44,12 @@ export class LPF2Hub extends Hub { this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x04, 0x05])); // Request hardware version this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x06, 0x02])); // Activate battery level reports - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports + if (this._voltagePort !== undefined) { + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._voltagePort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports + } + if (this._currentPort !== undefined) { + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._currentPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports + } if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); } @@ -379,32 +389,13 @@ export class LPF2Hub extends Hub { private _parseSensorMessage (data: Buffer) { - if ((data[3] === 0x3b && this.type === Consts.HubType.POWERED_UP_REMOTE)) { // Voltage (PUP Remote) - data = this._padMessage(data, 6); - const voltage = data.readUInt16LE(4); - this._voltage = 6400.0 * voltage / 3200.0 / 1000.0; + if (data[3] === this._voltagePort) { + const voltageRaw = data.readUInt16LE(4); + this._voltage = voltageRaw * this._voltageMaxV / this._voltageMaxRaw; return; - } else if ((data[3] === 0x3c && this.type === Consts.HubType.POWERED_UP_HUB)) { // Voltage (PUP Hub) - data = this._padMessage(data, 6); - const voltage = data.readUInt16LE(4); - this._voltage = 9620.0 * voltage / 3893.0 / 1000.0; - return; - } else if ((data[3] === 0x3c && this.type === Consts.HubType.CONTROL_PLUS_HUB)) { // Voltage (Control+ Hub) - data = this._padMessage(data, 6); - const voltage = data.readUInt16LE(4); - this._voltage = 9615.0 * voltage / 4095.0 / 1000.0; - return; - } else if (data[3] === 0x3c) { // Voltage (Others) - data = this._padMessage(data, 6); - const voltage = data.readUInt16LE(4); - this._voltage = 9600.0 * voltage / 3893.0 / 1000.0; - return; - } else if (data[3] === 0x3c && this.type === Consts.HubType.POWERED_UP_REMOTE) { // RSSI (PUP Remote) - return; - } else if (data[3] === 0x3b) { // Current (Others) - data = this._padMessage(data, 6); - const current = data.readUInt16LE(4); - this._current = 2444 * current / 4095.0; + } else if (data[3] === this._currentPort) { + const currentRaw = data.readUInt16LE(4); + this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw; return; } diff --git a/src/puphub.ts b/src/puphub.ts index e85bd93..a59bf72 100644 --- a/src/puphub.ts +++ b/src/puphub.ts @@ -31,6 +31,9 @@ export class PUPHub extends LPF2Hub { ); } + protected _currentPort = 0x3b; + protected _voltagePort = 0x3c; + protected _voltageMaxV = 9.62; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { super(device, autoSubscribe); diff --git a/src/pupremote.ts b/src/pupremote.ts index 888728f..d01c3f2 100644 --- a/src/pupremote.ts +++ b/src/pupremote.ts @@ -32,6 +32,9 @@ export class PUPRemote extends LPF2Hub { protected _ledPort = 0x34; + protected _voltagePort = 0x3b; + protected _voltageMaxV = 6.4; + protected _voltageMaxRaw = 3200; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { From 545e8ee1779d29bf64c8884b91c089d79b48b092 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Wed, 6 Nov 2019 20:29:20 +0100 Subject: [PATCH 2/4] remove unused _padMessage --- src/lpf2hub.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 4cd8218..d65aa22 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -379,14 +379,6 @@ export class LPF2Hub extends Hub { } - private _padMessage (data: Buffer, len: number) { - if (data.length < len) { - data = Buffer.concat([data, Buffer.alloc(len - data.length)]); - } - return data; - } - - private _parseSensorMessage (data: Buffer) { if (data[3] === this._voltagePort) { From f43863bd51d1b57a5a5f1088abfd57d79ac34777 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Wed, 6 Nov 2019 20:44:14 +0100 Subject: [PATCH 3/4] typo in voltage value --- src/controlplushub.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controlplushub.ts b/src/controlplushub.ts index 64accd7..b96b0c4 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -32,7 +32,7 @@ export class ControlPlusHub extends LPF2Hub { protected _currentPort = 0x3b; protected _voltagePort = 0x3c; - protected _voltageMaxV = 9.612; + protected _voltageMaxV = 9.615; protected _voltageMaxRaw = 4095; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { From 49f43f5dff9dd7704ff72959025acbb87a205ab9 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Wed, 13 Nov 2019 22:33:47 +0100 Subject: [PATCH 4/4] Unified 9.6 max voltage values --- src/controlplushub.ts | 1 - src/puphub.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/src/controlplushub.ts b/src/controlplushub.ts index b96b0c4..03298e9 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -32,7 +32,6 @@ export class ControlPlusHub extends LPF2Hub { protected _currentPort = 0x3b; protected _voltagePort = 0x3c; - protected _voltageMaxV = 9.615; protected _voltageMaxRaw = 4095; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { diff --git a/src/puphub.ts b/src/puphub.ts index a59bf72..a305511 100644 --- a/src/puphub.ts +++ b/src/puphub.ts @@ -33,7 +33,6 @@ export class PUPHub extends LPF2Hub { protected _currentPort = 0x3b; protected _voltagePort = 0x3c; - protected _voltageMaxV = 9.62; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { super(device, autoSubscribe);