/*
G45Demo.c: Demo program for G-4500

Object: Control DI/O and AI
        
Compiler: BC++ 3.1
          Turbo C ++ 1.01(3.01) 
          
Compile mode: large

Project: G45Demo.c
         ..\..\Lib\G4500.lib         
          
Hardware: G-4500

[02/Apr/2009] Modify by Yide
[22/Feb/2008] Create by Liam
*/
/************************************************************************/
/*  G-4500:  8 12-Bit A/D channels (Analog input) 0~20mA                */
/*           3 D/I channel                                              */
/*           3 D/O channel                                              */
/*           3 RS-232 channels                                          */
/************************************************************************/

/************************************************************************/
/*  [Software specific]                                                 */
/*      Input/Output range: 0mA     10mA    10mA    20mA                */
/*      Decimal integer:    0       2047    2048    4095                */
/*      Hexadecimal:        000      7FF    800     FFF                 */
/*                                                                      */
/*      [A/D]                                                           */
/*      sampling rate:  1800 data/sec (with floating convertion)        */
/*      Accuracy==>     Typical: +- 1 LSB (+- 2.4 mV)                   */
/*                      Maximum: +- 2 LSB (+- 4.8 mV)                   */  
/*      [D/A]                                                           */
/*      thoughput :     1300 data/sec (with floating convertion)        */
/*      Accuracy==>     Typical: +- 1 LSB (+- 2.4 mV)                   */
/*                      Maximum: +- 2 LSB (+- 4.8 mV)                   */      
/*      [D/O]                                                           */
/*      throughput:  can generates 2KHz square wave signals.            */
/************************************************************************/

/************************************************************************/
/*  [Caution]                                                           */
/*      The block 7 first 16 bytes of EEPROM on X board is used to      */
/*      store A/D & D/A calibration settings. When you use the EEPROM   */
/*      on X board, don't overwrite it.                                 */
/************************************************************************/

//#define _MINIOS7_STUDIO_

#ifdef _MINIOS7_STUDIO_
#include <icpdas.h>
#else
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include "G4500.h"
#endif

#define BUFSIZE 1024

int X305IO_AnalogIn_2(int iChannel);

void main(void)
{
    int iRet, iAction, idata, quit, i, j;
    int max, min;
    float fAOValue, fMax, fMin;
    unsigned int iLib;
    int iValue, iInChannel, iDIValue, iOutChannel, iDOValue, iStatus;
    float fValue;
    char OutpBuf[BUFSIZE], InpBuf[BUFSIZE];
    unsigned long lStartTime;
    unsigned char data;
	char date[32];
    
    InitLib();
    X305IO_Init();

	GetLibDate(date);                
    iLib=GetLibVersion();
	Puts("\r\nDemo program for G-4500\r\n");
    Print("\r\nG4500.lib vesion %X.%02X, Date:%s\r\n", iLib>>8, iLib&0xff, date);

    iLib=X305IO_GetLibVersion();        
    Print("X305IO.lib vesion %X.%02X\r\n", iLib>>8, iLib&0xff);
    
    while(iAction!=0)
    {
        iAction=0;
        quit=0;
        
        Puts("\r\n");
        Puts("1) Reads setting from EEPROM\r\n");
        Puts("2) A/D (Analog input)\r\n");
        Puts("3) Read DI by X305IO_Read_All_DI()\r\n");
        Puts("4) Read DI by X305IO_Read_One_DI()\r\n");
        Puts("5) Write DO by X305IO_Write_All_DO()\r\n");
        Puts("6) Write DO by X305IO_Write_One_DO()\r\n");
        Puts("7) DO Readback by X305IO_Read_All_DO()\r\n");
        Puts("8) DO Readback by X305IO_Read_One_DO()\r\n");
        Puts("\r\n");
        Puts("0) Quits demo program\r\n\r\n");
        Puts("Choose an option and press [Enter]: ");
        Scanf("%d", &iAction);
        Puts("\r\n");
        
        switch(iAction)
        {   
            // Reads settings from EEPROM(on X board)
            case 1:
                iValue=X305IO_Init();
                if((iValue&1)==0)
                    Print("EEPROM A/D Gain ==> [%8.6f]\n\r", X305IO_fAD_Gain); 
                else
                    Print("No setting in EEPROM, A/D Gain ==> %8.6f\n\r", X305IO_fAD_Gain);
                
                if((iValue&2)==0)
                    Print("EEPROM A/D Offset ==> [%8.6f]\n\r", X305IO_fAD_Offset);
                else
                    Print("No setting in EEPROM, A/D Offset ==> %8.6f\n\r", X305IO_fAD_Offset);
                    
                Puts("\r\n");
            break;
            
            // A/D (Analog input)
            case 2:
                Puts("Please select A/D channel(0~7): ");
                Scanf("%d", &iInChannel);
                
                if(iInChannel>7)
                {
                    Puts("Unknow Channel Number!!\r\n\r\n");
                    break;
                }
/*
                Method1:                
                fValue=X305IO_AnalogIn(iInChannel);
*/
                //Method2:
                fValue=X305IO_AnalogIn_HexToFloat(X305IO_AnalogIn_2(iInChannel));
                Print("AI[%d]= %6.4fmA\n\r\n\r", iInChannel, fValue);
            break;
            
            // Read DI (all channel)
            case 3:
                iDOValue=X305IO_Read_All_DI();
                
                Print("Digital Input ReadBack=> 0x%x\r\n\r\n", iDOValue);
            break;
            
            // Read DI (one channel)
            case 4:
                Print("Please select inport channel(0~2): ");
                Scanf("%d", &iInChannel);
                
                if(iInChannel>3)
                {
                    Puts("Unknow Channel Number!!\r\n\r\n");
                    break;
                }
                
                iDIValue=X305IO_Read_One_DI(iInChannel);
                
                if(iDIValue)
                    Print("Channel %d ==> OFF\r\n\r\n", iInChannel);
                else
                    Print("Channel %d ==> ON\r\n\r\n", iInChannel);
            break;
            
            // Write DO (all channels)
            case 5:
                Print("Please input Output value(0 ~ 0x7): ");
                Scanf("%x", &iDOValue);
                
                X305IO_Write_All_DO(iDOValue);

                Print("Output value=> 0x%x\r\n\r\n", iDOValue);
            break;
            
            // Write DO (one channel)
            case 6:
                Print("Please select outport channel(0) and status(0:OFF, 1:ON)\r\n");
                Print("Channel number and status:");
                
                Scanf("%d %d", &iOutChannel, &iStatus);
                
                if(iOutChannel>3)
                {
                    Puts("Unknow Channel Number!!\r\n\r\n");
                    break;
                }
                
                X305IO_Write_One_DO(iOutChannel, iStatus);
                                
                Print("Channel %d ==> %s\r\n\r\n", iOutChannel, (iStatus>0)?"ON":"OFF");
            break;
            
            // Read DO (all channels)
            case 7:
                iDOValue=X305IO_Read_All_DO();
                
                Print("Digital Output ReadBack=> 0x%x\r\n\r\n", iDOValue);
            break;
            
            // Read DO (one channels)
            case 8:
                Print("Please select outport channel(0~2): ");
                Scanf("%d", &iOutChannel);
                
                if(iOutChannel>3)
                {
                    Puts("Unknow Channel Number!!\r\n\r\n");
                    break;
                }
                
                iRet=X305IO_Read_One_DO(iOutChannel);
                 
                if(iRet)
                    Print("Channel %d ==> ON\r\n\r\n", iOutChannel);
                else
                    Print("Channel %d ==> OFF\r\n\r\n", iOutChannel);
            break;

            
            case 0:
            default:
                quit=1;
                break;
        }
        
        if(!quit)
        {
            Puts("Press any key to continue...\n\r");
            Getch();
        }
    }

}

int iAINum;
int X305IO_AnalogIn_2(int iChannel)
{
	X305IO_AnalogIn_SetChannel(iChannel);
	if(iAINum!=iChannel)
	    Delay(4);
	
	iAINum=iChannel;
	    
	return X305IO_AnalogIn_Hex();
}
