View Javadoc

1   /*
2    * Copyright 2007-2008 motej
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License. 
15   */
16  package motejx.adapters;
17  
18  import motej.IrPoint;
19  import motej.event.IrCameraEvent;
20  import motej.event.IrCameraListener;
21  
22  /**
23   * StatefullIrCameraAdapter implements IrCameraListener to give additional
24   * information about infrared points in the WiiMotes camera sight.
25   *
26   * @author djuxen
27   */
28  public abstract class StatefulIrCameraAdapter implements IrCameraListener {
29  
30  	private IrCameraEvent lastEvent;
31  
32  	public void irImageChanged(IrCameraEvent evt) {
33  		if (lastEvent != null) {
34  			for (int i = 0; i < 4; i++) {
35  				if (!evt.getIrPoint(i).equals(lastEvent.getIrPoint(i))) {
36  					IrPoint currentPoint = evt.getIrPoint(i);
37  					IrPoint lastPoint = lastEvent.getIrPoint(i);
38  
39  					if (isValid(currentPoint) && !isValid(lastPoint)) {
40  						pointAppeared(i, currentPoint);
41  					} else if (!isValid(currentPoint) && isValid(lastPoint)) {
42  						pointDisappeared(i, lastPoint);
43  					} else {
44  						pointChanged(i, currentPoint);
45  					}
46  				}
47  			}
48  		}
49  
50  		lastEvent = evt;
51  	}
52  
53  	/**
54  	 * Implement this method to get notified when an infrared point
55  	 * appears in the camera's view.
56  	 * @param slot The slot on which the point appeared
57  	 * @param p The point which appeared
58  	 */
59  	public abstract void pointAppeared(int slot, IrPoint p);
60  
61  	/**
62  	 * Implement this method to get notified when an infrared point
63  	 * moves in the camera's view.
64  	 * @param slot The slot on which the point moved
65  	 * @param p The point which changed
66  	 */
67  	public abstract void pointChanged(int slot, IrPoint p);
68  
69  	/**
70  	 * Implement this method to get notified when an infrared point
71  	 * disappears from the camera's view.
72  	 * @param slot The slot on which the point disappeared
73  	 * @param p The point which disappeared
74  	 */
75  	public abstract void pointDisappeared(int slot, IrPoint p);
76  
77  	private boolean isValid(IrPoint irPoint) {
78  		return irPoint.x != 1023 || irPoint.y != 1023;
79  	}
80  }