diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..2367bf6 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,13 @@ +pipeline: + + install: + image: node:10.15.1 + commands: + - apt-get update + - apt-get install -y bluetooth bluez libbluetooth-dev libudev-dev + - npm install + + build: + image: node:10.15.1 + commands: + - npm run all diff --git a/README.md b/README.md index ef956f1..a495bdd 100644 --- a/README.md +++ b/README.md @@ -74,19 +74,25 @@ const PoweredUP = require("node-poweredup"); const poweredUP = new PoweredUP.PoweredUP(); poweredUP.on("discover", async (hub) => { // Wait to discover a Hub + console.log(`Discovered ${hub.name}!`); await hub.connect(); // Connect to the Hub + console.log("Connected"); await hub.sleep(3000); // Sleep for 3 seconds before starting while (true) { // Repeat indefinitely + console.log("Running motor B at speed 75"); hub.setMotorSpeed("B", 75); // Start a motor attached to port B to run a 3/4 speed (75) indefinitely + console.log("Running motor A at speed 100 for 2 seconds"); await hub.setMotorSpeed("A", 100, 2000); // Run a motor attached to port A for 2 seconds at maximum speed (100) then stop await hub.sleep(1000); // Do nothing for 1 second + console.log("Running motor A at speed -50 for 1 seconds"); await hub.setMotorSpeed("A", -50, 1000); // Run a motor attached to port A for 1 second at 1/2 speed in reverse (-50) then stop await hub.sleep(1000); // Do nothing for 1 second } }); poweredUP.scan(); // Start scanning for Hubs +console.log("Scanning for Hubs..."); ``` More examples are available in the "examples" directory. diff --git a/docs/BoostMoveHub.html b/docs/BoostMoveHub.html index acbbfab..311d335 100644 --- a/docs/BoostMoveHub.html +++ b/docs/BoostMoveHub.html @@ -327,7 +327,7 @@
@@ -460,7 +460,7 @@ @@ -593,7 +593,7 @@ @@ -726,7 +726,7 @@ @@ -859,7 +859,7 @@ @@ -992,7 +992,7 @@ @@ -1250,7 +1250,7 @@ @@ -1376,7 +1376,7 @@ @@ -1502,7 +1502,7 @@ @@ -1674,7 +1674,7 @@ @@ -3499,7 +3499,7 @@ @@ -3719,7 +3719,7 @@ @@ -3895,7 +3895,7 @@ @@ -4072,7 +4072,7 @@ @@ -4280,7 +4280,7 @@ @@ -4969,7 +4969,7 @@ @@ -5585,7 +5585,7 @@ diff --git a/docs/DuploTrainBase.html b/docs/DuploTrainBase.html index 788b317..c4cf859 100644 --- a/docs/DuploTrainBase.html +++ b/docs/DuploTrainBase.html @@ -327,7 +327,7 @@ @@ -460,7 +460,7 @@ @@ -593,7 +593,7 @@ @@ -726,7 +726,7 @@ @@ -859,7 +859,7 @@ @@ -992,7 +992,7 @@ @@ -1250,7 +1250,7 @@ @@ -1376,7 +1376,7 @@ @@ -1502,7 +1502,7 @@ @@ -1674,7 +1674,7 @@ @@ -3162,7 +3162,7 @@ @@ -3382,7 +3382,7 @@ @@ -3558,7 +3558,7 @@ @@ -3735,7 +3735,7 @@ @@ -4188,7 +4188,7 @@ diff --git a/docs/Hub.html b/docs/Hub.html index 60e4cb5..fca24e1 100644 --- a/docs/Hub.html +++ b/docs/Hub.html @@ -307,7 +307,7 @@ @@ -426,7 +426,7 @@ @@ -545,7 +545,7 @@ @@ -664,7 +664,7 @@ @@ -783,7 +783,7 @@ @@ -902,7 +902,7 @@ @@ -984,7 +984,7 @@ @@ -1096,7 +1096,7 @@ @@ -1208,7 +1208,7 @@ @@ -1366,7 +1366,7 @@ @@ -1525,7 +1525,7 @@ @@ -1731,7 +1731,7 @@ @@ -1893,7 +1893,7 @@ @@ -2056,7 +2056,7 @@ @@ -2250,7 +2250,7 @@ @@ -2388,7 +2388,7 @@ @@ -2467,7 +2467,7 @@ diff --git a/docs/LPF2Hub.html b/docs/LPF2Hub.html index a3b7cdc..7e5c833 100644 --- a/docs/LPF2Hub.html +++ b/docs/LPF2Hub.html @@ -316,7 +316,7 @@ @@ -444,7 +444,7 @@ @@ -572,7 +572,7 @@ @@ -700,7 +700,7 @@ @@ -828,7 +828,7 @@ @@ -956,7 +956,7 @@ @@ -1052,7 +1052,7 @@ @@ -1173,7 +1173,7 @@ @@ -1294,7 +1294,7 @@ @@ -1461,7 +1461,7 @@ @@ -2275,7 +2275,7 @@ @@ -2490,7 +2490,7 @@ @@ -2661,7 +2661,7 @@ @@ -2833,7 +2833,7 @@ @@ -3036,7 +3036,7 @@ @@ -3693,7 +3693,7 @@ @@ -4444,7 +4444,7 @@ diff --git a/docs/PUPHub.html b/docs/PUPHub.html index 04fb070..4f1c8ee 100644 --- a/docs/PUPHub.html +++ b/docs/PUPHub.html @@ -327,7 +327,7 @@ @@ -460,7 +460,7 @@ @@ -593,7 +593,7 @@ @@ -726,7 +726,7 @@ @@ -859,7 +859,7 @@ @@ -992,7 +992,7 @@ @@ -1250,7 +1250,7 @@ @@ -1376,7 +1376,7 @@ @@ -1502,7 +1502,7 @@ @@ -1674,7 +1674,7 @@ @@ -3242,7 +3242,7 @@ @@ -3462,7 +3462,7 @@ @@ -3638,7 +3638,7 @@ @@ -3815,7 +3815,7 @@ @@ -4023,7 +4023,7 @@ @@ -4712,7 +4712,7 @@ @@ -5157,7 +5157,7 @@ diff --git a/docs/PUPRemote.html b/docs/PUPRemote.html index 1d828c2..ce53f0b 100644 --- a/docs/PUPRemote.html +++ b/docs/PUPRemote.html @@ -327,7 +327,7 @@ @@ -460,7 +460,7 @@ @@ -593,7 +593,7 @@ @@ -726,7 +726,7 @@ @@ -859,7 +859,7 @@ @@ -992,7 +992,7 @@ @@ -1088,7 +1088,7 @@ @@ -1214,7 +1214,7 @@ @@ -1340,7 +1340,7 @@ @@ -1512,7 +1512,7 @@ @@ -2359,7 +2359,7 @@ @@ -2579,7 +2579,7 @@ @@ -2755,7 +2755,7 @@ @@ -2932,7 +2932,7 @@ @@ -3409,7 +3409,7 @@ diff --git a/docs/PoweredUP.html b/docs/PoweredUP.html index 6e25b6f..6d0d8d5 100644 --- a/docs/PoweredUP.html +++ b/docs/PoweredUP.html @@ -1018,7 +1018,7 @@ diff --git a/docs/WeDo2SmartHub.html b/docs/WeDo2SmartHub.html index 8b4b380..dd9322e 100644 --- a/docs/WeDo2SmartHub.html +++ b/docs/WeDo2SmartHub.html @@ -320,7 +320,7 @@ @@ -448,7 +448,7 @@ @@ -576,7 +576,7 @@ @@ -704,7 +704,7 @@ @@ -832,7 +832,7 @@ @@ -960,7 +960,7 @@ @@ -1218,7 +1218,7 @@ @@ -1339,7 +1339,7 @@ @@ -1460,7 +1460,7 @@ @@ -1627,7 +1627,7 @@ @@ -3337,7 +3337,7 @@ @@ -3552,7 +3552,7 @@ @@ -3723,7 +3723,7 @@ @@ -3895,7 +3895,7 @@ @@ -4098,7 +4098,7 @@ @@ -4569,7 +4569,7 @@ @@ -5158,7 +5158,7 @@ diff --git a/docs/boostmovehub.js.html b/docs/boostmovehub.js.html index 0cc78ff..53ce8aa 100644 --- a/docs/boostmovehub.js.html +++ b/docs/boostmovehub.js.html @@ -351,7 +351,7 @@ exports.BoostMoveHub = BoostMoveHub; diff --git a/docs/classes.list.html b/docs/classes.list.html index 3c350f1..a148f25 100644 --- a/docs/classes.list.html +++ b/docs/classes.list.html @@ -335,7 +335,7 @@ @@ -1024,7 +1024,7 @@ @@ -2065,7 +2065,7 @@ @@ -2203,7 +2203,7 @@ @@ -2374,7 +2374,7 @@ @@ -3031,7 +3031,7 @@ @@ -4029,7 +4029,7 @@ @@ -4718,7 +4718,7 @@ @@ -5621,7 +5621,7 @@ @@ -6092,7 +6092,7 @@ @@ -6681,7 +6681,7 @@ diff --git a/docs/consts.js.html b/docs/consts.js.html index 627e1f4..dd176e9 100644 --- a/docs/consts.js.html +++ b/docs/consts.js.html @@ -300,7 +300,7 @@ var BLECharacteristic; diff --git a/docs/duplotrainbase.js.html b/docs/duplotrainbase.js.html index abc2847..29c81f5 100644 --- a/docs/duplotrainbase.js.html +++ b/docs/duplotrainbase.js.html @@ -300,7 +300,7 @@ exports.DuploTrainBase = DuploTrainBase; diff --git a/docs/global.html b/docs/global.html index 3aa556a..c2a8878 100644 --- a/docs/global.html +++ b/docs/global.html @@ -3272,7 +3272,7 @@ diff --git a/docs/hub.js.html b/docs/hub.js.html index a7035a0..bdf931a 100644 --- a/docs/hub.js.html +++ b/docs/hub.js.html @@ -177,16 +177,58 @@ class Hub extends events_1.EventEmitter { * @returns {Promise} Resolved upon successful connect. */ connect() { - return new Promise(async (connectResolve, connectReject) => { - if (this._bleDevice.connecting) { + return new Promise((connectResolve, connectReject) => { + const self = this; + if (this._isConnecting) { return connectReject("Already connecting"); } - else if (this._bleDevice.connected) { + else if (this._isConnected) { return connectReject("Already connected"); } this._isConnecting = true; - await this._bleDevice.connect(); - return connectResolve(); + this._peripheral.connect((err) => { + this._rssi = this._peripheral.rssi; + const rssiUpdateInterval = setInterval(() => { + this._peripheral.updateRssi((err, rssi) => { + if (!err) { + if (this._rssi !== rssi) { + this._rssi = rssi; + } + } + }); + }, 2000); + self._peripheral.on("disconnect", () => { + clearInterval(rssiUpdateInterval); + this._isConnecting = false; + this._isConnected = false; + this.emit("disconnect"); + }); + self._peripheral.discoverServices([], (err, services) => { + if (err) { + this.emit("error", err); + return; + } + debug("Service/characteristic discovery started"); + const servicePromises = []; + services.forEach((service) => { + servicePromises.push(new Promise((resolve, reject) => { + service.discoverCharacteristics([], (err, characteristics) => { + characteristics.forEach((characteristic) => { + this._characteristics[characteristic.uuid] = characteristic; + }); + return resolve(); + }); + })); + }); + Promise.all(servicePromises).then(() => { + debug("Service/characteristic discovery finished"); + this._isConnecting = false; + this._isConnected = true; + this.emit("connect"); + return connectResolve(); + }); + }); + }); }); } /** @@ -449,7 +491,7 @@ exports.Hub = Hub; diff --git a/docs/index.html b/docs/index.html index 43b292a..b4d0ebe 100644 --- a/docs/index.html +++ b/docs/index.html @@ -251,19 +251,25 @@ const poweredUP = new PoweredUP.PoweredUP(); poweredUP.on("discover", async (hub) => { // Wait to discover a Hub + console.log(`Discovered ${hub.name}!`); await hub.connect(); // Connect to the Hub + console.log("Connected"); await hub.sleep(3000); // Sleep for 3 seconds before starting while (true) { // Repeat indefinitely + console.log("Running motor B at speed 75"); hub.setMotorSpeed("B", 75); // Start a motor attached to port B to run a 3/4 speed (75) indefinitely + console.log("Running motor A at speed 100 for 2 seconds"); await hub.setMotorSpeed("A", 100, 2000); // Run a motor attached to port A for 2 seconds at maximum speed (100) then stop await hub.sleep(1000); // Do nothing for 1 second + console.log("Running motor A at speed -50 for 1 seconds"); await hub.setMotorSpeed("A", -50, 1000); // Run a motor attached to port A for 1 second at 1/2 speed in reverse (-50) then stop await hub.sleep(1000); // Do nothing for 1 second } }); -poweredUP.scan(); // Start scanning for HubsMore examples are available in the "examples" directory.
+poweredUP.scan(); // Start scanning for Hubs +console.log("Scanning for Hubs...");More examples are available in the "examples" directory.
Thanks go to Jorge Pereira (@JorgePe), Sebastian Raff (@hobbyquaker), Valentin Heun (@vheun), Johan Korten (@jakorten), and Andrey Pokhilko (@undera) for their various works, contributions, and assistance on figuring out the LEGO Boost, WeDo 2.0, and Powered UP protocols.
@@ -314,7 +320,7 @@ poweredUP.scan(); // Start scanning for HubsMore examples are av
diff --git a/docs/lpf2hub.js.html b/docs/lpf2hub.js.html index a911a94..14ca47a 100644 --- a/docs/lpf2hub.js.html +++ b/docs/lpf2hub.js.html @@ -503,7 +503,7 @@ exports.LPF2Hub = LPF2Hub; diff --git a/docs/poweredup.js.html b/docs/poweredup.js.html index 949a4c6..ecc71a3 100644 --- a/docs/poweredup.js.html +++ b/docs/poweredup.js.html @@ -286,7 +286,7 @@ exports.PoweredUP = PoweredUP; diff --git a/docs/puphub.js.html b/docs/puphub.js.html index d607e32..bbf87f0 100644 --- a/docs/puphub.js.html +++ b/docs/puphub.js.html @@ -308,7 +308,7 @@ exports.PUPHub = PUPHub; diff --git a/docs/pupremote.js.html b/docs/pupremote.js.html index 54b6563..a9c8e3c 100644 --- a/docs/pupremote.js.html +++ b/docs/pupremote.js.html @@ -238,7 +238,7 @@ exports.PUPRemote = PUPRemote; diff --git a/docs/quicksearch.html b/docs/quicksearch.html index a2e2fda..52e499e 100644 --- a/docs/quicksearch.html +++ b/docs/quicksearch.html @@ -7,7 +7,7 @@