/*
If have any problem in using this demo program, please mail to service@icpdas.com

This demo shows some features:
	1. This demo will show the "Firmware Version" , "Lib built date" and Gain/Offset for Calibration of i-8017H/i-8017HS/i-8017HW
		If this demo get wrong AI data for 8017H,8017HS or 8017HW, please send these message to service@icpdas.com
	2. For i-8017HS or i-8017HW, it has two input signal mode, Differential(Isolated)/Single Ended(None Isolated). 
		Default is Differential(Isolated).
	3. For old 8017HL.lib it must i8017H_Set_ChannelGainMode before use i8017H_Get_AD_FValue and i8017H_Get_AD_HValue,
		Now it can be done only use i8017H_ReadAI
	4. 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	"..\..\..\..\Lib\vp2k.h"
#include	"..\..\..\..\Lib\8017H.h"

char gainStr[5][32]={"+/-10V","+/-5V","+/-2.5V","+/-1.25V","+/-20mA"};
short jumper;
short firmware;
void ShowGainInfo(int slot,int gain);
int main(void)
{
	int maxCh=8;
    float fVal=0;  
    
    char sTemp[20];
    int iGain=0;
    char libDate[32];
    
	int i,slot,findIO=0;
    unsigned long T1,T3;
    
    
    
    InitLib();    
    for(i=0;i<GetNumberOfSlot();i++)
    {    	
    	if(NameOfModule[i]==17)
    	{
    		slot=i;
    		i8017H_Init(slot);
    		findIO=1;
    		break;
    	}	    		
    }
    if(findIO==0)
    {
    	Print("There is no i-8017H found in slot\n");
    	Delay(2000);
    	return 0;
    }
    

    
    Print("\n****************************");
    Print("\nThis demo show how to use i8017H_ReadAI to read hex and float format analog input data.");
    i8017H_GetFirmwareVersion(slot,&firmware);
    Print("\nLattice Version =: %04X",firmware);
    Print("\nLibrary Version =: %04X",i8017H_GetLibVersion());
    i8017H_GetLibDate(libDate);
    Print("\nBuild Date =: %s",libDate);
    Print("\n****************************\n");
    i8017H_GetSingleEndJumper(slot,&jumper);
	if(jumper)
	{
		maxCh=16;
        Print("8017H/8017HS Input Mode=Single-End\n\r");
    }
    else
    {
    	maxCh=8;
        Print("8017H/8017HS Input Mode=Differential\n\r");
    }
        
   
    Print("Gain\n\r");
    Print("    0=%s\n\r",gainStr[0]);
    Print("    1=%s\n\r",gainStr[1]);
    Print("    2=%s\n\r",gainStr[2]);
    Print("    3=%s\n\r",gainStr[3]);
    Print("    4=%s\n\r",gainStr[4]);
    Print("  Please choose (0~4):");
    LineInput(sTemp,20);
    iGain=atoi(sTemp);    		
    ShowGainInfo(slot,iGain);    
    
    
    
  
     T1=GetTimeTicks();
     
     for (;;)
	 {
	 	T3=GetTimeTicks();
	 	

	 	if ((T3-T1) >1000) // read frequency every 300 ms
	    {
		    T1=T3;
			for(i=0;i<maxCh;i++)
			{
		    	i8017H_ReadAI(slot,i,iGain,&fVal);
		    	if(i!=maxCh-1 && i!=7)
		    	{
		    		
		    			Print("[%02d]=[%.3f]\t",i,fVal);
		    		
		    	}
		    	else
		    	{
		    		
		    			Print("[%02d]=[%.3f]\n",i,fVal);
		    		
		    	}
		    }
		    Print("\n-----------------------------------------------------------------------------\n");
	
		} 
	
		 if (Kbhit()) 
		 {
		 	i=Getch(); 
		 	if ((i=='q') || (i=='Q')) 
		 		return 0;		 	
		 }		 
	}
}


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