// DemoLM73.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include "LM73.h"
#include "LM73_hw.h"

 typedef struct {						
	double	ActualTemp;	
	DWORD   TempStatus;
	double  UpperLimit;	
	double	LowerLimit;	
	UCHAR	ResolValue;		
	DWORD 	AlertSetting;	
} tResults, *ptResults;

 void InitResults(tResults* pResults)
 {
	 pResults->ActualTemp = 0;
	 pResults->TempStatus = 0;
	 pResults->UpperLimit = 0;
	 pResults->LowerLimit = 0;
	 pResults->ResolValue = 0;
	 pResults->AlertSetting = 0;
 }

void InitValues(tLM73Values* pValues)
{
	pValues->Temperature	= 0;
	pValues->Selection		= 0;
	pValues->Value			= 0;
	pValues->Error			= 0;
}

DWORD ReadTemperature(tResults* pResults)
{
	DWORD Status = ERROR_SUCCESS;
	tLM73Values DeviceValues;


	// get LM73 temperature  value
	if(ERROR_SUCCESS != (Status = LM73TempGet(&DeviceValues)))
			{
				printf("Error LM73TempGet: %d\n", Status);
				return Status;
			}

	pResults->ActualTemp = DeviceValues.Temperature;

	return Status;
}

DWORD ReadLimit(tResults* pResults)
{
	DWORD Status = ERROR_SUCCESS;
	tLM73Values DeviceValues;



	InitValues(&DeviceValues);

			DeviceValues.Selection = SEL_UPPER_TEMP_LIMIT;

			// get upper temperature limit value
			if(ERROR_SUCCESS != (Status = LM73TempLimitGet(&DeviceValues)))
			{
				printf("Error LM73TempLimitGet: 0x%x\n", DeviceValues.Error);
				return Status;
			}
			pResults->UpperLimit =  DeviceValues.Temperature;

	InitValues(&DeviceValues);
			
			DeviceValues.Selection = SEL_LOWER_TEMP_LIMIT;

			// get lower temperature limit value
			if(ERROR_IO_DEVICE == (Status = LM73TempLimitGet(&DeviceValues)))
			{
				printf("Error LM73TempLimitGet: 0x%x\n", DeviceValues.Error);
				return Status;
			}

			pResults->LowerLimit = DeviceValues.Temperature;


	InitValues(&DeviceValues);

			// get temperature resolution value
			if(ERROR_IO_DEVICE == (Status = LM73TempResolutionGet(&DeviceValues)))
			{
				printf("Error LM73TempResolutionGet: 0x%x\n", DeviceValues.Error);
				return Status;
			}
			
			pResults->ResolValue = DeviceValues.Value;
	

	return Status;
}

DWORD SetLimit(void)
{
	DWORD Status = ERROR_SUCCESS;
	tLM73Values DeviceValues;
	tResults Results;
	int   sel,sel2 ;
	float temp;

	do
	{
		printf_s("\n   ======Set Limits=================\n");
		printf_s("      1 Set upper LM73 temp limit\n");
		printf_s("      2 Set lower LM73 temp limit\n");
		printf_s("      3 Set temp resolution\n");
		printf_s("      0 = Return\n\n");

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

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

				printf_s("  Set upper limit to (max %d C) ?: ",LM73_TEMP_MAX);
				do {
					scanf_s("%f", &temp);
				} while ((temp < LM73_TEMP_MIN) || (temp > LM73_TEMP_MAX));

				DeviceValues.inTemperature = temp;			
				DeviceValues.Selection = SEL_UPPER_TEMP_LIMIT;

				break;
		case 2:
				printf_s("  Set lower limit to (min %d C) ?: ",LM73_TEMP_MIN);
				do {
					scanf_s("%f", &temp);
				} while ((temp < LM73_TEMP_MIN) || (temp > LM73_TEMP_MAX ));

				DeviceValues.inTemperature = temp;			
				DeviceValues.Selection = SEL_LOWER_TEMP_LIMIT;
				
				break;

		case 3:
				printf_s("\n   ======Set temp resolution=================\n");
				printf_s("      1 11bit resolution\n");
				printf_s("      2 12bit resolution\n");
				printf_s("      3 13bit resolution\n");
				printf_s("      4 14bit resolution\n");
				printf_s("      0 = Return\n\n");

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

				if (sel > 0) 
				{
					DeviceValues.Selection = sel2;
				}
				else
					return 0;
			
			break;

		default:
			return 0;
		}

		if (sel < 3)
		{
			printf("Set temp limit to: %f  (0x%000x)\n",temp,temp);
			// set  temperature limit value
			if(ERROR_SUCCESS != (Status = LM73TempLimitSet(&DeviceValues)))
			{
					printf("Error LM73TempLimitSet: %d\n", Status);
					printf("  Device Error: 0x%x\n", DeviceValues.Error);
					return Status;
			}
			ReadLimit(&Results);
		}
		else if(sel == 3)
		{
			printf("Set resolution to: %d bit\n",sel2+10);
			// set  temperature resolution
			if(ERROR_SUCCESS != (Status = LM73TempResolutionSet(&DeviceValues)))
			{
					printf("Error LM73TempLimitSet: %d\n", Status);
					printf("  Device Error: 0x%x\n", DeviceValues.Error);
					return Status;
			}

		}

	} while (sel != 0);

	return Status;
}

DWORD SetAlert(void)
{
	DWORD Status;
	tLM73Values DeviceValues;
	int   sel ;


	InitValues(&DeviceValues);

	Status = LM73TempIsAlertEnabled(&DeviceValues);


    if(ERROR_IO_DEVICE == Status)
	{
		printf("Device error LM73TempIsAlertEnabled: %d\n", DeviceValues.Error);
		return Status;
	}
	else
		if (DeviceValues.Value == 1)
			printf("\n+++++ Alert is presently enabled! ++++++\n");
		else
			printf("\n+++++ Alert is presently disabled ++++++\n");
	

	do
	{
		printf_s("\n   ======Set Alert =================\n");
		printf_s("      1 Set Temperature Alert ON\n");
		printf_s("      2 Set Temperature Alert OFF\n");
		printf_s("      0 = Return\n\n");

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


		if (sel == 0)
			break;
		
		
		InitValues(&DeviceValues);

		DeviceValues.Selection = sel;

			// set temperature alert 
			if(ERROR_SUCCESS != (Status = LM73TempAlertSet(&DeviceValues)))
			{
				printf("Error LM73TempAlertSet: %d\n", DeviceValues.Error);
				return Status;
			}
			printf("\n+++++++++++++++Result:++++++++++++++++++++++++++++\n");
			if (sel == 1)
				printf("    Alert enable bit has been set to ON");
			else
				printf("    Alert enable bit has been set to OFF");
			printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");
		
	} while (sel != 0);

		return ERROR_SUCCESS;

}

DWORD GetTempStatus(tResults* pResults)
{

	DWORD Status;
	tLM73Values DeviceValues;


	InitValues(&DeviceValues);

	Status = LM73TempStatusGet(&DeviceValues);
	if(ERROR_SUCCESS != (Status = LM73TempStatusGet(&DeviceValues)))
	{
		printf("Error LM73TempStatusGet: %d\n", DeviceValues.Error);
		return Status;
	}
	pResults->TempStatus = DeviceValues.Value;

	return Status;
}

void DisplayData(void)
{
	tResults Results;
	tLM73Values DeviceValues;
	DWORD Status;

	InitResults(&Results);
	
	
	

	GetTempStatus(&Results);

		printf("\n     +++++++++++++++  LM73 data:+++++++++++++++++++++++++\n");
		printf("     |                                                  |\n");
	if (Results.TempStatus == HTEMP)
		printf("     | Temperature status:      Temperature is too high |\n");
	else if (Results.TempStatus == LTEMP)
		printf("     | Temperature status:      Temperature is too low  |\n");
	else if (Results.TempStatus == NTEMP)
		printf("     | Temperature status:      Temperature is in limits|\n");
	else 
		printf("     | Temperature status:      unknown                 |\n");

		printf("     +--------------------------------------------------+\n");

	InitValues(&DeviceValues);
	Status = LM73TempIsAlertEnabled(&DeviceValues);
	if (ERROR_SUCCESS == Status)
		Results.AlertSetting = DeviceValues.Value;

	if (Results.AlertSetting == 1)
		printf("     | Alert status:            enabled!                |\n");
	else if (Results.AlertSetting == 2)
		printf("     | Alert status:            disabled                |\n");
	else
		printf("     | Alert status:            unknown                |\n");
	
	ReadTemperature(&Results);	

		printf("     +--------------------------------------------------+\n");
		printf("     | Actual LM73 Temperature:%f                 \n", Results.ActualTemp);

	ReadLimit(&Results);

		printf("     +--------------------------------------------------+\n");
		printf("     | Upper LM73 Temp Limit:  %f                 \n", Results.UpperLimit);
		printf("     +--------------------------------------------------+\n");
		printf("     | Lower LM73 Temp Limit:  %f                 \n", Results.LowerLimit);
		printf("     +--------------------------------------------------+\n");
		printf("     | LM73 Temp Resolution:   %d bit                   \n", Results.ResolValue);
		printf("     ++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n");

}


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

	DWORD Status = ERROR_SUCCESS;
	int sel ;


	printf("Init LM73\n");

	Status = LM73Init();

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

	

	do {
		Status = ERROR_SUCCESS;
        printf_s("\n	******************************\n");
		printf_s("	* Demo for LM73 SMBus API *\n");
        printf_s("	******************************\n");
		printf_s("	  1 Read LM73 data \n");
		printf_s("	  2 Set temperature limits / -resolution\n");
		printf_s("	  3 Set temperature alert on/off\n");
		printf_s("	  0 = Exit\n\n");
        printf_s("	******************************\n");

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

		switch(sel)
		{
		case 2:
			Status = SetLimit();
			break;
		case 3:
			SetAlert();
			break;
		case 1:
			 DisplayData();
			break;
		}

	} while (sel != 0);



	return 0;
}


