From d515fcd66205f34514117b8e43100540fb2d2b86 Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Thu, 28 Jun 2018 17:58:26 +0100 Subject: [PATCH] _writeMessage function for LPF2Hub --- lpf2hub.ts | 88 ++++++++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 53 deletions(-) diff --git a/lpf2hub.ts b/lpf2hub.ts index e0e1e00..4bb9cec 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -73,7 +73,7 @@ export class LPF2Hub extends Hub { await super.connect(); const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); - characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]), false); + this._writeMessage(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); debug("Connect completed"); return resolve(); }); @@ -88,32 +88,18 @@ export class LPF2Hub extends Hub { */ public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { - const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; - if (characteristic) { - let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]); - characteristic.write(data, false); - if (color === false) { - color = 0; - } - data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]); - characteristic.write(data, false); + let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]); + this._writeMessage(data); + if (color === false) { + color = 0; } + data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]); + this._writeMessage(data); return resolve(); }); } - // setLEDRGB (red, green, blue) { - // const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL]; - // if (characteristic) { - // let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x03]); - // characteristic.write(data); - // data = Buffer.from([0x0a, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, red, green, blue]); - // characteristic.write(data); - // } - // } - - /** * Set the motor speed on a given port. * @method LPF2Hub#setMotorSpeed @@ -124,22 +110,19 @@ export class LPF2Hub extends Hub { */ public setMotorSpeed (port: string, speed: number, time: number) { return new Promise((resolve, reject) => { - const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; - if (characteristic) { - const portObj = this._ports[port]; - if (time) { - portObj.busy = true; - const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - data.writeUInt16LE(time > 65535 ? 65535 : time, 6); - characteristic.write(data, false); - portObj.finished = () => { - return resolve(); - }; - } else { - const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - characteristic.write(data, false); + const portObj = this._ports[port]; + if (time) { + portObj.busy = true; + const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + data.writeUInt16LE(time > 65535 ? 65535 : time, 6); + this._writeMessage(data); + portObj.finished = () => { return resolve(); - } + }; + } else { + const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + this._writeMessage(data); + return resolve(); } }); } @@ -155,34 +138,33 @@ export class LPF2Hub extends Hub { */ public setMotorAngle (port: string, angle: number, speed: number = 100) { return new Promise((resolve, reject) => { - const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; - if (characteristic) { - const portObj = this._ports[port]; - portObj.busy = true; - const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]); - data.writeUInt32LE(angle, 6); - data.writeUInt8(this._mapSpeed(speed), 10); - characteristic.write(data, false); - portObj.finished = () => { - return resolve(); - }; - } + const portObj = this._ports[port]; + portObj.busy = true; + const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]); + data.writeUInt32LE(angle, 6); + data.writeUInt8(this._mapSpeed(speed), 10); + this._writeMessage(data); + portObj.finished = () => { + return resolve(); + }; }); } protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { - const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; - if (characteristic) { - characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), false, callback); - } + this._writeMessage(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); } protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { + this._writeMessage(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); + } + + + private _writeMessage (message: Buffer, callback?: () => void) { const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; if (characteristic) { - characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), false, callback); + characteristic.write(message, false, callback); } }