From 705fbd341a17c2dd22e834e5acff0f2caa24732e Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 19 Dec 2019 10:46:38 -0800 Subject: [PATCH] writeDirect function on device --- src/devices/basicmotor.ts | 8 +------- src/devices/device.ts | 8 ++++++++ src/devices/hubled.ts | 25 ++----------------------- src/devices/light.ts | 8 +------- src/index-browser.ts | 2 +- src/index-node.ts | 2 +- src/poweredup-browser.ts | 2 +- src/poweredup-node.ts | 2 +- 8 files changed, 16 insertions(+), 41 deletions(-) diff --git a/src/devices/basicmotor.ts b/src/devices/basicmotor.ts index ad203d7..eb3be90 100644 --- a/src/devices/basicmotor.ts +++ b/src/devices/basicmotor.ts @@ -22,13 +22,7 @@ export class BasicMotor extends Device { */ public setPower (power: number) { return new Promise((resolve) => { - if (this.isWeDo2SmartHub) { - const data = Buffer.from([this.portId, 0x01, 0x02, mapSpeed(power)]); - this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - } else { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(power)]); - this.send(data); - } + this.writeDirect(0x00, Buffer.from([mapSpeed(power)])); return resolve(); }); } diff --git a/src/devices/device.ts b/src/devices/device.ts index dc98815..cf2d617 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -77,6 +77,14 @@ export class Device extends EventEmitter { return this._isWeDo2SmartHub; } + public writeDirect (mode: number, data: Buffer, callback?: () => void) { + if (this.isWeDo2SmartHub) { + this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + } else { + this.send(Buffer.concat([Buffer.from([0x81, this.portId, 0x11, 0x51, mode]), data]), Consts.BLECharacteristic.LPF2_ALL, callback); + } + } + public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) { this._ensureConnected(); this.hub.send(data, characteristic, callback); diff --git a/src/devices/hubled.ts b/src/devices/hubled.ts index b7c2605..2275cec 100644 --- a/src/devices/hubled.ts +++ b/src/devices/hubled.ts @@ -26,7 +26,7 @@ export class HubLED extends Device { if (typeof color === "boolean") { color = 0; } - this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, color])); + this.writeDirect(0x00, Buffer.from([color])); return resolve(); }); } @@ -45,28 +45,7 @@ export class HubLED extends Device { if (this.mode !== HubLED.Mode.RGB) { this.subscribe(HubLED.Mode.RGB); } - this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x01, red, green, blue])); - return resolve(); - }); - } - - - - /** - * Set the light brightness. - * @method Light#brightness - * @param {number} brightness Brightness value between 0-100 (0 is off) - * @returns {Promise} Resolved upon successful completion of command. - */ - public setBrightness (brightness: number) { - return new Promise((resolve) => { - if (this.isWeDo2SmartHub) { - const data = Buffer.from([this.portId, 0x01, 0x02, brightness]); - this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - } else { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]); - this.send(data); - } + this.writeDirect(0x00, Buffer.from([red, green, blue])); return resolve(); }); } diff --git a/src/devices/light.ts b/src/devices/light.ts index 5021615..8d2ae6b 100644 --- a/src/devices/light.ts +++ b/src/devices/light.ts @@ -20,13 +20,7 @@ export class Light extends Device { */ public setBrightness (brightness: number) { return new Promise((resolve) => { - if (this.isWeDo2SmartHub) { - const data = Buffer.from([this.portId, 0x01, 0x02, brightness]); - this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - } else { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]); - this.send(data); - } + this.writeDirect(0x00, Buffer.from([brightness])); return resolve(); }); } diff --git a/src/index-browser.ts b/src/index-browser.ts index d38f8a3..ae5f377 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -4,8 +4,8 @@ import { PoweredUP } from "./poweredup-browser"; import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; -import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; +import { MoveHub } from "./hubs/movehub"; import { RemoteControl } from "./hubs/remotecontrol"; import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; diff --git a/src/index-node.ts b/src/index-node.ts index ce294e9..ca403d1 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -4,8 +4,8 @@ import { PoweredUP } from "./poweredup-node"; import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; -import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; +import { MoveHub } from "./hubs/movehub"; import { RemoteControl } from "./hubs/remotecontrol"; import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; diff --git a/src/poweredup-browser.ts b/src/poweredup-browser.ts index 339a1b5..f986034 100644 --- a/src/poweredup-browser.ts +++ b/src/poweredup-browser.ts @@ -2,8 +2,8 @@ import { WebBLEDevice } from "./webbleabstraction"; import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; -import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; +import { MoveHub } from "./hubs/movehub"; import { RemoteControl } from "./hubs/remotecontrol"; import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; diff --git a/src/poweredup-node.ts b/src/poweredup-node.ts index 798a77d..2e95d11 100644 --- a/src/poweredup-node.ts +++ b/src/poweredup-node.ts @@ -4,8 +4,8 @@ import { NobleDevice } from "./nobleabstraction"; import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; -import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; +import { MoveHub } from "./hubs/movehub"; import { RemoteControl } from "./hubs/remotecontrol"; import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub";