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

 DESCRIPTION
       Code for the MCP9808 temperature sensor

 USAGE
       
*****************************************************************************/
#include "mcp9808.h"

void mcp9808_set_conf(ADDRESS addr, unsigned short ConfR, char* data){
	data[0] = MCP9808_CONF_REG;
	data[1] = (unsigned char)ConfR;
	data[2] = (unsigned char)(ConfR>>8);
	IICTransmit(addr,3,data);
}
unsigned short mcp9808_read_conf(ADDRESS addr, char* data){
	data[0] = MCP9808_CONF_REG;
	IICTransmit(addr,1,data);
	delay(1000);
	IICReceive(addr,2,data);
	return (unsigned short)(data[0]<<8 | data[1]);
}

void mcp9808_set_tupper(ADDRESS addr, unsigned short ConfR, char* data){
	data[0] = MCP9808_TUPPER_REG;
	data[1] = (unsigned char)ConfR;
	data[2] = (unsigned char)(ConfR>>8);
	IICTransmit(addr,3,data);
}

unsigned short mcp9808_read_tupper(ADDRESS addr, char* data){
	data[0] = MCP9808_TUPPER_REG;
	IICTransmit(addr,1,data);
	delay(1000);
	IICReceive(addr,2,data);
	return (unsigned short)(data[0]<<8 | data[1]);
}

void mcp9808_set_tlower(ADDRESS addr, unsigned short ConfR, char* data){
	data[0] = MCP9808_TLOWER_REG;
	data[1] = (unsigned char)ConfR;
	data[2] = (unsigned char)(ConfR>>8);
	IICTransmit(addr,3,data);
}

unsigned short mcp9808_read_tlower(ADDRESS addr, char* data){
	data[0] = MCP9808_TLOWER_REG;
	IICTransmit(addr,1,data);
	delay(1000);
	IICReceive(addr,2,data);
	return (unsigned short)(data[0]<<8 | data[1]);
}

void mcp9808_set_tcrit(ADDRESS addr, unsigned short ConfR, char* data){
	data[0] = MCP9808_TCRIT_REG;
	data[1] = (unsigned char)ConfR;
	data[2] = (unsigned char)(ConfR>>8);
	IICTransmit(addr,3,data);
}

unsigned short mcp9808_read_tcrit(ADDRESS addr, char* data){
	data[0] = MCP9808_TCRIT_REG;
	IICTransmit(addr,1,data);
	delay(1000);
	IICReceive(addr,2,data);
	return (unsigned short)(data[0]<<8 | data[1]);
}

#ifdef __NO_FLOAT__
decimal mcp9808_readTemp_decimal(ADDRESS addr, char* data){
	data[0] = MCP9808_TEMP_REG;
	IICTransmit(addr,1,data);
	delay(500);
	IICReceive(addr,2,data);
	if(data[0] & data[1] == 0xFF) return IIC_INVALID_DATA;
	if (data[0] & MCP9808_TEMP_MSB_TAvsTCRIT_MASK){	/* TA vs. TCRIT */
	}
	if (data[0] & MCP9808_TEMP_MSB_TAvsTUPPER_MASK){	/* TA > TUPPER */
	}
	if (data[0] & MCP9808_TEMP_MSB_TavsTLOWER_MASK){	/* TA < TLOWER */
	}
	data[0] &= MCP9808_TEMP_MSB_FLAG_MASK;	/* Clear flag bits */
	if ((data[0] & MCP9808_TEMP_MSB_SIGN_MASK) == MCP9808_TEMP_MSB_SIGN_MASK){
		data[0] &= 0x0F;	/* Clear flag bits */
		return (decimal) (((((long int)(data[0]<<8 | (data[1]))) * 100) / 16) - 25600);
	}else{
		data[0] &=  0x0F;	/* Clear flag bits */
		return (decimal) ((((unsigned long int)(data[0]<<8 | (data[1]))) * 100) / 16);
	}
}
char mcp9808_readTemp_char(ADDRESS addr, char* data){
	return (char)(mcp9808_readTemp_decimal(addr, data)/100);
}
#else
float mcp9808_readTemp_float(unsigned char addr){
	int i = 0;
	data[0] = MCP9808_TEMP_REG;
	IICTransmit(addr,1,data);
	IICReceive(addr,2,data);
	if(data[0] & data[1] == 0xFF) return 9999;
	if (data[0] & MCP9808_TEMP_MSB_TAvsTCRIT_MASK){
	}
	if (data[0] & MCP9808_TEMP_MSB_TAvsTUPPER_MASK){
	}
	if (data[0] & MCP9808_TEMP_MSB_TavsTLOWER_MASK){
	} 
	data[0] &= MCP9808_TEMP_MSB_FLAG_MASK;
	if ((data[0] & MCP9808_TEMP_MSB_SIGN_MASK) == MCP9808_TEMP_MSB_SIGN_MASK){
		data[0] &= 0x0F;
		return (float) (((unsigned int)(data[0]<<8 | (data[1]))) / 16.0) - 256.0;
	}else{
		data[0] &=  0x0F;
		return (float) (((unsigned int)(data[0]<<8 | (data[1]))) / 16.0);
	}
}

char mcp9808_readTemp_char(unsigned char addr){
	return (char) mcp9808_readTemp_float(addr);
}
#endif

unsigned short mcp9808_read_mnf_id(ADDRESS addr, char* data){
	data[0] = MCP9808_MNF_ID_REG;
	IICTransmit(addr,1,data);
	delay(1000);
	IICReceive(addr,2,data);
	return (unsigned short)(data[0]<<8 | data[1]);
}

unsigned short mcp9808_read_dev_id(ADDRESS addr, char* data){
	data[0] = MCP9808_DEV_ID_REG;
	IICTransmit(addr,1,data);
	delay(1000);
	IICReceive(addr,2,data);
	return (unsigned short)(data[0]<<8 | data[1]);
}

void mcp9808_set_res(ADDRESS addr, unsigned char ConfR, char* data){
	data[0] = MCP9808_RES_REG;
	data[1] = ConfR;
	IICTransmit(addr,2,data);
}
unsigned char mcp9808_read_res(ADDRESS addr, char* data){
	data[0] = MCP9808_RES_REG;
	IICTransmit(addr,1,data);
	delay(1000);
	IICReceive(addr,2,data);
	return data[0];
}

