Added artificial (read: client side) timing for motors on the WeDo 2.0 Smart Hub

This commit is contained in:
Nathan Kunicki 2018-07-26 18:12:31 +01:00
parent 137929607f
commit 1f0f007e9d
4 changed files with 39 additions and 32 deletions

29
DOCS.md
View File

@ -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) ⇒ <code>Promise</code>
* [.setLEDRGB(red, green, blue)](#WeDo2Hub+setLEDRGB) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#WeDo2Hub+setMotorSpeed) ⇒ <code>Promise</code>
* [.playSound(frequency, time)](#WeDo2Hub+playSound) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed)](#WeDo2Hub+setMotorSpeed) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
@ -343,6 +343,20 @@ Set the color of the LED on the Hub via RGB values.
| green | <code>number</code> |
| blue | <code>number</code> |
<a name="WeDo2Hub+setMotorSpeed"></a>
### weDo2Hub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| speed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| [time] | <code>number</code> | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. |
<a name="WeDo2Hub+playSound"></a>
### weDo2Hub.playSound(frequency, time) ⇒ <code>Promise</code>
@ -356,19 +370,6 @@ Play a sound on the Hub's in-built buzzer
| frequency | <code>number</code> | |
| time | <code>number</code> | How long the sound should play for (in milliseconds). |
<a name="WeDo2Hub+setMotorSpeed"></a>
### weDo2Hub.setMotorSpeed(port, speed) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| speed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
<a name="Hub+connect"></a>
### weDo2Hub.connect() ⇒ <code>Promise</code>

View File

@ -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) ⇒ <code>Promise</code>
* [.setLEDRGB(red, green, blue)](#WeDo2Hub+setLEDRGB) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed, [time])](#WeDo2Hub+setMotorSpeed) ⇒ <code>Promise</code>
* [.playSound(frequency, time)](#WeDo2Hub+playSound) ⇒ <code>Promise</code>
* [.setMotorSpeed(port, speed)](#WeDo2Hub+setMotorSpeed) ⇒ <code>Promise</code>
* [.connect()](#Hub+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Hub+disconnect) ⇒ <code>Promise</code>
* [.subscribe(port, [mode])](#Hub+subscribe) ⇒ <code>Promise</code>
@ -393,6 +393,20 @@ Set the color of the LED on the Hub via RGB values.
| green | <code>number</code> |
| blue | <code>number</code> |
<a name="WeDo2Hub+setMotorSpeed"></a>
### weDo2Hub.setMotorSpeed(port, speed, [time]) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| speed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
| [time] | <code>number</code> | How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. |
<a name="WeDo2Hub+playSound"></a>
### weDo2Hub.playSound(frequency, time) ⇒ <code>Promise</code>
@ -406,19 +420,6 @@ Play a sound on the Hub's in-built buzzer
| frequency | <code>number</code> | |
| time | <code>number</code> | How long the sound should play for (in milliseconds). |
<a name="WeDo2Hub+setMotorSpeed"></a>
### weDo2Hub.setMotorSpeed(port, speed) ⇒ <code>Promise</code>
Set the motor speed on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| speed | <code>number</code> | For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. |
<a name="Hub+connect"></a>
### weDo2Hub.connect() ⇒ <code>Promise</code>

View File

@ -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) {

View File

@ -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();
}
});
}