Chaining instructions

This commit is contained in:
Nathan Kunicki 2018-06-21 09:29:16 +01:00
parent 3d9f477c0c
commit feb00741c9
3 changed files with 87 additions and 49 deletions

View File

@ -60,16 +60,19 @@ export class BoostHub extends Hub {
* @param {number} color - A number representing one of the LED color consts. * @param {number} color - A number representing one of the LED color consts.
*/ */
public setLEDColor (color: number | boolean) { public setLEDColor (color: number | boolean) {
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; return new Promise((resolve, reject) => {
if (characteristic) { const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]); if (characteristic) {
characteristic.write(data, false); let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
if (color === false) { characteristic.write(data, false);
color = 0; 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]); return resolve();
characteristic.write(data, false); });
}
} }
@ -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. * @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) { public setMotorSpeed (port: string, speed: number, time: number) {
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; return new Promise((resolve, reject) => {
if (characteristic) { const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (time) { if (characteristic) {
const data = Buffer.from([0x0c, 0x00, 0x81, this._ports[port].value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]); const portObj = this._ports[port];
data.writeUInt16LE(time > 65535 ? 65535 : time, 6); if (time) {
characteristic.write(data, false); portObj.busy = true;
} else { const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]);
const data = Buffer.from([0x0a, 0x00, 0x81, this._ports[port].value, 0x11, 0x01, speed, 0x64, 0x7f, 0x03]); data.writeUInt16LE(time > 65535 ? 65535 : time, 6);
characteristic.write(data, false); 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. * @param {number} [speed=100] - How fast the motor should be rotated.
*/ */
public setMotorAngle (port: string, angle: number, speed: number = 100) { public setMotorAngle (port: string, angle: number, speed: number = 100) {
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; return new Promise((resolve, reject) => {
if (characteristic) { const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
const data = Buffer.from([0x0e, 0x00, 0x81, this._ports[port].value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]); if (characteristic) {
data.writeUInt32LE(angle, 6); const portObj = this._ports[port];
data.writeInt8(speed, 10); portObj.busy = true;
characteristic.write(data, false); 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: case 0x82:
{ {
this._parsePortAction(data); this._parsePortAction(data);
break;
} }
} }
} }
@ -233,7 +252,13 @@ export class BoostHub extends Hub {
return; return;
} }
// NK: Handle callbacks when port finished here. if (data[4] === 0x0a) {
port.busy = false;
if (port.finished) {
port.finished();
port.finished = null;
}
}
} }

View File

@ -8,12 +8,16 @@ export class Port {
public value: number; public value: number;
public connected: boolean; public connected: boolean;
public type: Consts.Devices; public type: Consts.Devices;
public busy: boolean;
public finished: (() => void) | null;
constructor (id: string, value: number) { constructor (id: string, value: number) {
this.id = id; this.id = id;
this.value = value; this.value = value;
this.connected = false; this.connected = false;
this.busy = false;
this.finished = null;
this.type = Consts.Devices.UNKNOWN; this.type = Consts.Devices.UNKNOWN;
} }

View File

@ -56,17 +56,20 @@ export class WeDo2Hub extends Hub {
* @param {number} color - A number representing one of the LED color consts. * @param {number} color - A number representing one of the LED color consts.
*/ */
public setLEDColor (color: number | boolean) { public setLEDColor (color: number | boolean) {
const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; return new Promise((resolve, reject) => {
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
if (motorCharacteristic && portCharacteristic) { const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); if (motorCharacteristic && portCharacteristic) {
portCharacteristic.write(data, false); let data = Buffer.from([0x06, 0x17, 0x01, 0x01]);
if (color === false) { portCharacteristic.write(data, false);
color = 0; 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 * @param {number} blue
*/ */
public setLEDRGB (red: number, green: number, blue: number) { public setLEDRGB (red: number, green: number, blue: number) {
const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; return new Promise((resolve, reject) => {
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
if (motorCharacteristic && portCharacteristic) { const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]); if (motorCharacteristic && portCharacteristic) {
portCharacteristic.write(data1, false); const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]);
const data2 = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); portCharacteristic.write(data1, false);
motorCharacteristic.write(data2, 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. * @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) { public setMotorSpeed (port: string, speed: number) {
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; return new Promise((resolve, reject) => {
if (characteristic) { const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]), false); if (characteristic) {
} characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]), false);
return resolve();
}
});
} }