Better examples

This commit is contained in:
Nathan Kellenicki 2018-08-03 15:33:12 +01:00
parent e60598bba9
commit 6e3435376c
4 changed files with 119 additions and 25 deletions

View File

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

View File

@ -16,7 +16,7 @@ lpf2.on("discover", async (hub) => { // Wait to discover hubs
}); });
let color = 0; let color = 1;
setInterval(() => { setInterval(() => {
const hubs = lpf2.getConnectedHubs(); // Get an array of all connected hubs const hubs = lpf2.getConnectedHubs(); // Get an array of all connected hubs
@ -25,7 +25,7 @@ setInterval(() => {
}) })
color++; color++;
if (color > 10) { if (color > 10) {
color = 0; color = 1;
} }
}, 2000); }, 2000);

View File

@ -1,31 +1,46 @@
const LPF2 = require("..").LPF2; const LPF2 = require("..");
const lpf2 = new LPF2(); const lpf2 = new LPF2.LPF2();
lpf2.scan(); // Start scanning for hubs 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 lpf2.on("discover", async (hub) => { // Wait to discover a train
console.log("Connected to Hub!");
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 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); moving = false;
await hub.wait(2000); hub.setMotorSpeed(TRAIN_MOTOR_PORT, 0);
hub.setMotorSpeed("A", -40); 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); moving = false;
await hub.wait(2000); hub.setMotorSpeed(TRAIN_MOTOR_PORT, 0);
hub.setMotorSpeed("A", 40); await hub.sleep(STOP_DELAY);
hub.setMotorSpeed(TRAIN_MOTOR_PORT, -TRAIN_SPEED);
await hub.sleep(2000);
moving = true;
} }

View File

@ -1,7 +1,7 @@
const LPF2 = require(".."); const LPF2 = require("..");
const lpf2 = new LPF2.LPF2(); const lpf2 = new LPF2.LPF2();
lpf2.scan(); // Start scanning for Vernie lpf2.scan(); // Start scanning
console.log("Looking for Vernie and Remote..."); 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 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; vernie = hub;
await vernie.connect(); await vernie.connect();
console.log("Connected to Vernie!"); console.log("Connected to Vernie!");
} else if (hub.type === LPF2.Consts.Hubs.POWERED_UP_REMOTE) { } else if (hub instanceof LPF2.PUPRemote) {
remote = hub; remote = hub;
remote.on("button", async (button, state) => { remote.on("button", async (button, state) => {
@ -61,9 +61,8 @@ lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote
} }
if (vernie && remote) { if (vernie && remote) {
const color = Math.floor(Math.random() * 10) + 1; vernie.setLEDColor(LPF2.Consts.Colors.BLUE);
vernie.setLEDColor(color); remote.setLEDColor(LPF2.Consts.Colors.BLUE);
remote.setLEDColor(color);
console.log("You're now ready to go!"); console.log("You're now ready to go!");
} }