// KomLEDDemo.cpp : Demo application for KomLed API.
//

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

UCHAR leds;	//number of LEDs in the system

//function declarations:
void GetInfo();					//Get available modes and colors for specified LED
void GetLed();					//Get mode and color of one specified LED
void SetLed();					//Set mode and color of one specified LED
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 0x%08x (%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("KomLEDDemo - demo application for KomLED API library.\n\n");
	
	//get available leds
	printf("Available LEDs: ");
	status=KomLedGetNumOfLeds(&leds);
	ExitIfError(status);

	//printf them out
	printf("%d (indices: 0..%d),",leds,leds-1);	

	do
	{
		//ask for operation:
		printf("\n\nOperation:\n 1. Get LED info,\n 2. Get LED status,\n 3. Set LED status,\n 4. Get RestoreOnRestart mode,\n 5. Set RestoreOnRestart mode,\n 0. Exit,\nYour choice: ");
		scanf("%d",&choice);
		
		//perform chosen operation:
		switch (choice)
		{
			case 1:	
				GetInfo();
				break;

			case 2:
				GetLed();
				break;

			case 3:
				SetLed();
				break;

			case 4:
				GetRestoreOnRestart();
				break;

			case 5:
				SetRestoreOnRestart();
				break;

			default:
				break;
		}

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

	return 0;
}

//****************************************************************************
// GetInfo - Gets available modes and colors for a chosen LED.
//			Demonstrates the usage of KomLedGetInfo
void GetInfo()
{	int led;
	DWORD status;
	KomLedInfo info;

	printf("\nChoose which LED (0..%d): ",leds-1);
	scanf("%d",&led);
			
	printf("Getting LED #%d info... ",led);
	status=KomLedGetInfo((UCHAR)led,&info);

	ExitIfError(status,TRUE);

	//printf them out
	printf("LED #%d name is: %s\n",info.ledIdx,info.ledName);

	printf("Available modes: OFF");
	if ((info.availModes)&KOMLED_MODE_ON)
		printf(" ON");
	if ((info.availModes)&KOMLED_MODE_SYS)
		printf(" SYSTEM");
	if ((info.availModes)&KOMLED_MODE_BLINK)
		printf(" BLINKING");
	if ((info.availModes)&KOMLED_MODE_BLINK_FAST)
		printf(" FAST_BLINKING");

	printf("\nAvailable colors:");
	if ((info.availColors)&KOMLED_COLOR_GREEN)
		printf(" green");
	if ((info.availColors)&KOMLED_COLOR_RED)
		printf(" red");
	if ((info.availColors)&KOMLED_COLOR_AMBER)
		printf(" amber\n\n");
}

//****************************************************************************
// GetLed - Gets current mode and color of a chosen LED.
//			Demonstrates the usage of KomLedGet
void GetLed()
{	int led;
	ULONG mode,color;
	DWORD status;

	printf("\nChoose which LED (0..%d): ",leds-1);
	scanf("%d",&led);
			
	printf("Getting LED #%d mode... ",led);
	status=KomLedGet((UCHAR)led,&mode,&color);

	ExitIfError(status,TRUE);

	printf("LED #%d mode is: ",led);
	switch (mode)
	{	
		case KOMLED_MODE_OFF:
			printf("OFF");
			break;

		case KOMLED_MODE_ON:
			printf("ON");
			break;

		case KOMLED_MODE_SYS:
			printf("SYSTEM");
			break;

		case KOMLED_MODE_BLINK:
			printf("BLINKING");
			break;

		case KOMLED_MODE_BLINK_FAST:
			printf("FAST_BLINKING");
			break;

		default:
			printf("UNKNOWN");
	}

	//if mode is OFF or SYSTEM, color is undefined.
	if ((mode!=KOMLED_MODE_OFF) && (mode!=KOMLED_MODE_SYS))
	{
		printf("\n\nLED #%d color is: ",led);

		switch (color)
		{
			case KOMLED_COLOR_GREEN:
				printf("GREEN");
				break;

			case KOMLED_COLOR_RED:
				printf("RED");
				break;

			case KOMLED_COLOR_AMBER:
				printf("AMBER");
				break;

			default:
				printf("UNKNOWN");
		}
	}
}

//****************************************************************************
// SetLed - Sets mode and color of a chosen LED.
//			Demonstrates the usage of KomLedSet
void SetLed()
{	int led;
	ULONG mode,color=0;
	DWORD status;

	printf("\nChoose which LED (0..%d): ",leds-1);
	scanf("%d",&led);

	printf("\nModes:\n 1. OFF\n 2. ON\n 3. SYSTEM\n 4. BLINKING\n 5. FAST_BLINKING\nChoose mode: ");
	scanf("%d",&mode);

	if ((mode!=1) && (mode!=3))
	{
		printf("\nColors:\n 1. GREEN\n 2. RED\n 3. AMBER\nChoose color: ");
		scanf("%d",&color);
	}

	//convert the values to API values:
	switch (mode)
	{
		case 1:
			mode=KOMLED_MODE_OFF;
			break;

		case 2:
			mode=KOMLED_MODE_ON;
			break;

		case 3:
			mode=KOMLED_MODE_SYS;
			break;

		case 4:
			mode=KOMLED_MODE_BLINK;
			break;

		case 5:
			mode=KOMLED_MODE_BLINK_FAST;
			break;

		default:
			break;
			//do nothing - this way we are able to test invalid parameter conditions
	}

	//if mode is OFF or SYSTEM, color is undefined.
	if ((mode!=KOMLED_MODE_OFF) && (mode!=KOMLED_MODE_SYS))
	{
		switch (color)
		{
			case 1:
				color=KOMLED_COLOR_GREEN;
				break;

			case 2:
				color=KOMLED_COLOR_RED;
				break;

			case 3:
				color=KOMLED_COLOR_AMBER;
				break;

			default:
				break;
				//do nothing - this way we are able to test invalid parameter conditions
		}
	}
			
	printf("Setting LED #%d mode... ",led);
	status=KomLedSet(led,mode,color);

	ExitIfError(status);

	printf("LED #%d mode set correctly",led);
}

//****************************************************************************
// GetRestoreOnRestart - Gets the RestoreOnRestart setting
//			Demonstrates the usage of KomLedGetRestoreStatus
void GetRestoreOnRestart()
{	BOOL fRestore;
	DWORD status;
	
	printf("\n\nGetting RestoreOnRestart mode... ");
	status=KomLedGetRestoreStatus(&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 KomLedSetRestoreStatus
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=KomLedSetRestoreStatus(TRUE);
	}
	else	//turn off
	{
		printf("Turning RestoreOnRestart OFF... ");
		status=KomLedSetRestoreStatus(FALSE);
	}

	ExitIfError(status,TRUE);
}

//End of file
