From feb00741c914a09bda0cc4b9ff433c9f90a64b86 Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Thu, 21 Jun 2018 09:29:16 +0100 Subject: [PATCH] Chaining instructions --- boosthub.ts | 79 +++++++++++++++++++++++++++++++++++------------------ port.ts | 4 +++ wedo2hub.ts | 53 ++++++++++++++++++++--------------- 3 files changed, 87 insertions(+), 49 deletions(-) diff --git a/boosthub.ts b/boosthub.ts index 3702a37..4fdebc7 100644 --- a/boosthub.ts +++ b/boosthub.ts @@ -60,16 +60,19 @@ export class BoostHub extends Hub { * @param {number} color - A number representing one of the LED color consts. */ public setLEDColor (color: number | boolean) { - 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; + 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); } - data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]); - characteristic.write(data, false); - } + return resolve(); + }); } @@ -92,17 +95,25 @@ export class BoostHub extends Hub { * @param {number} [time] - How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. */ public setMotorSpeed (port: string, speed: number, time: number) { - const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; - if (characteristic) { - if (time) { - const data = Buffer.from([0x0c, 0x00, 0x81, this._ports[port].value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]); - data.writeUInt16LE(time > 65535 ? 65535 : time, 6); - characteristic.write(data, false); - } else { - const data = Buffer.from([0x0a, 0x00, 0x81, this._ports[port].value, 0x11, 0x01, speed, 0x64, 0x7f, 0x03]); - characteristic.write(data, false); + 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, 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, speed, 0x64, 0x7f, 0x03]); + characteristic.write(data, false); + return resolve(); + } } - } + }); } @@ -114,13 +125,20 @@ export class BoostHub extends Hub { * @param {number} [speed=100] - How fast the motor should be rotated. */ public setMotorAngle (port: string, angle: number, speed: number = 100) { - const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; - if (characteristic) { - const data = Buffer.from([0x0e, 0x00, 0x81, this._ports[port].value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]); - data.writeUInt32LE(angle, 6); - data.writeInt8(speed, 10); - characteristic.write(data, false); - } + 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.writeInt8(speed, 10); + characteristic.write(data, false); + portObj.finished = () => { + return resolve(); + }; + } + }); } @@ -186,6 +204,7 @@ export class BoostHub extends Hub { case 0x82: { this._parsePortAction(data); + break; } } } @@ -233,7 +252,13 @@ export class BoostHub extends Hub { return; } - // NK: Handle callbacks when port finished here. + if (data[4] === 0x0a) { + port.busy = false; + if (port.finished) { + port.finished(); + port.finished = null; + } + } } diff --git a/port.ts b/port.ts index 9ea9979..2f3ae31 100644 --- a/port.ts +++ b/port.ts @@ -8,12 +8,16 @@ export class Port { public value: number; public connected: boolean; public type: Consts.Devices; + public busy: boolean; + public finished: (() => void) | null; constructor (id: string, value: number) { this.id = id; this.value = value; this.connected = false; + this.busy = false; + this.finished = null; this.type = Consts.Devices.UNKNOWN; } diff --git a/wedo2hub.ts b/wedo2hub.ts index 132ed5e..27f59a5 100644 --- a/wedo2hub.ts +++ b/wedo2hub.ts @@ -56,17 +56,20 @@ export class WeDo2Hub extends Hub { * @param {number} color - A number representing one of the LED color consts. */ public setLEDColor (color: number | boolean) { - const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; - const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; - if (motorCharacteristic && portCharacteristic) { - let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); - portCharacteristic.write(data, false); - if (color === false) { - color = 0; + return new Promise((resolve, reject) => { + const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; + const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; + if (motorCharacteristic && portCharacteristic) { + let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); + portCharacteristic.write(data, false); + if (color === false) { + color = 0; + } + data = Buffer.from([0x06, 0x04, 0x01, color]); + motorCharacteristic.write(data, false); + return resolve(); } - data = Buffer.from([0x06, 0x04, 0x01, color]); - motorCharacteristic.write(data, false); - } + }); } @@ -78,14 +81,17 @@ export class WeDo2Hub extends Hub { * @param {number} blue */ public setLEDRGB (red: number, green: number, blue: number) { - const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; - const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; - if (motorCharacteristic && portCharacteristic) { - const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]); - portCharacteristic.write(data1, false); - const data2 = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); - motorCharacteristic.write(data2, false); - } + return new Promise((resolve, reject) => { + const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; + const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; + if (motorCharacteristic && portCharacteristic) { + const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]); + portCharacteristic.write(data1, false); + const data2 = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); + motorCharacteristic.write(data2, false); + return resolve(); + } + }); } @@ -96,10 +102,13 @@ export class WeDo2Hub extends Hub { * @param {number} speed - For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. */ public setMotorSpeed (port: string, speed: number) { - const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; - if (characteristic) { - characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]), false); - } + return new Promise((resolve, reject) => { + const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; + if (characteristic) { + characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]), false); + return resolve(); + } + }); }