#include "i2c_helper.h"

// static I2C_HandleTypeDef *pADS1115_I2C = &hi2c1;

// osEventFlagsId_t i2chelperEventHandle;
// const osEventFlagsAttr_t i2chelperEvent_attributes = {
//   .name = "i2chelperEvent"
// };

I2CHelper_TypeDef * I2CHelper_Init( )
{
    I2CHelper_TypeDef *Handle = (I2CHelper_TypeDef *)malloc(sizeof(I2CHelper_TypeDef));
    if (Handle == NULL)
    {
        return NULL;
    }
	// Handle->delaytime_trans_byte = 0;
    return Handle;
}
void I2CHelper_Set_Hi2c( I2CHelper_TypeDef *i2chelper,  I2C_HandleTypeDef * hi2c  )
{
	i2chelper->hi2c = hi2c;
}
void I2CHelper_Set_Mode( I2CHelper_TypeDef *i2chelper,  I2C_HELPER_MODE_TypeDef mode  )
{
	i2chelper->mode	= mode;
}
void I2CHelper_Set_Trans_Type( I2CHelper_TypeDef *i2chelper,  I2C_HELPER_TRANS_TypeDef trans_type  )
{
	i2chelper->trans_type	= trans_type;
}
void I2CHelper_Set_Rcv_Type( I2CHelper_TypeDef *i2chelper,  I2C_HELPER_RCV_TypeDef rcv_type  )
{
	i2chelper->rcv_type	= rcv_type;
}

int I2CHelper_Set_callback_func_obj( I2CHelper_TypeDef *i2chelper,   void * obj,  it_callback callback )
{
	i2chelper->obj	= obj;
	i2chelper->callback	= callback;
	return 0;
}

void I2CHelper_Set_rcv_buf( I2CHelper_TypeDef *i2chelper, uint8_t *buf, uint32_t buf_size )
{
	i2chelper->receive_buf	= buf;
	i2chelper->receive_size	= buf_size;
	i2chelper->receive_data_con	= 0;

}
void I2CHelper_Set_Trans_Buf( I2CHelper_TypeDef *i2chelper, uint8_t *buf, uint32_t buf_size )
{
	i2chelper->trans_buf	= buf;
	i2chelper->trans_size	= buf_size;
	i2chelper->trans_data_con	= 0;
}

int I2CHelper_Write( I2CHelper_TypeDef *i2chelper,  uint8_t * buf, uint16_t size)
{
	// 考虑主从机模式
	int st = -1;
	switch ( i2chelper->mode == I2C_HELPER_MODE_MASTER )
	{
	case I2C_HELPER_MODE_MASTER:
		if ( i2chelper->trans_type == I2C_HELPER_TRANS_POLLING )
		{
			st = HAL_I2C_Master_Transmit( i2chelper->hi2c, i2chelper->i2c_write_address
						, buf, size, 0xFFFF );
		}
		if ( i2chelper->trans_type == I2C_HELPER_TRANS_IT )
		{
			// st = HAL_I2C_Master_Transmit_IT( i2chelper->hi2c, i2chelper->i2c_write_address
			// 			, buf, size  );	
			st = HAL_I2C_Master_Transmit_IT( i2chelper->hi2c, i2chelper->i2c_write_address, buf,size  );			
		}
		if ( i2chelper->trans_type == I2C_HELPER_TRANS_DMA )
		{
			// st = HAL_I2C_Master_Transmit_DMA( i2chelper->hi2c, i2chelper->i2c_write_address
			// 			, buf, size  );			
			st = HAL_I2C_Master_Transmit_DMA( i2chelper->hi2c, i2chelper->i2c_write_address, buf, size );		
		}
		break;
	case I2C_HELPER_MODE_SLAVE:
		break;	
	default:
		break;
	}
	return st;
}

int I2CHelper_Read(I2CHelper_TypeDef *i2chelper,  uint8_t * buf, uint16_t size)
{
	int st = -1;
	switch ( i2chelper->mode == I2C_HELPER_MODE_MASTER )
	{
	case I2C_HELPER_MODE_MASTER:
		if ( i2chelper->rcv_type == I2C_HELPER_TRANS_POLLING )
		{
			st = HAL_I2C_Master_Receive( i2chelper->hi2c, i2chelper->i2c_write_address
						, buf, size, 0xFFFF );
		}
		if ( i2chelper->rcv_type == I2C_HELPER_TRANS_IT )
		{
			// st = HAL_I2C_Master_Transmit_IT( i2chelper->hi2c, i2chelper->i2c_write_address
			// 			, buf, size  );	
			st = HAL_I2C_Master_Receive_IT( i2chelper->hi2c, i2chelper->i2c_write_address
						, buf, size );			
		}
		if ( i2chelper->rcv_type == I2C_HELPER_TRANS_DMA )
		{
			// st = HAL_I2C_Master_Transmit_DMA( i2chelper->hi2c, i2chelper->i2c_write_address
			// 			, buf, size  );			
			st = HAL_I2C_Master_Receive_DMA( i2chelper->hi2c, i2chelper->i2c_write_address
						, buf, size );		
		}
		break;
	case I2C_HELPER_MODE_SLAVE:
		break;	
	default:
		break;
	}
	return st;
}

int I2CHelper_Flags_Set(I2CHelper_TypeDef *i2chelper,  I2C_HELPER_Event_TypeDef evt_type )
{
	return osEventFlagsSet(i2chelper, evt_type);
}

// int I2CHelper_Flags_Clear(I2CHelper_TypeDef *i2chelper,  I2C_HELPER_Event_TypeDef evt_type )
// {
// 	return osEventFlagsClear(i2chelper, evt_type);
// }

// int I2CHelper_Flags_Wait(I2CHelper_TypeDef *i2chelper,  uint32_t delay )
// {
// 	return 	osEventFlagsWait ( 
//                 i2chelperEventHandle
//                 , I2C_HELPER_Event_READY|I2C_HELPER_Event_INIT|I2C_HELPER_Event_TRANS_ONLY|I2C_HELPER_Event_DATA_RCV
//                 , 0
//                 , delay );
// }