LED example across multiple hubs

This commit is contained in:
Nathan Kunicki 2018-06-26 23:21:37 +01:00
parent 36a83f78fa
commit b4cd5fd40b
3 changed files with 27 additions and 128 deletions

27
examples/leds.js Normal file
View File

@ -0,0 +1,27 @@
const LPF2 = require("..").LPF2;
const lpf2 = new LPF2();
lpf2.scan(); // Start scanning for hubs
console.log("Looking for Hubs...");
lpf2.on("discover", async (hub) => { // Wait to discover hubs
await hub.connect(); // Connect to hub
console.log("Connected to Hub!");
});
let color = 0;
setInterval(() => {
const hubs = lpf2.getConnectedDevices();
hubs.forEach((hub) => {
hub.setLEDColor(color);
})
color++;
if (color > 10) {
color = 0;
}
}, 2000);

View File

@ -1,46 +0,0 @@
const LPF2 = require("./lpf2.js");
const lpf2 = new LPF2();
let wedoUUID = "0ae95acf801e47f9bda4752392756eed",
wedoHub = null,
boostUUID = "782a5fbbcef64c5cb31ab4791c191f5d",
boostHub = null;
lpf2.scan();
lpf2.on("discover", (hub) => {
if (hub.uuid === wedoUUID) {
wedoHub = hub;
wedoHub.connect();
wedoHub.on("distance", (port, distance) => {
console.log(`Distance ${distance} received on WeDo port ${port}`);
if (boostHub) {
if (distance < 30) {
boostHub.setMotorSpeed("C", 40);
} else {
boostHub.setMotorSpeed("C", 0);
}
}
});
return;
}
if (hub.uuid === boostUUID) {
boostHub = hub;
boostHub.connect();
boostHub.on("distance", (port, distance) => {
console.log(`Distance ${distance} received on Boost port ${port}`);
if (wedoHub) {
if (distance < 30) {
wedoHub.setMotorSpeed("B", 40);
} else {
wedoHub.setMotorSpeed("B", 0);
}
}
});
return;
}
});

82
test.js
View File

@ -1,82 +0,0 @@
const LPF2 = require(".");
const lpf2 = new LPF2.LPF2();
//lpf2.autoSubscribe = false;
lpf2.scan();
let moveHub = null,
moveHubUUID = "e6e40e4f00e34dbe955da2b187adcd2f",
moveHub2 = null,
moveHub2UUID = "c72413db7ce24411967ff25184d4609a",
wedoHub = null,
wedoHubUUID = "f4924139c6684be19840f97738c707f3";
lpf2.on("discover", (hub) => {
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}`);
if (distance < 90) {
if (wedoHub) wedoHub.setMotorSpeed("B", 40);
if (moveHub2) moveHub2.setMotorSpeed("D", 40);
} else {
if (wedoHub) wedoHub.setMotorSpeed("B", 0);
if (moveHub2) moveHub2.setMotorSpeed("D", 0);
}
});
} else if (hub.uuid === moveHub2UUID) {
moveHub2 = hub;
console.log("Connected to Move Hub 2");
moveHub2.on("distance", (port, distance) => {
//console.log(`Distance ${distance} received on port ${port}`);
if (distance < 90) {
if (wedoHub) wedoHub.setMotorSpeed("B", 40);
if (moveHub) moveHub.setMotorSpeed("D", 40);
} else {
if (wedoHub) wedoHub.setMotorSpeed("B", 0);
if (moveHub) moveHub.setMotorSpeed("D", 0);
}
});
} else if (hub.uuid === wedoHubUUID) {
wedoHub = hub;
console.log("Connected to Smart Hub");
wedoHub.on("distance", (port, distance) => {
//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) moveHub2.setMotorSpeed("D", 0);
}
});
}
});
});
let color = 0;
setInterval(() => {
color = color > 10 ? 1 : color + 1;
if (moveHub) moveHub.setLEDColor(color);
if (moveHub2) moveHub2.setLEDColor(color);
if (wedoHub) wedoHub.setLEDColor(color);
}, 2000);