#include<stdlib.h>
#include "iso813.h"

float ISO813_AD_Float( WORD wBase, WORD wChannel, WORD wGainCode, WORD wBipolar,WORD wJump10v  );
WORD  ISO813_ADs_Float(WORD  wBase,     WORD  wChannel, WORD wGainCode,
	      WORD wBipolar, WORD wJump10v,float *fBuffer, DWORD dwDataNo, WORD *wTmpBuf  );
float ISO813_AD2F(WORD wHex, WORD wGainCode, WORD wBipolar,  WORD wJump10v);


/*------------------------------------------*/
/* Bipolar,  1:Bipolar   0:Unipolar         */
/* Jump10v,  1:10v       0:20v              */
/*------------------------------------------*/
float ISO813_AD_Float( WORD wBase, WORD wChannel, WORD wGainCode, WORD wBipolar,WORD wJump10v  )
{
	WORD wVal ;

	wVal = ISO813_AD_Hex(wBase, wChannel , wGainCode);
	if ( wVal != ISO813_TimeOutError )
		return( ISO813_AD2F(wVal, wBipolar, wGainCode, wJump10v) ); /* return fVal or AdError2 */
	else
		return ( ISO813_AdError2 );

}


/*--------------------------------------------------------*/
/* Bipolar,  1:Bipolar   0:Unipolar                       */
/* Jump10v,  1:10v       0:20v                            */
/* 'fBuffer' point to a float array with 'dwDataNo' size. */
/*--------------------------------------------------------*/
WORD ISO813_ADs_Float(WORD  wBase,     WORD  wChannel, WORD wGainCode,
	      WORD wBipolar, WORD wJump10v,float *fBuffer, DWORD dwDataNo, WORD *wTmpBuf  )
{
    DWORD i;
    float ZeroBase, FullRange, VoltageRange;

    if ( wGainCode > 4 ) return ISO813_AdError;
    if ( wGainCode == 0 && ( wBipolar==0 ) && (wJump10v==0) ) return ISO813_AdError;

    if (wBipolar==1) {
	ZeroBase  = 2048.0;
	FullRange = 2048.0;
	if (wJump10v==1)
		VoltageRange = 5.0;
	else
		VoltageRange = 10.0;
	}
    else {
	ZeroBase  = 0.0;
	FullRange = 4095.0;

	if (wJump10v==1)
		VoltageRange = 10.0;
	else
		VoltageRange = 20.0;
    }

    if ( ISO813_ADs_Hex(wBase, wChannel, wGainCode, wTmpBuf , dwDataNo) == ISO813_NoError )
    {
	for (i=0;i<dwDataNo;i++)
	    fBuffer[i] =  (((wTmpBuf[i]-ZeroBase)/FullRange)*VoltageRange)/(float)(1<<wGainCode) ;
    }
    else
	return ISO813_AdError;
    return ISO813_NoError;
}


/*-----------------------------------------------------*/
/* Return voltage value or -100.0 if any error occurs  */
/* or parameter is out of range.                       */
/* Bipolar,  1:Bipolar   0:Unipolar                    */
/* Jump10v,  1:10v       0:20v                         */
/*-----------------------------------------------------*/
float ISO813_AD2F(WORD wHex, WORD wGainCode, WORD wBipolar,  WORD wJump10v)
{
    float ZeroBase, FullRange, VoltageRange;

    if ( wGainCode > 4 ) return(ISO813_AdError2);
    if ( wGainCode == 0 && ( wBipolar==0 ) && (wJump10v==0) ) return(ISO813_AdError2);

    if (wBipolar==1) {
	ZeroBase  = 2048.0;
	FullRange = 2048.0;
	if (wJump10v==1)
		VoltageRange = 5.0;
	else
		VoltageRange = 10.0;
	}
    else {
	ZeroBase  = 0.0;
	FullRange = 4095.0;

	if (wJump10v==1)
		VoltageRange = 10.0;
	else
		VoltageRange = 20.0;
    }

    return( (((wHex-ZeroBase)/FullRange)*VoltageRange)/(float)(1<<wGainCode) );
}
