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 package org.ximtec.igesture.io.wiimote;
41
42 import java.awt.Color;
43 import java.awt.Dimension;
44 import java.io.IOException;
45 import java.util.List;
46 import java.util.logging.Level;
47 import java.util.logging.Logger;
48
49 import javax.bluetooth.RemoteDevice;
50 import javax.swing.BorderFactory;
51
52 import org.wiigee.control.WiimoteWiigee;
53 import org.wiigee.device.Wiimote;
54 import org.wiigee.event.AccelerationEvent;
55 import org.wiigee.event.AccelerationListener;
56 import org.wiigee.event.ButtonListener;
57 import org.wiigee.event.ButtonPressedEvent;
58 import org.wiigee.event.ButtonReleasedEvent;
59 import org.wiigee.event.MotionStartEvent;
60 import org.wiigee.event.MotionStopEvent;
61
62 import org.ximtec.igesture.Recogniser;
63 import org.ximtec.igesture.core.Gesture;
64 import org.ximtec.igesture.core.GestureSample3D;
65 import org.ximtec.igesture.io.AbstractGestureDevice;
66 import org.ximtec.igesture.util.Constant;
67 import org.ximtec.igesture.util.additions3d.AccelerationSample;
68 import org.ximtec.igesture.util.additions3d.Accelerations;
69 import org.ximtec.igesture.util.additions3d.Point3D;
70 import org.ximtec.igesture.util.additions3d.RecordedGesture3D;
71 import org.ximtec.igesture.util.additionswiimote.WiiMoteTools;
72
73 public class WiiReader extends
74 AbstractGestureDevice<RecordedGesture3D, Point3D> implements
75 ButtonListener, AccelerationListener {
76
77 private static final Logger LOGGER = Logger.getLogger(WiiReader.class.getName());
78
79
80
81
82 private WiimoteWiigee wiigee;
83
84 private Wiimote wiimote;
85
86 private RecordedGesture3D recordedGesture;
87
88 private GestureSample3D gesture;
89
90 private Accelerations accelerations;
91
92 private boolean recording;
93
94 private int recordButton = org.wiigee.device.Wiimote.BUTTON_B;
95
96 private Recogniser recogniser;
97 private RemoteDevice device;
98
99
100
101
102 public WiiReader(String address, String name, RemoteDevice device)
103 {
104 this.accelerations = new Accelerations();
105
106 this.recordedGesture = new RecordedGesture3D();
107 this.gesture = new GestureSample3D(this,"", recordedGesture);
108
109 this.device = device;
110
111 setDeviceID(address);
112 setName(name);
113 setDeviceType(Constant.TYPE_3D);
114 setConnectionType(Constant.CONNECTION_BLUETOOTH);
115 }
116
117
118
119
120
121
122 public WiiReaderPanel getPanel() {
123 return getPanel(new Dimension(200, 200));
124 }
125
126
127
128
129
130
131
132
133 public WiiReaderPanel getPanel(Dimension dimension) {
134
135 WiiReaderPanel panel = new WiiReaderPanel(this);
136 panel.setSize(dimension);
137 panel.setPreferredSize(dimension);
138 panel.setOpaque(true);
139 panel.setBackground(Color.WHITE);
140 panel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
141
142
143
144 return panel;
145
146 }
147
148
149
150
151 @Override
152 public void clear() {
153 recordedGesture = new RecordedGesture3D();
154 gesture = new GestureSample3D(this,"", recordedGesture);
155
156
157
158 fireGestureEvent(gesture);
159 }
160
161
162
163
164 @Override
165 public void dispose() {
166 removeAllListener();
167 clear();
168 }
169
170 @Override
171 public List<Point3D> getChunks() {
172
173 return null;
174 }
175
176
177
178
179 @Override
180 public Gesture<RecordedGesture3D> getGesture() {
181 RecordedGesture3D newGesture = new RecordedGesture3D();
182 newGesture.setAccelerations(this.gesture.getGesture()
183 .getAccelerations());
184 newGesture.setPoints(this.gesture.getGesture().getPoints());
185 return new GestureSample3D(this,this.gesture.getName(), newGesture);
186 }
187
188
189
190
191 @Override
192 public void init() {
193 System.out.println("Initializing WiiReader...");
194 try {
195 wiimote = new Wiimote(getDeviceID(),true,true);
196
197
198 wiimote.setRecognitionButton(0);
199 wiimote.setTrainButton(0);
200 wiimote.setCloseGestureButton(0);
201
202 wiimote.addButtonListener(this);
203
204 wiimote.addAccelerationListener(this);
205
206
207 setIsConnected(true);
208 } catch (IOException e) {
209 LOGGER.log(Level.SEVERE,"Could not connect to WiiMote. Please make the device discoverable and try to reconnect.",e);
210 }
211 }
212
213
214
215
216
217 public void disconnect() {
218 if (this.wiimote != null)
219 {
220 this.wiimote.disconnect();
221 setIsConnected(false);
222 }
223 }
224
225
226
227
228 @Override
229 public void buttonPressReceived(ButtonPressedEvent event) {
230
231 if (event.getButton() == recordButton) {
232
233 if (!recording) {
234 accelerations.clear();
235 recording = true;
236 }
237 }
238 }
239
240
241
242
243
244 @Override
245 public void buttonReleaseReceived(ButtonReleasedEvent event) {
246
247 if (recording) {
248
249 this.gesture.setGesture(WiiMoteTools.accelerationsToTraces(this.accelerations));
250 gesture.setName("GestureSample3D taken from WiiMote at system time: "
251 + System.currentTimeMillis());
252
253
254
255
256
257 recording = false;
258
259 try {
260 wiimote.vibrateForTime(200);
261 } catch (IOException e) {
262 e.printStackTrace();
263 }
264 fireGestureEvent(getGesture());
265 if(recogniser != null)
266 {
267 recogniser.recognise(gesture);
268 }
269 }
270
271 }
272
273
274
275
276
277 @Override
278 public void accelerationReceived(AccelerationEvent event) {
279 if (recording) {
280
281 AccelerationSample sample = new AccelerationSample(event.getX(),
282 event.getY(), event.getZ(), System.currentTimeMillis());
283
284 this.accelerations.addSample(sample);
285 }
286 }
287
288 @Override
289 public void motionStartReceived(MotionStartEvent event) {
290 }
291
292 @Override
293 public void motionStopReceived(MotionStopEvent event) {
294 }
295
296 public void setRecogniser(Recogniser recogniser)
297 {
298 this.recogniser = recogniser;
299 }
300
301 @Override
302 public void connect() {
303 init();
304 }
305
306 }