|
| 1 | +import sys, os, ConfigParser |
| 2 | +from leap import Leap, CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture |
| 3 | + |
| 4 | +class Motion_Control_Listener(Leap.Listener): #The Listener that we attach to the controller. This listener is for motion control |
| 5 | + def __init__(self, mouse): |
| 6 | + super(Motion_Control_Listener, self).__init__() #Initialize like a normal listener |
| 7 | + |
| 8 | + def on_init(self, controller): |
| 9 | + self.init_list_of_commands() |
| 10 | + |
| 11 | + print "Initialized" |
| 12 | + |
| 13 | + def init_list_of_commands(self): |
| 14 | + self.config = ConfigParser.ConfigParser() |
| 15 | + self.config.read("./commands.ini") |
| 16 | + |
| 17 | + self.commands = [ |
| 18 | + ScreentapCommand(), |
| 19 | + SwiperightCommand(), |
| 20 | + SwipeleftCommand(), |
| 21 | + CounterclockwiseCommand(), |
| 22 | + ClockwiseCommand(), |
| 23 | + KeytapCommand() |
| 24 | + ] |
| 25 | + |
| 26 | + def on_connect(self, controller): |
| 27 | + controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE); |
| 28 | + controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP); |
| 29 | + controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP); |
| 30 | + controller.enable_gesture(Leap.Gesture.TYPE_SWIPE); |
| 31 | + |
| 32 | + print "Connected" |
| 33 | + |
| 34 | + def on_disconnect(self, controller): |
| 35 | + print "Disconnected" |
| 36 | + |
| 37 | + def on_exit(self, controller): |
| 38 | + print "Exited" |
| 39 | + |
| 40 | + def on_frame(self, controller): |
| 41 | + frame = controller.frame() #Grab the latest 3D data |
| 42 | + if not frame.hands.empty: #Make sure we have some hands to work with |
| 43 | + for command in self.commands: |
| 44 | + if(command.applicable(frame)): |
| 45 | + self.execute(frame, command.name) |
| 46 | + |
| 47 | + def execute(self, frame, command_name): |
| 48 | + number_for_fingers = self.get_fingers_code(frame) |
| 49 | + if(self.config.has_option(command_name, number_for_fingers)): |
| 50 | + syscommand = self.config.get(command_name, number_for_fingers) |
| 51 | + print(syscommand) |
| 52 | + os.system(syscommand) |
| 53 | + |
| 54 | + def get_fingers_code(self, frame): |
| 55 | + return "%dfinger" % len(frame.fingers) |
| 56 | + |
| 57 | +class ScreentapCommand(): |
| 58 | + def __init__(self): |
| 59 | + self.name = "screentap" |
| 60 | + |
| 61 | + def applicable(self, frame): |
| 62 | + return(frame.gestures()[0].type == Leap.Gesture.TYPE_SCREEN_TAP) |
| 63 | + |
| 64 | +class KeytapCommand(): |
| 65 | + def __init__(self): |
| 66 | + self.name = "keytap" |
| 67 | + |
| 68 | + def applicable(self, frame): |
| 69 | + return(frame.gestures()[0].type == Leap.Gesture.TYPE_KEY_TAP) |
| 70 | + |
| 71 | +class SwiperightCommand(): |
| 72 | + def __init__(self): |
| 73 | + self.name = "swiperight" |
| 74 | + |
| 75 | + def applicable(self, frame): |
| 76 | + swipe = SwipeGesture(frame.gestures()[0]) |
| 77 | + return(swipe.state == Leap.Gesture.STATE_STOP |
| 78 | + and swipe.type == Leap.Gesture.TYPE_SWIPE |
| 79 | + and swipe.direction[0] < 0) |
| 80 | + |
| 81 | +class SwipeleftCommand(): |
| 82 | + def __init__(self): |
| 83 | + self.name = "swipeleft" |
| 84 | + |
| 85 | + def applicable(self, frame): |
| 86 | + swipe = SwipeGesture(frame.gestures()[0]) |
| 87 | + return(swipe.state == Leap.Gesture.STATE_STOP |
| 88 | + and swipe.type == Leap.Gesture.TYPE_SWIPE |
| 89 | + and swipe.direction[0] > 0) |
| 90 | + |
| 91 | +class ClockwiseCommand(): |
| 92 | + def __init__(self): |
| 93 | + self.name = "clockwise" |
| 94 | + |
| 95 | + def applicable(self, frame): |
| 96 | + circle = CircleGesture(frame.gestures()[0]) |
| 97 | + return(circle.state == Leap.Gesture.STATE_STOP and |
| 98 | + circle.type == Leap.Gesture.TYPE_CIRCLE and |
| 99 | + circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4) |
| 100 | + |
| 101 | +class CounterclockwiseCommand(): |
| 102 | + def __init__(self): |
| 103 | + self.name = "counterclockwise" |
| 104 | + |
| 105 | + def applicable(self, frame): |
| 106 | + circle = CircleGesture(frame.gestures()[0]) |
| 107 | + return(circle.state == Leap.Gesture.STATE_STOP and |
| 108 | + circle.type == Leap.Gesture.TYPE_CIRCLE and |
| 109 | + circle.pointable.direction.angle_to(circle.normal) > Leap.PI/4) |
0 commit comments