LED setting and button pressing

This commit is contained in:
Nathan Kunicki 2018-06-14 13:06:43 +01:00
parent fd0d87864f
commit ca9d9e18d4
3 changed files with 74 additions and 1 deletions

View File

@ -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;

View File

@ -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",

View File

@ -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;