#include "stm8s.h"
#include "htu21.h"
//#include "i2c.h"
#include "i2c_master_poll.h"
#include "delay.h"

#define HTU_T_MEASURE 0xF3
#define HTU_H_MEASURE 0xF5

static uint8_t buf[3];
static const uint8_t addr = 0x40;
//static t_i2c_status i2cs;
static uint8_t i2cs;
static uint32_t val;

static uint8_t CalcSht21Crc(uint8_t *data, uint8_t nbrOfBytes);

htu_err_t htu_GetTemperature(int16_t * temperature) {
  i2cs = 0;//i2c_wr_reg(addr, HTU_T_MEASURE);
  buf[0] = HTU_T_MEASURE;
  I2C_WriteBytes(addr, 1, buf);
  if (i2cs == 0) {
    Delay(90);
    //i2c_rd_reg(addr, buf);
    I2C_ReadBytes(addr, 3, buf);
//    if(buf[2] == CalcSht21Crc(buf,2)) {
      val = buf[0];
      val <<= 8;
      val |= (buf[1] & 0xfc);
      // Temp = val * 175.72 / 2^16 - 46.85
      val *= 4393; // 175.72/4*100
      val *= 100; // for .xx degree
      val += 819200; // for round
      val /= 1638400; // 2^16/4*100
      *temperature = val - 4685;
      return HTU_OK;
//    } else {
//      *temperature = 1;
//      return HTU_CRC_Temp;
//    }
  } else {
    *temperature = 777;
    return HTU_I2C;
  }
}

htu_err_t htu_GetHumidity(uint16_t * humidity) {
  i2cs = 0;//i2c_wr_reg(addr, HTU_H_MEASURE);
  buf[0] = HTU_H_MEASURE;
  I2C_WriteBytes(addr, 1, buf);
  if (i2cs == 0) {
    Delay(60);
    //i2c_rd_reg(addr, buf);
    I2C_ReadBytes(addr, 3, buf);
//    if(buf[2] == CalcSht21Crc(buf,2)) {
      val = buf[0];
      val <<= 8;
      val |= (buf[1] & 0xfc);
      // Humidity = val * 125 / 2^16 - 6
      val *= 125;
      val *= 10; // for .x %H
      val += 32768; // for round
      val /= 65536;
      *humidity = val - 60;
      return HTU_OK;
//    } else {
//      *humidity = 1;
//      return HTU_CRC_Humidity;
//    }
  } else {
    *humidity = 555;
    return HTU_I2C;
  }
}

static uint8_t CalcSht21Crc(uint8_t *data, const uint8_t nbrOfBytes) {
	// CRC
	//const u16t POLYNOMIAL = 0x131; //P(x)=x^8+x^5+x^4+1 = 100110001
	
	uint8_t byteCtr,bit,crc;

	crc = 0;

	//calculates 8-Bit checksum with given polynomial
	for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
	{ 
		crc ^= (data[byteCtr]);
		for (bit = 8; bit > 0; --bit)
		{
			if (crc & 0x80) crc = (crc << 1) ^ 0x131;
				else 		crc = (crc << 1);
		}
	}
	return(crc);
}
