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.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  //@import org.wiigee.device.Wiimote;
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  	// The panel to draw planes and graphs on
82  	private WiiReaderPanel currentPanel;
83  	// Instance of WiiGee for WiiMote device
84  	private WiimoteWiigee wiigee;
85  	// The WiiMote this listener is listening to
86  	private Wiimote wiimote;
87  	// The RecordedGesture3D that will contain the position data
88  	private RecordedGesture3D recordedGesture;
89  	// The GestureSample3D that will contain recordedGesture
90  	private GestureSample3D gesture;
91  	// List of recorded accelerations from the wiimote
92  	private Accelerations accelerations;
93  	// Indicator if accelerations recording is in progress
94  	private boolean recording;
95  	// Button that is used to start and stop recording
96  	private int recordButton = org.wiigee.device.Wiimote.BUTTON_B;
97  
98  	private Recogniser recogniser;
99  
100 	/**
101 	 * Constructor
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 	 * Returns the panel with standard dimension
113 	 * 
114 	 * @return
115 	 */
116 	public WiiReaderPanel getPanel() {
117 		return getPanel(new Dimension(200, 200));
118 	}
119 
120 	/**
121 	 * Returns the panel
122 	 * 
123 	 * @param dimension
124 	 *            The dimension of the panel
125 	 * @return The panel belonging to this WiiReader
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 	 * Clears the WiiReader
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 		// TODO Auto-generated method stub
165 		return null;
166 	}
167 
168 	/**
169 	 * Returns a copy of the recorded gesture in a GestureSample3D
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 	 * Initializes the WiiReader, starts device detection etc.
182 	 */
183 	@Override
184 	public void init() {
185 		System.out.println("Initializing WiiReader...");
186 		try {
187 			
188 			
189 			// Create WiiGee instance
190 			wiigee = new WiimoteWiigee();
191 			// Retrieve array of WiiMotes in range
192 			Wiimote[] wiimotes = wiigee.getDevices();
193 			// If a wiimote was found in range
194 			if (wiimotes[0] != null)
195 				// Take the first wiimote in the list
196 				this.wiimote = wiimotes[0];
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 			//MODIFY >
208 			setIsConnected(true);
209 			//MODIFY <
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);//MODIFY
213 		}
214 	}
215 
216 	/**
217 	 * Disconnects the WiiMote
218 	 */
219 	public void disconnect() {
220 		if (this.wiimote != null)
221 		{
222 			this.wiimote.disconnect();
223 		}
224 	}
225 
226 	/**
227 	 * When the recording button is pressed this method starts recording
228 	 */
229 	@Override
230 	public void buttonPressReceived(ButtonPressedEvent event) {
231 		// If recording button is pressed
232 		if (event.getButton() == recordButton) {
233 			// If not already recording
234 			if (!recording) {
235 				accelerations.clear(); // Clear accelerations list
236 				recording = true; // Start recording
237 			}
238 		}		
239 	}
240 
241 	/**
242 	 * When the button is released the recoding stops and the recorded gesture
243 	 * is displayed on the panel
244 	 */
245 	@Override
246 	public void buttonReleaseReceived(ButtonReleasedEvent event) {
247 		// If the device is recording
248 		if (recording) {
249 			// Convert accelerations list to a gesture
250 			this.gesture.setGesture(WiiMoteTools.accelerationsToTraces(this.accelerations));
251 			gesture.setName("GestureSample3D taken from WiiMote at system time: "
252 							+ System.currentTimeMillis());
253 
254 			// Paint the gesture on the panel
255 			this.currentPanel.paintComponent(currentPanel.getGraphics());
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 }