From 831177314379da794437f73669336cc22f627d98 Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Thu, 2 Aug 2018 16:32:31 +0100 Subject: [PATCH] Simplified speed tuples --- boostmovehub.ts | 18 +++--------------- consts.ts | 5 +++-- puphub.ts | 12 ++---------- 3 files changed, 8 insertions(+), 27 deletions(-) diff --git a/boostmovehub.ts b/boostmovehub.ts index 1b10c5a..ade6ad2 100644 --- a/boostmovehub.ts +++ b/boostmovehub.ts @@ -86,11 +86,7 @@ export class BoostMoveHub extends LPF2Hub { portObj.busy = true; let data = null; if (portObj.id === "AB") { - if (speed instanceof Array) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); - } else { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } + data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); } else { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); @@ -117,11 +113,7 @@ export class BoostMoveHub extends LPF2Hub { portObj.busy = true; let data = null; if (portObj.id === "AB") { - if (speed instanceof Array) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); - } else { - data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } + data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); } else { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); @@ -161,11 +153,7 @@ export class BoostMoveHub extends LPF2Hub { portObj.busy = true; let data = null; if (portObj.id === "AB") { - if (speed instanceof Array) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); - } else { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } + data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); } else { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); diff --git a/consts.ts b/consts.ts index 1dd8327..6141fd3 100644 --- a/consts.ts +++ b/consts.ts @@ -22,7 +22,7 @@ export enum Devices { export enum Colors { - NONE = 0, + BLACK = 0, PINK = 1, PURPLE = 2, BLUE = 3, @@ -32,7 +32,8 @@ export enum Colors { YELLOW = 7, ORANGE = 8, RED = 9, - WHITE = 10 + WHITE = 10, + NONE = 255 } export enum ButtonStates { diff --git a/puphub.ts b/puphub.ts index 44e7a5a..e703623 100644 --- a/puphub.ts +++ b/puphub.ts @@ -95,11 +95,7 @@ export class PUPHub extends LPF2Hub { if (time) { let data = null; if (portObj.id === "AB") { - if (speed instanceof Array) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]); - } else { - data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]); - } + data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed)]); } else { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]); @@ -118,11 +114,7 @@ export class PUPHub extends LPF2Hub { } else { let data = null; if (portObj.id === "AB") { - if (speed instanceof Array) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]); - } else { - data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]); - } + data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed)]); } else { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);