#include "hdc1050.h"
#include "i2c.h"
#include "hardware.h"
char temp[3];
char humi[3];

void HDC1050_config(uint8_t conf)
{
	uint8_t config[] = {HDC1050_CONF_REG_ADDR, conf, 0};
	I2CMasterWriteBuf(HDC1050_SLAVE_ADDRESS, config, 3);
}
	
void HDC1050_requestConversion()                // request new temp/humi conversion
{
    uint8_t reqC[] = {0};                        // buffer to initiate single temp/humi conversion           
    I2CMasterWriteBuf(HDC1050_SLAVE_ADDRESS, reqC, 1);
}

void HDC1050_getTempHumi()                      // read temp/humi data from sensor
{
	uint8_t tmp;
    uint8_t reqR[5];							// receive buffer for temp/humi 
	reqR[0] = HDC1050_TEMP_REG_ADDR;	
    I2CMasterReadBuf(HDC1050_SLAVE_ADDRESS, reqR, 4);
    tmp = ((((reqR[0] << 8) + reqR[0])*165 + 0x8000) >> 16) - 40;	// compute temp in Celcius
//    temp = 9*temp/5 + 32;  	// convert temp to Fahrenheit
	temp[0] = (tmp / 10) + '0';
	temp[1] = (tmp % 10) + '0';
	temp[2] = 0;
    tmp = ((((reqR[2] << 8) + reqR[3])*100 + 0x8000) >> 16) - 16;	// compute humi
    if (tmp > 99) tmp = 99;
	humi[0] = (tmp / 10) + '0';
	humi[1] = (tmp % 10) + '0';
	humi[2] = 0;
}    

