diff --git a/DOCS.md b/DOCS.md index 7bf3463..43a94d6 100644 --- a/DOCS.md +++ b/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) ⇒ Promise * [.setLEDRGB(red, green, blue)](#WeDo2Hub+setLEDRGB) ⇒ Promise + * [.playSound(frequency, time)](#WeDo2Hub+playSound) ⇒ Promise * [.setMotorSpeed(port, speed)](#WeDo2Hub+setMotorSpeed) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise * [.disconnect()](#Hub+disconnect) ⇒ Promise @@ -342,6 +343,19 @@ Set the color of the LED on the Hub via RGB values. | green | number | | blue | number | + + +### weDo2Hub.playSound(frequency, time) ⇒ Promise +Play a sound on the Hub's in-built buzzer + +**Kind**: instance method of [WeDo2Hub](#WeDo2Hub) +**Returns**: Promise - Resolved upon successful completion of command (ie. once the sound has finished playing). + +| Param | Type | Description | +| --- | --- | --- | +| frequency | number | | +| time | number | How long the sound should play for (in milliseconds). | + ### weDo2Hub.setMotorSpeed(port, speed) ⇒ Promise diff --git a/README.md b/README.md index 822338b..22a4ab5 100644 --- a/README.md +++ b/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) ⇒ Promise * [.setLEDRGB(red, green, blue)](#WeDo2Hub+setLEDRGB) ⇒ Promise + * [.playSound(frequency, time)](#WeDo2Hub+playSound) ⇒ Promise * [.setMotorSpeed(port, speed)](#WeDo2Hub+setMotorSpeed) ⇒ Promise * [.connect()](#Hub+connect) ⇒ Promise * [.disconnect()](#Hub+disconnect) ⇒ Promise @@ -392,6 +393,19 @@ Set the color of the LED on the Hub via RGB values. | green | number | | blue | number | + + +### weDo2Hub.playSound(frequency, time) ⇒ Promise +Play a sound on the Hub's in-built buzzer + +**Kind**: instance method of [WeDo2Hub](#WeDo2Hub) +**Returns**: Promise - Resolved upon successful completion of command (ie. once the sound has finished playing). + +| Param | Type | Description | +| --- | --- | --- | +| frequency | number | | +| time | number | How long the sound should play for (in milliseconds). | + ### weDo2Hub.setMotorSpeed(port, speed) ⇒ Promise diff --git a/wedo2hub.ts b/wedo2hub.ts index 581d414..190588e 100644 --- a/wedo2hub.ts +++ b/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.