From 243d65699f5bb96e64c6cd12d06e50f03db03c42 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Sun, 3 Nov 2019 10:41:26 +0100 Subject: [PATCH 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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));