WiimoteDataHandler.java~
9.82 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package org.mote.wiimote.whiteboard;
import java.awt.Point;
import java.util.Collection;
import java.util.Collections;
import java.util.EventObject;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.jdesktop.application.Application;
import org.jdesktop.application.Application.ExitListener;
import org.mote.wiimote.whiteboard.calibration.WiimoteCalibration;
import org.mote.wiimote.whiteboard.ds.IRDot;
import org.mote.wiimote.whiteboard.ds.Wiimote;
import org.mote.wiimote.whiteboard.mouse.CursorControlStrategy;
import org.mote.wiimote.whiteboard.mouse.Mouse;
import org.mote.wiimote.whiteboard.mouse.smoothing.MouseSmoothingStrategy;
import org.mote.wiimote.whiteboard.preferences.WWPreferences;
import org.mote.wiimote.whiteboard.preferences.WWPreferences.PreferencesListener;
import wiiremotej.WiiRemote;
import wiiremotej.WiiRemoteJ;
import wiiremotej.event.WRButtonEvent;
import wiiremotej.event.WRIREvent;
import wiiremotej.event.WRStatusEvent;
import wiiremotej.event.WiiDeviceDiscoveredEvent;
import wiiremotej.event.WiiDeviceDiscoveryListener;
import wiiremotej.event.WiiRemoteAdapter;
public class WiimoteDataHandler extends WiiRemoteAdapter implements ExitListener, WiiDeviceDiscoveryListener, PreferencesListener {
public static interface WiimoteDataListener {
public void wiimoteConnected(Wiimote wiimote);
public void wiimoteDisconnected(Wiimote wiimote);
public void irLights(Wiimote wiimote, IRDot[] lights);
public void irWarped(Map<Wiimote, IRDot[]> data, Point[] warped);
public void batteryLevel(Wiimote wiimote, double level);
}
private Map<WiiRemote, Wiimote> remotes = new LinkedHashMap<WiiRemote, Wiimote>(WWPreferences.WIIMOTES, 1f);
private Map<WiiRemote, WRIREvent> events = new LinkedHashMap<WiiRemote, WRIREvent>(WWPreferences.WIIMOTES, 1f);
private final WiimoteCalibration calibration;
private static final WWPreferences prefs = WWPreferences.getPreferences();
private final Set<WiimoteDataListener> listener = Collections.synchronizedSet(new HashSet<WiimoteDataListener>());
private boolean cursorControl = true;
private MouseSmoothingStrategy mss[] = new MouseSmoothingStrategy[4];
private CursorControlStrategy cursorControlStrategy;
public WiimoteDataHandler(WiimoteCalibration calibration) {
this.calibration = calibration;
Application.getInstance().addExitListener(this);
prefs.addPreferencesListener(this);
preferencesChanged();
new WiimoteConnector(this).connect();
}
public void enableIR(Wiimote wiimote) throws Exception {
if (wiimote != null && wiimote.getWiiRemote().isConnected()) {
wiimote.getWiiRemote().setIRSensorEnabled(true, WRIREvent.BASIC, WWPreferences.SENSITIVITY_SETTINGS);
}
WiimoteWhiteboard.getLogger().info(String.format("(Re-)Setting IR sensor of Wiimote %d: %s", wiimote.getId(), (wiimote != null && wiimote.getWiiRemote().isConnected() ? "done" : "not connected")));
}
void addRemote(final WiiRemote remote) {
try {
int id = remotes.size()+1;
final Wiimote wiimote = new Wiimote(remote, remote.getBluetoothAddress(), id);
remotes.put(remote, wiimote);
remote.setAccelerometerEnabled(false);
enableIR(wiimote);
remote.setLEDIlluminated(id-1, true);
remote.setUseMouse(false);
synchronized (listener) {
for (WiimoteDataListener l : listener)
l.wiimoteConnected(wiimote);
}
} catch (Exception e) {
e.printStackTrace();
WiimoteWhiteboard.getLogger().log(Level.SEVERE, "Error on configuring Wii Remote", e);
}
remote.addWiiRemoteListener(this);
// update battery level every minute
new Thread(new Runnable() {
public void run() {
while (true) {
try {
// triggers #statusReported(WRStatusEvent)
if (remote.isConnected())
remote.requestStatus();
} catch (Exception e) {
e.printStackTrace();
WiimoteWhiteboard.getLogger().log(Level.WARNING, "Error on requesting status from Wii Remote", e);
}
try {
Thread.sleep(60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
/*
* EXIT
*/
public void willExit(EventObject event) {
for (WiiRemote remote : remotes.keySet())
remote.disconnect();
}
public boolean canExit(EventObject event) {
return true;
}
/*
* WIIREMOTEJ LISTENER
*/
@Override
public synchronized void IRInputReceived(WRIREvent e) {
events.put(e.getSource(), e);
// wait till data from all connected wiimotes was received once and only process input on data from first wiimote to reduce the number of times the function gets called
if (events.size() == getNumberOfConnectedWiimotes() && remotes.get(e.getSource()).getId() == 1)
IRInputReceived();
}
private void IRInputReceived() {
boolean firstDotVisible = false;
Map<Wiimote, IRDot[]> data = new LinkedHashMap<Wiimote, IRDot[]>();
for (WiiRemote r : events.keySet()) {
Wiimote wiimote = remotes.get(r);
IRDot[] dots = IRDot.getIRDots(events.get(r).getIRLights());
synchronized (listener) {
for (WiimoteDataListener l : listener)
l.irLights(wiimote, dots);
}
// exclude points from uncalibrated wiimotes during "normal operation"
if (!calibration.isDone() || calibration.isCalibrated(wiimote)) {
firstDotVisible = firstDotVisible || dots[0] != null;
data.put(wiimote, dots);
}
}
if (calibration.isDone()) {
// should always be true, but just in case...
if (calibration.isAnyCalibrated(data.keySet())) {
Point warped[] = calibration.warp(data);
for (int i = 0; i < 4; i++) {
if (warped[i] != null) {
warped[i] = mss[i].translate(warped[i]);
} else {
mss[i].reset();
}
}
if (isCursorControl()) {
cursorControlStrategy.process(warped[0]);
} else {
// if (Mouse.LEFT_BUTTON.isPressed())
Mouse.LEFT_BUTTON.setPressed(false);
// if (Mouse.RIGHT_BUTTON.isPressed())
Mouse.RIGHT_BUTTON.setPressed(false);
}
// if (warped[0] != null) {
// // normal operation after calibration has been done
// if (isCursorControl()) {
// Mouse.move(warped[0]);
// rcs.process(warped[0]);
// if (prefs.isLeftClick() && !(prefs.isRightClick() && rcs.trigger())) {
// Mouse.LEFT_BUTTON.setPressed(true);
// }
// }
// } else {
// rcs.process(null);
// Mouse.LEFT_BUTTON.setPressed(false);
// }
synchronized (listener) {
for (WiimoteDataListener l : listener)
l.irWarped(data, warped);
}
}
} else if (calibration.inProgress()) {
if (firstDotVisible)
calibration.process(data);
}
}
@Override
public void statusReported(WRStatusEvent e) {
synchronized (listener) {
for (WiimoteDataListener l : listener)
l.batteryLevel(remotes.get(e.getSource()), e.getBatteryLevel());
}
}
@Override
public void buttonInputReceived(WRButtonEvent e) {
if (e.isOnlyPressed(WRButtonEvent.A)) {
calibration.start(getConnectedWiimotes());
} else if (e.isOnlyPressed(WRButtonEvent.HOME)) {
Application.getInstance().exit();
}
}
public void wiiDeviceDiscovered(WiiDeviceDiscoveredEvent e) {
if (e.getWiiDevice() instanceof WiiRemote) {
addRemote((WiiRemote)e.getWiiDevice());
}
}
public void findFinished(int numberFound) {
}
@Override
public void disconnected() {
// TODO support dis-/reconnecting?
WiiRemoteJ.stopFind();
WiiRemote remove = null;
for (WiiRemote remote : remotes.keySet()) {
if (!remote.isConnected()) {
remove = remote;
synchronized (listener) {
for (WiimoteDataListener l : listener)
l.wiimoteDisconnected(remotes.get(remote));
}
break;
}
}
if (remove != null) remotes.remove(remove);
}
/*
* LISTENER
*/
public void addWiimoteDataListener(WiimoteDataListener l) {
listener.add(l);
}
public void removeWiimoteDataListener(WiimoteDataListener l) {
listener.remove(l);
}
/*
* PREFERENCES STUFF
*/
public void preferencesChanged() {
updateMSS();
updateCursorControlStrategy();
// final int newNumWiimotes = prefs.getNumberOfWiimotes();
// if (numWiimotes != newNumWiimotes) {
// WiiRemoteJ.stopFind();
// if (newNumWiimotes > getNumberOfConnectedWiimotes()) {
// WiiRemoteJ.findRemotes(this, newNumWiimotes - getNumberOfConnectedWiimotes());
// }
// numWiimotes = newNumWiimotes;
// }
}
@SuppressWarnings("unchecked")
private void updateCursorControlStrategy() {
if (cursorControlStrategy == null || !cursorControlStrategy.getClass().getName().equals(prefs.getCursorControl())) {
try {
Class<?> c = Class.forName(prefs.getCursorControl());
cursorControlStrategy = ((Class<? extends CursorControlStrategy>)c).newInstance();
} catch (Exception e) {
e.printStackTrace();
WiimoteWhiteboard.getLogger().log(Level.SEVERE, "Cursor Control Method error.", e);
}
}
}
@SuppressWarnings("unchecked")
private void updateMSS() {
if (mss[0] == null || !mss[0].getClass().getName().equals(prefs.getMouseSmoothing())) {
try {
Class<?> c = Class.forName(prefs.getMouseSmoothing());
for (int i = 0; i < 4; i++)
mss[i] = ((Class<? extends MouseSmoothingStrategy>)c).newInstance();
} catch (Exception e) {
e.printStackTrace();
WiimoteWhiteboard.getLogger().log(Level.SEVERE, "Mouse Movement Smoothing error.", e);
}
}
}
/*
* GETTER & SETTER
*/
public boolean isConnected() {
for (WiiRemote remote : remotes.keySet()) {
if (remote.isConnected()) return true;
}
return false;
}
public boolean isConnected(Wiimote wiimote) {
return wiimote != null && wiimote.getWiiRemote().isConnected();
}
public int getNumberOfConnectedWiimotes() {
return remotes.size();
}
public Collection<Wiimote> getConnectedWiimotes() {
return remotes.values();
}
public boolean isCursorControl() {
return cursorControl;
}
public void setCursorControl(boolean cursorControl) {
this.cursorControl = cursorControl;
}
}