/******************************************************************************
** Copyright 2015 MBARI - Monterey Bay Aquarium Research Institute
*******************************************************************************
*******************************************************************************
** Summary  : Routines to access HIH6130 Temp/Humidity Sensor via I2C
** Filename : hih6130.c
** Author   : Luke Coletti
** Project  :
** Version  : 1.0
** Compiler : MetroWerks Code Warrior v8.3
** Created  : 12/15/14
** Archived :
*******************************************************************************
** Modification History:
** 01/02/15 : CF2 Port, LJC
*******************************************************************************/

#include        <cfxpico.h>             // Persistor BIOS and I/O Definitions
#include        <math.h>
#include	<io_i2c.h>
#include        <hih6130.h>


int HIH6130_ReadTempHumidity(float *Temp, float *Humi)
{
	uchar status;
	ushort t_cts, h_cts;

	*Temp = *Humi = -1.0;

	if( HIH6130_ReadSensor( &t_cts, &h_cts, &status ) == I2C_ACK ) {
		*Temp = ((float)t_cts / (float)HIH6130_FS_CTS) * 165.0 - 40.0;
		*Humi = ((float)h_cts / (float)HIH6130_FS_CTS) * 100.0;
	}

	return( status );   // SEE HIH6130 STATUS CODES IN HIH6130.H

}


int HIH6130_ReadSensor(ushort *Temp_cts, ushort *Hum_cts, uchar *Status)
{
	uchar Hum_h, Hum_l, Temp_h, Temp_l;
	int ret;


	I2C_Select( I2C_BUS_SYS );

	I2C_Start();                                            //I2C Start (S)
	ret =  I2C_WrByte( HIH6130_ADDR_WR );           //Send Slave Addr + Wr to Request Measurement
	I2C_Stop();                                             //I2C Stop (P)
	RTCDelayMicroSeconds( 75000 );                                          //Allow 75ms to complete Measurement Cycle (36.65ms is spec)
	I2C_Start();                                            //I2C Start (S)
	ret &= I2C_WrByte( HIH6130_ADDR_RD );           //Send Slave Addr + Rd to Fetch Measurment Data
	I2C_RdByte( &Hum_h );                                                           //Read Humidity High Byte
	I2C_WrAck();
	I2C_RdByte( &Hum_l );                                                   //Read Humidity Low  Byte
	I2C_WrAck();
	I2C_RdByte( &Temp_h );                                                  //Read Temperature High Byte
	I2C_WrAck();
	I2C_RdByte( &Temp_l );                                                  //Read Temperature Low  Byte
	I2C_WrNak();
	I2C_Stop();                                             //I2C Stop (P)

	I2C_Select( I2C_BUS_DISABLE );

	if( ret == I2C_ACK ) {                                                          //If device ACK'd every transmitted byte, then we're in good shape
		*Status = (uchar)( (Hum_h >> 6) & 0x03 );
		*Hum_cts  = (ushort)( (((ushort)(Hum_h & 0x3f)) << 8) | Hum_l );
		*Temp_cts = (ushort)( ((((ushort)Temp_h) << 8) | Temp_l) >> 2 );
	}
	else
		ret = ERROR;

	return( ret );

}

//
// calculates dew point
// input:   humidity [%RH], temperature [�C]
// output:  dew point [�C]
//
float calc_dewpoint(float h, float t)
{
	float logEx,dew_point;

	logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);

	dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);

	return( dew_point );

}

