/*  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 "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		8
#define MAX_DIN_NUMBER		8

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

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

	WCHAR sin,smode,slotIndex;
	printf("DIO Test Program\n");
	//printf("\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");
	printf("\t (5) Get Counter Value\n");
	printf("\t (6) Clear Counter Value\n");
	printf("\t (7) Set all DOUT value high\n");
	printf("\t (8) Set all DIN value low\n");

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

				getwchar();
				printf("Please enter the slot number you want to use, 1 or 2?\n");
				printf("(If there is only 1 card in the machine, you must to enter 1.)\n");
				slotIndex=getwchar();
				index=_wtoi(&slotIndex);
				if((index!=1) && (index!=2)){
					printf("You just can enter 1 or 2.\n");
					return -1;
				}

				//Open dio
				hDIO=mxdgio_open(index);

				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:

				getwchar();
				printf("Please enter the slot number you want to use, 1 or 2?\n");
				printf("(If there is only 1 card in the machine, you must to enter 1.)\n");
				slotIndex=getwchar();
				index=_wtoi(&slotIndex);
				if((index!=1) && (index!=2)){
					printf("You just can enter 1 or 2.\n");
					return -1;
				}

				//Open dio
				hDIO=mxdgio_open(index);

				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:
             
				getwchar();
				printf("Please enter the slot number you want to use, 1 or 2?\n");
				printf("(If there is only 1 card in the machine, you must to enter 1.)\n");
				slotIndex=getwchar();
				index=_wtoi(&slotIndex);
				if((index!=1) && (index!=2)){
					printf("You just can enter 1 or 2.\n");
					return -1;
				}

				//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_open(index);
				
				//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:

				getwchar();
				printf("Please enter the slot number you want to use, 1 or 2?\n");
				printf("(If there is only 1 card in the machine, you must to enter 1.)\n");
				slotIndex=getwchar();
				index=_wtoi(&slotIndex);
				if((index!=1) && (index!=2)){
					printf("You just can enter 1 or 2.\n");
					return -1;
				}
				
				// if char == '4', Get both digital input and digital output
				//Open dio
				hDIO=mxdgio_open(index);

				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;

			case 5:

				getwchar();
				printf("Please enter the slot number you want to use, 1 or 2?\n");
				printf("(If there is only 1 card in the machine, you must to enter 1.)\n");
				slotIndex=getwchar();
				index=_wtoi(&slotIndex);
				if((index!=1) && (index!=2)){
					printf("You just can enter 1 or 2.\n");
					return -1;
				}

				// if char == '5', Get count number of counter
				//Open dio
				hDIO=mxdgio_open(index);

				nCount = mxdgio_get_counter(hDIO);
				printf("nCount = %d ",nCount);

				//Close DIO
              mxdgio_close(hDIO);
				break;

			case 6:

				getwchar();
				printf("Please enter the slot number you want to use, 1 or 2?\n");
				printf("(If there is only 1 card in the machine, you must to enter 1.)\n");
				slotIndex=getwchar();
				index=_wtoi(&slotIndex);
				if((index!=1) && (index!=2)){
					printf("You just can enter 1 or 2.\n");
					return -1;
				}

				// if char == '6', Clear counter
				//Open dio
				hDIO=mxdgio_open(index);

				mxdgio_clear_counter(hDIO);

				//Close DIO
              mxdgio_close(hDIO);
				break;

			case 7:

				getwchar();
				printf("Please enter the slot number you want to use, 1 or 2?\n");
				printf("(If there is only 1 card in the machine, you must to enter 1.)\n");
				slotIndex=getwchar();
				index=_wtoi(&slotIndex);
				if((index!=1) && (index!=2)){
					printf("You just can enter 1 or 2.\n");
					return -1;
				}

				// if char == '6', Clear counter
				//Open dio
				hDIO=mxdgio_open(index);

				nDout=mxdgio_set_all_output_signal_high(hDIO);

				//Close DIO
                mxdgio_close(hDIO);
				break;
			
			case 8:

				getwchar();
				printf("Please enter the slot number you want to use, 1 or 2?\n");
				printf("(If there is only 1 card in the machine, you must to enter 1.)\n");
				slotIndex=getwchar();
				index=_wtoi(&slotIndex);
				if((index!=1) && (index!=2)){
					printf("You just can enter 1 or 2.\n");
					return -1;
				}

				// if char == '6', Clear counter
				//Open dio
				hDIO=mxdgio_open(index);

				nDout=mxdgio_set_all_output_signal_low(hDIO);

				//Close DIO
                mxdgio_close(hDIO);
				break;
        }
		getwchar();
		
		printf("\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");
		printf("\t (5) Get Counter Value\n");
		printf("\t (6) Clear Counter Value\n");
		printf("\t (7) Set all DOUT value low\n");
		printf("\t (8) Set all DIN value low\n");

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

	return 0;
}

