Chaining instructions
This commit is contained in:
parent
3d9f477c0c
commit
feb00741c9
33
boosthub.ts
33
boosthub.ts
@ -60,6 +60,7 @@ 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) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
||||||
if (characteristic) {
|
if (characteristic) {
|
||||||
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
|
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
|
||||||
@ -70,6 +71,8 @@ export class BoostHub extends Hub {
|
|||||||
data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]);
|
data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]);
|
||||||
characteristic.write(data, false);
|
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.
|
* @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) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
||||||
if (characteristic) {
|
if (characteristic) {
|
||||||
|
const portObj = this._ports[port];
|
||||||
if (time) {
|
if (time) {
|
||||||
const data = Buffer.from([0x0c, 0x00, 0x81, this._ports[port].value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]);
|
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);
|
data.writeUInt16LE(time > 65535 ? 65535 : time, 6);
|
||||||
characteristic.write(data, false);
|
characteristic.write(data, false);
|
||||||
|
portObj.finished = () => {
|
||||||
|
return resolve();
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
const data = Buffer.from([0x0a, 0x00, 0x81, this._ports[port].value, 0x11, 0x01, speed, 0x64, 0x7f, 0x03]);
|
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, speed, 0x64, 0x7f, 0x03]);
|
||||||
characteristic.write(data, false);
|
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) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
||||||
if (characteristic) {
|
if (characteristic) {
|
||||||
const data = Buffer.from([0x0e, 0x00, 0x81, this._ports[port].value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]);
|
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.writeUInt32LE(angle, 6);
|
||||||
data.writeInt8(speed, 10);
|
data.writeInt8(speed, 10);
|
||||||
characteristic.write(data, false);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
port.ts
4
port.ts
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,6 +56,7 @@ 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) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
|
const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
|
||||||
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
||||||
if (motorCharacteristic && portCharacteristic) {
|
if (motorCharacteristic && portCharacteristic) {
|
||||||
@ -66,7 +67,9 @@ export class WeDo2Hub extends Hub {
|
|||||||
}
|
}
|
||||||
data = Buffer.from([0x06, 0x04, 0x01, color]);
|
data = Buffer.from([0x06, 0x04, 0x01, color]);
|
||||||
motorCharacteristic.write(data, false);
|
motorCharacteristic.write(data, false);
|
||||||
|
return resolve();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -78,6 +81,7 @@ 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) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
|
const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
|
||||||
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
||||||
if (motorCharacteristic && portCharacteristic) {
|
if (motorCharacteristic && portCharacteristic) {
|
||||||
@ -85,7 +89,9 @@ export class WeDo2Hub extends Hub {
|
|||||||
portCharacteristic.write(data1, false);
|
portCharacteristic.write(data1, false);
|
||||||
const data2 = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
|
const data2 = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
|
||||||
motorCharacteristic.write(data2, false);
|
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) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
|
||||||
if (characteristic) {
|
if (characteristic) {
|
||||||
characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]), false);
|
characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]), false);
|
||||||
|
return resolve();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user