/*  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.
*/

#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include "..\release\devices.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 8
#define MAX_DIN_NUMBER 8

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;
	int nCounter=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) Set DOUT value\n");
	printf("\t (3) Get counter value\n");
	printf("\t (4) Clear counter value\n");

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

				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', Set the digital output
            case 2:
             	//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);
		
				//Open DIO
				hDIO=mxdgio_epm3438_open(0);
				
				//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 3:
				// if char == '3', Get the counter value
				//Open dio
				hDIO=mxdgio_epm3438_open(0);

				//get the counter value
				nCounter=mxdgio_epm3438_get_counter(hDIO);
				printf("counter value=%d(0x%x)\n",nCounter,nCounter);

				//Close DIO
                mxdgio_close(hDIO);
				break;

			case 4:
				// if char == '4', Clear the counter value
				//Open dio
				hDIO=mxdgio_epm3438_open(0);
				
				//Clear the counter value
				mxdgio_epm3438_clear_counter(hDIO);
				printf("counter has been reset.\n");

				//Close DIO
                mxdgio_close(hDIO);
				break;


			default:
				break;
        }
		getwchar();
        sin = getwchar();
        n = _wtoi(&sin);
    } while (n != 0);

	return 0;
}

