/
vernie_remote.js
85 lines (70 loc) · 3.15 KB
/
vernie_remote.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
*
* This example allows you to connect Vernie and a Powered UP Remote Control to your laptop, and enables the control of Vernie with the Remote.
*
*/
const PoweredUP = require("..");
const poweredUP = new PoweredUP.PoweredUP();
poweredUP.scan(); // Start scanning
console.log("Looking for Vernie and Remote...");
let vernie = null;
let remote = null;
poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote
if (hub.type === PoweredUP.Consts.HubType.MOVE_HUB) {
vernie = hub;
await vernie.connect();
const led = await vernie.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED);
led.setColor(PoweredUP.Consts.Color.BLUE);
console.log(`Connected to Vernie (${vernie.name})!`);
} else if (hub.type === PoweredUP.Consts.HubType.REMOTE_CONTROL) {
remote = hub;
remote.on("remoteButton", async (device, { event }) => {
if (vernie) {
const leftTrack = await vernie.waitForDeviceAtPort("A");
const rightTrack = await vernie.waitForDeviceAtPort("B");
const head = await vernie.waitForDeviceAtPort("D");
console.log(event);
switch (event) {
case PoweredUP.Consts.ButtonState.UP: // If up is pressed, move the track forward
{
console.log(device.portName);
device.portName === "LEFT" ? leftTrack.setSpeed(50) : rightTrack.setSpeed(50);
break;
}
case PoweredUP.Consts.ButtonState.DOWN: // If down is pressed, move the track backwards
{
device.portName === "LEFT" ? leftTrack.setSpeed(-50) : rightTrack.setSpeed(-50);
break;
}
case PoweredUP.Consts.ButtonState.RELEASED: // Stop the track when the button is released
{
device.portName === "LEFT" ? leftTrack.setPower(0) : rightTrack.setPower(0);
break;
}
case PoweredUP.Consts.ButtonState.STOP: // Move the head left or right when a red button is pressed
{
await head.rotateByDegrees(35, device.portName === "LEFT" ? -20 : 20);
break;
}
}
}
});
hub.on("button", async ({ event }) => {
console.log(event);
if (vernie) {
const head = await vernie.waitForDeviceAtPort("D");
if (event === PoweredUP.Consts.ButtonState.PRESSED) {
await head.rotateByDegrees(80, 20);
await head.rotateByDegrees(80, -20);
}
}
});
await remote.connect();
const led = await remote.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED);
led.setColor(PoweredUP.Consts.Color.BLUE);
console.log(`Connected to Powered UP Remote (${remote.name})!`);
}
if (vernie && remote) {
console.log("You're now ready to go!");
}
});