/*
	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[])
{

	HANDLE	hDIO;
	int port_no;
	int data;
	int	intDout, intDin;
	int	nDout = 0;
	int port = 0, mode = 0;
	int n = 0;
	int nSuccessDO = 0, nSuccessDIO = 0;
	int nFailDO = 0, nFailDIO = 0;
	int portout[MAX_DOUT_NUMBER] = { 0 };
	int nTest;

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

	sin = getwchar();
	n = _wtoi(&sin);

	do
	{
		switch (n)
		{
			// if char == '1', Set the digital output
		case 1:
			//Open DIO
			hDIO = mxdgio_open();

			//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) = \n");
			smode = getwchar();
			data = _wtoi(&smode);

			//Get DOUT Value
			for (int i = 0; i < MAX_DOUT_NUMBER; i++)
			{
				intDout = mxdgio_get_output_signal(hDIO, i);
				portout[i] = intDout;
			}

			for (int i = 0; i < MAX_DOUT_NUMBER; i++)
			{
				if (i == port_no)
				{
					//Set DOUT
					if (data == 1)
					{
						nDout = mxdgio_set_output_signal_high(hDIO, port_no);
					}
					else
					{
						nDout = mxdgio_set_output_signal_low(hDIO, port_no);
					}
				}
				else
				{
					//Save the original value
					if (portout[i] == 0)
						mxdgio_set_output_signal_low(hDIO, i);
					else if (portout[i] == 1)
						mxdgio_set_output_signal_high(hDIO, i);
				}
			}
			if (nDout == -1)
			{
				printf("Set digital output fail!\n");
			}
			else
			{
				printf("Set digital output success!\n");
			}

			//Close DIO
			mxdgio_close(hDIO);
			break;

			// if char == '2', Get both digital input and digital output
		case 2:
			//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;

			// if char == '3', Test Program
		case 3:
			//Open dio
			hDIO = mxdgio_open();

			//Get the number of the test
			//getwchar();
			printf("Input the number of the test = \n");
			scanf("%d", &nTest);

			for (int i = 0; i < nTest; i++)
			{
				//Get DOUT Value
				for (int j = 0; j < MAX_DOUT_NUMBER; j++)
				{
					intDout = mxdgio_get_output_signal(hDIO, j);
					portout[j] = intDout;
				}

				port_no = rand() % 8;

				for (int k = 0; k < MAX_DOUT_NUMBER; k++)
				{
					if (k == port_no)
					{
						//Set DOUT
						if (portout[port_no] == 0)
						{
							nDout = mxdgio_set_output_signal_high(hDIO, port_no);
						}
						else
						{
							nDout = mxdgio_set_output_signal_low(hDIO, port_no);
						}
					}
					else
					{
						//Save the original value
						if (portout[k] == 0)
							mxdgio_set_output_signal_low(hDIO, k);
						else if (portout[k] == 1)
							mxdgio_set_output_signal_high(hDIO, k);
					}
				}

				for (int l = 0; l < MAX_DOUT_NUMBER; l++)
				{
					// Check the specific DO had been set
					if (l == port_no)
					{
						//DOUT checking
						if (portout[port_no] != mxdgio_get_output_signal(hDIO, l))
							nSuccessDO++;
						else
						{
							nFailDO++;
							printf("DOOUT %d error\n", l);
						}
					}
					else
					{
						if (portout[l] == mxdgio_get_output_signal(hDIO, l))
							nSuccessDO++;
						else
						{
							nFailDO++;
							printf("DOUT %d error\n", l);
						}
					}

					//DIO checking
					if (mxdgio_get_output_signal(hDIO, l) == mxdgio_get_input_signal(hDIO, l))
						nSuccessDIO++;
					else
					{
						nFailDIO++;
						printf("DIO %d error\n", l);
					}
				}
			}

			//Show the results of DO checking
			printf("DOUT Success:%d\n", nSuccessDO);
			printf("DOUT Fail:%d\n\n", nFailDO);

			//Show the results of DIO checking
			printf("DIO Success:%d\n", nSuccessDIO);
			printf("DIO Fail:%d\n\n", nFailDIO);

			nSuccessDO = 0;
			nSuccessDIO = 0;
			nFailDO = 0;
			nFailDIO = 0;

			//Close DIO
			mxdgio_close(hDIO);
			break;

			break;
		}

		printf("DIO Test Program\n");
		printf("\t (0) Exit Program\n");
		printf("\t (1) Set DOUT value\n");
		printf("\t (2) Display both DIN and DOUT\n");
		printf("\t (3) Test Program\n");

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

	return 0;
}

