-
Notifications
You must be signed in to change notification settings - Fork 65
/
MotionControl.py
121 lines (96 loc) · 4.67 KB
/
MotionControl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import sys, os, ConfigParser
from leap import Leap, CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
class Motion_Control_Listener(Leap.Listener): #The Listener that we attach to the controller. This listener is for motion control
def __init__(self, mouse):
super(Motion_Control_Listener, self).__init__() #Initialize like a normal listener
def on_init(self, controller):
self.read_config() #Read the config file
self.init_list_of_commands() #Initialize the list of recognized commands
print "Initialized"
def read_config(self):
self.config = ConfigParser.ConfigParser()
self.config.read("./commands.ini")
def init_list_of_commands(self):
#Initialize all commands an put it in an array
self.commands = [
ScreentapCommand(),
SwiperightCommand(),
SwipeleftCommand(),
CounterclockwiseCommand(),
ClockwiseCommand(),
KeytapCommand()
]
def on_connect(self, controller):
#Enable all 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);
print "Connected"
def on_disconnect(self, controller):
print "Disconnected"
def on_exit(self, controller):
print "Exited"
def on_frame(self, controller):
frame = controller.frame() #Grab the latest 3D data
if not frame.hands.is_empty: #Make sure we have some hands to work with
for command in self.commands: #Loop all enabled commands
if(command.applicable(frame)): #If the motion associated to the command is triggered
self.execute(frame, command.name) #Execute the command
def execute(self, frame, command_name):
number_for_fingers = self.get_fingers_code(frame) #Get a text correspond to the number of fingers
if(self.config.has_option(command_name, number_for_fingers)): #If the command if finded in the config file
syscommand = self.config.get(command_name, number_for_fingers) #Prepare the command
print(syscommand)
os.system(syscommand) #Execute the command
def get_fingers_code(self, frame):
return "%dfinger" % len(frame.fingers)
class ScreentapCommand():
def __init__(self):
self.name = "screentap"
#The name of the command in the config file
#Return true if the command is applicable
def applicable(self, frame):
return(frame.gestures()[0].type == Leap.Gesture.TYPE_SCREEN_TAP)
class KeytapCommand():
def __init__(self):
self.name = "keytap" #The name of the command in the config file
#Return true if the command is applicable
def applicable(self, frame):
return(frame.gestures()[0].type == Leap.Gesture.TYPE_KEY_TAP)
class SwiperightCommand():
def __init__(self):
self.name = "swiperight" #The name of the command in the config file
#Return true if the command is applicable
def applicable(self, frame):
swipe = SwipeGesture(frame.gestures()[0])
return(swipe.state == Leap.Gesture.STATE_STOP
and swipe.type == Leap.Gesture.TYPE_SWIPE
and swipe.direction[0] < 0)
class SwipeleftCommand():
def __init__(self):
self.name = "swipeleft" #The name of the command in the config file
#Return true if the command is applicable
def applicable(self, frame):
swipe = SwipeGesture(frame.gestures()[0])
return(swipe.state == Leap.Gesture.STATE_STOP
and swipe.type == Leap.Gesture.TYPE_SWIPE
and swipe.direction[0] > 0)
class ClockwiseCommand():
def __init__(self):
self.name = "clockwise" #The name of the command in the config file
#Return true if the command is applicable
def applicable(self, frame):
circle = CircleGesture(frame.gestures()[0])
return(circle.state == Leap.Gesture.STATE_STOP and
circle.type == Leap.Gesture.TYPE_CIRCLE and
circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4)
class CounterclockwiseCommand():
def __init__(self):
self.name = "counterclockwise" #The name of the command in the config file
#Return true if the command is applicable
def applicable(self, frame):
circle = CircleGesture(frame.gestures()[0])
return(circle.state == Leap.Gesture.STATE_STOP and
circle.type == Leap.Gesture.TYPE_CIRCLE and
circle.pointable.direction.angle_to(circle.normal) > Leap.PI/4)