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.algorithm.rubine3d.tools;
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.commerce.base.Constants;
51 import javax.swing.BorderFactory;
52
53 import org.sigtec.util.Constant;
54 import org.wiigee.control.WiimoteWiigee;
55 import org.wiigee.device.Wiimote;
56 import org.wiigee.event.AccelerationEvent;
57 import org.wiigee.event.AccelerationListener;
58 import org.wiigee.event.ButtonListener;
59 import org.wiigee.event.ButtonPressedEvent;
60 import org.wiigee.event.ButtonReleasedEvent;
61 import org.wiigee.event.MotionStartEvent;
62 import org.wiigee.event.MotionStopEvent;
63
64
65 import org.ximtec.igesture.Recogniser;
66 import org.ximtec.igesture.core.Gesture;
67 import org.ximtec.igesture.core.GestureSample3D;
68 import org.ximtec.igesture.io.AbstractGestureDevice;
69 import org.ximtec.igesture.util.additions3d.AccelerationSample;
70 import org.ximtec.igesture.util.additions3d.Accelerations;
71 import org.ximtec.igesture.util.additions3d.Point3D;
72 import org.ximtec.igesture.util.additions3d.RecordedGesture3D;
73 import org.ximtec.igesture.util.additionswiimote.WiiMoteTools;
74
75 public class WiiReader extends
76 AbstractGestureDevice<RecordedGesture3D, Point3D> implements
77 ButtonListener, AccelerationListener {
78
79 private static final Logger LOGGER = Logger.getLogger(WiiReader.class.getName());
80
81
82 private WiiReaderPanel currentPanel;
83
84 private WiimoteWiigee wiigee;
85
86 private Wiimote wiimote;
87
88 private RecordedGesture3D recordedGesture;
89
90 private GestureSample3D gesture;
91
92 private Accelerations accelerations;
93
94 private boolean recording;
95
96 private int recordButton = org.wiigee.device.Wiimote.BUTTON_B;
97
98 private Recogniser recogniser;
99
100
101
102
103 public WiiReader() {
104
105 this.accelerations = new Accelerations();
106 this.currentPanel = new WiiReaderPanel(this);
107 this.recordedGesture = new RecordedGesture3D();
108 this.gesture = new GestureSample3D(this,"", recordedGesture);
109 }
110
111
112
113
114
115
116 public WiiReaderPanel getPanel() {
117 return getPanel(new Dimension(200, 200));
118 }
119
120
121
122
123
124
125
126
127 public WiiReaderPanel getPanel(Dimension dimension) {
128 if (currentPanel == null) {
129 WiiReaderPanel panel = new WiiReaderPanel(this);
130 panel.setSize(dimension);
131 panel.setPreferredSize(dimension);
132 panel.setOpaque(true);
133 panel.setBackground(Color.WHITE);
134 panel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
135 currentPanel = panel;
136 }
137 return currentPanel;
138
139 }
140
141
142
143
144 @Override
145 public void clear() {
146 recordedGesture = new RecordedGesture3D();
147 gesture = new GestureSample3D(this,"", recordedGesture);
148 if (currentPanel != null) {
149 currentPanel.clear();
150 }
151 }
152
153
154
155
156 @Override
157 public void dispose() {
158 removeAllListener();
159 clear();
160 }
161
162 @Override
163 public List<Point3D> getChunks() {
164
165 return null;
166 }
167
168
169
170
171 @Override
172 public Gesture<RecordedGesture3D> getGesture() {
173 RecordedGesture3D newGesture = new RecordedGesture3D();
174 newGesture.setAccelerations(this.gesture.getGesture()
175 .getAccelerations());
176 newGesture.setPoints(this.gesture.getGesture().getPoints());
177 return new GestureSample3D(this,this.gesture.getName(), newGesture);
178 }
179
180
181
182
183 @Override
184 public void init() {
185 System.out.println("Initializing WiiReader...");
186 try {
187
188
189
190 wiigee = new WiimoteWiigee();
191
192 Wiimote[] wiimotes = wiigee.getDevices();
193
194 if (wiimotes[0] != null)
195
196 this.wiimote = wiimotes[0];
197
198 wiimote.setRecognitionButton(0);
199 wiimote.setTrainButton(0);
200 wiimote.setCloseGestureButton(0);
201
202 wiimote.addButtonListener(this);
203
204 wiimote.addAccelerationListener(this);
205 System.out.println("WiiReader added as a listener for gesture and button events.");
206
207
208 setIsConnected(true);
209
210
211 } catch (IOException e) {
212 LOGGER.log(Level.SEVERE,"Could not connect to WiiMote. Please make the device discoverable and try to reconnect.",e);
213 }
214 }
215
216
217
218
219 public void disconnect() {
220 if (this.wiimote != null)
221 {
222 this.wiimote.disconnect();
223 }
224 }
225
226
227
228
229 @Override
230 public void buttonPressReceived(ButtonPressedEvent event) {
231
232 if (event.getButton() == recordButton) {
233
234 if (!recording) {
235 accelerations.clear();
236 recording = true;
237 }
238 }
239 }
240
241
242
243
244
245 @Override
246 public void buttonReleaseReceived(ButtonReleasedEvent event) {
247
248 if (recording) {
249
250 this.gesture.setGesture(WiiMoteTools.accelerationsToTraces(this.accelerations));
251 gesture.setName("GestureSample3D taken from WiiMote at system time: "
252 + System.currentTimeMillis());
253
254
255 this.currentPanel.paintComponent(currentPanel.getGraphics());
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 }