/******************************** PARALLEL PORT INTERFACING ********************************/
/* 
										GROUP MAMBERS
										 UMER JAVAID
									BURHAN UD DIN ABBASI
										QALAB E ABBAS
									    TAIMUR MEHMOOD
 */

#include <iostream>												//Header for cout and cin.
#include <windows.h>											//Sleep time.
#pragma comment (lib,"./Inpout32.lib")							//For getting access to the port.

using namespace std;											
void _stdcall Out32(short PortAddress, short data);				
int binEq(int control_array[]);									/*Function prototype for converting
																  binary to decimal*/
int main()														//Main function
{
	int  data=0;		   						//Creating the array and variable.

	while(!GetAsyncKeyState(VK_ESCAPE))							//Loop for sending data until Escape is pressed.
	{	    	
		int control_array[8]={0,0,0,0,0,0,0,0};					//Initializing the array to zero.
		
		cout <<"			PARALLEL PORT INTERFACING				"<<endl<<endl;
		cout <<"Controlling an RC Car via LPT1"<<endl<<"				USER GUIDE"<<endl<<endl;
		cout <<"Press UP key to move FORWARD"<<endl;			//Displaying functions of keys.
		cout <<"Press DOWN key to move BACKWARD"<<endl;			//for the help of user.
		cout <<"Press LEFT key to turn LEFT"<<endl;		
		cout <<"Press RIGHT key to turn RIGHT"<<endl;			
		cout <<"Press ESCAPE key to EXIT "<<endl<<endl;

/******************************** CHECKING THE KEY STATES ********************************/
		
		if (GetAsyncKeyState(VK_UP))						//If key is pressed condition is true
		{														//body of if will be executed.
			cout << "			FORWARD\t";									//display the function of the key.
			control_array[7]=1;									//set the corresponding value of the array
		}														//to 1 i.e. give output on pin prescribed.
						
		if (GetAsyncKeyState(VK_DOWN))						//similarly checking other key states.
		{
			cout << "			BACKWARD";
			control_array[6]=1;
		}
		if (GetAsyncKeyState(VK_LEFT))
		{
			cout << "			LEFT";
			control_array[5]=1;
		}
		if (GetAsyncKeyState(VK_RIGHT))
		{
			cout << "			RIGHT";
			control_array[4]=1;
		}


//******************************** IF INPUT CONTRADICTS **********************************//
				
		if(control_array[7]==control_array[6])
		{															//If left and right keys are pressed at the same time then nullify both.
			control_array[7]=0;
			control_array[6]=0;
		}
		if(control_array[5]==control_array[4])
		{															//If forward and backward keys are pressed at the same time then nullify both.
			control_array[5]=0;
			control_array[4]=0;
		}

//********************************** SENDING DTAT TO LPT1 **********************************//
		
		data = binEq(control_array);								//Converting into decimal equivalent.
		cout << endl << endl << "Decimal Equivalent is: " << data << endl;	//to get the number of pin activated.
		Out32(0x378,data);											//Sending data to LPT1(Parallel port).
		
//********************************** DISPLAYING ACTIVATED PIN NUMBER **********************************//
		{
			if (control_array[7] == 1)
			cout << endl << "Pin no. 2 is ACTIVATED" ;
			if (control_array[6] == 1)
			cout << endl << "Pin no. 3 is ACTIVATED" ;
			if (control_array[5] == 1)
			cout << endl << "Pin no. 4 is ACTIVATED" ;
			if (control_array[4] == 1)
			cout << endl << "Pin no. 5 is ACTIVATED" ;
		}

		Sleep(20);													//refresh rate preventing the wastage of CPU cycles.
		system("cls");												//Clear the screen.
	}
	return 0;
}

int binEq(int control_array[] )					//Function defination.
{												//A function to convert an 8 digit binary number into decimal
	int data=0;
	for(int n=0;n<8;n++)
	{
		data = (2*data+control_array[n]);									
	}																
	return data;
}