View Javadoc
1   package com.starphoenixmedia.candle_pos.scale;
2   
3   /**
4    *
5    * @author jlhawkwell
6    */
7   public class ScaleAction extends ScaleData
8   {
9   	private final ScaleUnits su;
10  	private final ScaleMode sm;
11  	private final double[] readingD;
12  
13  	ScaleAction(ScaleData data)
14  	{
15  
16  		su = data.getUnit();
17  		sm = data.getMode();
18  		raw = data.getRaw();
19  		reading = data.getReading();
20  
21  		readingD = ScaleData.parseValues(su, raw);
22  	}
23  
24  	public double[] convertData(ScaleUnits newUnit, byte[] data)
25  	{
26  		// FAST FAIL
27  		if ( newUnit == su ) { return readingD; }
28  
29  		// convert to grams
30  		double grams = readingD[0];
31  		if ( su == ScaleUnits.UNIT_g ) { grams = readingD[0]; }
32  		else if ( su == ScaleUnits.UNIT_kg ) { grams = readingD[0] * 1000; }
33  		else if ( (su == ScaleUnits.UNIT_lbozd) || (su == ScaleUnits.UNIT_lbozf) )
34  		{
35  			grams = readingD[0] * 16;
36  			grams += readingD[1];
37  			if ( su == ScaleUnits.UNIT_lbozf )
38  			{
39  				switch ( data[7] )
40  				{
41  					case 1: grams += 0.25d; break;
42  					case 2: grams += 0.5d; break;
43  					case 3: grams += 0.75d; break;
44  				}
45  			}
46  			grams = grams * 28.35;
47  		}
48  
49  		// convert out
50  		if ( newUnit == ScaleUnits.UNIT_g ) { return new double[]{grams}; }
51  		else if ( newUnit == ScaleUnits.UNIT_kg ) { return new double[]{grams / 1000}; }
52  		else if ( (newUnit == ScaleUnits.UNIT_lbozd) || (newUnit == ScaleUnits.UNIT_lbozf) )
53  		{
54  			double[] ret = {0,0,0};
55  			grams = grams / 28.35;
56  			ret[0] = Math.floor(grams / 16);
57  			grams -= ret[0];
58  			if ( newUnit == ScaleUnits.UNIT_lbozf )
59  			{
60  				ret[1] = Math.floor(grams);
61  				grams -= ret[1];
62  				if ( grams < 0.25) { ret[2] = 0; }
63  				else if ( grams < 0.5 ) { ret[2] = 1; }
64  				else if ( grams < 0.75 ) { ret[2] = 2; }
65  				else if ( grams < 1 ) { ret[2] = 3; }
66  				else { ret[2] = 0; }
67  				return ret;
68  			}
69  			else
70  			{
71  				ret[1] = grams;
72  				return new double[]{ret[0], ret[1]};
73  			}
74  		}
75  		return new double[]{};
76  	}
77  }