From 7d0c4f73bae0b4f386a31bfd5f3e85fe1c65fce4 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 22 Dec 2020 16:05:57 -0800 Subject: [PATCH] Added example --- examples/mario.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/mario.js diff --git a/examples/mario.js b/examples/mario.js new file mode 100644 index 0000000..12337d1 --- /dev/null +++ b/examples/mario.js @@ -0,0 +1,40 @@ +/* + * + * This demonstrates connecting to LEGO Super Mario. + * + */ + +const PoweredUP = require(".."); + +const poweredUP = new PoweredUP.PoweredUP(); +poweredUP.scan(); // Start scanning for hubs + +console.log("Looking for Mario..."); + +poweredUP.on("discover", async (hub) => { // Wait to discover hubs + + if (hub instanceof PoweredUP.Mario) { + const mario = hub; + await mario.connect(); // Connect to Mario + console.log(`Connected to Mario!`); + + const pants = await mario.waitForDeviceByType(PoweredUP.Consts.DeviceType.MARIO_PANTS_SENSOR); + pants.on("pants", ({ pants }) => { + console.log("Pants detected", pants); + }); + + const barcodeSensor = await mario.waitForDeviceByType(PoweredUP.Consts.DeviceType.MARIO_BARCODE_SENSOR); + barcodeSensor.on("barcode", ({ barcode, color }) => { + if (color) { + console.log("Color detected", color); + } else if (barcode) { + console.log("Barcode detected", barcode); + } + }); + + mario.on("disconnect", () => { + console.log("Mario disconnected"); + }); + } + +}); \ No newline at end of file