Created
February 20, 2015 11:01
-
-
Save tomhartley/4c4522b3097369805b5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
# 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, thread, time, os | |
from math import ceil | |
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture | |
import time | |
XLEFT = -200 | |
XRIGHT = 200 | |
YTOP = 400 | |
YBOTTOM = 200 | |
def map_x(i): | |
a= int((i - XLEFT) / (1.0 * (XRIGHT - XLEFT)) * 100) | |
if (a<0): | |
return 0 | |
if (a>99): | |
return 99 | |
return a | |
def map_y(i): | |
a = int((-i + YTOP) / (1.0 * (YTOP - YBOTTOM)) * 50) | |
if (a<0): | |
return 0 | |
if (a>49): | |
return 49 | |
return a | |
def dothething(x1,y1, x2, y2): | |
# print input_x, input_y | |
if not (x1 == None and y1 == None): | |
x1 = map_x(x1) | |
y1 = map_y(y1) | |
else: | |
x1 = -100 | |
y1 = -100 | |
if not (x2 == None and y2 == None): | |
x2 = map_x(x2) | |
y2 = map_y(y2) | |
else: | |
x2 = -100 | |
y2 = -100 | |
# return | |
#print input_x, input_y | |
# print(chr(27) + "[2J") | |
sys.stderr.write("\x1b[2J\x1b[H") | |
for y in range(50): | |
s = "" | |
for x in range(100): | |
if (abs(x1- x)<=1 and abs(y1-y)<=1) or (abs(x2-x) <= 1 and abs(y2-y) <= 1): | |
s += "*" | |
else: | |
s +=" " | |
print s | |
time.sleep(0.02) | |
class SampleListener(Leap.Listener): | |
finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky'] | |
bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal'] | |
state_names = ['STATE_INVALID', 'STATE_START', 'STATE_UPDATE', 'STATE_END'] | |
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())) | |
x1, y1, x2, y2 = None, None, None, None | |
# Get hands | |
for i in range(len(frame.hands)): | |
hand = frame.hands[i] | |
handType = "Left hand" if hand.is_left else "Right hand" | |
# print " %s, id %d, position: %s" % ( | |
# handType, hand.id, 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 " 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) | |
# # Get arm bone | |
# arm = hand.arm | |
# print " Arm direction: %s, wrist position: %s, elbow position: %s" % ( | |
# arm.direction, | |
# arm.wrist_position, | |
# arm.elbow_position) | |
# Get fingers | |
for finger in hand.fingers: | |
if finger.type()==1: | |
#print "Found it!" | |
# print " %s finger, id: %d, length: %fmm, width: %fmm" % ( | |
# self.finger_names[finger.type()], | |
# finger.id, | |
# finger.length, | |
# finger.width) | |
# Get bones | |
for b in range(0, 4): | |
bone = finger.bone(b) | |
if bone.type==3: | |
if i==0: | |
x1 = bone.next_joint[0] | |
y1 = bone.next_joint[1] | |
else: | |
x2 = bone.next_joint[0] | |
y2 = bone.next_joint[1] | |
#print " Bone: %s, start: %s, end: %s, direction: %s" % ( | |
# self.bone_names[bone.type], | |
# bone.prev_joint, | |
# bone.next_joint, | |
# bone.direction) | |
dothething(x1,y1,x2,y2) | |
# Get tools | |
for tool in frame.tools: | |
print " Tool id: %d, position: %s, direction: %s" % ( | |
tool.id, tool.tip_position, tool.direction) | |
# # Get 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/2: | |
# 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_names[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_names[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_names[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_names[gesture.state], | |
# screentap.position, screentap.direction ) | |
#if not (frame.hands.is_empty and frame.gestures().is_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..." | |
try: | |
sys.stdin.readline() | |
except KeyboardInterrupt: | |
pass | |
finally: | |
# Remove the sample listener when done | |
controller.remove_listener(listener) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment