-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sample.cs
217 lines (191 loc) · 7.17 KB
/
Sample.cs
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
/******************************************************************************\
* Copyright (C) 2012-2016 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. *
\******************************************************************************/
using System;
using System.Threading;
using Leap;
using Newtonsoft.Json.Linq;
class SampleListener
{
public static JObject _sJSON = new JObject();
static SampleListener()
{
_sJSON["thumb"] = 0;
_sJSON["index"] = 0;
_sJSON["middle"] = 0;
_sJSON["ring"] = 0;
_sJSON["pinky"] = 0;
}
public void OnInit(Controller controller)
{
Console.WriteLine("Initialized");
}
public void OnConnect(object sender, DeviceEventArgs args)
{
Console.WriteLine("Connected");
}
public void OnDisconnect(object sender, DeviceEventArgs args)
{
Console.WriteLine("Disconnected");
}
public void OnFrame(object sender, FrameEventArgs args)
{
// Get the most recent frame and report some basic information
Frame frame = args.frame;
/*
Console.WriteLine(
"Frame id: {0}, timestamp: {1}, hands: {2}",
frame.Id, frame.Timestamp, frame.Hands.Count
);
*/
if (frame.Hands.Count == 0)
{
_sJSON["thumb"] = 0;
_sJSON["index"] = 0;
_sJSON["middle"] = 0;
_sJSON["ring"] = 0;
_sJSON["pinky"] = 0;
}
foreach (Hand hand in frame.Hands)
{
/*
Console.WriteLine(" Hand id: {0}, palm position: {1}, fingers: {2}",
hand.Id, hand.PalmPosition, hand.Fingers.Count);
*/
// Get fingers
foreach (Finger finger in hand.Fingers)
{
Bone bone1 = finger.Bone(Bone.BoneType.TYPE_PROXIMAL);
Bone bone2 = finger.Bone(Bone.BoneType.TYPE_DISTAL);
if (finger.Type == Finger.FingerType.TYPE_THUMB)
{
_sJSON["thumb"] = bone1.Direction.AngleTo(bone2.Direction) * 180f / Math.PI;
}
else if (finger.Type == Finger.FingerType.TYPE_INDEX)
{
_sJSON["index"] = bone1.Direction.AngleTo(bone2.Direction) * 180f / Math.PI;
}
else if (finger.Type == Finger.FingerType.TYPE_MIDDLE)
{
_sJSON["middle"] = bone1.Direction.AngleTo(bone2.Direction) * 180f / Math.PI;
}
else if (finger.Type == Finger.FingerType.TYPE_RING)
{
_sJSON["ring"] = bone1.Direction.AngleTo(bone2.Direction) * 180f / Math.PI;
}
else if (finger.Type == Finger.FingerType.TYPE_PINKY)
{
_sJSON["pinky"] = bone1.Direction.AngleTo(bone2.Direction) * 180f / Math.PI;
}
}
/*
// Get the hand's normal vector and direction
Vector normal = hand.PalmNormal;
Vector direction = hand.Direction;
// Calculate the hand's pitch, roll, and yaw angles
Console.WriteLine(
" Hand pitch: {0} degrees, roll: {1} degrees, yaw: {2} degrees",
direction.Pitch * 180.0f / (float)Math.PI,
normal.Roll * 180.0f / (float)Math.PI,
direction.Yaw * 180.0f / (float)Math.PI
);
// Get the Arm bone
Arm arm = hand.Arm;
Console.WriteLine(
" Arm direction: {0}, wrist position: {1}, elbow position: {2}",
arm.Direction, arm.WristPosition, arm.ElbowPosition
);
// Get fingers
foreach (Finger finger in hand.Fingers)
{
Console.WriteLine(
" Finger id: {0}, {1}, length: {2}mm, width: {3}mm",
finger.Id,
finger.Type.ToString(),
finger.Length,
finger.Width
);
// Get finger bones
Bone bone;
for (int b = 0; b < 4; b++)
{
bone = finger.Bone((Bone.BoneType)b);
Console.WriteLine(
" Bone: {0}, start: {1}, end: {2}, direction: {3}",
bone.Type, bone.PrevJoint, bone.NextJoint, bone.Direction
);
}
}
*/
}
/*
if (frame.Hands.Count != 0)
{
Console.WriteLine("Detected hands: {0}", frame.Hands.Count);
}
*/
}
public void OnServiceConnect(object sender, ConnectionEventArgs args)
{
Console.WriteLine("Service Connected");
}
public void OnServiceDisconnect(object sender, ConnectionLostEventArgs args)
{
Console.WriteLine("Service Disconnected");
}
public void OnServiceChange(Controller controller)
{
Console.WriteLine("Service Changed");
}
public void OnDeviceFailure(object sender, DeviceFailureEventArgs args)
{
Console.WriteLine("Device Error");
Console.WriteLine(" PNP ID:" + args.DeviceSerialNumber);
Console.WriteLine(" Failure message:" + args.ErrorMessage);
}
public void OnLogMessage(object sender, LogEventArgs args)
{
switch (args.severity)
{
case Leap.MessageSeverity.MESSAGE_CRITICAL:
Console.WriteLine("[Critical]");
break;
case Leap.MessageSeverity.MESSAGE_WARNING:
Console.WriteLine("[Warning]");
break;
case Leap.MessageSeverity.MESSAGE_INFORMATION:
Console.WriteLine("[Info]");
break;
case Leap.MessageSeverity.MESSAGE_UNKNOWN:
Console.WriteLine("[Unknown]");
break;
}
Console.WriteLine("[{0}] {1}", args.timestamp, args.message);
}
}
class Sample
{
public static void MainOld()
{
using (Leap.IController controller = new Leap.Controller())
{
controller.SetPolicy(Leap.Controller.PolicyFlag.POLICY_ALLOW_PAUSE_RESUME);
// Set up our listener:
SampleListener listener = new SampleListener();
controller.Connect += listener.OnServiceConnect;
controller.Disconnect += listener.OnServiceDisconnect;
controller.FrameReady += listener.OnFrame;
controller.Device += listener.OnConnect;
controller.DeviceLost += listener.OnDisconnect;
controller.DeviceFailure += listener.OnDeviceFailure;
controller.LogMessage += listener.OnLogMessage;
// Keep this process running until Enter is pressed
Console.WriteLine("Press any key to quit...");
Console.ReadLine();
}
}
}