-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSample.m
163 lines (143 loc) · 6.36 KB
/
Sample.m
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
/******************************************************************************\
* Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. *
* Leap Motion proprietary and confidential. Not for distribution. *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
\******************************************************************************/
#import "Sample.h"
@implementation Sample
{
LeapController *controller;
}
- (void)run
{
controller = [[LeapController alloc] init];
[controller addListener:self];
NSLog(@"running");
[[NSRunLoop currentRunLoop] run]; // required for performSelectorOnMainThread:withObject
}
#pragma mark - SampleListener Callbacks
- (void)onInit:(NSNotification *)notification
{
NSLog(@"Initialized");
}
- (void)onConnect:(NSNotification *)notification;
{
NSLog(@"Connected");
LeapController *aController = (LeapController *)[notification object];
[aController enableGesture:LEAP_GESTURE_TYPE_CIRCLE enable:YES];
[aController enableGesture:LEAP_GESTURE_TYPE_KEY_TAP enable:YES];
[aController enableGesture:LEAP_GESTURE_TYPE_SCREEN_TAP enable:YES];
[aController enableGesture:LEAP_GESTURE_TYPE_SWIPE enable:YES];
}
- (void)onDisconnect:(NSNotification *)notification;
{
NSLog(@"Disconnected");
}
- (void)onExit:(NSNotification *)notification;
{
NSLog(@"Exited");
}
- (void)onFrame:(NSNotification *)notification;
{
LeapController *aController = (LeapController *)[notification object];
// Get the most recent frame and report some basic information
LeapFrame *frame = [aController frame:0];
// NSLog(@"Frame id: %lld, timestamp: %lld, hands: %ld, fingers: %ld, tools: %ld, gestures: %ld",
// [frame id], [frame timestamp], [[frame hands] count],
// [[frame fingers] count], [[frame tools] count], [[frame gestures:nil] count]);
if ([[frame hands] count] != 0) {
// Get the first hand
LeapHand *hand = [[frame hands] objectAtIndex:0];
// Check if the hand has any fingers
NSArray *fingers = [hand fingers];
if ([fingers count] != 0) {
// Calculate the hand's average finger tip position
LeapVector *avgPos = [[LeapVector alloc] init];
for (int i = 0; i < [fingers count]; i++) {
LeapFinger *finger = [fingers objectAtIndex:i];
avgPos = [avgPos plus:[finger tipPosition]];
}
avgPos = [avgPos divide:[fingers count]];
// NSLog(@"Hand has %ld fingers, average finger tip position %@",
// [fingers count], avgPos);
}
// Get the hand's sphere radius and palm position
// NSLog(@"Hand sphere radius: %f mm, palm position: %@",
// [hand sphereRadius], [hand palmPosition]);
// Get the hand's normal vector and direction
const LeapVector *normal = [hand palmNormal];
const LeapVector *direction = [hand direction];
// Calculate the hand's pitch, roll, and yaw angles
// NSLog(@"Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees\n",
// [direction pitch] * LEAP_RAD_TO_DEG,
// [normal roll] * LEAP_RAD_TO_DEG,
// [direction yaw] * LEAP_RAD_TO_DEG);
}
NSArray *gestures = [frame gestures:nil];
for (int g = 0; g < [gestures count]; g++) {
LeapGesture *gesture = [gestures objectAtIndex:g];
if(gesture.state != LEAP_GESTURE_STATE_STOP){
return;
}
switch (gesture.type) {
case LEAP_GESTURE_TYPE_CIRCLE: {
LeapCircleGesture *circleGesture = (LeapCircleGesture *)gesture;
// Calculate the angle swept since the last frame
float sweptAngle = 0;
if(circleGesture.state != LEAP_GESTURE_STATE_START) {
LeapCircleGesture *previousUpdate = (LeapCircleGesture *)[[aController frame:1] gesture:gesture.id];
sweptAngle = (circleGesture.progress - previousUpdate.progress) * 2 * LEAP_PI;
}
// NSLog(@"Circle id: %d, %@, progress: %f, radius %f, angle: %f degrees",
// circleGesture.id, [Sample stringForState:gesture.state],
// circleGesture.progress, circleGesture.radius, sweptAngle * LEAP_RAD_TO_DEG);
break;
}
case LEAP_GESTURE_TYPE_SWIPE: {
LeapSwipeGesture *swipeGesture = (LeapSwipeGesture *)gesture;
NSLog(@"Swipe id: %d, %@, position: %@, direction: %@, speed: %f",
swipeGesture.id, [Sample stringForState:swipeGesture.state],
swipeGesture.position, swipeGesture.direction, swipeGesture.speed);
break;
}
case LEAP_GESTURE_TYPE_KEY_TAP: {
LeapKeyTapGesture *keyTapGesture = (LeapKeyTapGesture *)gesture;
// NSLog(@"Key Tap id: %d, %@, position: %@, direction: %@",
// keyTapGesture.id, [Sample stringForState:keyTapGesture.state],
// keyTapGesture.position, keyTapGesture.direction);
break;
}
case LEAP_GESTURE_TYPE_SCREEN_TAP: {
LeapScreenTapGesture *screenTapGesture = (LeapScreenTapGesture *)gesture;
// NSLog(@"Screen Tap id: %d, %@, position: %@, direction: %@",
// screenTapGesture.id, [Sample stringForState:screenTapGesture.state],
// screenTapGesture.position, screenTapGesture.direction);
break;
}
default:
NSLog(@"Unknown gesture type");
break;
}
}
// if (([[frame hands] count] > 0) || [[frame gestures:nil] count] > 0) {
// NSLog(@" ");
// }
}
+ (NSString *)stringForState:(LeapGestureState)state
{
switch (state) {
case LEAP_GESTURE_STATE_INVALID:
return @"STATE_INVALID";
case LEAP_GESTURE_STATE_START:
return @"STATE_START";
case LEAP_GESTURE_STATE_UPDATE:
return @"STATE_UPDATED";
case LEAP_GESTURE_STATE_STOP:
return @"STATE_STOP";
default:
return @"STATE_INVALID";
}
}
@end