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

#include "stdafx.h"
#include "HmitrDemo.h"
#include "HmitrApi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

	LPCWSTR messg[] =
	{
		L"The ambient light value is   ",
		L"The Night threshold (nt) is  ",
		L"The Day threshold   (dt) is  ",
		L"Current Backlight level:     ",
		L"Night Backlight level(nbl):  ",
		L"Day Backlight level  (dbl):  ",
		L"LED No.:",
		L"Ambient temperature:"
	};


// The one and only application object

CWinApp theApp;

using namespace std;





void ChgBacklightLevel()
{
	int choice;
	int ret;
	int step;
	int cur_lvl = -1;
	int max_lvl = 250;
	int min_lvl = 1;
	int max_val = 0;
	int value, *pval;
	pval = &value;
	


	printf("HMITR Demo - Change Backlight Level:\n\n");

	do
	{

		//get current level setting
		ret = hmi_backlight_read(0,pval);
		if (ret == 0)
			cur_lvl = *pval;

		//get max level setting
		ret = hmi_backlight_read(2,pval);
		if (ret == 0)
			max_lvl = *pval;

		//get max level setting
		ret = hmi_amblight_read(1,pval);
		if (ret == 0)
			min_lvl = *pval;

		//ask for operation:
		printf("\n\nSelect:\n");
		printf("	1 Decrease Backlight\n");
		printf("	2 Increase Backlight\n");
		printf("	0 = back to main menue\n");
		
		do
		{
			scanf_s("%d",&choice);
		} while ((choice<0) || (choice>2));

		if (choice > 0)
		{
				do
				{
					printf("Current level: %d\n",cur_lvl);
					if(choice==2)
					{
						max_val = max_lvl-cur_lvl;
						printf("Increase backlight by (max %d)?: ",max_val);
					}
					else
					{
						max_val = cur_lvl-min_lvl;
						printf("Decrease backlight by (max %d)?: ",max_val);
					}
					scanf_s("%d",&step);
				} while ((step<0) || (step>max_val));

				if(step == 0)
					choice = 0;
		}

		switch (choice)
		{
			case 1:	
				ret = hmi_dec_brightness(step);
				if(ret == 0)
					printf("\nBacklight decreased - success!\n");
				else
					printf("\nHMITR error: %d  Last error: %d\n",ret,GetLastError());

				break;

			case 2:
				ret = hmi_inc_brightness(step);
				if(ret == 0)
					printf("\nBacklight increased - success!\n");
				else
					printf("\nHMITR error: %d  Last error: %d\n",ret,GetLastError());

				break;
				
				break;

			default:
				break;
		}
	} while (choice != 0);
}



void GetAmbientLight()
{
	
	int ret,i;
	int value, *pval;
	pval = &value;
    

	printf("HMITR Demo - Reading Light Values:\n\n");

			for (i=0;i<3;i++)
			{
	  			ret = hmi_amblight_read(i,pval);
			      if(ret == 0)
					wprintf_s(L"\n%s%d\n",messg[i],*pval);
				  else
					wprintf_s(L"\n%sHMITR error: %s\n",messg[i],comm_errors[ret*(-1)]);
			}

			for (i=0;i<3;i++)
			{
	  			ret = hmi_backlight_read(i,pval);
			      if(ret == 0)
					wprintf(L"\n%s%d\n",messg[i+3],*pval);
				  else
					wprintf(L"\n%sHMITR error: (%d) %s\n",messg[i],ret,comm_errors[ret*(-1)]);
			}
}


void SetLightValues()
{
	int val;
	int choice;
	int ret;

	printf("HMITR Demo - Set light thresholds:\n\n");

	do
	{
		ret = 0;
		
		GetAmbientLight();
		//ask for operation:
		printf("\nSelect:\n");
		printf("	1 Set Day backlight threshold   (dt)\n");
		printf("	2 Set Night backlight threshold (nt)\n");
		printf("	3 Set max Day backlight value   (dbl)\n");
		printf("	4 Set min Night backlight value (nbl)\n");
		printf("	0 = back to main menue\n");
		
		do
		{
			scanf_s("%d",&choice);
		} while ((choice<0) || (choice>4));



			switch (choice)
			{
				case 1:
					do
					{
						printf("Set Day threshold (dt) to (max 255)?: ");
						scanf_s("%d",&val);

					} while ((val > 0xFF) && (val < 0));

					ret = hmi_amblight_write(2, val);
							
					break;
				case 2:
					do
					{
						printf("Set Night threshold (nt) to (min 0)?: ");
						scanf_s("%d",&val);

					} while ((val > 0xFF) && (val < 0));

					ret = hmi_amblight_write(1, val);

					break;
	
				case 3:
					do
					{
						printf("Set max Day backlight value (dbl) to (max 255)?: ");
						scanf_s("%d",&val);

					} while ((val > 0xFF) && (val < 0));

					ret = hmi_backlight_write(2, val);

					break;

				case 4:
					do
					{
						printf("Set min Night backlight value (nbl) to (min 0)?: ");
						scanf_s("%d",&val);

					} while ((val > 0xFF) && (val < 0));

					ret = hmi_backlight_write(1, val);

					break;



				default:
					break;
			}
	
	} while (choice != 0);

	val = 0;

	if (ret != 0)
		wprintf(L"\nHMITR error: (%d) %s\n",ret,comm_errors[ret*(-1)]);
	
}

void GetLed()
{
	unsigned char i;
	int ret;
	int value, *pval;
	pval = &value;

	printf("HMITR Demo - Get present LED modes:\n\n");


			for (i=0;i<4;i++)
			{
	  			ret = hmi_led_read(i,pval);

				wprintf_s(L"\nLED %d:",i+1); 
			      if(ret == 0)
					  wprintf_s(L"  mode: %d\n",*pval);
				  else
					wprintf_s(L"\n HMITR error: %s\n",comm_errors[ret*(-1)]);
			}
	
}

void SetLed()
{
	int led;
	int mode;
	int ret;

	printf("HMITR Demo - Set LED mode:\n\n");

		    printf("Select desired LED\n");	
			printf("	1 = Pwr On LED \n");
			printf("	2 = Status LED \n");
			printf("	3 = Th LED \n");
			printf("	4 = GP LED\n");
			printf("	0 = back to main menue\n");
			do
			{
				scanf_s("%d",&led);
			} while ((led > 4) && (led < 0));


		if (led > 0)
		{
			
			wprintf_s(L"\nSelect mode for LED %d:\n",led);
			led = led-1;
			printf("	0 LED off\n");
			printf("	1 LED on\n");
			printf("	2 LED blinking\n");
			printf("	3 LED fast blinking\n");
			
			do
			{
				scanf_s("%d",&mode);
			} while ((mode<0) || (mode>3));

			ret = hmi_led_write(led,mode); 
			if (ret != 0)
				wprintf(L"\nHMITR error: (%d) %s\n",ret,comm_errors[ret*(-1)]);
			else
				wprintf(L"\nLED %d has been set to mode %d\n",led+1,mode);
		}
}

void ReadAmbTemp()
{
	int ret, value, *pval;
	pval = &value;
	printf("HMITR Demo - Read ambient temperature:\n\n");

	 ret = hmi_ambtemp_read(pval);
		if(ret == 0)
			wprintf_s(L"\n%s%d\n",messg[7],*pval);
		else
			wprintf_s(L"\n%s%d HMITR error: %s\n",messg[7],comm_errors[ret*(-1)]);

}

void ReadBoardId()
{
	int ret, value, *pval;
	pval = &value;
	printf("HMITR Demo - Board ID:\n\n");
	
	ret = hmi_boardident_read(0,pval);
		if(ret == 0)
			wprintf_s(L"\nBoard ID: %d\n",*pval);
		else
			wprintf_s(L"\nBoard ID: HMITR error: %s\n",comm_errors[ret*(-1)]);
	ret = hmi_boardident_read(1,pval);
		if(ret == 0)
			wprintf_s(L"\nHardware Function ID: %d\n",*pval);
		else
			wprintf_s(L"\nHardware Function ID: HMITR error: %s\n",comm_errors[ret*(-1)]);
	ret = hmi_boardident_read(2,pval);
		if(ret == 0)
			wprintf_s(L"\nFirmware Function ID: %d\n",*pval);
		else
			wprintf_s(L"\nFirmware Function ID: HMITR error: %s\n",comm_errors[ret*(-1)]);
	ret = hmi_boardident_read(3,pval);
		if(ret == 0)
			wprintf_s(L"\nFirmware Revision ID: %d\n",*pval);
		else
			wprintf_s(L"\nFirmware Revision ID: HMITR error: %s\n",comm_errors[ret*(-1)]);

}
static void WatchdogTriggerThread(void)
{
	
	int ret;

	do
	{
		ret = hmi_wd_trigger();
		// triggering watchdog
		if(ret != 0)
		{
			wprintf_s(L"\nERROR triggering watchdog: (%d) Last error: %d\n", ret,GetLastError());
			getwchar();
			return;
		}

		// let's sleep for 200ms 
		Sleep(200);
		
	}while(1);

	
}

void TriggerWdg()
{
	int ret, value, *pval;
	pval = &value;
	printf("HMITR Demo - Watchdog Test:\n\n");
	
	HANDLE	hTrigger = INVALID_HANDLE_VALUE;

	fflush(stdin);

	// start a trigger thread
	hTrigger = CreateThread((LPSECURITY_ATTRIBUTES)NULL, 0,
									 (LPTHREAD_START_ROUTINE)WatchdogTriggerThread,
									  NULL, 0, NULL);


		wprintf(L"\n Press Enter to cause a timeout...\n");

		getwchar();
		fflush(stdin);

		TerminateThread(hTrigger, 0);

		wprintf(L"\nTriggering watchdog stopped\n");

	ret = hmi_wdtim_write(5000);
		if(ret != 0)
			wprintf_s(L"\nWatchdog write timeout: HMITR error: %s\n",comm_errors[ret*(-1)]);

	ret = hmi_wdtim_read(pval);
		if(ret == 0)
		{
			wprintf_s(L"\nWatchdog timeout in : %d ms\n",*pval);
			wprintf_s(L"(Backlight will be switched of for a few seconds)\n",*pval);
		}
		else
			wprintf_s(L"\nWatchdog read timeout: HMITR error: %s\n",comm_errors[ret*(-1)]);


		Sleep(10000);

	ret = hmi_blonoff_write(1);
		if(ret == 0)
			wprintf_s(L"\nBacklight on again\n");
		else
			wprintf_s(L"\nBacklight on: HMITR error: %s\n",comm_errors[ret*(-1)]);
	return;	
}

void BuzzerCtrl()
{
	int choice, choice1,val;
	int ret, value, *pval;
	pval = &value;

	printf("HMITR Demo - Buzzer control:\n\n");

		    printf("Select \n");	
			printf("	1 Buzzer alarm \n");
			printf("	2 Keypress click \n");
			printf("	0 = back to main menue\n");
			do
			{
				scanf_s("%d",&choice);
			} while ((choice<0) || (choice>2));

			switch (choice)
			{
				case 1:
					printf("Buzzer alarm loudness \n");	
					printf("	1 read loudness \n");
					printf("	2 set loudness \n");
					printf("	0 = back to main menue\n");
					do
					{
						scanf_s("%d",&choice1);
					} while ((choice1<0) || (choice1>2));
					switch (choice1)
					{
					case 1:
						ret = hmi_buzzeralarm_read(pval);
						if(ret == 0)
							wprintf_s(L"\nBuzzer alarm loudness : %d \n",*pval);
						else
							wprintf_s(L"\nBuzzer alarm read: HMITR error: %s\n",comm_errors[ret*(-1)]);
						choice1 = 0;
						break;
					case 2:
						do
						{
							printf("Set Buzzer alarm loudness to (max 255)?: ");
							scanf_s("%d",&val);

						} while ((val > 0xFF) && (val < 0));

						ret = hmi_buzzeralarm_write(val);
						if(ret == 0)
							wprintf_s(L"\nBuzzer alarm loudness set to: %d \n",val);
						else
							wprintf_s(L"\nBuzzer alarm write: HMITR error: %s\n",comm_errors[ret*(-1)]);
						choice1 = 0;
						break;
	
					} while (choice1 != 0);
	
					choice = 0;
					break;

				case 2:
					printf("Keypress click loudness \n");	
					printf("	1 read loudness \n");
					printf("	2 set loudness \n");
					printf("	0 = back to main menue\n");
					do
					{
						scanf_s("%d",&choice1);
					} while ((choice1<0) || (choice1>2));
					switch (choice1)
					{
					case 1:
						ret = hmi_buzzerclick_read(pval);
						if(ret == 0)
							wprintf_s(L"\nKeypress click loudness : %d \n",*pval);
						else
							wprintf_s(L"\nKeypress click read: HMITR error: %s\n",comm_errors[ret*(-1)]);
						choice1 = 0;
						break;
					case 2:
						do
						{
							printf("Set Keypress click loudness to (max 255)?: ");
							scanf_s("%d",&val);

						} while ((val > 0xFF) && (val < 0));

						ret = hmi_buzzerclick_write(val);
						if(ret == 0)
							wprintf_s(L"\nKeypress click loudness set to: %d \n",val);
						else
							wprintf_s(L"\nKeypress click write: HMITR error: %s\n",comm_errors[ret*(-1)]);
						choice1 = 0;
						break;
	
					} while (choice1 != 0);
					choice = 0;
					break;

				default:
					break;
			} while (choice != 0);

}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{



	int     choice, ret;
	
    

		



	printf("HMITR Demo - demo application for HMITR Panel.\n\n");

		ret = hmiapi_open(ComPort);

		if (ret != 0 )
		{
			printf("Communication error - %d\n\n",ret);
			return 1;
		}
		printf("HMITR Com Port initilized!\n\n");

		ReadBoardId();
	do
	{
		//ask for operation:
		printf("\n\nMain Menu:\n");
		printf("  1 Change Brightness\n");
		printf("  2 Get light value settings\n");
		printf("  3 Set light thresholds\n");
		printf("  4 Get LED mode\n");
		printf("  5 Set LED mode\n");
		printf("  6 Read ambient temperature\n");
		printf("  7 Buzzer control\n");
		printf("  8 Watchdog test\n");
		printf("  0 Exit\n");
		
		do
		{
			scanf_s("%d",&choice);
		} while ((choice<0) || (choice>8));
		
		//perform chosen operation:
		switch (choice)
		{
			case 1:	
				ChgBacklightLevel();
				break;

			case 2:
				GetAmbientLight();
				break;

			case 3:
				SetLightValues();
				break;

			case 4:
				GetLed();
				break;

			case 5:
				SetLed();
				break;

			case 6:
				ReadAmbTemp();
				break;

			case 7:
				BuzzerCtrl();
				break;

			case 8:
				TriggerWdg();
				break;

			default:
				break;
		}

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

	if (ret == 0)
		hmiapi_close();

	}

	return nRetCode;
}
