diff --git a/boosthub.js b/boosthub.js index 0fb16af..5db2a17 100644 --- a/boosthub.js +++ b/boosthub.js @@ -34,7 +34,9 @@ class BoostHub extends Hub { connect (callback) { debug("Connecting to Boost Move Hub"); super.connect(() => { - this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.Boost.ALL], this._parseMessage.bind(this)); + const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL]; + this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); + characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); debug("Connect completed"); if (callback) { callback(); @@ -43,6 +45,18 @@ class BoostHub extends Hub { } + setLEDColor (color) { + const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL]; + if (characteristic) { + if (color === false) { + color = 0; + } + const data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]); + characteristic.write(data); + } + } + + setMotorSpeed (port, speed, time) { const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL]; if (characteristic) { @@ -70,6 +84,11 @@ class BoostHub extends Hub { _parseMessage (data) { switch (data[2]) { + case 0x01: + { + this._parseDeviceInfo(data); + break; + } case 0x04: { this._parsePortMessage(data); @@ -84,6 +103,21 @@ class BoostHub extends Hub { } + _parseDeviceInfo (data) { + + if (data[3] === 2) { + if (data[5] === 1) { + this.emit("button", Consts.Button.PRESSED); + return; + } else if (data[5] === 0) { + this.emit("button", Consts.Button.RELEASED); + return; + } + } + + } + + _parsePortMessage (data) { let port = null; diff --git a/consts.js b/consts.js index 5820b1b..ea0651e 100644 --- a/consts.js +++ b/consts.js @@ -15,6 +15,23 @@ const Consts = { BOOST_MOVE_HUB_MOTOR: 39, BOOST_TILT: 40 }, + Colors: { + NONE: 0, + PINK: 1, + PURPLE: 2, + BLUE: 3, + LIGHT_BLUE: 4, + CYAN: 5, + GREEN: 6, + YELLOW: 7, + ORANGE: 8, + RED: 9, + WHITE: 10 + }, + Button: { + PRESSED: 0, + RELEASED: 1 + }, BLE: { Name: { WEDO2_SMART_HUB_NAME: "LPF2 Smart Hub 2 I/O", diff --git a/wedo2hub.js b/wedo2hub.js index e0fb127..8721238 100644 --- a/wedo2hub.js +++ b/wedo2hub.js @@ -32,6 +32,7 @@ class WeDo2Hub extends Hub { super.connect(() => { this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE], this._parsePortMessage.bind(this)); this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.SENSOR_VALUE], this._parseSensorMessage.bind(this)); + this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.BUTTON], this._parseSensorMessage.bind(this)); debug("Connect completed"); if (callback) { callback(); @@ -39,6 +40,18 @@ class WeDo2Hub extends Hub { }) } + + setLEDColor (color) { + const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.MOTOR_VALUE_WRITE]; + if (characteristic) { + if (color === false) { + color = 0; + } + const data = Buffer.from([0x06, 0x04, 0x01, color]); + characteristic.write(data); + } + } + setMotorSpeed (port, speed) { const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.MOTOR_VALUE_WRITE]; @@ -124,6 +137,15 @@ class WeDo2Hub extends Hub { _parseSensorMessage (data) { + + + if (data[0] === 1) { + this.emit("button", Consts.Button.PRESSED); + return; + } else if (data[0] === 0) { + this.emit("button", Consts.Button.RELEASED); + return; + } let port = null;