// PwrExample.cpp : Defines the entry point for the console application.
//

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


#define MAX_SOCKET_NUMBER 2
#define PWR_INDEX	 3
#define PWR_OFF	 1
#define PWR_ON	 0

int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE	hMxPWR;
	int port_no=0;
	int	nLED = 0;
	int port = 0,mode = 0;
	int n = 0;
	int on = 1;
	int data=0;
	int	input=0,output=0;

	bool inverseFlag=false;
	if(PWR_OFF==1){
		inverseFlag=true;
	}else{
		inverseFlag=false;
	}
	

	WCHAR sin,smode;
	printf("Mini PCIE Power Contol Test Program\n");
	printf("\t (0) Exit Program\n");
    printf("\t (1) Display Power condition\n");
    printf("\t (2) Set Power on/off \n");

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

	do
    {
        switch (n)
        {
			case 1:
				//Open power control
				hMxPWR=mxgpio_open();

				for(int i = 0; i < MAX_SOCKET_NUMBER; i++)
				{
					//Get digital input
					input=mxgpio_get_data(hMxPWR, PWR_INDEX + i);
					if( input == PWR_OFF){
						printf("Socket%d is off\n",i+1);
					}else if( input == PWR_ON){
						printf("Socket%d is on\n",i+1);
					}
				}
				//Close LED
                mxgpio_close(hMxPWR);
                break;

			case 2:
				//Get Port Number
				getwchar();
				printf("Input the Socket Number (1 ~ %d) = \n",MAX_SOCKET_NUMBER);
				smode = getwchar();
				port_no = _wtoi(&smode)-1 + PWR_INDEX;
			
				if( (_wtoi(&smode) >(MAX_SOCKET_NUMBER)) || (_wtoi(&smode) < 1) ){
					printf("The port value you input must betwwen 1 ~ %d ",MAX_SOCKET_NUMBER);
					break;
				}

				//Get Value
                getwchar();
                printf("Input 0 or 1 (0 = Off, 1 = On) = ");
				smode = getwchar();
				data = _wtoi(&smode);
		
				if( (data!=PWR_OFF) && (data!=PWR_ON) ){
					printf("The value you input must be %d or %d. ",PWR_OFF,PWR_ON);
					break;
				}

				//Open LED
				hMxPWR = mxgpio_open();

				//Set LED
				if(inverseFlag){
					if(data==PWR_OFF){
						output = mxgpio_set_data(hMxPWR, port_no, PWR_ON);
					}else{
						output = mxgpio_set_data(hMxPWR, port_no, PWR_OFF);
					}
				}else{
					output = mxgpio_set_data(hMxPWR, port_no, data);
				}
				
				if(output==-1)
				{
					printf("Set Power fail!\n");
				}
				else
				{
					printf("Set Power success!\n");
				}

				//Close LED
				mxgpio_close(hMxPWR);
                
				
				break;
		}
		getwchar();
		printf("\n");
		printf("Mini PCIE Power Contol Test Program\n");
		printf("\t (0) Exit Program\n");
		printf("\t (1) Display Power condition\n");
		printf("\t (2) Set Power on/off \n");
        sin = getwchar();
        n = _wtoi(&sin);
    } while (n != 0);

	return 0;
}

