- Quick mockup of Leap Motion stuff

master
Steve Clement 2013-08-01 18:58:12 +02:00
parent a1cd294192
commit a7519e6a88
7 changed files with 1644 additions and 0 deletions

View File

@ -10,6 +10,8 @@ PoC/
= This plays pre-defined patterns
-> life
= Game of Life
-> leap
= Leap motion controller demo
-> cheap_random
= dirty hack to show random windows
-> 7seg
@ -25,6 +27,7 @@ PoC/
-> testcolors
= Test the 16x32 RGB Panel
Leap motion Python issues: under OS-X make sure you use the stock Python interpreter (/usr/bin/python)
The shift Array code is from this Tutorial page:

197
PoC/leap/HandsPoC.py Executable file
View File

@ -0,0 +1,197 @@
#!/usr/bin/python
################################################################################
# Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. #
# Leap Motion proprietary and confidential. Not for distribution. #
# Use subject to the terms of the Leap Motion SDK Agreement available at #
# https://developer.leapmotion.com/sdk_agreement, or another agreement #
# between Leap Motion and you, your company or other organization. #
################################################################################
import Leap, sys
import serial, time, os
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
tty_USB = "/dev/tty.usbserial-A7005FGN"
ser = serial.Serial(port = tty_USB, baudrate=9600)
ser.close()
ser.open()
time.sleep(1)
class SampleListener(Leap.Listener):
def on_init(self, controller):
print "Initialized"
def on_connect(self, controller):
print "Connected"
# Enable gestures
controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
def on_disconnect(self, controller):
# Note: not dispatched when running in a debugger.
print "Disconnected"
def on_exit(self, controller):
print "Exited"
def on_frame(self, controller):
# Get the most recent frame and report some basic information
frame = controller.frame()
##print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
## frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))
if not frame.hands.empty:
# Get the first hand
hand = frame.hands[0]
# Check if the hand has any fingers
fingers = hand.fingers
# Check if there is a fist
if hand and fingers.empty:
print "We have a fist, so let's make the ball vanish"
if ser.isOpen():
ser.write(str(0))
time.sleep(0.3)
# if len(frame.hands) == 1 and len(fingers) == 5:
# print "We have a one full hand, so let's make the ball appear"
# if ser.isOpen():
# ser.write(str(1))
# time.sleep(0.3)
if len(frame.hands) == 2 and len(fingers) == 5:
print "We have a two full hands, so let's make the balls appear"
if ser.isOpen():
ser.write(str(2))
time.sleep(0.3)
if len(frame.hands) == 2 and len(fingers) == 2:
print "Bye Bye"
##if ser.isOpen():
## ser.write(str(9))
time.sleep(0.3)
if not fingers.empty:
# Calculate the hand's average finger tip position
avg_pos = Leap.Vector()
for finger in fingers:
avg_pos += finger.tip_position
avg_pos /= len(fingers)
##print "Hand has %d fingers, average finger tip position: %s" % (
## len(fingers), avg_pos)
# Get the hand's sphere radius and palm position
##print "Hand sphere radius: %f mm, palm position: %s" % (
## hand.sphere_radius, hand.palm_position[0])
#print "Number of Hands: %s - Number of fingers: %s - Palm position: %s" % ( len(frame.hands), len(fingers), hand.palm_position[0])
if len(frame.hands) == 1 and len(fingers) == 5 and -170 < hand.palm_position[0] < -85:
print "w5"
if ser.isOpen():
ser.write(str(4))
time.sleep(0.3)
if len(frame.hands) == 1 and len(fingers) == 5 and -84 < hand.palm_position[0] < 0:
print "w10"
if ser.isOpen():
ser.write(str(5))
time.sleep(0.3)
if len(frame.hands) == 1 and len(fingers) == 5 and 1 < hand.palm_position[0] < 85:
print "w15"
if ser.isOpen():
ser.write(str(6))
time.sleep(0.3)
if len(frame.hands) == 1 and len(fingers) == 5 and 86 < hand.palm_position[0] < 170:
print "w20"
if ser.isOpen():
ser.write(str(7))
time.sleep(0.3)
# Get the hand's normal vector and direction
normal = hand.palm_normal
direction = hand.direction
# Calculate the hand's pitch, roll, and yaw angles
##print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (
## direction.pitch * Leap.RAD_TO_DEG,
## normal.roll * Leap.RAD_TO_DEG,
## direction.yaw * Leap.RAD_TO_DEG)
# Gestures
for gesture in frame.gestures():
if gesture.type == Leap.Gesture.TYPE_CIRCLE:
circle = CircleGesture(gesture)
# Determine clock direction using the angle between the pointable and the circle normal
if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
clockwiseness = "clockwise"
else:
clockwiseness = "counterclockwise"
# Calculate the angle swept since the last frame
swept_angle = 0
if circle.state != Leap.Gesture.STATE_START:
previous_update = CircleGesture(controller.frame(1).gesture(circle.id))
swept_angle = (circle.progress - previous_update.progress) * 2 * Leap.PI
##print "Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % (
## gesture.id, self.state_string(gesture.state),
## circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness)
if gesture.type == Leap.Gesture.TYPE_SWIPE:
swipe = SwipeGesture(gesture)
##print "Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
## gesture.id, self.state_string(gesture.state),
## swipe.position, swipe.direction, swipe.speed)
if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
keytap = KeyTapGesture(gesture)
print "Key Tap id: %d, %s, position: %s, direction: %s" % (
gesture.id, self.state_string(gesture.state),
keytap.position, keytap.direction )
if ser.isOpen():
ser.write(str(3))
time.sleep(0.3)
if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
screentap = ScreenTapGesture(gesture)
##print "Screen Tap id: %d, %s, position: %s, direction: %s" % (
## gesture.id, self.state_string(gesture.state),
## screentap.position, screentap.direction )
#if not (frame.hands.empty and frame.gestures().empty):
# print ""
def state_string(self, state):
if state == Leap.Gesture.STATE_START:
return "STATE_START"
if state == Leap.Gesture.STATE_UPDATE:
return "STATE_UPDATE"
if state == Leap.Gesture.STATE_STOP:
return "STATE_STOP"
if state == Leap.Gesture.STATE_INVALID:
return "STATE_INVALID"
def main():
# Create a sample listener and controller
listener = SampleListener()
controller = Leap.Controller()
# Have the sample listener receive events from the controller
controller.add_listener(listener)
# Keep this process running until Enter is pressed
print "Press Enter to quit..."
sys.stdin.readline()
# Remove the sample listener when done
controller.remove_listener(listener)
if __name__ == "__main__":
main()

1082
PoC/leap/Leap.py Normal file

File diff suppressed because it is too large Load Diff

BIN
PoC/leap/LeapPython.so Executable file

Binary file not shown.

142
PoC/leap/Sample-Vanilla.py Executable file
View File

@ -0,0 +1,142 @@
#!/usr/bin/python
################################################################################
# Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. #
# Leap Motion proprietary and confidential. Not for distribution. #
# Use subject to the terms of the Leap Motion SDK Agreement available at #
# https://developer.leapmotion.com/sdk_agreement, or another agreement #
# between Leap Motion and you, your company or other organization. #
################################################################################
import Leap, sys
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
class SampleListener(Leap.Listener):
def on_init(self, controller):
print "Initialized"
def on_connect(self, controller):
print "Connected"
# Enable gestures
controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
def on_disconnect(self, controller):
# Note: not dispatched when running in a debugger.
print "Disconnected"
def on_exit(self, controller):
print "Exited"
def on_frame(self, controller):
# Get the most recent frame and report some basic information
frame = controller.frame()
print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))
if not frame.hands.empty:
# Get the first hand
hand = frame.hands[0]
# Check if the hand has any fingers
fingers = hand.fingers
if not fingers.empty:
# Calculate the hand's average finger tip position
avg_pos = Leap.Vector()
for finger in fingers:
avg_pos += finger.tip_position
avg_pos /= len(fingers)
print "Hand has %d fingers, average finger tip position: %s" % (
len(fingers), avg_pos)
# Get the hand's sphere radius and palm position
print "Hand sphere radius: %f mm, palm position: %s" % (
hand.sphere_radius, hand.palm_position)
# Get the hand's normal vector and direction
normal = hand.palm_normal
direction = hand.direction
# Calculate the hand's pitch, roll, and yaw angles
print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (
direction.pitch * Leap.RAD_TO_DEG,
normal.roll * Leap.RAD_TO_DEG,
direction.yaw * Leap.RAD_TO_DEG)
# Gestures
for gesture in frame.gestures():
if gesture.type == Leap.Gesture.TYPE_CIRCLE:
circle = CircleGesture(gesture)
# Determine clock direction using the angle between the pointable and the circle normal
if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
clockwiseness = "clockwise"
else:
clockwiseness = "counterclockwise"
# Calculate the angle swept since the last frame
swept_angle = 0
if circle.state != Leap.Gesture.STATE_START:
previous_update = CircleGesture(controller.frame(1).gesture(circle.id))
swept_angle = (circle.progress - previous_update.progress) * 2 * Leap.PI
print "Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % (
gesture.id, self.state_string(gesture.state),
circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness)
if gesture.type == Leap.Gesture.TYPE_SWIPE:
swipe = SwipeGesture(gesture)
print "Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
gesture.id, self.state_string(gesture.state),
swipe.position, swipe.direction, swipe.speed)
if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
keytap = KeyTapGesture(gesture)
print "Key Tap id: %d, %s, position: %s, direction: %s" % (
gesture.id, self.state_string(gesture.state),
keytap.position, keytap.direction )
if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
screentap = ScreenTapGesture(gesture)
print "Screen Tap id: %d, %s, position: %s, direction: %s" % (
gesture.id, self.state_string(gesture.state),
screentap.position, screentap.direction )
if not (frame.hands.empty and frame.gestures().empty):
print ""
def state_string(self, state):
if state == Leap.Gesture.STATE_START:
return "STATE_START"
if state == Leap.Gesture.STATE_UPDATE:
return "STATE_UPDATE"
if state == Leap.Gesture.STATE_STOP:
return "STATE_STOP"
if state == Leap.Gesture.STATE_INVALID:
return "STATE_INVALID"
def main():
# Create a sample listener and controller
listener = SampleListener()
controller = Leap.Controller()
# Have the sample listener receive events from the controller
controller.add_listener(listener)
# Keep this process running until Enter is pressed
print "Press Enter to quit..."
sys.stdin.readline()
# Remove the sample listener when done
controller.remove_listener(listener)
if __name__ == "__main__":
main()

220
PoC/leap/leap.ino Normal file
View File

@ -0,0 +1,220 @@
// testcolors demo for RGBmatrixPanel library.
// Renders 512 colors on a 16x32 RGB LED matrix.
// Library supports 4096 colors, but there aren't that many pixels!
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
#define CLK 8 // MUST be on PORTB!
#define LAT A3
#define OE 9
#define A A0
#define B A1
#define C A2
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);
int incomingByte = 0; // for incoming serial data
void setup() {
matrix.begin();
Serial.begin(9600);
//myWindows(random(1,21));
int window = 13;
int brightness = 255;
int del = 100;
// White is 7,7,7 (3bit) 255,255,255 (8bit)
uint8_t red=255, green=255, blue=255;
int w_init[] = { 1, 7, 13, 19, 15 };
int w_jump[] = { 15, 14, 13, 12, 11, 11, 12, 13, 14, 15 };
while ( true ) {
// Initialize field
for (int i=0; i < 5; i++) {
myWindows(w_init[i], brightness, red, green, blue);
delay(del-20);
}
//myWindows(16, brightness, red, green, blue);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color888(0, 0, 0));
matrix.swapBuffers(false);
// myWindows(15, brightness, red, green, blue);
Serial.println("rdy");
// 48 == 0 -- 49 == 1
// send data only when you receive data:
while (incomingByte != 57 ) {
//Serial.println("Waiting :)");
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
switch (incomingByte){
// No hands or Fist
case 48:
matrix.fillScreen(matrix.Color888(0, 0, 0));
break;
// One Hand
case 49:
myWindows(5, brightness, 0, 0, 0);
myWindows(10, brightness, 0, 0, 0);
myWindows(15, brightness, red, green, blue);
myWindows(20, brightness, 0, 0, 0);
break;
// Two hands, two balls
case 50:
myWindows(5, brightness, 0, 0, 0);
myWindows(10, brightness, 0, 0, 0);
myWindows(15, brightness, red, green, blue);
myWindows(20, brightness, red, green, blue);
break;
// Make ball jump
case 51:
for (int i=0; i < 11; i++) {
myWindows(w_jump[i], brightness, red, green, blue);
delay(del);
matrix.fillScreen(matrix.Color888(0, 0, 0));
}
myWindows(15, brightness, red, green, blue);
break;
// Move ball horizontally w5
case 52:
myWindows(5, brightness, red, green, blue);
myWindows(15, brightness, 0, 0, 0);
myWindows(10, brightness, 0, 0, 0);
myWindows(20, brightness, 0, 0, 0);
break;
// Move ball horizontally w10
case 53:
myWindows(5, brightness, 0, 0, 0);
myWindows(10, brightness, red, green, blue);
myWindows(15, brightness, 0, 0, 0);
myWindows(20, brightness, 0, 0, 0);
break;
// Move ball horizontally w15
case 54:
myWindows(5, brightness, 0, 0, 0);
myWindows(10, brightness, 0, 0, 0);
myWindows(15, brightness, red, green, blue);
myWindows(20, brightness, 0, 0, 0);
break;
// Move ball horizontally w20
case 55:
myWindows(5, brightness, 0, 0, 0);
myWindows(15, brightness, 0, 0, 0);
myWindows(10, brightness, 0, 0, 0);
myWindows(20, brightness, red, green, blue);
break;
default:
matrix.fillScreen(matrix.Color888(0, 0, 0));
break;
}
// say what you got:
//Serial.print("I received: ");
//Serial.println(incomingByte, DEC);
}
delay(100);
}
incomingByte=0;
myWindows(5, brightness, red, green, blue);
myWindows(10, brightness, red, green, blue);
myWindows(15, brightness, red, green, blue);
myWindows(20, brightness, red, green, blue);
delay(10000);
}
}
int myWindows (int w, int br, int r, int g, int b){
switch (w) {
case 1:
// Window 1
matrix.fillRect(1,12,4,3, matrix.Color888(r,g,b));
break;
case 2:
// Window 2
matrix.fillRect(7,12,4,3, matrix.Color888(r,g,b));
break;
case 3:
// Window 3
matrix.fillRect(13,12,4,3, matrix.Color888(r,g,b));
break;
case 4:
// Window 4
matrix.fillRect(19,12,4,3, matrix.Color888(r,g,b));
break;
case 5:
// Window 5
matrix.fillRect(25,12,4,3, matrix.Color888(r,g,b));
break;
case 6:
// Window 6
matrix.fillRect(1,8,4,3, matrix.Color888(r,g,b));
break;
case 7:
// Window 7
matrix.fillRect(7,8,4,3, matrix.Color888(r,g,b));
break;
case 8:
// Window 8
matrix.fillRect(13,8,4,3, matrix.Color888(r,g,b));
break;
case 9:
// Window 9
matrix.fillRect(19,8,4,3, matrix.Color888(r,g,b));
break;
case 10:
// Window 10
matrix.fillRect(25,8,4,3, matrix.Color888(r,g,b));
break;
case 11:
// Window 11
matrix.fillRect(1,4,4,3, matrix.Color888(r,g,b));
break;
case 12:
// Window 12
matrix.fillRect(7,4,4,3, matrix.Color888(r,g,b));
break;
case 13:
// Window 13
matrix.fillRect(13,4,4,3, matrix.Color888(r,g,b));
break;
case 14:
// Window 14
matrix.fillRect(19,4,4,3, matrix.Color888(r,g,b));
break;
case 15:
// Window 15
matrix.fillRect(25,4,4,3, matrix.Color888(r,g,b));
break;
case 16:
// Window 16
matrix.fillRect(1,0,4,3, matrix.Color888(r,g,b));
break;
case 17:
// Window 17
matrix.fillRect(7,0,4,3, matrix.Color888(r,g,b));
break;
case 18:
// Window 18
matrix.fillRect(13,0,4,3, matrix.Color888(r,g,b));
break;
case 19:
// Window 19
matrix.fillRect(19,0,4,3, matrix.Color888(r,g,b));
break;
case 20:
// Window 20
matrix.fillRect(25,0,4,3, matrix.Color888(r,g,b));
break;
default:
break;
}
}
void loop() {
// do nothing
}

BIN
PoC/leap/libLeap.dylib Executable file

Binary file not shown.