Sensor example

This commit is contained in:
Nathan Kellenicki 2019-01-31 21:05:14 -08:00
parent 748c886df5
commit 666e55b6e3
2 changed files with 58 additions and 4 deletions

54
examples/sensor_test.js Normal file
View File

@ -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}`) ;
});
});

View File

@ -20,7 +20,8 @@ poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote
vernie = hub; vernie = hub;
await vernie.connect(); 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) { } else if (hub instanceof PoweredUP.PUPRemote) {
remote = hub; remote = hub;
@ -63,12 +64,11 @@ poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote
}) })
await remote.connect(); 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) { if (vernie && remote) {
vernie.setLEDColor(PoweredUP.Consts.Color.BLUE);
remote.setLEDColor(PoweredUP.Consts.Color.BLUE);
console.log("You're now ready to go!"); console.log("You're now ready to go!");
} }