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

#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include "mxgpio.h"
/*
	index[n]: 0  ; BIT 0
			  1  ; BIT 1
	          2  ; BIT 2
	          3  ; BIT 3
			  ....
	data[n]: 0  ; Digital LOW
	         1  ; Digital HIGH
	         
*/
#define SlotNumer	 2
#define Slot1SocketNumer	 2
#define Slot2SocketNumer	 2
#define Slot1Index	 0
#define Slot2Index	 2

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

	WCHAR sin,smode;
	printf("Power Control Test Program\n");
    printf("\t (0) Exit Program\n");
    printf("\t (1) Display Power\n");
    printf("\t (2) Set Power value\n");

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

	do
    {
        switch (n)
        {
            // if char == '1', display the digital input
            case 1:
				//Open LED
				hMxLED=mxgpio_open();

				printf("Slot1:\n");
				for(int i=0;i<Slot1SocketNumer;i++)
				{
					//Get digital input
					port_no=Slot1Index+i;
					input=mxgpio_get_data(hMxLED,port_no);
					if((i%2 == 0)){
						if(input == 0){
							printf("Socket%d is On\n",i);
						}else if(input == 1){
							printf("Socket%d is Off\n",i);
						}
					}else{
						if(input == 0){
							printf("Socket%d is Off\n",i);
						}else if(input == 1){
							printf("Socket%d is On\n",i);
						}
					}
				}
				printf("\n");

				printf("Slot2:\n");
				for(int i=0;i<Slot2SocketNumer;i++)
				{
					//Get digital input
					port_no=Slot2Index+i;
					input=mxgpio_get_data(hMxLED,port_no);
					//printf("Power%d = %d\n",port_no-Slot2Index,input);
					if((i%2 == 0)){
						if(input == 0){
							printf("Socket%d is On\n",i);
						}else if(input == 1){
							printf("Socket%d is Off\n",i);
						}
					}else{
						if(input == 0){
							printf("Socket%d is Off\n",i);
						}else if(input == 1){
							printf("Socket%d is On\n",i);
						}
					}
				}

				//Close LED
                mxgpio_close(hMxLED);
                break;
				
			case 2:

				//Get Port Number
				getwchar();
				printf("Input the Slot Number (0 ~ %d) = \n",SlotNumer-1);
				smode=getwchar();
				slot_no=_wtoi(&smode);

				if( slot_no== 0){
					getwchar();
			        printf("Input the socket number (0 ~ %d) = \n",Slot1SocketNumer-1);
					smode=getwchar();
					if(_wtoi(&smode)>(Slot1SocketNumer-1) || socket_no<0){
						printf("Input Slot Number must between 0 ~ %d.\n",Slot1SocketNumer-1);
						return -1;
					}
					socket_no=_wtoi(&smode) + Slot1Index;

					

					getwchar();
			        printf("Input 1 to turn the power on , input 0 to turn the power off. \n");
					smode=getwchar();
					data=_wtoi(&smode);
					hMxLED=mxgpio_open();

					if((socket_no%2 == 0)){
						if(data == 0){
							output=mxgpio_set_data(hMxLED, socket_no, 1);
						}else if(data == 1){
							output=mxgpio_set_data(hMxLED, socket_no, 0);
						}else{
							printf("Input data must be 0 or 1.\n");
							return -1;
						}
					}else if(socket_no%2 == 1){
						if(data == 0){
							output=mxgpio_set_data(hMxLED, socket_no, 0);
						}else if(data == 1){
							output=mxgpio_set_data(hMxLED, socket_no, 1);
						}else{
							printf("Input data must be 0 or 1.\n");
							return -1;
						}
					}
				
					

					if(output==-1)
					{
						printf("Set LED fail!\n");
					}
					else
					{
						printf("Set LED success!\n");
					}
					//Close LED
					mxgpio_close(hMxLED);
				}else if( slot_no== 1){
					getwchar();
			        printf("Input the socket number (0 ~ %d) = \n",Slot2SocketNumer-1);
					smode=getwchar();
					if(_wtoi(&smode)>(Slot2SocketNumer-1) || socket_no<0){
						printf("Input Slot Number must between 0 ~ %d.\n",Slot1SocketNumer-1);
						return -1;
					}
					socket_no=_wtoi(&smode) + Slot2Index;

					

					getwchar();
			        printf("Input 1 to turn the power on , input 0 to turn the power off. \n");
					smode=getwchar();
					data=_wtoi(&smode);
					hMxLED=mxgpio_open();
					hMxLED=mxgpio_open();
					
					
				
					if((socket_no%2 == 0)){
						if(data == 0){
							output=mxgpio_set_data(hMxLED, socket_no, 1);
						}else if(data == 1){
							output=mxgpio_set_data(hMxLED, socket_no, 0);
						}else{
							printf("Input data must be 0 or 1.\n");
							return -1;
						}
					}else if(socket_no%2 == 1){
						if(data == 0){
							output=mxgpio_set_data(hMxLED, socket_no, 0);
						}else if(data == 1){
							output=mxgpio_set_data(hMxLED, socket_no, 1);
						}else{
							printf("Input data must be 0 or 1.\n");
							return -1;
						}
					}
				

					if(output==-1)
					{
						printf("Set LED fail!\n");
					}
					else
					{
						printf("Set LED success!\n");
					}
					//Close LED
					mxgpio_close(hMxLED);
				}else{
					printf("Input the socket number must between 0 ~ %d.\n",Slot1SocketNumer-1);
					return -1;
				}
			
				break;

				default:
					return -1;
				

        }
		getwchar();
		printf("\n");
		printf("Power Control Test Program\n");
		printf("\t (0) Exit Program\n");
		printf("\t (1) Display Power\n");
		printf("\t (2) Set Power value\n");
        sin = getwchar();
        n = _wtoi(&sin);
    } while (n != 0);
	

	return 0;
}

