Permalink
Browse files

adding leap stuff

  • Loading branch information...
1 parent 02e92d6 commit c4b9a9ec32c6e7f1d945dfc361e4f7c5fa7d6e43 @jsjohnst jsjohnst committed Sep 29, 2013
Showing with 129 additions and 0 deletions.
  1. +1 −0 index.html
  2. +128 −0 leapcopter.js
View
@@ -1,4 +1,5 @@
<script src="/socket.io/socket.io.js"></script>
+<script src="/leapcopter.js"></script>
<script>
var socket = io.connect('http://192.168.33.110');
//socket.emit('my other event', { my: 'data' });
View
@@ -0,0 +1,128 @@
+var output = document.getElementById("output");
+
+var inflight = false;
+var last_sub = null;
+
+function debounce(func, wait, immediate) {
+ var timeout, result;
+ return function() {
+ var context = this, args = arguments;
+ var later = function() {
+ timeout = null;
+ if (!immediate) result = func.apply(context, args);
+ };
+ var callNow = immediate && !timeout;
+ clearTimeout(timeout);
+ timeout = setTimeout(later, wait);
+ if (callNow) result = func.apply(context, args);
+ return result;
+ };
+};
+
+function sendCommandImmediate(cmd) {
+ socket.emit(cmd);
+}
+
+var sendCommand = debounce(function(cmd) {
+ sendCommandImmediate(cmd);
+ return true;
+}, 250, true);
+
+function gestureRecognized(gesture, sub) {
+ if(!inflight) {
+ if(gesture == "multi" && sub == "up") {
+ sendCommandImmediate("blink")
+ inflight = true;
+ }
+ return;
+ } else {
+ if(gesture == "swipe" && sub) {
+ sendCommand(sub);
+ } else if(gesture == "spin" && sub) {
+ sendCommandImmediate(sub);
+ } else if(gesture == "multi" && sub == "down") {
+ sendCommandImmediate("land");
+ inflight = false;
+ }
+ }
+}
+
+function handleMovement(gesture) {
+ gestureRecognized("swipe", detectMovement(gesture));
+}
+
+function detectMovement(gesture) {
+ var d = gesture.direction;
+ var velocity = 0;
+ var plane = null;
+ for(var index in d) {
+ if(Math.abs(d[index]) > Math.abs(velocity)) {
+ velocity = d[index];
+ plane = index;
+ }
+ }
+ switch(plane) {
+ case "0":
+ if(velocity < 0) {
+ return "left";
+ } else {
+ return "right";
+ }
+ break;
+
+ case "1":
+ if(velocity < 0) {
+ return "down";
+ } else {
+ return "up";
+ }
+ break;
+
+ case "2":
+ if(velocity < 0) {
+ return "forward";
+ } else {
+ return "back";
+ }
+ break;
+
+ }
+}
+
+function handleCircle(event, gesture) {
+ var pointable = event.pointables[0];
+ if(!pointable) {
+ console.log("No pointable");
+ return;
+ }
+ if(Leap.vec3.dot(pointable.direction, gesture.normal) > 0) {
+ gestureRecognized("spin", "clockwise");
+ } else {
+ gestureRecognized("spin", "counterclockwise");
+ }
+}
+
+var controllerOptions = {enableGestures: true};
+
+Leap.loop(controllerOptions, function(event) {
+ if (event.hands.length == 2 && event.gestures.length == 2) {
+ var hand1 = detectMovement(event.gestures[0]);
+ var hand2 = detectMovement(event.gestures[1]);
+
+ if(hand1 == "up" && hand2 == "up") {
+ return gestureRecognized("multi", "up");
+ } else if(hand1 == "down" && hand2 == "down") {
+ return gestureRecognized("multi", "down");
+ }
+ }
+
+ if(event.gestures.length > 0) {
+ var g = event.gestures[0];
+ if(g.type == "circle" && g.state == "stop") {
+ handleCircle(event, g);
+ } else {
+ handleMovement(g);
+ }
+ }
+});
+

0 comments on commit c4b9a9e

Please sign in to comment.