/*  Copyright (C) MOXA Inc. All rights reserved.

    This software is distributed under the terms of the
    MOXA License.  See the file COPYING-MOXA for details.
*/

/* V2201-W7E 
   First release by Sean Hsieh 2015.03.05
*/

#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include "mxdgio.h"



/*
	index[n]: 0  ; BIT 0
			  1  ; BIT 1
	          2  ; BIT 2
	          3  ; BIT 3
			  ....
	data[n]: 0  ; Digital LOW
	         1  ; Digital HIGH
	         
*/

#define MAX_DOUT_NUMBER		4
#define MAX_DIN_NUMBER		4

int _tmain(int argc, _TCHAR* argv[])
{

	HANDLE	hDIO;
	int port_no;
	int data;
	int	intDout,intDin;
	int	nDout=0;
	int port=0,mode=0;
	int n=0;

	WCHAR sin,smode;
	printf("UART Mode Test Program\n");
    printf("\t (0) Exit Program\n");
    printf("\t (1) Display DIN\n");
    printf("\t (2) Display DOUT\n");
	printf("\t (3) Set DOUT value\n");
	printf("\t (4) Display both DIN and DOUT\n");

	sin=getwchar();
	n=_wtoi(&sin);
	
	do
    {
        switch (n)
        {
            // if char == '1', display the digital input
            case 1:
				//Open dio
				hDIO=mxdgio_open();

				for(int i=0;i<MAX_DIN_NUMBER;i++)
				{
					//Get digital input
					port_no=i;
					intDin=mxdgio_get_input_signal(hDIO,port_no);
					printf("Din%d = %d\n",port_no,intDin);
				}

				//Close DIO
                mxdgio_close(hDIO);
                break;

			// if char == '2', display the digital output
			case 2:
				//Open dio
				hDIO=mxdgio_open();

				for(int i=0;i<MAX_DOUT_NUMBER;i++)
				{
					//Get digital input
					port_no=i;
					intDin=mxdgio_get_output_signal(hDIO,port_no);
					printf("Dout%d = %d\n",port_no,intDin);
				}

				//Close DIO
                mxdgio_close(hDIO);
				break;
            // if char == '3', Set the digital output
            case 3:
             
				//Get Port Number
				getwchar();
				printf("Input the Port Number (0 ~ %d) = \n",MAX_DOUT_NUMBER-1);
				smode=getwchar();
				port_no=_wtoi(&smode);
			
				//Get Value
                getwchar();
                printf("Input the value (0 or 1) = ");
				smode=getwchar();
				data=_wtoi(&smode);
		
				if( (port_no >(MAX_DOUT_NUMBER-1)) || (port_no < 0) ){
					printf("The value you input must betwwen 0 ~ %d ",MAX_DOUT_NUMBER-1);
					break;
				}

				//Open DIO
				hDIO=mxdgio_open();
				
				//Set DOUT
				if(data==1)
				{
					nDout=mxdgio_set_output_signal_high(hDIO,port_no);
				}
				else
				{
					nDout=mxdgio_set_output_signal_low(hDIO,port_no);
				}
				if(nDout==-1)
				{
					printf("Set digital output fail!\n");
				}
				else
				{
					printf("Set digital output success!\n");
				}

				//Close DIO
				mxdgio_close(hDIO);
                break;

			case 4:
				// if char == '4', Get both digital input and digital output
				//Open dio
				hDIO=mxdgio_open();

				for(int i=0;i<MAX_DIN_NUMBER;i++)
				{
					//Get digital input
					port_no=i;
					intDin=mxdgio_get_input_signal(hDIO,port_no);
					if(intDin==-1)
					{
						printf("\n");
					}
					else
					{
						printf("Din%d = %d ",port_no,intDin);
					}
					intDout=mxdgio_get_output_signal(hDIO,port_no);
					if(intDout==-1)
					{
						printf("\n");
					}
					else
					{
						printf(", Dout%d = %d\n",port_no,intDout);
					}
					//printf("Din%d = %d, Dout%d = %d\n",port_no,intDin,port_no,intDout);
				}

				//Close DIO
                mxdgio_close(hDIO);
				break;
        }
		getwchar();
		printf("\nUART Mode Test Program\n");
		printf("\t (0) Exit Program\n");
		printf("\t (1) Display DIN\n");
		printf("\t (2) Display DOUT\n");
		printf("\t (3) Set DOUT value\n");
		printf("\t (4) Display both DIN and DOUT\n");
	
        sin = getwchar();
        n = _wtoi(&sin);
    } while (n != 0);

	return 0;
}

