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 motejx.extensions.balanceboard.BalanceBoardCalibrationData.Sensor;
19  import motejx.extensions.balanceboard.BalanceBoardCalibrationData.Weight;
20  
21  /**
22   * 
23   * <p>
24   * @author Kohei Matsumura
25   * @author <a href="mailto:vfritzsch@users.sourceforge.net">Volker Fritzsch</a>
26   */
27  public class BalanceBoardEvent {
28      private BalanceBoard source;
29      private int topRight;
30      private int bottomRight;
31      private int topLeft;
32      private int bottomLeft;
33      
34      public BalanceBoardEvent(BalanceBoard source, int topRight, int bottomRight, int topLeft, int bottomLeft) {
35          this.source = source;
36          this.topRight = topRight;
37          this.bottomRight = bottomRight;
38          this.topLeft = topLeft;
39          this.bottomLeft = bottomLeft;
40      }
41  
42  	public BalanceBoard getSource() {
43  		return source;
44  	}
45  
46  	public int getTopRight() {
47  		return topRight;
48  	}
49  
50  	public int getBottomRight() {
51  		return bottomRight;
52  	}
53  
54  	public int getTopLeft() {
55  		return topLeft;
56  	}
57  
58  	public int getBottomLeft() {
59  		return bottomLeft;
60  	}
61  
62  	protected int interpolate(Sensor sensor, int raw) {
63  		BalanceBoardCalibrationData calibrationData = source.getCalibrationData();
64  		
65  		if (raw < calibrationData.getCalibration(sensor, Weight.KG_0)) {
66  			return 0;
67  		}
68  		
69  		if (raw < calibrationData.getCalibration(sensor, Weight.KG_17)) {
70  			return (int) (0  + (((raw - calibrationData.getCalibration(sensor, Weight.KG_0))  * (17d -  0)) / (calibrationData.getCalibration(sensor, Weight.KG_17) - calibrationData.getCalibration(sensor, Weight.KG_0))));
71  		}
72  		
73  		if (raw < calibrationData.getCalibration(sensor, Weight.KG_34)) {
74  			return (int) (17d + (((raw - calibrationData.getCalibration(sensor, Weight.KG_17)) * (34d - 17d)) / (calibrationData.getCalibration(sensor, Weight.KG_34) - calibrationData.getCalibration(sensor, Weight.KG_17))));
75  		}
76  		
77  		return (int) (((double) raw / (double) calibrationData.getCalibration(sensor, Weight.KG_34)) * 34d);
78  	}
79  	public int getTopRightInterpolated() {
80  		return interpolate(Sensor.A, topRight);
81  	}
82  
83  	public int getBottomRightInterpolated() {
84  		return interpolate(Sensor.B, bottomRight);
85  	}
86  
87  	public int getTopLeftInterpolated() {
88  		return interpolate(Sensor.C, topLeft);
89  	}
90  
91  	public int getBottomLeftInterpolated() {
92  		return interpolate(Sensor.D, bottomLeft);
93  	}
94  	
95  }