/* USER CODE BEGIN 1 */
/*
 * HAL_I2C_TMP75.c
 *
 *  Created on: 10. 3. 2018
 *      Author: Dušan Benda
 */

#include "HAL_I2C_TMP75.h"

#define 	address_temp_sensor_1	(0x48)
#define 	address_temp_sensor_2	(0x49)
#define		address_temp_sensor_3	(0x4A)
#define 	temperature_register	(0x00)
#define		configuration_register	(0x01)
#define		temp_low_register		(0x02)
#define		temp_high_register		(0x03)



uint8_t address_temp_sensor[3] = {0x48,0x49,0x4A};


extern I2C_HandleTypeDef hi2c1;

uint8_t HAL_return_temperature_TMP75(uint8_t channel)
{
	uint16_t temperature = 0;
	uint8_t address = 0;
	uint8_t temp_data[2];
	uint8_t *p_data;
	p_data = temp_data;
	if ((channel == 1) || (channel == 2) || (channel == 3))
	{
		switch (channel)
		{
			case 1: address = address_temp_sensor_1;
				break;
			case 2: address = address_temp_sensor_2;
				break;
			case 3: address = address_temp_sensor_3;
				break;
		}
		if(HAL_I2C_IsDeviceReady(&hi2c1,(address<<1), 1, 15) == HAL_OK)
		{
			HAL_I2C_Mem_Read(&hi2c1,(address<<1),temperature_register,2,p_data,2,100);
			temperature = (((temp_data[0] << 8) | (temp_data[1])) >> 4);
		}
	}
	return (temperature >> 4);
}

uint8_t HAL_return_configuration_TMP75(uint8_t channel)
{
	uint8_t configuration = 0;
	uint8_t address = 0;
	if ((channel == 1) || (channel == 2) || (channel == 3))
	{
		switch (channel)
		{
			case 1: address = address_temp_sensor_1;
				break;
			case 2: address = address_temp_sensor_2;
				break;
			case 3: address = address_temp_sensor_3;
				break;
		}
		if(HAL_I2C_IsDeviceReady(&hi2c1,(address<<1), 1, 15) == HAL_OK)
		{
			HAL_I2C_Mem_Read(&hi2c1,(address<<1),configuration_register,1,&configuration,1,100);
		}
	}
	return configuration;
}

void HAL_set_configuration_TMP75(uint8_t channel)
{
	uint8_t address = 0;
	uint8_t configuration = 0x00 | // shut down deactivated
							0x00 | // thermostat mode deactivated
							0x00 | // polarity deactivated
							0x10 | // fault queue 4x consecutive faults
							0x00 | // resolution 9-bit
							0x00;  // one shot deactivated

	if ((channel == 1) || (channel == 2) || (channel == 3))
	{
		switch (channel)
		{
			case 1: address = address_temp_sensor_1;
				break;
			case 2: address = address_temp_sensor_2;
				break;
			case 3: address = address_temp_sensor_3;
				break;
		}
		if(HAL_I2C_IsDeviceReady(&hi2c1,(address<<1), 1, 15) == HAL_OK)
		{
			HAL_I2C_Mem_Write(&hi2c1,(address<<1),configuration_register,1,&configuration,1,100);
		}
	}
}

void HAL_set_high_temperature_TMP75(uint8_t channel, uint8_t temperature)
{
	if ((temperature <= 128) && (temperature >= 0))
	{
		uint8_t address = 0;
		uint8_t temp_data[2];
		uint8_t *p_data;
		p_data = temp_data;
		temp_data[0] = temperature;
		temp_data[1] = 0;
		if ((channel == 1) || (channel == 2) || (channel == 3))
		{
			switch (channel)
			{
				case 1: address = address_temp_sensor_1;
					break;
				case 2: address = address_temp_sensor_2;
					break;
				case 3: address = address_temp_sensor_3;
					break;
			}
			if(HAL_I2C_IsDeviceReady(&hi2c1,(address<<1), 1, 15) == HAL_OK)
			{
				HAL_I2C_Mem_Write(&hi2c1,(address<<1),temp_high_register,1,p_data,2,100);
			}
		}
	}
}

void HAL_set_low_temperature_TMP75(uint8_t channel, uint8_t temperature)
{
	if ((temperature <= 128) && (temperature >= 0))
	{
		uint8_t address = 0;
		uint8_t temp_data[2];
		uint8_t *p_data;
		p_data = temp_data;
		temp_data[0] = temperature;
		temp_data[1] = 0;
		if ((channel == 1) || (channel == 2) || (channel == 3))
		{
			switch (channel)
			{
				case 1: address = address_temp_sensor_1;
					break;
				case 2: address = address_temp_sensor_2;
					break;
				case 3: address = address_temp_sensor_3;
					break;
			}
			if(HAL_I2C_IsDeviceReady(&hi2c1,(address<<1), 1, 15) == HAL_OK)
			{
				HAL_I2C_Mem_Write(&hi2c1,(address<<1),temp_low_register,1,p_data,2,100);
			}
		}
	}
}

uint8_t HAL_return_high_temperature_TMP75(uint8_t channel)
{
	uint16_t temperature = 0;
	uint8_t address = 0;
	uint8_t temp_data[2];
	uint8_t *p_data;
	p_data = temp_data;
	if ((channel == 1) || (channel == 2) || (channel == 3))
	{
		switch (channel)
		{
			case 1: address = address_temp_sensor_1;
				break;
			case 2: address = address_temp_sensor_2;
				break;
			case 3: address = address_temp_sensor_3;
				break;
		}
		if(HAL_I2C_IsDeviceReady(&hi2c1,(address<<1), 1, 15) == HAL_OK)
		{
			HAL_I2C_Mem_Read(&hi2c1,(address<<1),temp_high_register,1,p_data,2,10000);
			temperature = (((temp_data[0] << 8) | (temp_data[1])) >> 4);
		}
	}
	return (temperature >> 4);
}

uint8_t HAL_return_low_temperature_TMP75(uint8_t channel)
{
	uint16_t temperature = 0;
	uint8_t address = 0;
	uint8_t temp_data[2];
	uint8_t *p_data;
	p_data = temp_data;
	if ((channel == 1) || (channel == 2) || (channel == 3))
	{
		switch (channel)
		{
			case 1: address = address_temp_sensor_1;
				break;
			case 2: address = address_temp_sensor_2;
				break;
			case 3: address = address_temp_sensor_3;
				break;
		}
		if(HAL_I2C_IsDeviceReady(&hi2c1,(address<<1), 1, 15) == HAL_OK)
		{
			HAL_I2C_Mem_Read(&hi2c1,(address<<1),temp_low_register,1,p_data,2,100);
			temperature = (((temp_data[0] << 8) | (temp_data[1])) >> 4);
		}
	}
	return (temperature >> 4);
}
/* USER CODE END 1 */
