-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreeFingers.java
199 lines (167 loc) · 6.71 KB
/
threeFingers.java
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
/******************************************************************************\
* 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 java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.FingerList;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Gesture;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.Listener;
import com.leapmotion.leap.Vector;
class SampleListener4 extends Listener {
private boolean state;
private int stateCount = 0;
public void onInit(Controller controller) {
System.out.println("Initialized");
}
public void onConnect(Controller controller) {
System.out.println("Connected");
controller.enableGesture(Gesture.Type.TYPE_SWIPE);
controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
}
public void onDisconnect(Controller controller) {
//Note: not dispatched when running in a debugger.
System.out.println("Disconnected");
}
public void onExit(Controller controller) {
System.out.println("Exited");
}
public void onFrame(Controller controller) {
// Get the most recent frame and report some basic information
Frame frame = controller.frame();
Frame prevFrame = controller.frame(1);
if (!frame.hands().isEmpty()) {
// Get the first hand
Hand hand = frame.hands().get(0);
FingerList fingers = hand.fingers();
float avgXpos = 0;
for (Finger finger:fingers)
{
avgXpos += finger.stabilizedTipPosition().getX();
}
avgXpos = avgXpos/fingers.count();
System.out.println("average X position = " + avgXpos);
if (fingers.count() == 3)
{
System.out.println("3");
executePost("https://agent.electricimp.com/dPaNRLhSUoqc?finger1=open&finger2=open&finger3=open&spinner=center","");
}
else if (fingers.count() == 2)
{
if (avgXpos < -25)
{
System.out.println("right down");
executePost("https://agent.electricimp.com/dPaNRLhSUoqc?finger1=open&finger2=open&finger3=close&spinner=center","");
}
else if(avgXpos < -15)
{
System.out.println("middle down");
executePost("https://agent.electricimp.com/dPaNRLhSUoqc?finger1=open&finger2=close&finger3=open&spinner=center","");
}
else
{
System.out.println("left down");
executePost("https://agent.electricimp.com/dPaNRLhSUoqc?finger1=close&finger2=open&finger3=open&spinner=center","");
}
}
else if (fingers.count() == 1)
{
if (avgXpos < -50)
{
System.out.println("left up");
executePost("https://agent.electricimp.com/dPaNRLhSUoqc?finger1=open&finger2=close&finger3=close&spinner=center","");
}
else if(avgXpos < -20)
{
System.out.println("middle up");
executePost("https://agent.electricimp.com/dPaNRLhSUoqc?finger1=close&finger2=open&finger3=close&spinner=center","");
}
else
{
System.out.println("right up");
executePost("https://agent.electricimp.com/dPaNRLhSUoqc?finger1=close&finger2=close&finger3=open&spinner=center","");
}
}
else
{
System.out.println("all down");
executePost("https://agent.electricimp.com/dPaNRLhSUoqc?finger1=close&finger2=close&finger3=close&spinner=center","");
}
}
}
public static String executePost(String targetURL, String urlParameters)
{
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
class threeFingers {
public static void main(String[] args) {
// Create a sample listener and controller
SampleListener4 listener = new SampleListener4();
Controller controller = new Controller();
// Have the sample listener receive events from the controller
controller.addListener(listener);
// Keep this process running until Enter is pressed
System.out.println("Press Enter to quit...");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
// Remove the sample listener when done
controller.removeListener(listener);
}
}