Potentially working?

This commit is contained in:
Nathan Kellenicki 2018-08-28 20:28:39 -07:00
parent 97e48a8c6a
commit ef69af5c5f
4 changed files with 36 additions and 20 deletions

View File

@ -56,14 +56,19 @@ export class BoostMoveHub extends LPF2Hub {
* @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
*/
public setMotorSpeed (port: string, speed: number | [number, number], time?: number) {
public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) {
const portObj = this._portLookup(port);
if (portObj.id !== "AB" && speed instanceof Array) {
throw new Error(`Port ${portObj.id} can only accept a single speed`);
}
if (typeof time === "boolean") {
if (time === true) {
portObj.cancelEventTimer();
}
time = undefined;
}
return new Promise((resolve, reject) => {
if (time) {
if (time && typeof time === "number") {
if (portObj.type === Consts.Devices.BOOST_TACHO_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) {
portObj.busy = true;
@ -83,10 +88,11 @@ export class BoostMoveHub extends LPF2Hub {
// @ts-ignore: The type of speed is properly checked at the start
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
const timeout = setTimeout(() => {
const timeout = global.setTimeout(() => {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();
// @ts-ignore: The type of time is properly checked at the start
}, time);
portObj.setEventTimer(timeout);
}
@ -132,7 +138,7 @@ export class BoostMoveHub extends LPF2Hub {
return new Promise((resolve, reject) => {
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
.on("changeSpeed", (speed) => {
this.setMotorSpeed(port, speed);
this.setMotorSpeed(port, speed, true);
})
.on("finished", resolve);
});
@ -188,7 +194,7 @@ export class BoostMoveHub extends LPF2Hub {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
if (time) {
const timeout = setTimeout(() => {
const timeout = global.setTimeout(() => {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();

2
hub.ts
View File

@ -199,7 +199,7 @@ export class Hub extends EventEmitter {
*/
public sleep (delay: number) {
return new Promise((resolve) => {
setTimeout(resolve, delay);
global.setTimeout(resolve, delay);
});
}

View File

@ -62,7 +62,7 @@ export class PUPHub extends LPF2Hub {
* @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
*/
public setMotorSpeed (port: string, speed: number | [number, number], time?: number) {
public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) {
const portObj = this._portLookup(port);
if (portObj.id !== "AB" && speed instanceof Array) {
throw new Error(`Port ${portObj.id} can only accept a single speed`);
@ -74,9 +74,14 @@ export class PUPHub extends LPF2Hub {
throw new Error(`Port ${portObj.id} requires both motors be of the same type`);
}
}
if (typeof time === "boolean") {
if (time === true) {
portObj.cancelEventTimer();
}
time = undefined;
}
return new Promise((resolve, reject) => {
if (time) {
if (time && typeof time === "number") {
let data = null;
if (portObj.id === "AB") {
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed)]);
@ -85,7 +90,7 @@ export class PUPHub extends LPF2Hub {
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
}
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
const timeout = setTimeout(() => {
const timeout = global.setTimeout(() => {
let data = null;
if (portObj.id === "AB") {
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, 0x00, 0x00]);
@ -126,7 +131,7 @@ export class PUPHub extends LPF2Hub {
return new Promise((resolve, reject) => {
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
.on("changeSpeed", (speed) => {
this.setMotorSpeed(port, speed);
this.setMotorSpeed(port, speed, true);
})
.on("finished", resolve);
});
@ -148,7 +153,7 @@ export class PUPHub extends LPF2Hub {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
if (time) {
const timeout = setTimeout(() => {
const timeout = global.setTimeout(() => {
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve();

View File

@ -124,13 +124,18 @@ export class WeDo2SmartHub extends Hub {
* @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
*/
public setMotorSpeed (port: string, speed: number, time?: number) {
public setMotorSpeed (port: string, speed: number, time?: number | boolean) {
const portObj = this._portLookup(port);
if (typeof time === "boolean") {
if (time === true) {
portObj.cancelEventTimer();
}
time = undefined;
}
return new Promise((resolve, reject) => {
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)]));
if (time) {
const timeout = setTimeout(() => {
if (time && typeof time === "number") {
const timeout = global.setTimeout(() => {
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00]));
return resolve();
}, time);
@ -157,7 +162,7 @@ export class WeDo2SmartHub extends Hub {
return new Promise((resolve, reject) => {
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
.on("changeSpeed", (speed) => {
this.setMotorSpeed(port, speed);
this.setMotorSpeed(port, speed, true);
})
.on("finished", resolve);
});
@ -177,7 +182,7 @@ export class WeDo2SmartHub extends Hub {
data.writeUInt16LE(frequency, 3);
data.writeUInt16LE(time, 5);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
setTimeout(resolve, time);
global.setTimeout(resolve, time);
});
}
@ -196,7 +201,7 @@ export class WeDo2SmartHub extends Hub {
const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
if (time) {
const timeout = setTimeout(() => {
const timeout = global.setTimeout(() => {
const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
return resolve();