From b4cd5fd40b09693b6a8ed457d9471dccf0c84577 Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Tue, 26 Jun 2018 23:21:37 +0100 Subject: [PATCH] LED example across multiple hubs --- examples/leds.js | 27 ++++++++++++++++ multipletest.js | 46 --------------------------- test.js | 82 ------------------------------------------------ 3 files changed, 27 insertions(+), 128 deletions(-) create mode 100644 examples/leds.js delete mode 100644 multipletest.js delete mode 100644 test.js diff --git a/examples/leds.js b/examples/leds.js new file mode 100644 index 0000000..66b58b9 --- /dev/null +++ b/examples/leds.js @@ -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); \ No newline at end of file diff --git a/multipletest.js b/multipletest.js deleted file mode 100644 index c2e3851..0000000 --- a/multipletest.js +++ /dev/null @@ -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; - } - -}); \ No newline at end of file diff --git a/test.js b/test.js deleted file mode 100644 index f52c5f0..0000000 --- a/test.js +++ /dev/null @@ -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);