This commit is contained in:
parent
38bdea2642
commit
7d0c4f73ba
40
examples/mario.js
Normal file
40
examples/mario.js
Normal file
@ -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");
|
||||
});
|
||||
}
|
||||
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user