LED setting and button pressing
This commit is contained in:
parent
fd0d87864f
commit
ca9d9e18d4
36
boosthub.js
36
boosthub.js
@ -34,7 +34,9 @@ class BoostHub extends Hub {
|
|||||||
connect (callback) {
|
connect (callback) {
|
||||||
debug("Connecting to Boost Move Hub");
|
debug("Connecting to Boost Move Hub");
|
||||||
super.connect(() => {
|
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");
|
debug("Connect completed");
|
||||||
if (callback) {
|
if (callback) {
|
||||||
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) {
|
setMotorSpeed (port, speed, time) {
|
||||||
const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
|
const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
|
||||||
if (characteristic) {
|
if (characteristic) {
|
||||||
@ -70,6 +84,11 @@ class BoostHub extends Hub {
|
|||||||
_parseMessage (data) {
|
_parseMessage (data) {
|
||||||
|
|
||||||
switch (data[2]) {
|
switch (data[2]) {
|
||||||
|
case 0x01:
|
||||||
|
{
|
||||||
|
this._parseDeviceInfo(data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 0x04:
|
case 0x04:
|
||||||
{
|
{
|
||||||
this._parsePortMessage(data);
|
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) {
|
_parsePortMessage (data) {
|
||||||
|
|
||||||
let port = null;
|
let port = null;
|
||||||
|
17
consts.js
17
consts.js
@ -15,6 +15,23 @@ const Consts = {
|
|||||||
BOOST_MOVE_HUB_MOTOR: 39,
|
BOOST_MOVE_HUB_MOTOR: 39,
|
||||||
BOOST_TILT: 40
|
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: {
|
BLE: {
|
||||||
Name: {
|
Name: {
|
||||||
WEDO2_SMART_HUB_NAME: "LPF2 Smart Hub 2 I/O",
|
WEDO2_SMART_HUB_NAME: "LPF2 Smart Hub 2 I/O",
|
||||||
|
22
wedo2hub.js
22
wedo2hub.js
@ -32,6 +32,7 @@ class WeDo2Hub extends Hub {
|
|||||||
super.connect(() => {
|
super.connect(() => {
|
||||||
this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE], this._parsePortMessage.bind(this));
|
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.SENSOR_VALUE], this._parseSensorMessage.bind(this));
|
||||||
|
this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.BUTTON], this._parseSensorMessage.bind(this));
|
||||||
debug("Connect completed");
|
debug("Connect completed");
|
||||||
if (callback) {
|
if (callback) {
|
||||||
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) {
|
setMotorSpeed (port, speed) {
|
||||||
const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.MOTOR_VALUE_WRITE];
|
const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.MOTOR_VALUE_WRITE];
|
||||||
@ -124,6 +137,15 @@ class WeDo2Hub extends Hub {
|
|||||||
|
|
||||||
|
|
||||||
_parseSensorMessage (data) {
|
_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;
|
let port = null;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user