// KomDioDemo.cpp : Demo application for KomDio API.
//

#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <winioctl.h>
#include <KomDio_api.h>

UCHAR Inputs, Outputs;	//number of available inputs and outputs

//function declarations:
void GetInput();					//Get level of input pin
void GetOutput();					//Get level of output pin
void SetOutput();					//Set level of output pin
void GetRestoreOnRestart();		//Get RestoreOnRestart mode
void SetRestoreOnRestart();		//Set RestoreOnRestart mode


//****************************************************************************
// ExitIfError - helper function that checks if the status is OK.
//				In error conditions it prints error message and closes the 
//				application.
void ExitIfError(DWORD status,BOOL printOK=FALSE)
{
	if (status!=ERROR_SUCCESS)
	{	char buf[256];
		
		FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,
						NULL, status, 0, buf, 255, NULL);

		printf("error %d (%s)\n",status,buf);

		exit(status);
	}

	if (printOK==TRUE)
		printf("OK\n");
}

//****************************************************************************
// Application's main entry point
int main(int argc, char* argv[])
{	DWORD status;

	int choice;

	//Hallo message 
	printf("KomDioDemo - demo application for KomDIO API library.\n\n");
	
	//get available inputs and outputs
	status=KomDioGetNumOfIOs(&Inputs, &Outputs);
	ExitIfError(status);

	//print them out
	printf("Available Inputs: %01d\n", (int) Inputs);
	printf("Available Outputs: %01d\n", (int) Outputs);

	do
	{
		//ask for operation:
		printf("\n\nOperation:\n 1. Get Input status\n 2. Set Output status\n 3. Get Output status\n 0. Exit,\nYour choice: ");
		scanf_s("%d",&choice);
		
		//perform chosen operation:
		switch (choice)
		{
			case 1:	
				GetInput();
				break;

			case 2:
				SetOutput();
				break;

			case 3:
				GetOutput();
				break;

			case 4:
				GetRestoreOnRestart();
				break;

			case 5:
				SetRestoreOnRestart();
				break;

			default:
				break;
		}

	} while ((choice>0) && (choice<6));

	return 0;
}

//****************************************************************************
// GetInput - Gets current level of input pin
//			Demonstrates the usage of KomDioGetInput
void GetInput()
{	
	UCHAR Input;
	UCHAR PinNo;
	DWORD status;

	printf("\nChoose which Input (0..%d): ",Inputs-1);
	scanf_s("%d",&PinNo);
			
	printf("Getting Input #%d level... ", PinNo);
	status=KomDioGetInput(PinNo, &Input);

	ExitIfError(status,TRUE);

	printf("Input #%d level is: %s", PinNo,(Input>0)?"high":"low");
}

//****************************************************************************
// SetLed - Sets level of output pin.
//			Demonstrates the usage of KomDioSetOutput
void SetOutput()
{	
	UCHAR Output,IOstatus;
	UCHAR PinNo;
	DWORD status;

	status = KomDioGetIoStatus(&IOstatus);
	ExitIfError(status,TRUE);

	if (IOstatus == KOM_RCFG_DISABLED)
	{
			printf("\nRear IO is disabled - Output pin level cannot be set!");
	}
	else
	if(IOstatus == KOM_RCFG_COM)
	{
		printf("\nRear IO configuration is set to COM1,COM2 - Output pin level cannot be set!");
	}
	else
	{
		printf("\nChoose which Output (0..%d): ",Outputs-1);
		scanf_s("%d",&PinNo);

		printf("\nLevel:\n 0. Low\n 1. High\nChoose mode: ");
		scanf_s("%d",&Output);

		if(Output)
			printf("Setting Output #%d level to high\n", PinNo);
		else
			printf("Setting Output #%d level to low\n", PinNo);

		status=KomDioSetOutput(PinNo, Output);

		ExitIfError(status);

		
		status=KomDioGetOutput(PinNo, &Output);

		ExitIfError(status,TRUE);

		printf("Output #%d level has been set to: %s", PinNo,(Output>0)?"high":"low");
	}
}

//****************************************************************************
// GetInput - Gets level of output pin.
//			Demonstrates the usage of KomDioGetOutput
void GetOutput()
{	
	UCHAR Output;
	UCHAR PinNo;
	DWORD status;

	printf("\nChoose which Output (0..%d): ",Outputs-1);
	scanf_s("%d",&PinNo);
			
	printf("Getting Output #%d level... ", PinNo);
	status=KomDioGetOutput(PinNo, &Output);

	ExitIfError(status,TRUE);

	printf("Output #%d level is: %s", PinNo,(Output>0)?"high":"low");
}

//****************************************************************************
// GetRestoreOnRestart - Gets the RestoreOnRestart setting
//			Demonstrates the usage of KomDioGetRestoreStatus
void GetRestoreOnRestart()
{	BOOL fRestore;
	DWORD status;
	
	printf("\n\nGetting RestoreOnRestart mode... ");
	status=KomDioGetRestoreStatus(&fRestore);

	ExitIfError(status,TRUE);

	if (fRestore==TRUE)
		printf("\nRestoreOnRestart is ON.\n");
	else
		printf("\nRestoreOnRestart is OFF.\n");
}

//****************************************************************************
// SetRestoreOnRestart - Sets the RestoreOnRestart setting
//			Demonstrates the usage of KomDioSetRestoreStatus
void SetRestoreOnRestart()
{	char ch;
	DWORD status;

	printf("Enable RestoreOnRestart? (Y for yes): ");
	getchar();
	ch=getchar();

	if ((ch=='y') || (ch=='Y'))
	{
		printf("Turning RestoreOnRestart ON... ");
		status=KomDioSetRestoreStatus(TRUE);
	}
	else	//turn off
	{
		printf("Turning RestoreOnRestart OFF... ");
		status=KomDioSetRestoreStatus(FALSE);
	}

	ExitIfError(status,TRUE);
}

//End of file
