Moved toBin and toHex to utils

This commit is contained in:
Michal Szafranski 2019-11-12 19:27:44 +01:00
parent 3ab3f97122
commit 5f13c98529
3 changed files with 15 additions and 26 deletions

View File

@ -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:

View File

@ -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) {

View File

@ -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");
}