/* Demo for I-9014 modules.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/* File level history (record changes for this file here.)

   v 0.0.0  1 Sep 2011 
     create, blah blah... */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include "msw.h"
#include "i8014W.h"

int main(int argc, char **argv)
{
	int RetValue,slot, iGain,i=0, maxCh, firmware,isofirm,jumper,ch=0,ret;
	unsigned short gainValue;
	float fVal;
	short hVal;
	char libDate[32];
	char gainStr[5][32]={"+/-10V","+/-5V","+/-2.5V","+/-1.25V","+/-20mA"};
	short offsetValue;
	struct timeval start,end;
	
	printf("\nWhich slot does I-9014 plut in?...  ");
	scanf("%d", &slot);
	printf("\n");
	if ((slot<0)&&(slot>8)){
		return FAILURE;
  }
  
	RetValue = Open_Slot(slot);
	if (RetValue >0) {
		printf("open Slot %d failed !\n",slot);
		return FAILURE;
	}
	
	printf("There are four types of gain value for I-9014: \n");
	printf("0)+/-10V   1)+/-5V   2)+/-2.5V   3)+/-1.25V   4)+/-20mA\n");
	printf("What gain do I-9014 select?...  ");
	scanf("%d", &iGain);
	
	if (i9014_Init(slot) != -1){
		printf("\nThis demo show how to use i9014_ReadAI to read hex and float format analog input data.");
		firmware = i9014_GetFirmwareVer_L1(slot);
		printf("\nFPGA Version =: %04X",firmware);
		printf("\nLibrary Version =: %04X",i9014_GetLibVersion());
		isofirm = i9014_GetFirmwareVer_L2(slot);
		printf("\nISO Version =: %04X",isofirm);

		jumper = i9014_GetSingleEndJumper(slot);
		if(jumper)
		{
			maxCh=16;
			printf("\nI-9014 Input Mode= Single-End\n\r");
		}
		else
		{
			maxCh=8;
			printf("\nI-9014 Input Mode= Differential\n\r");
		}   

		i9014_ReadGainOffset(slot,iGain,&gainValue, &offsetValue); 
		printf("Select Gain[%d]=%s ,the Calibrated Gain= %u, Calibrated Offset= %d\n\n",iGain,gainStr[iGain],gainValue,offsetValue);
	  
		gettimeofday(&start,NULL);
    
		for(i=0;i<maxCh;i++){
			ret=i9014_ReadAIHex(slot, i, iGain, &hVal);
			i9014_ReadAI(slot, i, iGain, &fVal);
			//printf("AIHex Value = %04x from Channel %d of slot %d, ret=%d\n", hVal, i, slot, ret);
			//printf("AI Value = %f from Channel %d of slot %d\n", fVal, i, slot);
			printf("slot %d, ch[%d]= [%f], [%0x4]\n", slot, i, fVal, hVal);
		}
    
		gettimeofday(&end,NULL); 
		printf("\r\nStart Time = %ld us \n\r",start.tv_usec); 
		printf("\r\nEnd Time = %ld us \n\r",end.tv_usec);
		printf("\r\ntotal E-S Time = %ldus = %ldms\n\r",end.tv_usec-start.tv_usec,(end.tv_usec-start.tv_usec)/1000);
	}
	else
		printf("I-9014 not found on Slot %d  !\n",slot);

	Close_Slot(slot);
	return SUCCESS;
}
