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

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include "LM95245.h"
#include "LM95245_hw.h"

void InitValues(tLM95245Values* pValues)
{
	pValues->LclTempMSB		= 0;
	pValues->LclTempLSB		= 0;
	pValues->RemTempMSB		= 0;
	pValues->RemTempLSB		= 0;
	pValues->uRemTempMSB	= 0;
	pValues->uRemTempLSB	= 0;
	pValues->Selection		= 0;
	pValues->Value	        = 0;
	pValues->Error			= 0;
}

DWORD ReadTemperature(void)
{
	DWORD Status = ERROR_SUCCESS;
	tLM95245Values Temperatures;

	int   sel ;
	UCHAR Sensor;


	do
	{
		printf_s("\n     ========Read Temperatures=============\n");
		printf_s("        1 Read internal temperature\n");
		printf_s("        2 Read remote temperature\n");
		printf_s("        0 = Return\n\n");

		do {
			scanf_s("%d", &sel);
		} while ((sel < 0) || (sel > 2));

		InitValues(&Temperatures);

		switch (sel)
		{
		case 1:
			Sensor = SEL_TEMP_INT;

			// get internal temperature sensor value
			if(ERROR_SUCCESS != (Status = LM95245TempGet(Sensor,&Temperatures)))
			{
				printf("Error LM95245TempGet: %d\n", Status);
				return Status;
			}

				printf("\n+++++++++++++++Result:++++++++++++++++++++++++++++\n");
				printf("  signed Internal temperature MSB: 0x%x = %d\n",   Temperatures.LclTempMSB,Temperatures.LclTempMSB);
				printf("  signed Internal temperature LSB:      = %f\n\n", Temperatures.LclTempLSB);
				printf("++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");

			break;

		case 2:
			Sensor = SEL_TEMP_RMT;

			// get remote temperature sensor value
			if(ERROR_SUCCESS != (Status = LM95245TempGet(Sensor,&Temperatures)))
			{
				printf("Error LM95245TempGet: %d\n", Status);
				return Status;
			}
				printf("\n+++++++++++++++Result:++++++++++++++++++++++++++++\n");
				printf("    signed Remote   temperature MSB: 0x%x = %d\n", Temperatures.RemTempMSB,Temperatures.RemTempMSB);
				printf("    signed Remote   temperature LSB:      = %f\n", Temperatures.RemTempLSB);
				printf("    unsigned Remote temperature MSB: 0x%x = %d\n", Temperatures.uRemTempMSB,Temperatures.uRemTempMSB);
				printf("    unsigned Remote temperature LSB:      = %f\n", Temperatures.uRemTempLSB);
				printf("++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");

		
			break;

		default:
			break;
		}

	} while (sel != 0);
	return Status;
}

DWORD ReadLimit(void)
{
	DWORD Status = ERROR_SUCCESS;
	tLM95245Values Temperatures;



	InitValues(&Temperatures);
	
	Temperatures.Selection = SEL_LIMIT_RMT_TCRIT;

	// get remotel temperature limit value
	if(ERROR_SUCCESS != (Status = LM95245TempLimitGet(&Temperatures)))
	{
		printf("Error LM95245TempLimitGet: %d\n", Status);
		return Status;
	}
		printf("\n+++++++++++++++Result:++++++++++++++++++++++++++++\n");
		printf("Remote T_Crit Temp Limit: 0x%x = %d\n", Temperatures.uRemTempMSB,Temperatures.uRemTempMSB);
		printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");

	InitValues(&Temperatures);
	
	Temperatures.Selection = SEL_LIMIT_LCL_TCRIT;

	// get internal temperature limit value
	if(ERROR_SUCCESS != (Status = LM95245TempLimitGet(&Temperatures)))
	{
		printf("Error LM95245TempLimitGet: %d\n", Status);
		return Status;
	}
		printf("\n+++++++++++++++Result:++++++++++++++++++++++++++++\n");
		printf("Local T_Crit Temp Limit: 0x%x = %d\n", Temperatures.uRemTempMSB,Temperatures.uRemTempMSB);
		printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");

	return Status;
}

DWORD SetLimit(void)
{
	DWORD Status = ERROR_SUCCESS;
	tLM95245Values Temperatures;
	int   sel, val ;

	do
	{
		val = 0;

		printf_s("\n   ======Set Limits=================\n");
		printf_s("      1 Set local T_crit limit\n");
		printf_s("      2 Set remote T_crit limit\n");
		printf_s("      0 = Return\n\n");

		do {
			scanf_s("%d", &sel);
		} while ((sel < 0) || (sel > 2));

		InitValues(&Temperatures);
		
		switch (sel)
		{
		case 1:

			printf_s("  Set local T_crit limit to (max %d C) ?: ",LM95245_INT_TCRIT_MAX);
			do {
				scanf_s("%d", &val);
			} while ((val <= 0) || (val > LM95245_INT_TCRIT_MAX));

			Temperatures.Value = (UCHAR)val;			
			Temperatures.Selection = SEL_LIMIT_LCL_TCRIT;

			// set remotel temperature limit value
			if(ERROR_SUCCESS != (Status = LM95245TempLimitSet(&Temperatures)))
			{
				printf("Error LM95245TempLimitSet: %d\n", Status);
				printf("  Device Error: 0x%x\n", Temperatures.Error);
				return Status;
			}
			
			InitValues(&Temperatures);

			Temperatures.Selection = SEL_LIMIT_LCL_TCRIT;

			// get internal temperature limit value
			if(ERROR_SUCCESS != (Status = LM95245TempLimitGet(&Temperatures)))
			{
				printf("Error LM95245TempLimitGet: %d\n", Status);
				return Status;
			}
				printf("\n+++++++++++++++Result:++++++++++++++++++++++++++++\n");
				printf("Local T_Crit Temp Limit has been set to: 0x%x = %d\n", Temperatures.uRemTempMSB,Temperatures.uRemTempMSB);
				printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");

				break;
		case 2:

			printf_s("  Set remote T_crit limit to (max %d C) ?: ",LM95245_RMT_TCRIT_MAX);
			do {
				scanf_s("%d", &val);
			} while ((val <= 0) || (val > LM95245_RMT_TCRIT_MAX));

			Temperatures.Value = (UCHAR)val;			
			Temperatures.Selection = SEL_LIMIT_RMT_TCRIT;

			// set remote temperature limit value
			if(ERROR_SUCCESS != (Status = LM95245TempLimitSet(&Temperatures)))
			{
				printf("Error LM95245TempLimitSet: %d\n", Status);
				printf("  Device Error: 0x%x\n", Temperatures.Error);
				return Status;
			}
			
			InitValues(&Temperatures);

			Temperatures.Selection = SEL_LIMIT_RMT_TCRIT;

			// get internal temperature limit value
			if(ERROR_SUCCESS != (Status = LM95245TempLimitGet(&Temperatures)))
			{
				printf("Error LM95245TempLimitGet: %d\n", Status);
				return Status;
			}
				printf("\n+++++++++++++++Result:++++++++++++++++++++++++++++\n");
				printf("Remote T_Crit Temp Limit has been set to: 0x%x = %d\n", Temperatures.uRemTempMSB,Temperatures.uRemTempMSB);
				printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");

			break;

		default:
			break;
		}

	} while (sel != 0);

	return Status;
}

DWORD SetTempFilter(void)
{
	DWORD Status = ERROR_SUCCESS;
	tLM95245Values Temperatures;
	int   sel ;
	BOOL Mode;

	do
	{
		printf_s("\n   ======Set remote temperature filter =================\n");
		printf_s("      1 Set filter ON\n");
		printf_s("      2 Set filter OFF\n");
		printf_s("      0 = Return\n\n");

	do {
			scanf_s("%d", &sel);
		} while ((sel < 0) || (sel > 2));

		InitValues(&Temperatures);
		
		switch (sel)
		{
		case 1:
			Mode = TRUE;
			break;
		case 2:
			Mode = FALSE;
			break;
		default:
			break;
		}
		if (sel != 0)
		{
			// get internal temperature sensor value
			if(ERROR_SUCCESS != (Status = LM95245TempFilterEnable (Mode,  &Temperatures)))
			{
				printf("Error LM95245TempFilterEnable: %d\n", Status);
				return Status;
			}
			printf("\n+++++++++++++++Result:++++++++++++++++++++++++++++\n");
			if (Mode)
			printf("    Remote Temperature Filter has been set to ON\n");
			else
			printf("    Remote Temperature Filter has been set to OFF\n");
			printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");
		}
	} while (sel != 0);

		return Status;

}


int _tmain(int argc, _TCHAR* argv[])
{

	DWORD Status = ERROR_SUCCESS;
	int sel ;



	Status = LM95245Init();

	if(ERROR_SUCCESS != Status)
	{
		printf("Error Init: %d\n", Status);
		return Status;
	}

	do {
		Status = ERROR_SUCCESS;
        printf_s("\n	******************************\n");
		printf_s("	* Demo for LM95245 SMBus API *\n");
        printf_s("	******************************\n");

		printf_s("	  1 Read temperatures\n");
		printf_s("	  2 Read temperature limits\n");
		printf_s("	  3 Set temperature limits\n");
		printf_s("	  4 Set remote temperature filter\n");
		printf_s("	  0 = Exit\n\n");
        printf_s("	******************************\n");

		do {
			scanf_s("%d", &sel);
		} while ((sel < 0) || (sel > 4));

		switch(sel)
		{
		case 1:
			Status = ReadTemperature();
			break;
		case 2:
			Status = ReadLimit();
			break;
		case 3:
			Status = SetLimit();
			break;
		case 4:
			Status = SetTempFilter();
			break;

		default:
			break;
		}

	} while (sel != 0);



	return 0;
}

