00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 import sys
00018 import time
00019
00020
00021
00022
00023
00024
00025
00026
00027 import threading
00028 import Leap
00029 from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
00030
00031
00032 class LeapInterface(Leap.Listener):
00033 def on_init(self, controller):
00034
00035
00036 self.hand = [0,0,0]
00037 self.right_hand = False
00038 self.left_hand = False
00039 self.hand_direction = [0,0,0]
00040 self.hand_normal = [0,0,0]
00041 self.hand_palm_pos = [0,0,0]
00042 self.hand_pitch = 0.0
00043 self.hand_yaw = 0.0
00044 self.hand_roll = 0.0
00045 print "Initialized Leap Motion Device"
00046
00047 def on_connect(self, controller):
00048 print "Connected to Leap Motion Controller"
00049
00050
00051 controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
00052 controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
00053 controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
00054 controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
00055
00056 def on_disconnect(self, controller):
00057
00058 print "Disconnected Leap Motion"
00059
00060 def on_exit(self, controller):
00061 print "Exited Leap Motion Controller"
00062
00063 def on_frame(self, controller):
00064
00065 frame = controller.frame()
00066
00067 print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
00068 frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))
00069
00070 if not frame.hands.is_empty:
00071
00072
00073
00074
00075 there_is_right_hand=False
00076 there_is_left_hand=False
00077
00078 for hand in frame.hands:
00079
00080 if hand.is_right:
00081 there_is_right_hand=True
00082 self.right_hand=hand
00083 elif hand.is_left:
00084 there_is_left_hand=True
00085
00086 self.left_hand=hand
00087
00088 if not there_is_right_hand:
00089 self.right_hand=False
00090
00091 if not there_is_left_hand:
00092 self.left_hand=False
00093
00094 self.hand = frame.hands[0]
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 normal = self.hand.palm_normal
00111 direction = self.hand.direction
00112 pos = self.hand.palm_position
00113
00114 self.hand_direction[0] = direction.x
00115 self.hand_direction[1] = direction.y
00116 self.hand_direction[2] = direction.z
00117 self.hand_normal[0] = normal.x
00118 self.hand_normal[1] = normal.y
00119 self.hand_normal[2] = normal.z
00120 self.hand_palm_pos[0] = pos.x
00121 self.hand_palm_pos[1] = pos.y
00122 self.hand_palm_pos[2] = pos.z
00123 self.hand_pitch = direction.pitch * Leap.RAD_TO_DEG
00124 self.hand_yaw = normal.yaw * Leap.RAD_TO_DEG
00125 self.hand_roll = direction.roll * Leap.RAD_TO_DEG
00126
00127
00128 print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (self.hand_pitch, self.hand_roll, self.hand_yaw)
00129
00130 '''
00131 # Gestures
00132 for gesture in frame.gestures():
00133 if gesture.type == Leap.Gesture.TYPE_CIRCLE:
00134 circle = CircleGesture(gesture)
00135
00136 # Determine clock direction using the angle between the pointable and the circle normal
00137 if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
00138 clockwiseness = "clockwise"
00139 else:
00140 clockwiseness = "counterclockwise"
00141
00142 # Calculate the angle swept since the last frame
00143 swept_angle = 0
00144 if circle.state != Leap.Gesture.STATE_START:
00145 previous_update = CircleGesture(controller.frame(1).gesture(circle.id))
00146 swept_angle = (circle.progress - previous_update.progress) * 2 * Leap.PI
00147
00148 print "Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % (
00149 gesture.id, self.state_string(gesture.state),
00150 circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness)
00151
00152 if gesture.type == Leap.Gesture.TYPE_SWIPE:
00153 swipe = SwipeGesture(gesture)
00154 print "Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
00155 gesture.id, self.state_string(gesture.state),
00156 swipe.position, swipe.direction, swipe.speed)
00157
00158 if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
00159 keytap = KeyTapGesture(gesture)
00160 print "Key Tap id: %d, %s, position: %s, direction: %s" % (
00161 gesture.id, self.state_string(gesture.state),
00162 keytap.position, keytap.direction )
00163
00164 if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
00165 screentap = ScreenTapGesture(gesture)
00166 print "Screen Tap id: %d, %s, position: %s, direction: %s" % (
00167 gesture.id, self.state_string(gesture.state),
00168 screentap.position, screentap.direction )
00169
00170 if not (frame.hands.empty and frame.gestures().empty):
00171 print ""
00172
00173 def state_string(self, state):
00174 if state == Leap.Gesture.STATE_START:
00175 return "STATE_START"
00176
00177 if state == Leap.Gesture.STATE_UPDATE:
00178 return "STATE_UPDATE"
00179
00180 if state == Leap.Gesture.STATE_STOP:
00181 return "STATE_STOP"
00182
00183 if state == Leap.Gesture.STATE_INVALID:
00184 return "STATE_INVALID"
00185 '''
00186
00187 def get_hand_direction(self):
00188 return self.hand_direction
00189
00190 def get_hand_normal(self):
00191 return self.hand_normal
00192
00193 def get_hand_palmpos(self):
00194 return self.hand_palm_pos
00195
00196 def get_hand_yaw(self):
00197 return self.hand_yaw
00198
00199 def get_hand_pitch(self):
00200 return self.hand_pitch
00201
00202 def get_hand_roll(self):
00203 return self.hand_roll
00204
00205
00206 class Runner(threading.Thread):
00207
00208 def __init__(self,arg=None):
00209 threading.Thread.__init__(self)
00210 self.arg=arg
00211 self.listener = LeapInterface()
00212 self.controller = Leap.Controller()
00213 self.controller.add_listener(self.listener)
00214
00215 def __del__(self):
00216 self.controller.remove_listener(self.listener)
00217
00218 def get_hand_direction(self):
00219 return self.listener.get_hand_direction()
00220
00221 def get_hand_normal(self):
00222 return self.listener.get_hand_normal()
00223
00224 def get_hand_palmpos(self):
00225 return self.listener.get_hand_palmpos()
00226
00227 def get_hand_roll(self):
00228 return self.listener.get_hand_roll()
00229
00230 def get_hand_pitch(self):
00231 return self.listener.get_hand_pitch()
00232
00233 def get_hand_yaw(self):
00234 return self.listener.get_hand_yaw()
00235
00236 def run (self):
00237 while True:
00238
00239 time.sleep(0.001)
00240