-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleap.js
76 lines (68 loc) · 1.96 KB
/
leap.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
if (process.argv.length < 3) {
console.log('Usage: node run.js PORT [ID1] [ID2]');
return;
}
var device = process.argv[2];
var SerialPort = require('serialport').SerialPort;
var bluetoothCubelet = new SerialPort(device, { baudrate: 38400 });
var cubelets = require('cubelets');
var cubelet1ID = process.argv.length >= 4 ? process.argv[3] : 0;
var cubelet2ID = process.argv.length >= 5 ? process.argv[4] : 0;
var cubelet1Value = 0;
var cubelet2Value = 0;
var Leap = require('leapjs');
var controller = new Leap.Controller();
var interactionBox = null;
function mapToCubeletValue(t) {
var p = interactionBox.normalizePoint(t);
var x = p[0];
var y = p[1];
var z = p[2];
return Math.max(0, Math.min(Math.round(255 * y), 255));
}
bluetoothCubelet.on('open', function() {
console.log('Bluetooth Cubelet connected...');
setInterval(function() {
console.log(cubelet1Value, cubelet2Value);
if (cubelet1ID) {
bluetoothCubelet.write(new cubelets
.SetBlockValueCommand(cubelet1ID, cubelet1Value)
.encode());
}
if (cubelet2ID) {
bluetoothCubelet.write(new cubelets
.SetBlockValueCommand(cubelet2ID, cubelet2Value)
.encode());
}
}, 250);
});
var lastFrame = null;
controller.on("frame", function(frame) {
if (frame.valid) {
if (!lastFrame) {
lastFrame = frame;
}
if (!interactionBox) {
interactionBox = new Leap.InteractionBox(frame.interactionBox);
}
if (frame.hands.length >= 1) {
cubelet1Value = mapToCubeletValue(frame.hands[0].palmPosition);
}
else {
cubelet1Value = 0;
}
if (frame.hands.length >= 2) {
cubelet2Value = mapToCubeletValue(frame.hands[1].palmPosition);
}
else {
cubelet2Value = 0;
}
lastFrame = frame;
}
});
controller.on('ready', function() {
console.log("Ready...");
controller.connection.send(JSON.stringify({"focused":true}));
});
controller.connect();
console.log("Waiting for device to connect...");