node-red-contrib-leap-motion
Version:
Node-Red nodes for leap motion
63 lines (61 loc) • 1.93 kB
JavaScript
var Leap=require('leapjs');
var RED = require(process.env.NODE_RED_HOME+"/red/red");
module.exports = function (RED) {
function Constructor (config) {
RED.nodes.createNode(this, config);
var node = this;
var direction="?";
var pastDirection;
var controller = Leap.loop({enableGestures: true}, function(frame){
pastDirection = direction;
if (frame.hands.length === 0) { //No hand detected
direction = 'NOHAND';
} else if (frame.hands.length > 0) {
for (var i = 0; i < frame.hands.length; i++) {
var hand = frame.hands[0];
var position = hand.palmPosition;
var previousFrame = controller.frame(1);
if (position[0] < 80 && position[0] > -80 && position[2] < 80 && position[2] > -80 ){
direction = 'CENTER'; //The hand is above the leap
}else{
if (position[0] > 80 ){
direction = 'RIGHT';
}
if (position[0] < -80 ){
direction = 'LEFT';
}
if (position[2] > 80 ){
direction = 'BACKWARD';
}
if (position[2] < -80 ){
direction = 'FORWARD';
}
}
}
}
if(pastDirection != direction){ //Send one message when the hand have a new position
switch (direction) {
case 'LEFT':
node.send({payload:"left"});
break;
case 'RIGHT':
node.send({payload:"right"});
break;
case 'FORWARD':
node.send({payload:"forward"});
break;
case 'BACKWARD':
node.send({payload:"backward"});
break;
case 'CENTER':
node.send({payload:"center"});
break;
case 'NOHAND':
node.send({payload:"nohand"});
break;
}
}
});
}
RED.nodes.registerType('leapposition', Constructor)
}