View Javadoc

1   /*
2    * @(#)$Id: WiiReader.java
3    *
4    * Author       :   Arthur Vogels, arthur.vogels@gmail.com
5    *
6    * Purpose      :	Listener for WiiMote input. Can have a WiiReaderpanel attached.
7    *
8    * There are three ways to use the WiiReader.
9    * 1) Manually
10   * After performing the gesture, you can get the gesture with getGesture() and do the recognition.
11   * 2) GestureEventListener interface
12   * Register with the WiiReader as a GestureEventListener, it will notify you when a gesture was performed. The recognition of the performed
13   * gesture can then be done.
14   * 3) Configure the WiiReader with a Recogniser
15   * When a gesture was performed, the recogniser will automatically try to recognise the gesture and notify GestureHandlers.
16   *  
17   * General steps to use:
18   * 	1. create the WiiReader
19   * 	2. connect to a wii remote
20   *  3. In case of GestureEventListener : register as a listener
21   *     In case of Recogniser : set this TuioReaders recogniser
22   *     
23   * -----------------------------------------------------------------------
24   *
25   * Revision Information:
26   *
27   * Date             Who         	Reason
28   *
29   * Dec 16, 2008     arthurvogels    Initial Release
30   *
31   * -----------------------------------------------------------------------
32   *
33   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
34   *
35   * This software is the proprietary information of ETH Zurich.
36   * Use is subject to license terms.
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  	// The panel to draw planes and graphs on
80  //	private WiiReaderPanel currentPanel;//@
81  	// Instance of WiiGee for WiiMote device
82  	private WiimoteWiigee wiigee;
83  	// The WiiMote this listener is listening to
84  	private Wiimote wiimote;
85  	// The RecordedGesture3D that will contain the position data
86  	private RecordedGesture3D recordedGesture;
87  	// The GestureSample3D that will contain recordedGesture
88  	private GestureSample3D gesture;
89  	// List of recorded accelerations from the wiimote
90  	private Accelerations accelerations;
91  	// Indicator if accelerations recording is in progress
92  	private boolean recording;
93  	// Button that is used to start and stop recording
94  	private int recordButton = org.wiigee.device.Wiimote.BUTTON_B;
95  
96  	private Recogniser recogniser;
97  	private RemoteDevice device;
98  	
99  	/**
100 	 * Constructor
101 	 */
102 	public WiiReader(String address, String name, RemoteDevice device)
103 	{
104 		this.accelerations = new Accelerations();
105 //		this.currentPanel = new WiiReaderPanel(this);//@
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 	 * Returns the panel with standard dimension
119 	 * 
120 	 * @return
121 	 */
122 	public WiiReaderPanel getPanel() {
123 		return getPanel(new Dimension(200, 200));
124 	}
125 
126 	/**
127 	 * Returns the panel
128 	 * 
129 	 * @param dimension
130 	 *            The dimension of the panel
131 	 * @return The panel belonging to this WiiReader
132 	 */
133 	public WiiReaderPanel getPanel(Dimension dimension) {
134 	//	if (currentPanel == null) {//@
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 		//	currentPanel = panel;//@
142 //		}//@
143 //		return currentPanel;//@
144 		return panel;
145 
146 	}
147 
148 	/**
149 	 * Clears the WiiReader
150 	 */
151 	@Override
152 	public void clear() {
153 		recordedGesture = new RecordedGesture3D();
154 		gesture = new GestureSample3D(this,"", recordedGesture);
155 //		if (currentPanel != null) {//@
156 //			currentPanel.clear();
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 		// TODO Auto-generated method stub
173 		return null;
174 	}
175 
176 	/**
177 	 * Returns a copy of the recorded gesture in a GestureSample3D
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 	 * Initializes the WiiReader, starts device detection etc.
190 	 */
191 	@Override
192 	public void init() {
193 		System.out.println("Initializing WiiReader...");
194 		try {
195 			wiimote = new Wiimote(getDeviceID(),true,true);
196 			
197 			//diable WiiGee internal training and recognition
198 			wiimote.setRecognitionButton(0);
199 			wiimote.setTrainButton(0);
200 			wiimote.setCloseGestureButton(0);			
201 			// Add this as a button listener
202 			wiimote.addButtonListener(this);
203 			// Add this as a gesture listener
204 			wiimote.addAccelerationListener(this);
205 //			System.out.println("WiiReader added as a listener for gesture and button events.");
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 	//MODIFY <
213 
214 	/**
215 	 * Disconnects the WiiMote
216 	 */
217 	public void disconnect() {
218 		if (this.wiimote != null)
219 		{
220 			this.wiimote.disconnect();
221 			setIsConnected(false);
222 		}
223 	}
224 
225 	/**
226 	 * When the recording button is pressed this method starts recording
227 	 */
228 	@Override
229 	public void buttonPressReceived(ButtonPressedEvent event) {
230 		// If recording button is pressed
231 		if (event.getButton() == recordButton) {
232 			// If not already recording
233 			if (!recording) {
234 				accelerations.clear(); // Clear accelerations list
235 				recording = true; // Start recording
236 			}
237 		}		
238 	}
239 
240 	/**
241 	 * When the button is released the recoding stops and the recorded gesture
242 	 * is displayed on the panel
243 	 */
244 	@Override
245 	public void buttonReleaseReceived(ButtonReleasedEvent event) {
246 		// If the device is recording
247 		if (recording) {
248 			// Convert accelerations list to a gesture
249 			this.gesture.setGesture(WiiMoteTools.accelerationsToTraces(this.accelerations));
250 			gesture.setName("GestureSample3D taken from WiiMote at system time: "
251 							+ System.currentTimeMillis());
252 
253 			// Paint the gesture on the panel
254 //			this.currentPanel.paintComponent(currentPanel.getGraphics());//@
255 					//replacement is already here see fireGestureEvent
256 			// Indicate recording stop
257 			recording = false;
258 			// Vibration to confirm gesture recording
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 	 * This method is executed when an acceleration event is received from the
275 	 * wiimote.
276 	 */
277 	@Override
278 	public void accelerationReceived(AccelerationEvent event) {
279 		if (recording) {
280 			// Create acceleration sample with time stamp
281 			AccelerationSample sample = new AccelerationSample(event.getX(),
282 					event.getY(), event.getZ(), System.currentTimeMillis());
283 			// Add sample to accelerations list
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 }