-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyLeapPyTest.py
270 lines (189 loc) · 6.56 KB
/
MyLeapPyTest.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import os, sys, inspect, thread, time, uinput
import Leap
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
device = uinput.Device([
uinput.BTN_LEFT,
uinput.BTN_RIGHT,
uinput.REL_X,
uinput.REL_Y,
])
class SampleListener(Leap.Listener):
###################################User variables...changable########################################
NumDragFin = 5
NumDClickFin = 2
NumRClickFin = 4
NumPointFin = 1
PrintCommands = 0
ContiniousMouseSpeed = 9 #speed od continious mouse movement with finger (the smaller num, the faster movement)
###################################Program variables...do not change!###############################
Hand = 0
Frame = 0
start_frame=0
OLDXpos = 0
OLDYpos = 0
NoTrackHandSphereRadius = 40 #closed hand is usually 30
PointHandSphereRadius = 180 #max limit with which you only controll the mouse movement
MouseClick = 0
DragClicked = 0
DubleClicked = 0
RightClicked = 0
SingleClicked = 0
OLDFingersExtended = 0
WaitBetweenCommandsCount = 0
BetweenCommandsMaxCount = 20
MaxJumpVal = 30 #max pixels crouser is allowed to jump on screen
THUMB = 0
INDEX = 1
MIDDLE = 2
RING = 3
PINKY = 4
def on_connect(self, controller):
print "Connected"
controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
def on_frame(self, controller):
frame = controller.frame()
#if fingers have been detected
if not frame.fingers.is_empty:
#hand info:
hand = frame.hands.rightmost
hand_pointables = hand.pointables
#make variables global
self.Hand = hand
self.Frame = frame
#stabilized_position = pointable.stabilized_tip_position
extended_finger_list = frame.fingers.extended()
FingersExtendedNum = len(extended_finger_list)
index_fingers =hand.fingers.finger_type(self.INDEX)
#print index_fingers
#if only one finger is extended, move mouse
if(FingersExtendedNum==self.NumPointFin or FingersExtendedNum ==self.NumDragFin):
PointingFingerTipPos = extended_finger_list[0].stabilized_tip_position
FingerTipVelocity = extended_finger_list[0].tip_velocity
#here you can choose the stabilazed or non stabilazed hand positioning
HandPosition = hand.stabilized_palm_position
#HandPosition = hand.palm_position
HandVelocity = hand.palm_velocity
AvgHandVel = (abs(HandVelocity[0])+abs(HandVelocity[1])+abs(HandVelocity[2]))/3
if(AvgHandVel>20 or self.DragClicked==1):
self.Mouse(HandPosition, HandVelocity, 1)
elif(AvgHandVel<=20):
self.ContiniousMouse(HandPosition, PointingFingerTipPos)
#reset clicks so you can click again
self.DubleClicked = 0
self.RightClicked = 0
self.SingleClicked = 0
#To avoid multiple commands per command
if(self.WaitingBetweenCommands(FingersExtendedNum)==0):
#duble click and avoid clicking continiously(DubleClicked)
if(FingersExtendedNum == self.NumDClickFin and self.DubleClicked ==0):
self.Click(1)
#self.DubleClicked = 1
#drag, avoid clicking multiple times per press(DragClicked)
if(FingersExtendedNum ==self.NumDragFin and self.DragClicked ==0):
self.Drag(1)
self.DragClicked = 1
#drop when finished dragging
elif(FingersExtendedNum !=self.NumDragFin and self.DragClicked ==1):
self.Drag(0)
self.DragClicked = 0
#Right click
if(FingersExtendedNum == self.NumRClickFin and self.RightClicked ==0):
self.RightClick()
self.RightClicked = 1
def Drag(self, Dragging):
#drag
if(Dragging ==1):
device.emit(uinput.BTN_LEFT,1)
if(self.PrintCommands==1):
print "Drag ON"
if(Dragging==0):
device.emit(uinput.BTN_LEFT,0)
if (self.PrintCommands==1):
print "Drag OFF"
def Click(self, NumOfClicks):
#duble click
if (NumOfClicks == 2):
device.emit(uinput.BTN_LEFT,1)
device.emit(uinput.BTN_LEFT,0)
device.emit(uinput.BTN_LEFT,1)
device.emit(uinput.BTN_LEFT,0)
if(self.PrintCommands==1):
print "Click Click"
if(NumOfClicks == 1):
#if you just entered remember th ebeginnig frame to reference the angle to
if(self.SingleClicked == 0):
self.start_frame = self.Frame
self.SingleClicked = 2
#print self.start_frame
HandRotation = self.Hand.rotation_angle(self.start_frame, Leap.Vector.y_axis)
print HandRotation
def RightClick(self):
device.emit(uinput.BTN_RIGHT,1)
device.emit(uinput.BTN_RIGHT,0)
if(self.PrintCommands==1):
print "RClick"
def Mouse(self, Pos, Velocity, HighLowVelocity):
Xpos = Pos[0]
Ypos = Pos[1]
#detect hand position
if(Xpos!=0):
Xpos = 280+Xpos
Xpos = 280*2-Xpos
#avoid making big jumps - adds smoothness
if(abs(self.OLDYpos-Ypos)>self.MaxJumpVal or abs(self.OLDXpos-Xpos)>self.MaxJumpVal):
self.OLDXpos = Xpos
self.OLDYpos = Ypos
ToYMove = self.OLDYpos-Ypos
ToXMove = self.OLDXpos-Xpos
self.OLDYpos = Ypos
self.OLDXpos = Xpos
#detect velocity and asume speed of cruzer
#High velocity(hand)
if (HighLowVelocity ==1):
XVel = abs(Velocity[0]/30)
YVel = abs(Velocity[1]/30)
#Accurate movement-low velocity(finger)
if(HighLowVelocity ==0):
XVel = abs(Velocity[0]/100)
YVel = abs(Velocity[1]/100)
device.emit(uinput.REL_X, int(ToXMove*XVel))
device.emit(uinput.REL_Y, int(ToYMove*YVel))
def ContiniousMouse (self, HandPos, FingerPos):
ToXMove = (HandPos[0]-FingerPos[0]-20)/self.ContiniousMouseSpeed
ToYMove = (HandPos[1]-FingerPos[1])/self.ContiniousMouseSpeed
#reverse x direction with (-1)
device.emit(uinput.REL_X, int((-1)*ToXMove))
device.emit(uinput.REL_Y, int(ToYMove))
def WaitingBetweenCommands(self, FingersExtended):
ToReturn = 1
if(FingersExtended == self.OLDFingersExtended):
self.WaitBetweenCommandsCount +=1
if(self.WaitBetweenCommandsCount>=self.BetweenCommandsMaxCount):
ToReturn = 0
if(FingersExtended != self.OLDFingersExtended):
self.WaitBetweenCommandsCount = 0
self.OLDFingersExtended = FingersExtended
return ToReturn
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)
def main1():
for i in range(100):
time.sleep(0.1)
device.emit(uinput.REL_X, i)
device.emit(uinput.REL_Y, 5)
if __name__ == "__main__":
main()
print "Clean exit"