/*
 * Implement sensor driver function in here.
 * Every sensor include init,write,read function.
 * Author:Jimbo
 */

#include <stdint.h>
#include <stdio.h>
#include "sensor_tbl.h"
#include "hal_interface_api.h"
#include "linux/fcntl.h"

/* 
 * sensor number: 1
 * sensor name: +12V
 * brief: ltc4280
 */
int sensor_1_init(void)
{
	return 0;	
}

int sensor_1_write(void)
{
	return 0;
}

int sensor_1_read(uint8_t *reading)
{
	uint8_t buf = 5;
//	int fd = open("/dev/i2c3", O_RDWR);
//	stm32_i2c_master_write(fd, 0xAA, &buf, 1);
//	stm32_i2c_master_read(fd, 0xAA, &buf, 1);
//	close(fd);
	//printf("read sensor 1: %#x!\n", buf);
	//*reading = buf;
	*reading = 0x10;
	return 0;
}

/* 
 * sensor number: 2
 * sensor name: +12V_Current
 * brief:  ltc4280
 */
int sensor_2_init(void)
{
//	log("init sensor 2!\r\n");
	return 0;	
}

int sensor_2_write(void)
{
//	log("write sensor 2!\r\n");
	return 0;
}

int sensor_2_read(uint8_t *reading)
{
	uint8_t buf = 4;
//	int fd = open("/dev/i2c3", O_RDWR);
//	stm32_i2c_master_write(fd, 0xAA, &buf, 1);
//	stm32_i2c_master_read(fd, 0xAA, &buf, 1);
//	close(fd);
	//*reading = buf;
	*reading = 0x20;
	return 0;
}

/* 
 * sensor number: 3
 * sensor name: Temp_Board
 * brief: G780 local 
 */
int sensor_3_init(void)
{
//	log("init sensor 2!\r\n");
	return 0;	
}

int sensor_3_write(void)
{
//	log("write sensor 2!\r\n");
	return 0;
}

int sensor_3_read(uint8_t *reading)
{
	uint8_t buf = 0;
//	int fd = open("/dev/i2c3", O_RDWR);
//	stm32_i2c_master_write(fd, 0x98, &buf, 1);
//	stm32_i2c_master_read(fd, 0x98, &buf, 1);
//	close(fd);
	//*reading = buf;
	*reading = 0x30;
	return 0;
}


/* 
 * sensor number: 4
 * sensor name: Temp_FPGA
 * brief: G780 remote 
 */
int sensor_4_init(void)
{
//	log("init sensor 2!\r\n");
	return 0;	
}

int sensor_4_write(void)
{
//	log("write sensor 2!\r\n");
	return 0;
}

int sensor_4_read(uint8_t *reading)
{
	uint8_t buf = 1;
//	int fd = open("/dev/i2c3", O_RDWR);
//	stm32_i2c_master_write(fd, 0x98, &buf, 1);
//	stm32_i2c_master_read(fd, 0x98, &buf, 1);
//	close(fd);
	//*reading = buf;
	*reading = 0x40;
	return 0;
}

/* 
 * sensor number: 5
 * sensor name: +1.5V
 * brief: adc_ch1
 */
int sensor_5_init(void)
{
	return 0;	
}

int sensor_5_write(void)
{
	return 0;
}

int sensor_5_read(uint8_t *reading)
{
	uint16_t buf;
//	stm32_adc_get_value(1, &buf);
	//*reading = buf>>4;
	*reading = 0x50;
	return 0;
}

/* 
 * sensor number: 6
 * sensor name: +0.75V
 * brief: adc_ch2
 */
int sensor_6_init(void)
{
	return 0;	
}

int sensor_6_write(void)
{
	return 0;
}

int sensor_6_read(uint8_t *reading)
{
	uint16_t buf;
//	stm32_adc_get_value(2, &buf);
	//*reading = buf>>4;
	*reading = 0x60;
	return 0;
}

/* 
 * sensor number: 7
 * sensor name: +1.8V
 * brief: adc_ch3
 */
int sensor_7_init(void)
{
	return 0;	
}

int sensor_7_write(void)
{
	return 0;
}

int sensor_7_read(uint8_t *reading)
{
	uint16_t buf;
//	stm32_adc_get_value(3, &buf);
	//*reading = buf>>4;
	*reading = 0x70;
	return 0;
}

/* 
 * sensor number: 8
 * sensor name: +1.2V
 * brief: adc_ch4
 */
int sensor_8_init(void)
{
	return 0;	
}

int sensor_8_write(void)
{
	return 0;
}

int sensor_8_read(uint8_t *reading)
{
	uint16_t buf;
//	stm32_adc_get_value(4, &buf);
	//*reading = buf>>4;
	*reading = 0x80;
	return 0;
}

/* 
 * sensor number: 9
 * sensor name: +1.0V
 * brief: adc_ch5
 */
int sensor_9_init(void)
{
	return 0;	
}

int sensor_9_write(void)
{
	return 0;
}

int sensor_9_read(uint8_t *reading)
{
	uint16_t buf;
//	stm32_adc_get_value(5, &buf);
	//*reading = buf>>4;
	*reading = 0x90;
	return 0;
}

/* 
 * sensor number: 10
 * sensor name: +2.5V
 * brief: adc_ch6
 */
int sensor_10_init(void)
{
	return 0;	
}

int sensor_10_write(void)
{
	return 0;
}

int sensor_10_read(uint8_t *reading)
{
	uint16_t buf;
//	stm32_adc_get_value(6, &buf);
	//*reading = buf>>4;
	*reading = 0xA0;
	return 0;
}

/* 
 * sensor number: 11
 * sensor name: +3.3V
 * brief: adc_ch7
 */
int sensor_11_init(void)
{
	return 0;	
}

int sensor_11_write(void)
{
	return 0;
}

int sensor_11_read(uint8_t *reading)
{
	uint16_t buf;
//	stm32_adc_get_value(7, &buf);
	//*reading = buf>>4;
	*reading = 0xB0;
	return 0;
}

sensor_tbl_t sensor_tbl[]=
{
	{
		1,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_1_read,	//read function
		sensor_1_write,	//write functon
		sensor_1_init,	//init function
	},
	
	{
		2,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_2_read,
		sensor_2_write,
		sensor_2_init,
	},
	
	{	
		3,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_3_read,
		sensor_3_write,
		sensor_3_init,
	},
	
	{	
		4,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_4_read,
		sensor_4_write,
		sensor_4_init,
	},
	
	{	
		5,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_5_read,
		sensor_5_write,
		sensor_5_init,
	},
	
	{	
		6,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_6_read,
		sensor_6_write,
		sensor_6_init,
	},
	
	{	
		7,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_7_read,
		sensor_7_write,
		sensor_7_init,
	},
	
	{	
		8,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_8_read,
		sensor_8_write,
		sensor_8_init,
	},
	
	{	
		9,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_9_read,
		sensor_9_write,
		sensor_9_init,
	},
	
	{	
		10,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_10_read,
		sensor_10_write,
		sensor_10_init,
	},
	
	{	
		11,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_11_read,
		sensor_11_write,
		sensor_11_init,
	}
	
};


sensor_tbl_t *getSensorDev(uint8_t sensorNum)
{
	uint16_t i = 0;
	uint16_t sensorCnt=0;
	sensorCnt = sizeof(sensor_tbl)/sizeof(sensor_tbl_t);
	
	for(i=0; i<sensorCnt; i++)
	{
		if(sensor_tbl[i].sensor_number == sensorNum)	
			return &sensor_tbl[i];
	}
	
	printf("Error: Can't find sensor, sensorNum = %#x\r\n", sensorNum);
	return NULL;
}
