Potentially working?
This commit is contained in:
parent
97e48a8c6a
commit
ef69af5c5f
@ -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.
|
* @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.
|
* @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);
|
const portObj = this._portLookup(port);
|
||||||
if (portObj.id !== "AB" && speed instanceof Array) {
|
if (portObj.id !== "AB" && speed instanceof Array) {
|
||||||
throw new Error(`Port ${portObj.id} can only accept a single speed`);
|
throw new Error(`Port ${portObj.id} can only accept a single speed`);
|
||||||
}
|
}
|
||||||
portObj.cancelEventTimer();
|
if (typeof time === "boolean") {
|
||||||
|
if (time === true) {
|
||||||
|
portObj.cancelEventTimer();
|
||||||
|
}
|
||||||
|
time = undefined;
|
||||||
|
}
|
||||||
return new Promise((resolve, reject) => {
|
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) {
|
if (portObj.type === Consts.Devices.BOOST_TACHO_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) {
|
||||||
portObj.busy = true;
|
portObj.busy = true;
|
||||||
@ -83,10 +88,11 @@ export class BoostMoveHub extends LPF2Hub {
|
|||||||
// @ts-ignore: The type of speed is properly checked at the start
|
// @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)]);
|
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
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]);
|
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||||
return resolve();
|
return resolve();
|
||||||
|
// @ts-ignore: The type of time is properly checked at the start
|
||||||
}, time);
|
}, time);
|
||||||
portObj.setEventTimer(timeout);
|
portObj.setEventTimer(timeout);
|
||||||
}
|
}
|
||||||
@ -132,7 +138,7 @@ export class BoostMoveHub extends LPF2Hub {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
|
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
|
||||||
.on("changeSpeed", (speed) => {
|
.on("changeSpeed", (speed) => {
|
||||||
this.setMotorSpeed(port, speed);
|
this.setMotorSpeed(port, speed, true);
|
||||||
})
|
})
|
||||||
.on("finished", resolve);
|
.on("finished", resolve);
|
||||||
});
|
});
|
||||||
@ -188,7 +194,7 @@ export class BoostMoveHub extends LPF2Hub {
|
|||||||
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]);
|
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||||
if (time) {
|
if (time) {
|
||||||
const timeout = setTimeout(() => {
|
const timeout = global.setTimeout(() => {
|
||||||
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
|
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||||
return resolve();
|
return resolve();
|
||||||
|
2
hub.ts
2
hub.ts
@ -199,7 +199,7 @@ export class Hub extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
public sleep (delay: number) {
|
public sleep (delay: number) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
setTimeout(resolve, delay);
|
global.setTimeout(resolve, delay);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
puphub.ts
17
puphub.ts
@ -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.
|
* @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.
|
* @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);
|
const portObj = this._portLookup(port);
|
||||||
if (portObj.id !== "AB" && speed instanceof Array) {
|
if (portObj.id !== "AB" && speed instanceof Array) {
|
||||||
throw new Error(`Port ${portObj.id} can only accept a single speed`);
|
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`);
|
throw new Error(`Port ${portObj.id} requires both motors be of the same type`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
portObj.cancelEventTimer();
|
if (typeof time === "boolean") {
|
||||||
|
if (time === true) {
|
||||||
|
portObj.cancelEventTimer();
|
||||||
|
}
|
||||||
|
time = undefined;
|
||||||
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (time) {
|
if (time && typeof time === "number") {
|
||||||
let data = null;
|
let data = null;
|
||||||
if (portObj.id === "AB") {
|
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)]);
|
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]);
|
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
||||||
}
|
}
|
||||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||||
const timeout = setTimeout(() => {
|
const timeout = global.setTimeout(() => {
|
||||||
let data = null;
|
let data = null;
|
||||||
if (portObj.id === "AB") {
|
if (portObj.id === "AB") {
|
||||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, 0x00, 0x00]);
|
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, 0x00, 0x00]);
|
||||||
@ -126,7 +131,7 @@ export class PUPHub extends LPF2Hub {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
|
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
|
||||||
.on("changeSpeed", (speed) => {
|
.on("changeSpeed", (speed) => {
|
||||||
this.setMotorSpeed(port, speed);
|
this.setMotorSpeed(port, speed, true);
|
||||||
})
|
})
|
||||||
.on("finished", resolve);
|
.on("finished", resolve);
|
||||||
});
|
});
|
||||||
@ -148,7 +153,7 @@ export class PUPHub extends LPF2Hub {
|
|||||||
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]);
|
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||||
if (time) {
|
if (time) {
|
||||||
const timeout = setTimeout(() => {
|
const timeout = global.setTimeout(() => {
|
||||||
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
|
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||||
return resolve();
|
return resolve();
|
||||||
|
@ -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.
|
* @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.
|
* @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);
|
const portObj = this._portLookup(port);
|
||||||
portObj.cancelEventTimer();
|
if (typeof time === "boolean") {
|
||||||
|
if (time === true) {
|
||||||
|
portObj.cancelEventTimer();
|
||||||
|
}
|
||||||
|
time = undefined;
|
||||||
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)]));
|
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)]));
|
||||||
if (time) {
|
if (time && typeof time === "number") {
|
||||||
const timeout = setTimeout(() => {
|
const timeout = global.setTimeout(() => {
|
||||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00]));
|
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00]));
|
||||||
return resolve();
|
return resolve();
|
||||||
}, time);
|
}, time);
|
||||||
@ -157,7 +162,7 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
|
this._calculateRamp(fromSpeed, toSpeed, time, portObj)
|
||||||
.on("changeSpeed", (speed) => {
|
.on("changeSpeed", (speed) => {
|
||||||
this.setMotorSpeed(port, speed);
|
this.setMotorSpeed(port, speed, true);
|
||||||
})
|
})
|
||||||
.on("finished", resolve);
|
.on("finished", resolve);
|
||||||
});
|
});
|
||||||
@ -177,7 +182,7 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
data.writeUInt16LE(frequency, 3);
|
data.writeUInt16LE(frequency, 3);
|
||||||
data.writeUInt16LE(time, 5);
|
data.writeUInt16LE(time, 5);
|
||||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
|
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]);
|
const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
|
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||||
if (time) {
|
if (time) {
|
||||||
const timeout = setTimeout(() => {
|
const timeout = global.setTimeout(() => {
|
||||||
const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]);
|
const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
|
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||||
return resolve();
|
return resolve();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user