diff --git a/src/devices/basicmotor.ts b/src/devices/basicmotor.ts index eb3be90..922d00f 100644 --- a/src/devices/basicmotor.ts +++ b/src/devices/basicmotor.ts @@ -15,8 +15,8 @@ export class BasicMotor extends Device { /** - * Set the motor speed. - * @method BasicMotor#power + * Set the motor power. + * @method BasicMotor#setPower * @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. * @returns {Promise} Resolved upon successful completion of command. */ diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index 3b22e50..a675a5b 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -27,6 +27,46 @@ export class TachoMotor extends BasicMotor { } } + /** + * Set the motor speed. + * @method TachoMotor#setSpeed + * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + * @returns {Promise} Resolved upon successful completion of command. + */ + public setSpeed (speed: [number, number] | number, time: number | undefined) { + if (!this.isVirtualPort && speed instanceof Array) { + throw new Error("Only virtual ports can accept multiple speeds"); + } + if (this.isWeDo2SmartHub) { + throw new Error("Motor speed is not available on the WeDo 2.0 Smart Hub"); + } + return new Promise((resolve) => { + this._busy = true; + if (speed === undefined || speed === null) { + speed = 100; + } + let message; + if (time !== undefined) { + if (speed instanceof Array) { + message = Buffer.from([0x81, this.portId, 0x11, 0x0a, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); + } else { + message = Buffer.from([0x81, this.portId, 0x11, 0x09, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + } + message.writeUInt16LE(time, 4); + } else { + if (speed instanceof Array) { + message = Buffer.from([0x81, this.portId, 0x11, 0x08, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); + } else { + message = Buffer.from([0x81, this.portId, 0x11, 0x07, mapSpeed(speed), 0x64, 0x03, 0x64, 0x7f, 0x03]); + } + } + this.send(message); + this._finished = () => { + return resolve(); + }; + }); + } + /** * Rotate a motor by a given angle. * @method TachoMotor#rotateByAngle @@ -34,13 +74,24 @@ export class TachoMotor extends BasicMotor { * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ - public rotateByAngle (angle: number, speed: number = 100) { + public rotateByAngle (angle: number, speed: [number, number] | number) { + if (!this.isVirtualPort && speed instanceof Array) { + throw new Error("Only virtual ports can accept multiple speeds"); + } if (this.isWeDo2SmartHub) { throw new Error("Angle rotation is not available on the WeDo 2.0 Smart Hub"); } return new Promise((resolve) => { this._busy = true; - const message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + if (speed === undefined || speed === null) { + speed = 100; + } + let message; + if (speed instanceof Array) { + message = Buffer.from([0x81, this.portId, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); + } else { + message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + } message.writeUInt32LE(angle, 4); this.send(message); this._finished = () => { diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index bd56b4e..2f503df 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -106,7 +106,7 @@ export class LPF2Hub extends BaseHub { if (firstDevice.type !== secondDevice.type) { throw new Error(`Both devices must be of the same type to create a virtual port`); } - this.send(Buffer.from([0x61, 0x01, firstPortId, secondPortId]), Consts.BLECharacteristic.LPF2_ALL); + this.send(Buffer.from([0x61, 0x01, firstDevice.portId, secondDevice.portId]), Consts.BLECharacteristic.LPF2_ALL); }