diff --git a/examples/sensor_test.js b/examples/sensor_test.js new file mode 100644 index 0000000..9dc5c56 --- /dev/null +++ b/examples/sensor_test.js @@ -0,0 +1,54 @@ +/* + * + * This example demonstrates usage of various sensor events. + * + */ + +const PoweredUP = require(".."); + +const poweredUP = new PoweredUP.PoweredUP(); +poweredUP.scan(); + +console.log("Looking for Powered UP Hubs..."); + +let vernie = null; +let remote = null; + +poweredUP.on("discover", async (hub) => { + + await hub.connect(); + console.log(`Connected to ${hub.name}!`); + + hub.on("disconnect", () => { + console.log(`Disconnected ${hub.name}`); + }) + + hub.on("tilt", (port, x, y) => { + console.log(`Tilt detected on port ${port} (X: ${x}, Y: ${y})`); + }); + + hub.on("distance", (port, distance) => { + console.log(`Motion detected on port ${port} (Distance: ${distance})`); + }); + + hub.on("color", (port, color) => { + console.log(`Color detected on port ${port} (Color: ${color})`); + }); + + hub.on("rotate", (port, rotation) => { + console.log(`Rotation detected on port ${port} (Rotation: ${rotation})`); + }); + + hub.on("button", (button, state) => { + console.log(`Button press detected (Button: ${button}, State: ${state})`); + }); + + hub.on("attach", (port, device) => { + console.log(`Device attached to port ${port} (Device ID: ${device})`) ; + }); + + hub.on("detach", (port) => { + console.log(`Device detached from port ${port}`) ; + }); + +}); \ No newline at end of file diff --git a/examples/vernie_remote.js b/examples/vernie_remote.js index 8f2afa4..c621030 100644 --- a/examples/vernie_remote.js +++ b/examples/vernie_remote.js @@ -20,7 +20,8 @@ poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote vernie = hub; await vernie.connect(); - console.log("Connected to Vernie!"); + vernie.setLEDColor(PoweredUP.Consts.Color.BLUE); + console.log(`Connected to Vernie (${vernie.name})!`); } else if (hub instanceof PoweredUP.PUPRemote) { remote = hub; @@ -63,12 +64,11 @@ poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote }) await remote.connect(); - console.log("Connected to Powered UP Remote!"); + remote.setLEDColor(PoweredUP.Consts.Color.BLUE); + console.log(`Connected to Powered UP Remote (${remote.name})!`); } if (vernie && remote) { - vernie.setLEDColor(PoweredUP.Consts.Color.BLUE); - remote.setLEDColor(PoweredUP.Consts.Color.BLUE); console.log("You're now ready to go!"); }