/****************************************************************************
 Title	:	TERMINAL FOR VISUALIZATION AND CONTROL CONDITIONS IN HOUSE
 Author	:	Patrik Szalay <xszala01@stud.feec.vutbr.cz>
 File	:	temp_meas.c
 Software:	CodeWarrior Development Studio Version: 10.6 Build Id:140329
 Target	:	MC9S08LH64

 DESCRIPTION
       Code for measuring algorithms

 USAGE
       
*****************************************************************************/

#include "temp_meas.h"


decimal measure(SCIRegAddress device)
{
	decimal value = MEAS_ERROR;
	byte data[4], measTimeOutCount = 0;
	bool measError = true;
	{
		switch(device)
		{
			case LM75_1: value = lm75_readTemp_decimal(LM75_1_ADDR, data); break;
			case LM75_2: value = lm75_readTemp_decimal(LM75_2_ADDR,data); break;
			case MCP9808_1: value = mcp9808_readTemp_decimal(MCP9808_1_ADDR,data); break;
			case MCP9808_2: value = mcp9808_readTemp_decimal(MCP9808_2_ADDR,data); break;
			case MCP9808_3: value = mcp9808_readTemp_decimal(MCP9808_3_ADDR,data); break;
			case HTU21D_TEMP: value = htu21d_readTemp_decimal(HTU21D_ADDR, data); break;
			case HTU21D_HUM: value = htu21d_readHum_decimal(HTU21D_ADDR, data); break;
			default: break;
		}
		if(value == IIC_INVALID_DATA || value == CRC_ERR)
		{
			measTimeOutCount++;
		}
		else
		{
			measError = false;
		}
	}while(!measError || !(measTimeOutCount >= MEAS_TIMEOUTCOUNT))
	
	if (measError)
	{
		return MEAS_ERROR;
	}
	else
	{
		return value;
	}
}




