From 5f13c98529dd84d0151a10b13a094b7c018b5331 Mon Sep 17 00:00:00 2001 From: Michal Szafranski Date: Tue, 12 Nov 2019 19:27:44 +0100 Subject: [PATCH] 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"); +}