View Javadoc

1   /*
2    * Copyright 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.extensions.balanceboard;
17  
18  import javax.swing.event.EventListenerList;
19  
20  import motej.AbstractExtension;
21  import motej.Mote;
22  import motej.event.DataEvent;
23  import motej.event.DataListener;
24  import motejx.extensions.balanceboard.BalanceBoardCalibrationData.Sensor;
25  import motejx.extensions.balanceboard.BalanceBoardCalibrationData.Weight;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * 
32   * <p>
33   * 
34   * @author Kohei Matsumura
35   * @author <a href="mailto:vfritzsch@users.sourceforge.net">Volker Fritzsch</a>
36   */
37  public class BalanceBoard extends AbstractExtension implements DataListener {
38  
39  	private Mote mote;
40  	
41  	private BalanceBoardCalibrationData calibrationData = new BalanceBoardCalibrationData();
42  	
43  	private Logger log = LoggerFactory.getLogger(BalanceBoard.class);
44  	
45  	private EventListenerList listenerList = new EventListenerList();
46  	
47  	/*
48  	 * (non-Javadoc)
49  	 * 
50  	 * @see motej.event.DataListener#dataRead(motej.event.DataEvent)
51  	 */
52  	public void dataRead(DataEvent evt) {
53  		
54  		if (evt.getError() == 0
55  				&& evt.getAddress()[0] == 0x00
56  				&& (evt.getAddress()[1] & 0xff) == 0x24
57  				&& evt.getPayload().length == 16 ) {
58  
59  			log.debug("First Part of Balance Board Calibration Data received.");
60  
61  			byte[] payload = evt.getPayload();
62  
63  			calibrationData.setCalibration(Sensor.A, Weight.KG_0, ((payload[0] & 0xff) << 8) ^ (payload[1] & 0xff));
64  			calibrationData.setCalibration(Sensor.B, Weight.KG_0, ((payload[2] & 0xff) << 8) ^ (payload[3] & 0xff));
65  			calibrationData.setCalibration(Sensor.C, Weight.KG_0, ((payload[4] & 0xff) << 8) ^ (payload[5] & 0xff));
66  			calibrationData.setCalibration(Sensor.D, Weight.KG_0, ((payload[6] & 0xff) << 8) ^ (payload[7] & 0xff));
67  			
68  			calibrationData.setCalibration(Sensor.A, Weight.KG_17, ((payload[8] & 0xff) << 8) ^ (payload[9] & 0xff));
69  			calibrationData.setCalibration(Sensor.B, Weight.KG_17, ((payload[10] & 0xff) << 8) ^ (payload[11] & 0xff));
70  			calibrationData.setCalibration(Sensor.C, Weight.KG_17, ((payload[12] & 0xff) << 8) ^ (payload[13] & 0xff));
71  			calibrationData.setCalibration(Sensor.D, Weight.KG_17, ((payload[14] & 0xff) << 8) ^ (payload[15] & 0xff));
72  		}
73  		
74  		if (evt.getError() == 0
75  				&& evt.getAddress()[0] == 0x00
76  				&& (evt.getAddress()[1] &0xff) == 0x34
77  				&& evt.getPayload().length == 8) {
78  			
79  			log.debug("Second Part of Balance Board Calibration Data received.");
80  
81  			byte[] payload = evt.getPayload();
82  			
83  			calibrationData.setCalibration(Sensor.A, Weight.KG_34, ((payload[0] & 0xff) << 8) ^ (payload[1] & 0xff));
84  			calibrationData.setCalibration(Sensor.B, Weight.KG_34, ((payload[2] & 0xff) << 8) ^ (payload[3] & 0xff));
85  			calibrationData.setCalibration(Sensor.C, Weight.KG_34, ((payload[4] & 0xff) << 8) ^ (payload[5] & 0xff));
86  			calibrationData.setCalibration(Sensor.D, Weight.KG_34, ((payload[6] & 0xff) << 8) ^ (payload[7] & 0xff));
87  		}
88  	}
89  
90  	/*
91  	 * (non-Javadoc)
92  	 * 
93  	 * @see motej.Extension#initialize()
94  	 */
95  	public void initialize() {
96  		mote.addDataListener(this);
97  		
98  		// initialize
99  		mote.writeRegisters(new byte[] { (byte) 0xa4, 0x00, 0x40}, new byte[] { 0x00 });
100 		
101 		// request calibration data
102 		mote.readRegisters(new byte[] { (byte) 0xa4, 0x00, 0x24 }, new byte[] { 0x00, 0x18 }); 
103 	}
104 
105 	public BalanceBoardCalibrationData getCalibrationData() {
106 		return calibrationData;
107 	}
108 
109 	/*
110 	 * (non-Javadoc)
111 	 * 
112 	 * @see motej.Extension#parseExtensionData(byte[])
113 	 */
114 	public void parseExtensionData(byte[] extensionData) {
115 		fireBalanceBoardEvent(extensionData);
116 	}
117 
118 	protected void fireBalanceBoardEvent(byte[] extensionData) {
119 		BalanceBoardListener[] listeners = listenerList.getListeners(BalanceBoardListener.class);
120 		if (listeners.length == 0) {
121 			return;
122 		}
123 		
124 //		if (log.isDebugEnabled()) {
125 //			StringBuffer sb = new StringBuffer();
126 //			log.debug("received:");
127 //			for (int i = 0; i < extensionData.length; i++) {
128 //				String hex = Integer.toHexString(extensionData[i] & 0xff);
129 //				sb.append(hex.length() == 1 ? "0x0" : "0x").append(hex).append(" ");
130 //				if ((i + 1) % 8 == 0) {
131 //					log.debug(sb.toString());
132 //					sb.delete(0, sb.length());
133 //				}
134 //			}
135 //			if (sb.length() > 0) {
136 //				log.debug(sb.toString());
137 //			}
138 //		}
139 		
140 		int weightA = ((extensionData[0] & 0xff) << 8) ^ (extensionData[1] & 0xff);
141 		int weightB = ((extensionData[2] & 0xff) << 8) ^ (extensionData[3] & 0xff);
142 		int weightC = ((extensionData[4] & 0xff) << 8) ^ (extensionData[5] & 0xff);
143 		int weightD = ((extensionData[6] & 0xff) << 8) ^ (extensionData[7] & 0xff);
144 //		log.debug("Weights: " + weightA + " - " + weightB + " - " + weightC + " - " + weightD);
145 		
146 		BalanceBoardEvent evt = new BalanceBoardEvent(this, weightA, weightB, weightC, weightD);
147 		for (BalanceBoardListener l : listeners) {
148 			l.balanceBoardChanged(evt);
149 		}
150 	}
151 
152 	public void addBalanceBoardListener(BalanceBoardListener listener) {
153 		listenerList.add(BalanceBoardListener.class, listener);
154 	}
155 	
156 	public void removeBalanceBoardListener(BalanceBoardListener listener) {
157 		listenerList.remove(BalanceBoardListener.class, listener);
158 	}
159 	
160 	/*
161 	 * (non-Javadoc)
162 	 * 
163 	 * @see motej.Extension#setMote(motej.Mote)
164 	 */
165 	public void setMote(Mote mote) {
166 		this.mote = mote;
167 	}
168 
169 }