/*
 * class zum auslesen des Buffers das ADS115
 * 
 * union dual
         {
             char      charBuff[8];
             int         intBuff[4];
          }
	dual myData;
 	Variable wird im Arduino belegt über myData.intBuff  und seriell mit myData.charBuff übertragen
 * 
 * 
 * 
 * ads.setGain(GAIN_TWOTHIRDS);  +/- 6.144V  1 bit = 0.1875mV (default) 0
 * ads.setGain(GAIN_ONE);        +/- 4.096V  1 bit = 0.125mV			1
 * ads.setGain(GAIN_TWO);        +/- 2.048V  1 bit = 0.0625mV			2
 * ads.setGain(GAIN_FOUR);       +/- 1.024V  1 bit = 0.03125mV		3
 * ads.setGain(GAIN_EIGHT);      +/- 0.512V  1 bit = 0.015625mV		4
 * ads.setGain(GAIN_SIXTEEN);    +/- 0.256V  1 bit = 0.0078125mV 		5
 * 
 * 
 * 
 * 
 */
public class AdsBuf2Float {

	byte[] bb ;
	int[] ii ;
	float[] ff;
	float multi; 
	
	public AdsBuf2Float(byte[] b, int bbi, int mm) {
		bb = new byte[bbi];
		ii  = new int[bbi/2];
		ff  = new float[bbi/2];
		multi = getMulti(mm);
		
		for (int i = 0; i < bbi; i++) {
			bb[i]=b[i];
		}
	}
	
	public float getMulti(int i ){
		float f = 0;
		switch (i) {
		case 1:
			f = (float) 0.125;
			break;
		case 2:
			f = (float) 0.0625;
			break;
		case 3:
			f = (float) 0.03125;
			break;
		case 4:
			f = (float) 0.015625;
			break;
		case 5:
			f = (float) 0.0078125;
			break;			
		default:
			f = (float) 0.1875;
			break;
		}
		return f;
		
		
	}
	public int unToBy(byte a)
	{
	    int b = a & 0xFF;
	    return b;
	}
	
	public float[] convert(byte[] bb2) {
		for (int i = 0; i < bb2.length; i++) {
			bb[i]=bb2[i];
		}
		int c=0;
		for (int i = 0; i < ii.length; i++) {
			ii[i] = bb[c+1]*256 + unToBy(bb[c]);
			c+=2;
			ff[i] = ii[i]*multi;
		}
		return ff;
	}
	
	

}
