#include <stdio.h>
#include "mini58series.h"
#include "VCNL3040.h"
#include "i2c_software_gpio.h"

static u32 VCNL3040_WriteCommand(ECommand_ID command,u16 Data);

u8 _VCNL3040_Addr = 0;
void VCNL3040_Init(u8 i2cAddress)
{
	_VCNL3040_Addr = i2cAddress;
	
	//power_on  ,ps output 16bits 
	VCNL3040_WriteCommand(PS_CONF1_2,0x080E);		
	
	//active force mode disable (normal mode) 
	//not interrupt mode   ;   current 180ma
	VCNL3040_WriteCommand(PS_CONF3_PS_MS,0x4700);	
	
	// high interrupt threshold setting  560
	VCNL3040_WriteCommand(PS_THDH,0x0230);	
	// low interrupt threshold setting  260
	VCNL3040_WriteCommand(PS_THDH,0x0104);	
	
	
}
static u32 VCNL3040_WriteCommand(ECommand_ID command,u16 Data)
{
	static u32 error = 0; // error code
	static u8 cmd_buf[4] = {0};
	error = 0;
	cmd_buf[0] = command ;
	cmd_buf[1] = Data ;
	cmd_buf[2] = Data>>8 ;
	I2C_SW_Send(_VCNL3040_Addr, cmd_buf,3);

	return error;
}
u16 VCNL3040_ReadBytes(ECommand_ID command)
{
	static u8 cmd_buf[4] = {0};
	cmd_buf[0] = command ;
	I2C_SW_Send_Without_Stop(_VCNL3040_Addr, cmd_buf,1);
	memset(cmd_buf,0,sizeof(cmd_buf));
	
	I2C_SW_Get_Without_Delay(_VCNL3040_Addr, cmd_buf, 2);
	//printf("BUFFER DATA = %x ,%x",cmd_buf[1],cmd_buf[0]);
	
    return (((u16)cmd_buf[1]<<8) | cmd_buf[0]); 
}



