Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an example to run basic functionality on the Duplo train #163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions examples/duplo_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const PoweredUP = require("..");
const poweredUP = new PoweredUP.PoweredUP();

poweredUP.on("discover", async (hub) => { // Wait to discover a Hub
console.log(`Discovered ${hub.name}!`);
await hub.connect(); // Connect to the Hub
console.log("Connected");
let devices = hub.getDevices()
for (let i = 0; i<devices.length; ++i){
let device = devices[i];
console.log(`Device port ${device.portName} type ${device.type}`);
}
const hubLed = PoweredUP.Consts.DeviceTypeNames.HUB_LED;
let lights = hub.getDevicesByType(hubLed);
for (let i = 0; i<lights.length; ++i){
let device = lights[i];
console.log(`HubLed port ${device.portName} type ${device.type}`);
}

const motor = await hub.waitForDeviceAtPort("MOTOR");
const light = await hub.waitForDeviceByType(hubLed);
console.log("Setting running light to RED for 2 seconds");
light.setColor(PoweredUP.Consts.ColorNames.RED);
await hub.sleep(2000);
console.log("Setting running light to WHITE");
light.setColor(PoweredUP.Consts.ColorNames.WHITE);

while (true) { // Repeat indefinitely
console.log("Running motor at speed 100 for 2 seconds");
motor.setPower(100); // Run a motor attached to port A for 2 seconds at maximum speed (100) then stop
await hub.sleep(2000);
console.log("Running motor at revrese speed 100 for 2 seconds");
motor.setPower(-100); // Run a motor attached to port A for 2 seconds at maximum speed (100) then stop
await hub.sleep(2000);
motor.brake();
}
});

poweredUP.scan(); // Start scanning for Hubs
console.log("Scanning for Hubs...");