Subscribe working
This commit is contained in:
parent
2d39fa7222
commit
00f1664840
7
hub.js
7
hub.js
@ -10,7 +10,7 @@ class Hub extends EventEmitter {
|
||||
|
||||
constructor (peripheral, autoSubscribe) {
|
||||
super();
|
||||
this.autoSubscribe = autoSubscribe || true;
|
||||
this.autoSubscribe = !!autoSubscribe;
|
||||
this._peripheral = peripheral;
|
||||
this._characteristics = {};
|
||||
this._batteryLevel = 100;
|
||||
@ -110,7 +110,7 @@ class Hub extends EventEmitter {
|
||||
case Consts.Devices.BOOST_MOVE_HUB_MOTOR:
|
||||
return 0x02;
|
||||
case Consts.Devices.BOOST_DISTANCE:
|
||||
return (Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08);
|
||||
return (this.type == Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08);
|
||||
case Consts.Devices.BOOST_TILT:
|
||||
return 0x04;
|
||||
default:
|
||||
@ -134,8 +134,9 @@ class Hub extends EventEmitter {
|
||||
_registerDeviceAttachment (port, type) {
|
||||
|
||||
if (port.connected) {
|
||||
port.type = type;
|
||||
if (this.autoSubscribe) {
|
||||
this._activatePortDevice(port.value, this._getModeForDeviceType(port.type), 0x00, 0x00);
|
||||
this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00);
|
||||
}
|
||||
} else {
|
||||
port.type = null;
|
||||
|
4
lpf2.js
4
lpf2.js
@ -39,9 +39,9 @@ class LPF2 extends EventEmitter {
|
||||
let hub = null;
|
||||
|
||||
if (WeDo2Hub.isWeDo2Hub(peripheral)) {
|
||||
hub = new WeDo2Hub(peripheral, autoSubscribe);
|
||||
hub = new WeDo2Hub(peripheral, this.autoSubscribe);
|
||||
} else if (BoostHub.isBoostHub(peripheral)) {
|
||||
hub = new BoostHub(peripheral autoSubscribe);
|
||||
hub = new BoostHub(peripheral, this.autoSubscribe);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
18
test.js
18
test.js
@ -2,26 +2,28 @@ const LPF2 = require("./lpf2.js");
|
||||
|
||||
const lpf2 = new LPF2();
|
||||
|
||||
//lpf2.autoSubscribe = false;
|
||||
lpf2.scan();
|
||||
|
||||
let moveHub = null,
|
||||
moveHubUUID = "782a5fbbcef64c5cb31ab4791c191f5d",
|
||||
moveHubUUID = "e6e40e4f00e34dbe955da2b187adcd2f",
|
||||
moveHub2 = null,
|
||||
moveHub2UUID = "2ea54c6e1c744406bdc8567daf3a692a",
|
||||
moveHub2UUID = "c72413db7ce24411967ff25184d4609a",
|
||||
wedoHub = null,
|
||||
wedoHubUUID = "0ae95acf801e47f9bda4752392756eed";
|
||||
wedoHubUUID = "f4924139c6684be19840f97738c707f3";
|
||||
|
||||
lpf2.on("discover", (hub) => {
|
||||
|
||||
console.log(hub.uuid);
|
||||
hub.connect(() => {
|
||||
|
||||
//console.log(hub.uuid);
|
||||
|
||||
if (hub.uuid === moveHubUUID) {
|
||||
moveHub = hub;
|
||||
console.log("Connected to Move Hub");
|
||||
|
||||
moveHub.on("distance", (port, distance) => {
|
||||
console.log(`Distance ${distance} received on port ${port}`);
|
||||
//console.log(`Distance ${distance} received on port ${port}`);
|
||||
if (distance < 90) {
|
||||
if (wedoHub) wedoHub.setMotorSpeed("B", 40);
|
||||
if (moveHub2) moveHub2.setMotorSpeed("D", 40);
|
||||
@ -36,7 +38,7 @@ lpf2.on("discover", (hub) => {
|
||||
console.log("Connected to Move Hub 2");
|
||||
|
||||
moveHub2.on("distance", (port, distance) => {
|
||||
console.log(`Distance ${distance} received on port ${port}`);
|
||||
//console.log(`Distance ${distance} received on port ${port}`);
|
||||
if (distance < 90) {
|
||||
if (wedoHub) wedoHub.setMotorSpeed("B", 40);
|
||||
if (moveHub) moveHub.setMotorSpeed("D", 40);
|
||||
@ -51,13 +53,13 @@ lpf2.on("discover", (hub) => {
|
||||
console.log("Connected to Smart Hub");
|
||||
|
||||
wedoHub.on("distance", (port, distance) => {
|
||||
console.log(`Distance ${distance} received on port ${port}`);
|
||||
//console.log(`Distance ${distance} received on port ${port}`);
|
||||
if (distance < 90) {
|
||||
if (moveHub) moveHub.setMotorSpeed("D", 40);
|
||||
if (moveHub2) moveHub2.setMotorSpeed("D", 40);
|
||||
} else {
|
||||
if (moveHub) moveHub.setMotorSpeed("D", 0);
|
||||
if (moveHub2) moveHub.setMotorSpeed("D", 0);
|
||||
if (moveHub2) moveHub2.setMotorSpeed("D", 0);
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user