Skip to content

Commit 88a6775

Browse files
committed
enable to do commands with motions
1 parent 40324df commit 88a6775

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

MotionControl.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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)

PyLeapMouse.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from leap import Leap, Mouse
55
from PalmControl import Palm_Control_Listener #For palm-tilt based control
66
from FingerControl import Finger_Control_Listener #For finger-pointing control
7+
from MotionControl import Motion_Control_Listener #For motion control
78

89
def show_help():
910
print "----------------------------------PyLeapMouse----------------------------------"
@@ -23,13 +24,21 @@ def main():
2324

2425
#Default
2526
finger_mode = True
27+
palm_mode = False
28+
motion_mode = False
2629
smooth_aggressiveness = 8
2730
smooth_falloff = 1.3
2831

2932
for i in range(0,len(sys.argv)):
3033
arg = sys.argv[i].lower()
3134
if "--palm" in arg:
3235
finger_mode = False
36+
palm_mode = True
37+
motion_mode = False
38+
if "--motion" in arg:
39+
finger_mode = False
40+
palm_mode = False
41+
motion_mode = True
3342
if "--smooth-falloff" in arg:
3443
smooth_falloff = float(sys.argv[i+1])
3544
if "--smooth-aggressiveness" in arg:
@@ -41,9 +50,12 @@ def main():
4150
if finger_mode: #Finger pointer mode
4251
listener = Finger_Control_Listener(Mouse, smooth_aggressiveness=smooth_aggressiveness, smooth_falloff=smooth_falloff)
4352
print "Using finger mode..."
44-
else: #Palm control mode
53+
elif palm_mode: #Palm control mode
4554
listener = Palm_Control_Listener(Mouse)
4655
print "Using palm mode..."
56+
elif motion_mode: #Motion control mode
57+
listener = Motion_Control_Listener(Mouse)
58+
print "Using motion mode..."
4759

4860

4961
controller = Leap.Controller() #Get a Leap controller

leap.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
if sys.platform == "darwin":
33
import OSX.Leap as Leap
44
import OSX.Mouse as Mouse
5+
from OSX.Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
56
elif 'linux' in sys.platform:
67
import Linux.Leap as Leap
78
import Linux.Mouse as Mouse
9+
from Linux.Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
810
else:
911
import Windows.Leap as Leap
1012
import Windows.Mouse as Mouse
13+
from Windows.Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture

0 commit comments

Comments
 (0)