/**************************************************************************************************
	Filename:       Sensor.c

	Description:    Functions to read Temperature and Humidity Sensor
					Data
					
	Copyright 2013, Unizen Technologies Pvt Ltd.
**************************************************************************************************/

/************************************************************************************
 * INCLUDES
 */
#include <stdio.h>
#include <string.h>
#include "ZComDef.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "onboard.h" 
#include "Sensor.h"
#include "zcl_Common_data.h"
#include "hal_lcd.h"
/*********************************************************************
 * MACROS
 */

/*********************************************************************
 * CONSTANTS
 */
/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */
byte zclSensor_TaskID;
byte buff[MAX_SEN_BIT] = {INIT_VAL};

/*********************************************************************
 * @fn	zcl_SensorInit
 *
 * @brief	Initialization function for the zcl layer.
 *
 * @param task_id 	[IN]	 ZCL task id
 *
 * @return      none
 */
void zcl_SensorInit( uint8 task_id )
{
    zclSensor_TaskID = task_id;
}

/*********************************************************************
 * @fn	zclHumiSensor_event_loop
 *
 * @brief	Event Loop Processor for Reading Sensor Data.
 *
 * @param task_id [IN]	Task id for Processing  Event
 * @param event   [IN]	Events to do for Reading Sensor value
 
 * @return		SUCCESS
 */
uint16 zclSensor_event_loop( uint8 task_id, uint16 events )
{
	(void)task_id; 
	
	if ( events & SENSOR_STATUS_EVT )
	{
		Sensor_reading(buff, MAX_SEN_BIT);
		osal_start_timerEx( zclSensor_TaskID,
								SENSOR_STATUS_EVT,
								SENSOR_STATUS_TIME );
		return (events ^ SENSOR_STATUS_EVT);
	}
    return SUCCESS;
}								

/** **************************************************************************
 *  @brief Process for reading Sensor
 *
 *  @param buff		[IN] 	buffer to store Sensor 40 Bits 
 * 	@param size		[IN]	Size of the Buffer   
 *
 *  @return		 None
 *      
 */
void Sensor_reading(uint8 buff[], uint8 size)
{
    uint8 portData;
	
	P0SEL &= ~(BIT_ONE << MAX_BIT);//make gpio
	P0DIR |= (BIT_ONE << MAX_BIT);//make output port
	SENSOR_PORT_PIN = LOW; 
	MicroWait(OUTPUT_LOW_DELAY);//18000
	SENSOR_PORT_PIN = HIGH;
	MicroWait(OUTPUT_HIGH_DELAY);//40
	P0DIR &= ~(BIT_ONE << MAX_BIT); //make input port
	MicroWait(INIT_INPUT_DELAY);
	//Check the response
	portData = SENSOR_PORT_PIN;
	if(portData == HIGH)	//If it line is High it is an Error
	{
		HalLcdWriteString("Condition nt met", HAL_LCD_LINE_1);
		HalLcdWriteString("Error", HAL_LCD_LINE_2);
	}
	MicroWait(SENSOR_RESPONSE_LOW_DELAY);//80
	portData = SENSOR_PORT_PIN;
	if(portData != HIGH) //If it is Low, it is an Error
	{
		HalLcdWriteString("Condition nt met", HAL_LCD_LINE_1);
		HalLcdWriteString("Error 2nd", HAL_LCD_LINE_2);
	}
	MicroWait(SENSOR_RESPONSE_HIGH_DELAY);//80
    read_data(buff, size);
}

/******************************************************************************
 * @fn	readSensor Data
 *
 * @brief	Read a bit by bit of Sensor
 *
 * @param buff	[IN] 	buffer to store Sensor 40 Bits 
 * @param size	[IN]	Size of the Buffer 
 * @return      none
 */
void read_data(uint8 buff[], uint8 size)
{ 
	uint8 i = INIT_VAL;
	uint8 port_data;
	uint32 count = INIT_VAL;
	HalLcdWriteString("entering here",3);/*TBR*/
	memset(buff, INIT_VAL, sizeof(buff));
	for(i=LOW; i<size; i++)
	{  
		port_data = SENSOR_PORT_PIN; 
		count = INIT_VAL;
		while(port_data != HIGH && count++ < MAX_COUNT)//Waiting for HIGH
			port_data = SENSOR_PORT_PIN;
		// Port Line is High Now.
		MicroWait(TRANS_DELAY);
		port_data = SENSOR_PORT_PIN;
		if(port_data == HIGH)
		{
			buff[i] = HIGH;
			while(port_data == HIGH)
				port_data = SENSOR_PORT_PIN;
		} 
	}      
}