/* If have any problem in using this demo program, please mail to service@icpdas.com
This demo shows some features:
	1. If this demo get wrong AI data for 8017H,8017HS or 8017HW, please send these message to service@icpdas.com
	2. For I-9017, it has two input signal mode, Differential(Isolated)/Single Ended(None Isolated). 
		Default is Differential(Isolated).
	3. For i-8000 system, it will spend much time to convert hex value to float format,if performance is the issue, 
		it can i8017H_ReadAIHex to get hex data then convert collected data to float.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include "msw.h"
#include "i8017W.h"

void ShowGainInfo(int slot,int gain);
static char gainStr[5][32]={"+/-10V","+/-5V","+/-2.5V","+/-1.25V","+/-20mA"};
static int slot;

int main(void)
{
	  int i, iGain=0, maxCh=8, channel;
    float fVal=0;  
    char c, sTemp[20], libDate[32], AIStr[2][32]={"I-9017","I-9017-15"};
    short ret=0, jumper=0, firmware, moduleID;
    unsigned long T1,T3;
 
       
    Open_Slot(slot); 
    for(slot=1 ; slot<9 ; slot++)
    {
        ret=I9017_Init(slot);
        if(ret==0)
        {
    	     printf("There is an I-9017 at slot %d\n", slot);
           break;
        }
        
        if (slot==9){
           printf("There is no I-9017 at backplane\n");
    	     return 0;
        }   
    }
    
    printf("\n****************************");
    printf("\nThis demo show how to use I9017_ReadAI to read hex and float format analog input data.");
    I9017_GetFirmwareVersion(slot,&firmware);
    printf("\nLattice Version =:0x%04X",firmware);
    printf("\nLibrary Version =:0x%04X",I9017_GetLibVersion());
    printf("\n****************************\n");
    
    
    printf("Please Input 0 ~ 4 to select test AI module: \n");
	  printf("\t0.  I-9017 \n");
	  printf("\t1.  I-9017-15 \n");
	  scanf("%hd",&moduleID);
	  
    printf("\nPlease input slot index of %s module: ",AIStr[moduleID]);
	  scanf("%d", &slot);  
    printf("\nProgram will test %s at slot %d\n", AIStr[moduleID], slot);
    
    if(moduleID ==1)  //I-9017-15
	  {
	 	    printf("Input 0 to set input type as Differential (total 15 channels)\n");
		    printf("Input 1 to set input type as Single-Ended (total 30 channels)\n");
		    scanf("%hd", &jumper);
		    I9017_Select_SingleEnd(slot, jumper);
		    if(jumper ==0)
			     maxCh = 15;
		    else
			     maxCh = 30;
	  }
	  else              //I-9017
	  {
		    I9017_GetSingleEndJumper(slot, &jumper);
		    if(jumper)
		    {
			      maxCh=16;
			      printf("\t %s Input Mode=Single-End and can have maximum 16 analog input\n\r", AIStr[moduleID]);
		    }
		    else
		    {
    		    maxCh=8;
			      printf("\t %s Input Mode=Differential and can have maximum 8 analog input\n\r", AIStr[moduleID]);
		    }	
	  }
    
    printf("Input gain for %s: \n", AIStr[moduleID]);
	  for(i=0;i<5;i++)
    {
    	 printf ("\n\t Select %d: %s",i,gainStr[i]);
    }
    printf("Please choose (0~4): ");
	  scanf("%d", &iGain);
	 
    ShowGainInfo(slot,iGain);    
  
    T1=GetTimeTicks_ms();
     
    for (;;)
	  {
	 	   T3=GetTimeTicks_ms();
	     if ((T3-T1) >1000) // read frequency every 1000 ms
	     {
		        T1=T3;
		         
			      for(channel=0; channel<maxCh; channel++)
			      {
		    	       ret = I9017_ReadAI(slot,channel,iGain,&fVal);
		    	       if( ret==0)
			           {				
				            printf("CH[%d]= %7.4f",channel, fVal);
				            if(iGain < 4)
					              printf("V\n");
				            else
					              printf("mA\n");
			           }
			           else
			           {
				            printf("ERROR! reading AI error in the channel %d, and slot %d error code = %d\n",channel, slot, ret);
			           }				    	           	      
		        }
		        printf("\n-----------------------------------------------------------------------------\n");
		   } 
	
	    printf("Press 'q' or 'Q' to exit program...  ");
	    scanf("%s",&c);
	    if(c=='q' || c=='Q'){ 
         Close_Slot(slot);
		     return 0;		 	
	    }
	
	  } //for
	  
	  Close_Slot(slot);
	  return SUCCESS;
}


void ShowGainInfo(int slot,int gain)
{
	unsigned short gVal=0;
	short oVal=0;
	printf("\n======================Gain Offset Information==================");
	if(gain<0 || gain > 4)
	{
		 printf("\nGain Value must between 0 ~ 4");
	}
	else
	{
		 printf("\nSelect Gain[%d]=%s for Slot[%d] " ,gain, gainStr[gain], slot);
		 I9017_ReadGainOffset_Info(slot,gain,&gVal,&oVal);
		 printf("\nThe Gain and Offset for Calibration is Gain=%u; Offset=%d",gVal,oVal);
	}
	printf("\n===============================================================\n");
}
