-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleap_motion_control.js
More file actions
86 lines (70 loc) · 1.75 KB
/
leap_motion_control.js
File metadata and controls
86 lines (70 loc) · 1.75 KB
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
86
"use strict";
var Cylon = require('cylon');
/**
* This robot sends messages to the hand_motion and key_tap_gesture channel using the leap motion
*/
Cylon.robot({
connections: {
server: {adaptor: 'mqtt', host: 'mqtt://localhost:1883'},
leapmotion: {adaptor: 'leapmotion'}
},
devices: {
leapmotion: {driver: 'leapmotion', connection: 'leapmotion'}
},
work: function (my) {
my.leapmotion.on('hand', (payload)=> {
var data;
try{
data = payload.sphereRadius
} catch(e) {
throw new Error(e)
}
var now = new Date();
var dataSet = JSON.stringify({'x': getTimeString(now), 'y': data});
my.server.publish('hand_motion', dataSet)
});
my.leapmotion.on('gesture', (payload)=>{
var state;
try{
state = payload.type
} catch(e){
console.log(e)
}
if(state == 'keyTap'){
my.server.publish('key_tap_gesture', state)
}
})
},
error: (err)=>{
console.warn("Rats")
}
}).start();
// Code borrowed from random signal package
var getTimeString = (now)=> {
var year = "" + now.getFullYear();
var month = "" + (now.getMonth() + 1);
if (month.length == 1) {
month = "0" + month
}
var day = "" + now.getDate();
if (day.length == 1) {
day = "0" + day
}
var hour = "" + now.getHours();
if (hour.length == 1) {
hour = "0" + hour
}
var minute = "" + now.getMinutes();
if (minute.length == 1) {
minute = "0" + minute
}
var second = "" + now.getSeconds();
if (second.length == 1) {
second = "0" + second
}
var ms = "" + now.getMilliseconds();
while (ms.length < 3) {
ms = "0" + ms
}
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + "." + ms;
};