#include	<stdlib.h>
#include	"..\lib\8000E.h"
#include	"..\lib\8017h.h"

float far fValue,fValuePointer[8192];
int far iValue,iValuePointer[8192];

void main(void)
{
	int Quit=0,i;
    int iSlot,iCh,iGain,iDataCount=800,iSamplingRate,iMode;
    
    char cKeyIn;
    char sTemp[20];
    
    unsigned long lStartTimeTicks,lStopTimeTicks;
    
    InitLib();
    Puts("/********************/\n\r");
    Puts("/*    8017H demo    */\n\r");
    Puts("/*                  */\n\r");
    Puts("/*  [27,Dec,2005]   */\n\r");                
    Puts("/********************/\n\r");
    
    Print ("\n\rInput slot of 8017H :");
    LineInput(sTemp,20);
    iSlot=atoi(sTemp);
    Init_8017H(iSlot);
	if(i8017H_Input_Mode_SingleEnd[iSlot])
        Print("8017H/8017HS Input Mode=Single-End\n\r");
    else
        Print("8017H/8017HS Input Mode=Differential\n\r");
        
   
    Print("Gain\n\r");
    Print("    0=+/-10V\n\r");
    Print("    1=+/-5V\n\r");
    Print("    2=+/-2.5V\n\r");
    Print("    3=+/-1.25V\n\r");
    Print("    4=+/-20mA\n\r");
    Print("  Please choose (0~4):");
    LineInput(sTemp,20);
    iGain=atoi(sTemp);
    
    while (!Quit)
    {
        Print("\n\r");
        Print("1.Get one data (Float Value).\n\r");
        Print("2.Get one data (Hex Value).\n\r");
        Print("3.Get multi data of one channel (Polling mode).\n\r");
        Print("4.Get multi data of one channel (Timer Interrupt mode).\n\r");
        Print("5.Get multi data of all channels (Timer Interrupt mode).\n\r");
        Print("Q to exit program.\n\r");
        Print(" Please Input your select ? ");
        cKeyIn=Getch();
        Print ("%c\n\r",cKeyIn);
        switch(cKeyIn)
        {
            case '1':
                Print("1.Single AD convert to Float.\n\r");
                Set_8017H_LED(iSlot,1); //Turn on the 1st LED.
                iMode=0;
                
                lStartTimeTicks=*TimeTicks;
                for(i=0;i<200;i++) //200 Loops to test the performance
                {
                    if(i8017H_Input_Mode_SingleEnd[iSlot]) //Single End Input Mode
                    {
                        for(iCh=0;iCh<16;iCh++)
                        {
                            Set_8017H_Channel_Gain_Mode(iSlot,iCh,iGain,iMode);
                            fValuePointer[iCh]=Get_AD_FValue(iGain);
                        }
                    }
                    else //Differential Input Mode
                    {
                        for(iCh=0;iCh<8;iCh++)
                        {
                            Set_8017H_Channel_Gain_Mode(iSlot,iCh,iGain,iMode);
                            fValuePointer[iCh]=Get_AD_FValue(iGain);
                        }
                    }
                }
                
                //Print the data
                if(i8017H_Input_Mode_SingleEnd[iSlot]) //Single End Input Mode
                {
                    Print("16 channels take %6.4f ms\n\r",(float)(*TimeTicks-lStartTimeTicks)/200.0);
                    for(iCh=0;iCh<16;iCh++)
                    {
                        Print("Ch%d=%+7.4f\n\r",iCh,fValuePointer[iCh]);
                    }
                }
                else //Differential Input Mode
                {
                    Print("8 channels take %6.4f ms\n\r",(float)(*TimeTicks-lStartTimeTicks)/200.0);
                    for(iCh=0;iCh<8;iCh++)
                    {
                        Print("Ch%d=%+7.3f\n\r",iCh,fValuePointer[iCh]);
                    }
                }
                
                break;
                
            case '2':
                Print("2.Single AD convert to HEX.\n\r");
                Set_8017H_LED(iSlot,2); //Turn one the 2nd LED.
                iMode=0;
                
                lStartTimeTicks=*TimeTicks;
                for(i=0;i<200;i++)  //200 Loops to test the performance
                {
                    if(i8017H_Input_Mode_SingleEnd[iSlot]) //Single End Input Mode
                    {
                        for(iCh=0;iCh<16;iCh++)
                        {
                            Set_8017H_Channel_Gain_Mode(iSlot,iCh,iGain,iMode);
                            iValuePointer[iCh]=Get_AD_HValue();
                        }
                    }
                    else //Differential Input Mode
                    {
                        for(iCh=0;iCh<8;iCh++)
                        {
                            Set_8017H_Channel_Gain_Mode(iSlot,iCh,iGain,iMode);
                            iValuePointer[iCh]=Get_AD_HValue();
                        }
                    }                        
                }
                
                //Print the data
                if(i8017H_Input_Mode_SingleEnd[iSlot]) //Single End Input Mode
                {
                    Print("8 channels takes %6.4f ms\n\r",(float)(*TimeTicks-lStartTimeTicks)/200.0);
                    for(iCh=0;iCh<16;iCh++)
                    {
                        Print("Ch%d=%04X\n\r",iCh,iValuePointer[iCh]);
                    }
                }
                else //Differential Input Mode
                {
                    Print("16 channels takes %6.4f ms\n\r",(float)(*TimeTicks-lStartTimeTicks)/200.0);
                    for(iCh=0;iCh<8;iCh++)
                    {
                        Print("Ch%d=%04X\n\r",iCh,iValuePointer[iCh]);
                    }
                }
                break;
                
            case '3':
                Print("3.AD Polling convert.\n\r");
                
                if(i8017H_Input_Mode_SingleEnd[iSlot]) 
                    Print("  Channel (0~15):"); //Single End Input Mode
                else
                    Print("  Channel (0~7):"); //Differential Input Mode
                    
                LineInput(sTemp,20);
                iCh=atoi(sTemp);
                
                Print("  Sampling point(max=8192):");
		        LineInput(sTemp,20);
		        iDataCount=atoi(sTemp);
                
                Set_8017H_LED(iSlot,4); //Turn on the 3rd LED.
                
                lStartTimeTicks=*TimeTicks;
                I8017H_AD_POLLING(iSlot,iCh,iGain,iDataCount,iValuePointer);
                lStopTimeTicks=*TimeTicks;
                Print("  Each sampling takes %6.4fms=%8.1fHz\n\r",
                     (float)(lStopTimeTicks-lStartTimeTicks)/(float)iDataCount,
                     (float)(iDataCount*1000.0)/(float)(lStopTimeTicks-lStartTimeTicks));
                Print("Press any key to print values\n\r");
                Getch();
                lStartTimeTicks=*TimeTicks;
                ARRAY_HEX_TO_FLOAT(iValuePointer,fValuePointer,iSlot,iGain,iDataCount);
                lStopTimeTicks=*TimeTicks;
                for(i=0;i<iDataCount;i++)
                {
                    Print (" %7.3f ",fValuePointer[i]);
                    if((i+1)%5==0)
                        Print("\n\r");
                }
                Print("Hex value convert to float value takes %dms\n\r",lStopTimeTicks-lStartTimeTicks);
                break;
                
            case '4':
                Print("4.AD Interrupt convert.\n\r");
                
                if(i8017H_Input_Mode_SingleEnd[iSlot]) 
                    Print("  Channel (0~15):"); //Single End Input Mode
                else
                    Print("  Channel (0~7):"); //Differential Input Mode
                LineInput(sTemp,20);
                iCh=atoi(sTemp);
                
                Print("  Sampling point(max=8192):");
		        LineInput(sTemp,20);
		        iDataCount=atoi(sTemp);
		        
                Print("  Sampling frequency(200~30,000Hz):");
		        LineInput(sTemp,20);
		        iSamplingRate=atoi(sTemp);
		        
                Set_8017H_LED(iSlot,8); //Turn on the 4th LED.
                
                lStartTimeTicks=*TimeTicks;
                I8017H_AD_INT(iSlot,iCh,iGain,iDataCount,iSamplingRate,iValuePointer);
                lStopTimeTicks=*TimeTicks;
                Print("All sampling takes %lu ms\n\r",*TimeTicks-lStartTimeTicks);
                
                Print("Press any key to print values\n\r");
                Getch();
                lStartTimeTicks=*TimeTicks;
                ARRAY_HEX_TO_FLOAT(iValuePointer,fValuePointer,iSlot,iGain,iDataCount);
                lStopTimeTicks=*TimeTicks;
                for(i=0;i<iDataCount;i++)
                {
                    Print (" %7.3f ",fValuePointer[i]);
                    if((i+1)%5==0)
                        Print("\n\r");
                }
                Print("Hex value convert to float value takes %dms\n\r",lStopTimeTicks-lStartTimeTicks);
                break;
                
            case '5':
                Print("5.AD Interrupt Channel Scan convert.\n\r");
                if(i8017H_Input_Mode_SingleEnd[iSlot]) 
                    Print("     Ch0->Ch1->....->Ch15->Ch0->Ch1....\n\r"); //Single End Input Mode
                else
                    Print("     Ch0->Ch1->....->Ch7->Ch0->Ch1....\n\r"); //Differential Input Mode
				
                Print("  Sampling point(max=8192):");
		        LineInput(sTemp,20);
		        iDataCount=atoi(sTemp);
		        
                Print("  Sampling frequency (200~20,000Hz):");
		        LineInput(sTemp,20);
		        iSamplingRate=atoi(sTemp);
		        
                Set_8017H_LED(iSlot,16);    //Turn on the 5th LED.
                
                lStartTimeTicks=*TimeTicks;
                I8017H_AD_INT_SCAN(iSlot,iGain,iDataCount,iSamplingRate,iValuePointer);
                Print("All sampling takes %lu ms\n\r",*TimeTicks-lStartTimeTicks);

                Print("Press any key to print values\n\r");
                Getch();
                lStartTimeTicks=*TimeTicks;
                ARRAY_HEX_TO_FLOAT(iValuePointer,fValuePointer,iSlot,iGain,iDataCount);
                lStopTimeTicks=*TimeTicks;
                for(i=0;i<iDataCount;i++)
                {
                    Print (" %7.3f ",fValuePointer[i]);
                    
                    if(i8017H_Input_Mode_SingleEnd[iSlot]) 
                    {
                        if((i+1)%16==0)
                            Print("\n\r");
                    }
                    else
                    {
                        if((i+1)%8==0)
                            Print("\n\r");
                    }    
                }
                Print("Hex value convert to float value takes %dms\n\r",lStopTimeTicks-lStartTimeTicks);
                break;

            case 'Q':
            case 'q':
                Quit=1;
            break;
        }
    }
}
