diff --git a/DOCS.md b/DOCS.md index 43a94d6..cd48484 100644 --- a/DOCS.md +++ b/DOCS.md @@ -296,8 +296,8 @@ Emits when an attached motor or sensor is detached from the Hub. * [new WeDo2Hub()](#new_WeDo2Hub_new) * [.setLEDColor(color)](#WeDo2Hub+setLEDColor) ⇒ Promise * [.setLEDRGB(red, green, blue)](#WeDo2Hub+setLEDRGB) ⇒ Promise + * [.setMotorSpeed(port, speed, [time])](#WeDo2Hub+setMotorSpeed) ⇒ Promise * [.playSound(frequency, time)](#WeDo2Hub+playSound) ⇒ Promise - * [.setMotorSpeed(port, speed)](#WeDo2Hub+setMotorSpeed) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise * [.disconnect()](#Hub+disconnect) ⇒ Promise * [.subscribe(port, [mode])](#Hub+subscribe) ⇒ Promise @@ -343,6 +343,20 @@ Set the color of the LED on the Hub via RGB values. | green | number | | blue | number | + + +### weDo2Hub.setMotorSpeed(port, speed, [time]) ⇒ Promise +Set the motor speed on a given port. + +**Kind**: instance method of [WeDo2Hub](#WeDo2Hub) +**Returns**: Promise - Resolved upon successful completion of command. If time is specified, this is once the motor is finished. + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| speed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | +| [time] | number | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. | + ### weDo2Hub.playSound(frequency, time) ⇒ Promise @@ -356,19 +370,6 @@ Play a sound on the Hub's in-built buzzer | frequency | number | | | time | number | How long the sound should play for (in milliseconds). | - - -### weDo2Hub.setMotorSpeed(port, speed) ⇒ Promise -Set the motor speed on a given port. - -**Kind**: instance method of [WeDo2Hub](#WeDo2Hub) -**Returns**: Promise - Resolved upon successful issuance of command. - -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| speed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | - ### weDo2Hub.connect() ⇒ Promise diff --git a/README.md b/README.md index 22a4ab5..ff48855 100644 --- a/README.md +++ b/README.md @@ -346,8 +346,8 @@ Emits when an attached motor or sensor is detached from the Hub. * [new WeDo2Hub()](#new_WeDo2Hub_new) * [.setLEDColor(color)](#WeDo2Hub+setLEDColor) ⇒ Promise * [.setLEDRGB(red, green, blue)](#WeDo2Hub+setLEDRGB) ⇒ Promise + * [.setMotorSpeed(port, speed, [time])](#WeDo2Hub+setMotorSpeed) ⇒ Promise * [.playSound(frequency, time)](#WeDo2Hub+playSound) ⇒ Promise - * [.setMotorSpeed(port, speed)](#WeDo2Hub+setMotorSpeed) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise * [.disconnect()](#Hub+disconnect) ⇒ Promise * [.subscribe(port, [mode])](#Hub+subscribe) ⇒ Promise @@ -393,6 +393,20 @@ Set the color of the LED on the Hub via RGB values. | green | number | | blue | number | + + +### weDo2Hub.setMotorSpeed(port, speed, [time]) ⇒ Promise +Set the motor speed on a given port. + +**Kind**: instance method of [WeDo2Hub](#WeDo2Hub) +**Returns**: Promise - Resolved upon successful completion of command. If time is specified, this is once the motor is finished. + +| Param | Type | Description | +| --- | --- | --- | +| port | string | | +| speed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | +| [time] | number | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. | + ### weDo2Hub.playSound(frequency, time) ⇒ Promise @@ -406,19 +420,6 @@ Play a sound on the Hub's in-built buzzer | frequency | number | | | time | number | How long the sound should play for (in milliseconds). | - - -### weDo2Hub.setMotorSpeed(port, speed) ⇒ Promise -Set the motor speed on a given port. - -**Kind**: instance method of [WeDo2Hub](#WeDo2Hub) -**Returns**: Promise - Resolved upon successful issuance of command. - -| Param | Type | Description | -| --- | --- | --- | -| port | string | | -| speed | number | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | - ### weDo2Hub.connect() ⇒ Promise diff --git a/lpf2hub.ts b/lpf2hub.ts index af4fac0..e88678f 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -116,7 +116,7 @@ export class LPF2Hub 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) { return new Promise((resolve, reject) => { const portObj = this._ports[port]; if (time) { diff --git a/wedo2hub.ts b/wedo2hub.ts index cb65de4..91ec104 100644 --- a/wedo2hub.ts +++ b/wedo2hub.ts @@ -95,12 +95,17 @@ export class WeDo2Hub extends Hub { * @method WeDo2Hub#setMotorSpeed * @param {string} port * @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 issuance of command. + * @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) { + public setMotorSpeed (port: string, speed: number, time?: number) { return new Promise((resolve, reject) => { this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)])); - return resolve(); + if (time) { + setTimeout(resolve, time); + } else { + return resolve(); + } }); }