diff --git a/examples/batmobile_remote.js b/examples/batmobile_remote.js new file mode 100644 index 0000000..b818da3 --- /dev/null +++ b/examples/batmobile_remote.js @@ -0,0 +1,80 @@ +const LPF2 = require(".."); + +const lpf2 = new LPF2.LPF2(); +lpf2.scan(); // Start scanning + +console.log("Looking for Batmobile and Remote..."); + +let batmobile = null; +let remote = null; + +lpf2.on("discover", async (hub) => { // Wait to Batmobile and Remote + + if (hub instanceof LPF2.PUPHub) { + + batmobile = hub; + await batmobile.connect(); + console.log("Connected to Batmobile!"); + + } else if (hub instanceof LPF2.PUPRemote) { + + remote = hub; + remote.on("button", async (button, state) => { + if (batmobile) { + switch (state) { + case LPF2.Consts.ButtonStates.UP: // If up is pressed, move the wheels forward + { + batmobile.setMotorSpeed(button === "LEFT" ? "A" : "B", 50); + break; + } + case LPF2.Consts.ButtonStates.DOWN: // If down is pressed, move the wheels backwards + { + batmobile.setMotorSpeed(button === "LEFT" ? "A" : "B", -50); + break; + } + case LPF2.Consts.ButtonStates.RELEASED: // Stop the wheels when the button is released + { + if (button !== "GREEN") { + batmobile.setMotorSpeed(button === "LEFT" ? "A" : "B", 0); + } + break; + } + case LPF2.Consts.ButtonStates.STOP: // When left red button is pressed, do a retreat. When right red button is pressed, scan the area. + { + if (button === "LEFT") { + await batmobile.setMotorSpeed("A", -100, 500); + await batmobile.setMotorSpeed("B", -100, 1000); + await batmobile.setMotorSpeed("A", -100, 1000); + await batmobile.setMotorSpeed("B", -100, 1000); + await batmobile.setMotorSpeed("A", -100, 500); + } else if (button === "RIGHT") { + await batmobile.setMotorSpeed("AB", [100, -100], 1000); + await batmobile.setMotorSpeed("AB", [-100, 100], 2000); + await batmobile.setMotorSpeed("AB", [100, -100], 2000); + await batmobile.setMotorSpeed("AB", [-100, 100], 1000); + } + break; + } + case LPF2.Consts.ButtonStates.PRESSED: // Do a wheelie when the green button is pressed + { + if (button === "GREEN") { + await batmobile.setMotorSpeed("AB", -100, 500); + await batmobile.setMotorSpeed("AB", 100, 1000); + } + break; + } + } + } + }) + + await remote.connect(); + console.log("Connected to Powered Up Remote!"); + } + + if (batmobile && remote) { + batmobile.setLEDColor(LPF2.Consts.Colors.WHITE); + remote.setLEDColor(LPF2.Consts.Colors.RED); + console.log("You're now ready to go!"); + } + +}); \ No newline at end of file diff --git a/examples/leds.js b/examples/leds.js index ff36d0b..8befd40 100644 --- a/examples/leds.js +++ b/examples/leds.js @@ -16,7 +16,7 @@ lpf2.on("discover", async (hub) => { // Wait to discover hubs }); -let color = 0; +let color = 1; setInterval(() => { const hubs = lpf2.getConnectedHubs(); // Get an array of all connected hubs @@ -25,7 +25,7 @@ setInterval(() => { }) color++; if (color > 10) { - color = 0; + color = 1; } }, 2000); \ No newline at end of file diff --git a/examples/train_reverse.js b/examples/train_reverse.js index 7b3eb23..95d17dd 100644 --- a/examples/train_reverse.js +++ b/examples/train_reverse.js @@ -1,31 +1,46 @@ -const LPF2 = require("..").LPF2; +const LPF2 = require(".."); -const lpf2 = new LPF2(); -lpf2.scan(); // Start scanning for hubs +const lpf2 = new LPF2.LPF2(); +lpf2.scan(); // Start scanning for trains -console.log("Looking for Hubs..."); +// Change these to make the train behave as you want +const FORWARD_DIRECTION_COLOR = LPF2.Consts.Colors.YELLOW; +const BACKWARDS_DIRECTION_COLOR = LPF2.Consts.Colors.RED; +const TRAIN_SPEED = 40; +const STOP_DELAY = 2000; +const TRAIN_MOTOR_PORT = "A"; -lpf2.on("discover", async (hub) => { // Wait to discover hubs +console.log("Looking for trains..."); - await hub.connect(); // Connect to hub - console.log("Connected to Hub!"); +lpf2.on("discover", async (hub) => { // Wait to discover a train + + let moving = true; + + await hub.connect(); // Connect to train + console.log(`Connected to ${hub.name}!`); await hub.wait(2000); // Wait two seconds before starting the train - hub.setMotorSpeed("A", 40); + hub.setMotorSpeed(TRAIN_MOTOR_PORT, TRAIN_SPEED); - hub.on("color", (port, color) => { + hub.on("color", async (port, color) => { - if (color === LPF2.Consts.Colors.YELLOW) { // If yellow is seen, stop the train, wait two seconds, and reverse direction + if (color === FORWARD_DIRECTION_COLOR && moving) { // If yellow is seen, stop the train, wait seconds, and reverse direction - hub.setMotorSpeed("A", 0); - await hub.wait(2000); - hub.setMotorSpeed("A", -40); + moving = false; + hub.setMotorSpeed(TRAIN_MOTOR_PORT, 0); + await hub.sleep(STOP_DELAY); + hub.setMotorSpeed(TRAIN_MOTOR_PORT, -TRAIN_SPEED); + await hub.sleep(2000); + moving = true; - } else if (color === LPF2.Consts.Colors.RED) { // If red is seen, stop the train, wait two seconds, and reverse direction + } else if (color === BACKWARDS_DIRECTION_COLOR && moving) { // If red is seen, stop the train, wait seconds, and reverse direction - hub.setMotorSpeed("A", 0); - await hub.wait(2000); - hub.setMotorSpeed("A", 40); + moving = false; + hub.setMotorSpeed(TRAIN_MOTOR_PORT, 0); + await hub.sleep(STOP_DELAY); + hub.setMotorSpeed(TRAIN_MOTOR_PORT, -TRAIN_SPEED); + await hub.sleep(2000); + moving = true; } diff --git a/examples/vernie_remote.js b/examples/vernie_remote.js index d621c37..a627c6e 100644 --- a/examples/vernie_remote.js +++ b/examples/vernie_remote.js @@ -1,7 +1,7 @@ const LPF2 = require(".."); const lpf2 = new LPF2.LPF2(); -lpf2.scan(); // Start scanning for Vernie +lpf2.scan(); // Start scanning console.log("Looking for Vernie and Remote..."); @@ -10,13 +10,13 @@ let remote = null; lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote - if (hub.type === LPF2.Consts.Hubs.BOOST_MOVE_HUB) { + if (hub instanceof LPF2.BoostMoveHub) { vernie = hub; await vernie.connect(); console.log("Connected to Vernie!"); - } else if (hub.type === LPF2.Consts.Hubs.POWERED_UP_REMOTE) { + } else if (hub instanceof LPF2.PUPRemote) { remote = hub; remote.on("button", async (button, state) => { @@ -61,9 +61,8 @@ lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote } if (vernie && remote) { - const color = Math.floor(Math.random() * 10) + 1; - vernie.setLEDColor(color); - remote.setLEDColor(color); + vernie.setLEDColor(LPF2.Consts.Colors.BLUE); + remote.setLEDColor(LPF2.Consts.Colors.BLUE); console.log("You're now ready to go!"); }