Added buzzer support to the WeDo 2.0 Smart Hub
This commit is contained in:
parent
364607f1b1
commit
98620bce72
14
DOCS.md
14
DOCS.md
@ -296,6 +296,7 @@ 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>
|
||||
* [.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>
|
||||
@ -342,6 +343,19 @@ Set the color of the LED on the Hub via RGB values.
|
||||
| green | <code>number</code> |
|
||||
| blue | <code>number</code> |
|
||||
|
||||
<a name="WeDo2Hub+playSound"></a>
|
||||
|
||||
### weDo2Hub.playSound(frequency, time) ⇒ <code>Promise</code>
|
||||
Play a sound on the Hub's in-built buzzer
|
||||
|
||||
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
|
||||
**Returns**: <code>Promise</code> - Resolved upon successful completion of command (ie. once the sound has finished playing).
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| 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>
|
||||
|
14
README.md
14
README.md
@ -346,6 +346,7 @@ 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>
|
||||
* [.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>
|
||||
@ -392,6 +393,19 @@ Set the color of the LED on the Hub via RGB values.
|
||||
| green | <code>number</code> |
|
||||
| blue | <code>number</code> |
|
||||
|
||||
<a name="WeDo2Hub+playSound"></a>
|
||||
|
||||
### weDo2Hub.playSound(frequency, time) ⇒ <code>Promise</code>
|
||||
Play a sound on the Hub's in-built buzzer
|
||||
|
||||
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
|
||||
**Returns**: <code>Promise</code> - Resolved upon successful completion of command (ie. once the sound has finished playing).
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| 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>
|
||||
|
24
wedo2hub.ts
24
wedo2hub.ts
@ -6,6 +6,7 @@ import { Port } from "./port.js";
|
||||
import * as Consts from "./consts";
|
||||
|
||||
import Debug = require("debug");
|
||||
import { resolve } from "path";
|
||||
const debug = Debug("wedo2hub");
|
||||
|
||||
|
||||
@ -80,14 +81,31 @@ export class WeDo2Hub extends Hub {
|
||||
*/
|
||||
public setLEDRGB (red: number, green: number, blue: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let data = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]);
|
||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||
data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
|
||||
let data = Buffer.from([0x06, 0x17, 0x01, 0x02]);
|
||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data);
|
||||
data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
|
||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Play a sound on the Hub's in-built buzzer
|
||||
* @method WeDo2Hub#playSound
|
||||
* @param {number} frequency
|
||||
* @param {number} time How long the sound should play for (in milliseconds).
|
||||
* @returns {Promise} Resolved upon successful completion of command (ie. once the sound has finished playing).
|
||||
*/
|
||||
public playSound (frequency: number, time: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]);
|
||||
data.writeUInt16LE(frequency, 3);
|
||||
data.writeUInt16LE(time, 5);
|
||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||
setTimeout(resolve, time);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the motor speed on a given port.
|
||||
|
Loading…
x
Reference in New Issue
Block a user