/*
    XDemo31: X310

    Compiler: BC++ 3.1
    Compile mode: large
    Project: user.c
             v7000.c
             vModbus.c           [after vcom3002.lib]
             ..\Lib\7186EL.Lib
             ..\Lib\tcp_dm32.lib
             ..\Lib\VcomNNNN.Lib, with NNNN being the lib file's version.
             ..\lib\xboard\X304.lib

    19AI 0      -> Analog inputs from channel 0
    19ai 1      -> Analog inputs from channel 1
    19AO 0 2.5  -> Analog outputs 2.5V to channel 0
    19ao 1 -3.5 -> Analog outputs -3.5V to channel 1
    19DI        -> Digital inputs
    19DO 3      -> Digital outputs (DO0~DO1 on, DO2~DO3 off)

    To use X310,  
        Step1: include "X310.h" to your c file
        Step2: add X310.lib into project file
        Step3: call X310_Init at beginnig of your c file.
   
    Hareware: uPAC-7186EX + X310
    
    [Dec 18, 2008] by Liam

*****************************************************************************
*   X310:   2 channel 12-Bit A/D (Analog input)                             *
*                   ch0: 0~20mA                                             *
*                   ch1: 0~10V                                              *
*           2 channel 12-Bit D/A (Analog output)0.0~10.0V                   *
*           3 channel D/I                                                   *
*           3 channel D/O                                                   *
*****************************************************************************

*****************************************************************************
*   [Software specific]                                                     *
*       Input/Output range: 0mA     20mA    +0.0V   +10.0V                  *
*       Decimal integer:    0       4095    0   4095                        *
*       Hexadecimal:        000     FFF 000 FFF                             *
*                                                                           *
*       [A/D]                                                               *
*       sampling rate:  1800 data/sec (with floating convertion)            *
*               Accuracy==>     Typical: +- 1 LSB (+/- 2.4 mV) (+/- 4.9 uA) *
*               Maximum: +- 2 LSB (+/- 4.8 mV) (+/- 9.8 uA)                 *    
*       [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 EEPROM block 7 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.                              *
*****************************************************************************
*/

#include <string.h>
#include <stdio.h>
#include <conio.h>
#include "..\lib\7186e.h"
#include "..\lib\tcpip32.h"
#include "..\lib\vxcomm.h"
#include "..\Lib\Xboard\X310.h"

void UserCount(void)
{
    /*
    User's timer trigger function.
    Please refer to XDemo04 for detail description.
    Please refer to XDemo09 for example code.
    */
}

void UserInit(void)
{
    /*
    Initialize user's program.
    Please refer to XDemo04 for detail description.
    Please refer to XDemo09 for example code.
    */  

    /*
    All of the program user.c needn't function Installcom().
    Becuase they have install When the Xserver initialize.
    If you want to change the baud rate.data format, 
    please using the function SetBaudrate().SetDataFormat().
    */
    
    InitLib();
    X310_Init();
    
    SetBaudrate1(115200L);
}

void UserLoopFun(void)
{
    /*
    VxComm.exe will call this function every scan time
    Please refer to XDemo11 for Real-time I/O control
    */   
}

int UserCmd(unsigned char *Cmd,unsigned char *Response)
{
    /*  
    Xserver executes this function when received a package 
    form TCP port 10000 and the first two bytes are "19".
    
    Funtion of Xserver, Please refer to XDemo04 for detail description.
    */  
    
    float fValue;
    char sMode[5];  // AO, AI, DO, DI 
    int i,iRet,iValue,iChannel;

    if(Cmd[0])
    {
        sscanf(Cmd,"%s",&sMode);
        strupr(sMode);

        //Analog input
        if(!strcmp(sMode,"AI"))
        {
            sscanf(Cmd+3,"%d",&iChannel);
            /*
            To void noise effect, you should do software fitering.
            Of couse, that will speed down sampling rate.
            */
            fValue=0;
            for(i=0;i<2;i++)
                fValue+=X310_AnalogIn(iChannel)/2.0;
            if(iChannel==0)
            {
                sprintf(Response,"AI[0]= %7.4f mA",fValue);
                printCom1("AI[0]= %7.4f mA\n\r",fValue);
            }
            else 
            {
                sprintf(Response,"AI[1]= %7.4f V",fValue);
                printCom1("AI[1]= %7.4f V\n\r",fValue);
            }
        }
        //Analog output
        else if(!strcmp(sMode,"AO"))
        {
            sscanf(Cmd+2,"%d %f",&iChannel,&fValue);
            X310_AnalogOut(iChannel,fValue);
            sprintf(Response,"AO[%d] %7.4f V OK.",iChannel,fValue);
            printCom1("AO[%d] %7.4f V OK.\n\r",iChannel,fValue);
        }
        else if(!strcmp(sMode,"DI"))
        {
            iValue=X310_DigitalIn();
            sprintf(Response,"DI=%01X",iValue);
            printCom1("DI=%01X\n\r",iValue);
        }               
        else if(!strcmp(sMode,"DO"))
        {
            sscanf(Cmd+3,"%x",&iValue);
            X310_DigitalOut(iValue);
            sprintf(Response,"DO %2X",iValue);
            printCom1("DO %01X\n\r",iValue);
        }
        return 1;               // return OK
    }
    return 0;       // return ERROR
}

int VcomUserBinaryCmd(TCPREADDATA *p)
{
    /* 
    VXCOMM.EXE 2.6.12(04,Sep,2001) or later will support this function.

    Xserver executes this function when received a package 
    form TCP port 10000 and the first two bytes are "23".
    
    Please refer to XDemo04 for detail description.
    Please refer to XDemo23 for example code.
    */
    return 1;   /* any value will be accept */
}

int VcomCmdUser(TCPREADDATA *p)  
{ 
    /*
    VCOM3005 (Feb,22,2002) or later will call this function for PortUser.
    When packets received by TCP PORT PortUser(user defined) 
    of 7188E/8000E, Xserver will call this function.

    Please refer to XDemo04 for detail description.
    */
    VcomSendSocket(p->Socket,p->ReadUartChar,p->Length);
    return 1;   /* any value will be accept */  
}

void PortUserStart(int skt)
{
    /* 
    XS8_3200.Lib Version 3.2.00 (20,Apr,2004) 
    or later version supports this function.
    
    When a TCP/IP client connects to the 7188E/8000E via the user's 
    defined port(PortUser), the Xserver calls the function once.

    Please refer to XDemo04 for detail description.
    */
    
    skt=skt; //do nothing
}
  
void Port9999Start(int skt)
{
    /* 
    XS8_3200.Lib Version 3.2.00 (20,Apr,2004) 
    or later version supports this function.
    
    When a TCP/IP client connects to the 7188E/8000E TCP port 9999, 
    the Xserver calls the function once.
 
    Please refer to XDemo04 for detail description.
    */
    
    skt=skt; //do nothing
}
 
void Port502Start(int skt)
{
    /* 
    XS8_3200.Lib Version 3.2.00 (20,Apr,2004) 
    or later version supports this function.
    
    When a TCP/IP client connects to the 7188E/8000E TCP port 502, 
    the Xserver calls the function once.
    
    Please refer to XDemo04 for detail description.
    */
    
    skt=skt; //do nothing
}
