/*
    XDemo24: Uses countdown timer

    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.

    When 7188E power on, the demo will start running countdown function.

    There are 8 countdown timer (channel 0 to channel 7)
    This demo uses channel 0.
    The countdown timer initial value is 1000 ms.
    When the countdown timer value become 0,
    the value of LED will increase.

    Hareware: uPAC-7186EXD

    [Dec 18, 2008] by Liam
*/

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

extern int bLedControl;

int iNumber;
unsigned long ulCountValue;
char cLED[6];

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.
    */
    
    InitLib();

    Init5DigitLed();    //Initialize the 5DigitLed
    bLedControl=0;      
        
    TimerOpen();
    CountDownTimerStart(0, 1000); // use channel 0, Count Value is 1000 ms
}

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

    CountDownTimerReadValue(0,&ulCountValue);
    if (ulCountValue==0)
    {
        CountDownTimerStart(0,1000);    // Restart the countdown timer with 1000 ms count value.
        iNumber++;
        
        //Shows value of iNumber on the 5-digit LED.
        sprintf(cLED,"%5d",iNumber);
        for (i=1;i<=5;i++)              
            Show5DigitLed(i,cLED[i-1]-'0'); 
    }
}

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.
    */  
    
    strcpy(Response,Cmd);
    return 1;               // return OK
}

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
}
