From d2f71aa9931c50c59d03773ea614e44869dcd44f Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Wed, 6 Nov 2019 07:45:11 +0100 Subject: [PATCH 01/12] 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 02/12] 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 03/12] 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 243d65699f5bb96e64c6cd12d06e50f03db03c42 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Sun, 3 Nov 2019 10:41:26 +0100 Subject: [PATCH 04/12] Extended port information requests --- src/lpf2hub.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 35c7f0f..8117c22 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -20,6 +20,8 @@ export class LPF2Hub extends Hub { return [t[0], t[1], t.substring(2, 4), t.substring(4)].join("."); } + public static sendPortInformationRequests: boolean = false; + protected _ledPort: number = 0x32; private _lastTiltX: number = 0; @@ -263,12 +265,15 @@ export class LPF2Hub extends Hub { } - private _parsePortMessage (data: Buffer) { let port = this._getPortForPortNumber(data[3]); +<<<<<<< HEAD if (data[4] === 0x01 && process.env["PORT_DEBUG_INFO"]) { +======= + if (data[4] === 0x01 && LPF2Hub.sendPortInformationRequests) { +>>>>>>> 1fa0f4a... Extended port information requests this._sendPortInformationRequest(data[3]); } @@ -301,11 +306,20 @@ export class LPF2Hub extends Hub { private _sendPortInformationRequest (port: number) { this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x01])); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x02])); // Mode combinations } private _parsePortInformationResponse (data: Buffer) { const port = data[3]; + if (data[4] === 2) { + const modeCombinationMasks: number[] = []; + for (let i = 5; i < data.length; i += 2) { + modeCombinationMasks.push(data.readUInt16LE(i)); + } + modeInfoDebug(`Port ${port}, mode combinations [${modeCombinationMasks.map((c) => c.toString(2)).join(", ")}]`); + return; + } const count = data[6]; const input = data.readUInt16LE(7); const output = data.readUInt16LE(9); @@ -317,6 +331,7 @@ export class LPF2Hub extends Hub { this._sendModeInformationRequest(port, i, 0x02); // PCT Range this._sendModeInformationRequest(port, i, 0x03); // SI Range this._sendModeInformationRequest(port, i, 0x04); // SI Symbol + this._sendModeInformationRequest(port, i, 0x80); // Value Format } } @@ -346,6 +361,12 @@ export class LPF2Hub extends Hub { case 0x04: // SI Symbol modeInfoDebug(`Port ${port}, mode ${mode}, SI symbol ${data.slice(6, data.length).toString()}`); break; + case 0x80: // Value Format + const numValues = data[6]; + const dataType = ["8bit", "16bit", "32bit", "float"][data[7]]; + const totalFigures = data[8]; + const decimals = data[9]; + modeInfoDebug(`Port ${port}, mode ${mode}, Value ${numValues} x ${dataType}, Decimal format ${totalFigures}.${decimals}`); } } From 8e144c83110d932a6018ca989aaf1d1772651967 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Sun, 3 Nov 2019 13:52:52 +0100 Subject: [PATCH 05/12] more verbose port information output --- src/hub.ts | 10 ++++++++++ src/lpf2hub.ts | 24 +++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/hub.ts b/src/hub.ts index 339a63e..e2b192c 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -381,6 +381,16 @@ export class Hub extends EventEmitter { } + protected _toHex (value: number, length: number = 2) { + return this._lpad(value.toString(16), length); + } + + + protected _toBin (value: number, length: number = 8) { + return this._lpad(value.toString(2), length); + } + + private _getModeForDeviceType (type: Consts.DeviceType) { switch (type) { case Consts.DeviceType.BASIC_MOTOR: diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 8117c22..3a76b27 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -20,7 +20,7 @@ export class LPF2Hub extends Hub { return [t[0], t[1], t.substring(2, 4), t.substring(4)].join("."); } - public static sendPortInformationRequests: boolean = false; + public sendPortInformationRequests: boolean = false; protected _ledPort: number = 0x32; @@ -268,12 +268,10 @@ export class LPF2Hub extends Hub { private _parsePortMessage (data: Buffer) { let port = this._getPortForPortNumber(data[3]); + const type = data.readUInt16LE(5); -<<<<<<< HEAD - if (data[4] === 0x01 && process.env["PORT_DEBUG_INFO"]) { -======= - if (data[4] === 0x01 && LPF2Hub.sendPortInformationRequests) { ->>>>>>> 1fa0f4a... Extended port information requests + if (data[4] === 0x01 && this.sendPortInformationRequests) { + modeInfoDebug(`Port ${this._toHex(data[3])}, type ${this._toHex(type, 4)} (${Consts.DeviceTypeNames[data[5]] || "unknown"})`); this._sendPortInformationRequest(data[3]); } @@ -286,7 +284,7 @@ export class LPF2Hub extends Hub { port = this._getPortForPortNumber(data[3]); if (port) { port.connected = true; - this._registerDeviceAttachment(port, data[5]); + this._registerDeviceAttachment(port, type); } else { return; } @@ -298,7 +296,7 @@ export class LPF2Hub extends Hub { } } else { port.connected = (data[4] === 0x01 || data[4] === 0x02) ? true : false; - this._registerDeviceAttachment(port, data[5]); + this._registerDeviceAttachment(port, type); } } @@ -317,13 +315,13 @@ export class LPF2Hub extends Hub { for (let i = 5; i < data.length; i += 2) { modeCombinationMasks.push(data.readUInt16LE(i)); } - modeInfoDebug(`Port ${port}, mode combinations [${modeCombinationMasks.map((c) => c.toString(2)).join(", ")}]`); + modeInfoDebug(`Port ${this._toHex(port)}, mode combinations [${modeCombinationMasks.map((c) => this._toBin(c, 0)).join(", ")}]`); return; } const count = data[6]; - const input = data.readUInt16LE(7); - const output = data.readUInt16LE(9); - modeInfoDebug(`Port ${port}, total modes ${count}, input modes ${input.toString(2)}, output modes ${output.toString(2)}`); + const input = this._toBin(data.readUInt16LE(7), count); + const output = this._toBin(data.readUInt16LE(9), count); + modeInfoDebug(`Port ${this._toHex(port)}, total modes ${count}, input modes ${input}, output modes ${output}`); for (let i = 0; i < count; i++) { this._sendModeInformationRequest(port, i, 0x00); // Mode Name @@ -342,7 +340,7 @@ export class LPF2Hub extends Hub { private _parseModeInformationResponse (data: Buffer) { - const port = data[3]; + const port = this._toHex(data[3]); const mode = data[4]; const type = data[5]; switch (type) { From 522a8c2e23125dcfac60bb86cca08c80ee3d1604 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Sun, 3 Nov 2019 14:00:16 +0100 Subject: [PATCH 06/12] Simplify enum name maps --- src/consts.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/consts.ts b/src/consts.ts index 0c70471..2cc2851 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -20,11 +20,7 @@ export enum HubType { // tslint:disable-next-line -export let HubTypeNames = Object.keys(HubType).reduce((result: {[hubType: string]: string}, item) => { - // @ts-ignore - result[HubType[item]] = item; - return result; -}, {}); +export const HubTypeNames = HubType; /** @@ -75,11 +71,7 @@ export enum DeviceType { // tslint:disable-next-line -export let DeviceTypeNames = Object.keys(DeviceType).reduce((result: {[deviceType: string]: string}, item) => { - // @ts-ignore - result[DeviceType[item]] = item; - return result; -}, {}); +export const DeviceTypeNames = DeviceType; /** @@ -114,11 +106,7 @@ export enum Color { // tslint:disable-next-line -export let ColorNames = Object.keys(Color).reduce((result: {[color: string]: string}, item) => { - // @ts-ignore - result[Color[item]] = item; - return result; -}, {}); +export const ColorNames = Color; /** From 83469886044b20f6388e9bb3311cbb284f77a662 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Sun, 3 Nov 2019 17:40:02 +0100 Subject: [PATCH 07/12] fix device type read regression --- src/lpf2hub.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 3a76b27..19134c0 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -268,7 +268,7 @@ export class LPF2Hub extends Hub { private _parsePortMessage (data: Buffer) { let port = this._getPortForPortNumber(data[3]); - const type = data.readUInt16LE(5); + const type = data[4] ? data.readUInt16LE(5) : 0; if (data[4] === 0x01 && this.sendPortInformationRequests) { modeInfoDebug(`Port ${this._toHex(data[3])}, type ${this._toHex(type, 4)} (${Consts.DeviceTypeNames[data[5]] || "unknown"})`); From a9fe059b3c28bd9fa0e36bab01e199ef61576c44 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Tue, 5 Nov 2019 21:35:43 +0100 Subject: [PATCH 08/12] Added missing device types --- src/consts.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/consts.ts b/src/consts.ts index 2cc2851..4a853e0 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -29,7 +29,10 @@ export const HubTypeNames = HubType; * @property {number} BASIC_MOTOR 1 * @property {number} TRAIN_MOTOR 2 * @property {number} LED_LIGHTS 8 - * @property {number} BOOST_LED 22 + * @property {number} VOLTAGE 20 + * @property {number} CURRENT 21 + * @property {number} PIEZO_TONE 22 + * @property {number} RGB_LIGHT 23 * @property {number} WEDO2_TILT 34 * @property {number} WEDO2_DISTANCE 35 * @property {number} BOOST_DISTANCE 37 @@ -43,6 +46,7 @@ export const HubTypeNames = HubType; * @property {number} CONTROL_PLUS_LARGE_MOTOR 46 * @property {number} CONTROL_PLUS_XLARGE_MOTOR 47 * @property {number} POWERED_UP_REMOTE_BUTTON 55 + * @property {number} RSSI 56 * @property {number} CONTROL_PLUS_ACCELEROMETER 58 * @property {number} CONTROL_PLUS_TILT 59 */ @@ -51,7 +55,10 @@ export enum DeviceType { BASIC_MOTOR = 1, TRAIN_MOTOR = 2, LED_LIGHTS = 8, - BOOST_LED = 22, + VOLTAGE = 20, + CURRENT = 21, + PIEZO_TONE = 22, + RGB_LIGHT = 23, WEDO2_TILT = 34, WEDO2_DISTANCE = 35, BOOST_DISTANCE = 37, @@ -65,6 +72,7 @@ export enum DeviceType { CONTROL_PLUS_LARGE_MOTOR = 46, CONTROL_PLUS_XLARGE_MOTOR = 47, POWERED_UP_REMOTE_BUTTON = 55, + RSSI = 56, CONTROL_PLUS_ACCELEROMETER = 58, CONTROL_PLUS_TILT = 59 } From 3ab3f971227407e2805c594c8f74e47a6bb09c9b Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Tue, 5 Nov 2019 21:56:06 +0100 Subject: [PATCH 09/12] report hw/sw versions for discovered devices --- src/lpf2hub.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 19134c0..c7af121 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -271,7 +271,11 @@ export class LPF2Hub extends Hub { const type = data[4] ? data.readUInt16LE(5) : 0; if (data[4] === 0x01 && this.sendPortInformationRequests) { - modeInfoDebug(`Port ${this._toHex(data[3])}, type ${this._toHex(type, 4)} (${Consts.DeviceTypeNames[data[5]] || "unknown"})`); + const typeName = Consts.DeviceTypeNames[data[5]] || "unknown"; + modeInfoDebug(`Port ${this._toHex(data[3])}, type ${this._toHex(type, 4)} (${typeName})`); + const hwVersion = LPF2Hub.decodeVersion(data.readInt32LE(7)); + const swVersion = LPF2Hub.decodeVersion(data.readInt32LE(11)); + modeInfoDebug(`Port ${this._toHex(data[3])}, hardware version ${hwVersion}, software version ${swVersion}`); this._sendPortInformationRequest(data[3]); } From 5f13c98529dd84d0151a10b13a094b7c018b5331 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Tue, 12 Nov 2019 19:27:44 +0100 Subject: [PATCH 10/12] Moved toBin and toHex to utils --- src/hub.ts | 19 ------------------- src/lpf2hub.ts | 15 ++++++++------- src/utils.ts | 7 +++++++ 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/hub.ts b/src/hub.ts index e2b192c..d1ae77e 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -372,25 +372,6 @@ export class Hub extends EventEmitter { return port; } - - protected _lpad (str: string, length: number) { - while (str.length < length) { - str = "0" + str; - } - return str; - } - - - protected _toHex (value: number, length: number = 2) { - return this._lpad(value.toString(16), length); - } - - - protected _toBin (value: number, length: number = 8) { - return this._lpad(value.toString(2), length); - } - - private _getModeForDeviceType (type: Consts.DeviceType) { switch (type) { case Consts.DeviceType.BASIC_MOTOR: diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index c7af121..339dc75 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -4,6 +4,7 @@ import { Hub } from "./hub"; import { Port } from "./port"; import * as Consts from "./consts"; +import { toBin, toHex } from "./utils"; import Debug = require("debug"); const debug = Debug("lpf2hub"); @@ -272,10 +273,10 @@ export class LPF2Hub extends Hub { if (data[4] === 0x01 && this.sendPortInformationRequests) { const typeName = Consts.DeviceTypeNames[data[5]] || "unknown"; - modeInfoDebug(`Port ${this._toHex(data[3])}, type ${this._toHex(type, 4)} (${typeName})`); + modeInfoDebug(`Port ${toHex(data[3])}, type ${toHex(type, 4)} (${typeName})`); const hwVersion = LPF2Hub.decodeVersion(data.readInt32LE(7)); const swVersion = LPF2Hub.decodeVersion(data.readInt32LE(11)); - modeInfoDebug(`Port ${this._toHex(data[3])}, hardware version ${hwVersion}, software version ${swVersion}`); + modeInfoDebug(`Port ${toHex(data[3])}, hardware version ${hwVersion}, software version ${swVersion}`); this._sendPortInformationRequest(data[3]); } @@ -319,13 +320,13 @@ export class LPF2Hub extends Hub { for (let i = 5; i < data.length; i += 2) { modeCombinationMasks.push(data.readUInt16LE(i)); } - modeInfoDebug(`Port ${this._toHex(port)}, mode combinations [${modeCombinationMasks.map((c) => this._toBin(c, 0)).join(", ")}]`); + modeInfoDebug(`Port ${toHex(port)}, mode combinations [${modeCombinationMasks.map((c) => toBin(c, 0)).join(", ")}]`); return; } const count = data[6]; - const input = this._toBin(data.readUInt16LE(7), count); - const output = this._toBin(data.readUInt16LE(9), count); - modeInfoDebug(`Port ${this._toHex(port)}, total modes ${count}, input modes ${input}, output modes ${output}`); + const input = toBin(data.readUInt16LE(7), count); + const output = toBin(data.readUInt16LE(9), count); + modeInfoDebug(`Port ${toHex(port)}, total modes ${count}, input modes ${input}, output modes ${output}`); for (let i = 0; i < count; i++) { this._sendModeInformationRequest(port, i, 0x00); // Mode Name @@ -344,7 +345,7 @@ export class LPF2Hub extends Hub { private _parseModeInformationResponse (data: Buffer) { - const port = this._toHex(data[3]); + const port = toHex(data[3]); const mode = data[4]; const type = data[5]; switch (type) { diff --git a/src/utils.ts b/src/utils.ts index 629b5ef..83650aa 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,2 +1,9 @@ // @ts-ignore export const isWebBluetooth = (typeof navigator !== "undefined" && navigator && navigator.bluetooth); + +export function toHex (value: number, length: number = 2) { + return value.toString(16).padStart(length, "0"); +} +export function toBin (value: number, length: number = 8) { + return value.toString(2).padStart(length, "0"); +} From e72f3f72f5cdff47103e03b0bbb3b7fcf8865bf3 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Wed, 13 Nov 2019 22:43:22 +0100 Subject: [PATCH 11/12] improved port information switching --- src/lpf2hub.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 339dc75..8d2c32a 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -21,8 +21,6 @@ export class LPF2Hub extends Hub { return [t[0], t[1], t.substring(2, 4), t.substring(4)].join("."); } - public sendPortInformationRequests: boolean = false; - protected _ledPort: number = 0x32; private _lastTiltX: number = 0; @@ -271,7 +269,7 @@ export class LPF2Hub extends Hub { let port = this._getPortForPortNumber(data[3]); const type = data[4] ? data.readUInt16LE(5) : 0; - if (data[4] === 0x01 && this.sendPortInformationRequests) { + if (data[4] === 0x01 && modeInfoDebug.enabled) { const typeName = Consts.DeviceTypeNames[data[5]] || "unknown"; modeInfoDebug(`Port ${toHex(data[3])}, type ${toHex(type, 4)} (${typeName})`); const hwVersion = LPF2Hub.decodeVersion(data.readInt32LE(7)); From 49f43f5dff9dd7704ff72959025acbb87a205ab9 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Wed, 13 Nov 2019 22:33:47 +0100 Subject: [PATCH 12/12] 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);