/*
 * ALS308.h
 *
 *  Created on: 2022年4月11日
 *      Author: amos
 */
#ifndef RGB381_H_
#define RGB381_H_


#include "onechip_include_header.h"

#define max3v(v1, v2, v3)   ((v1)<(v2)? ((v2)<(v3)?(v3):(v2)):((v1)<(v3)?(v3):(v1)))
#define min3v(v1, v2, v3)   ((v1)>(v2)? ((v2)>(v3)?(v3):(v2)):((v1)>(v3)?(v3):(v1)))
/****************************************************/
#define SMPTIME          30 //sample times
#define SAMP_SIZE        18
#define CALCU_SAMP_SIZE  180
#define CS_MAX			10

#define CS_RDR_MIN      50    //光信号最小值
/***************LTR381RGB address********************/
#define RGB381_ADDR          0x53
/***************LTR381RGB register define************/
#define RGB381_MainCtrl		 0x00  /*RGB operation mode control, SW reset*/
#define RGB381_MeasRate		 0x04  /*RGB measurement rate and resolution in Active Mode*/
#define RGB381_Gain  		 0x05  /*RGB analog Gain*/
#define RGB381_PartID		 0x06  /*Part number ID and revision ID*/
#define RGB381_MainState	 0x07  /*Power-On status, Interrupt status, Data status*/

#define RGB381_IR_DataLow	 0x0A  /*IR ADC measurement data, LSB*/
#define RGB381_IR_DataMid	 0x0B  /*IR ADC measurement data*/
#define RGB381_IR_DataHIG	 0x0C  /*IR ADC measurement data, MSB*/
#define RGB381_G_DataLow	 0x0D  /*G ADC measurement data, LSB*/
#define RGB381_G_DataMid	 0x0E  /*G ADC measurement data*/
#define RGB381_G_DataHIG	 0x0F  /*G ADC measurement data, MSB*/
#define RGB381_R_DataLow	 0x10  /*red ADC measurement data, LSB*/
#define RGB381_R_DataMid	 0x11  /*red ADC measurement data*/
#define RGB381_R_DataHIG	 0x12  /*CS red ADC measurement data, MSB*/
#define RGB381_B_DataLow	 0x13  /*CS blue ADC measurement data, LSB*/
#define RGB381_B_DataMid	 0x14  /*CS blue ADC measurement data*/
#define RGB381_B_DataHIG	 0x15  /*CS blue ADC measurement data, MSB*/

#define RGB381_INTCFG		 0x19  /*Interrupt configuration*/
#define RGB381_INTPST		 0x1A  /*Interrupt persist setting*/

#define RGB381_THRUP0		 0x21  /*RGB interrupt upper threshold, LSB*/
#define RGB381_THRUP1		 0x22  /*RGB interrupt upper threshold, intervening bits*/
#define RGB381_THRUP2		 0x23  /*RGB interrupt upper threshold, MSB*/
#define RGB381_THRLW0		 0x24  /*RGB interrupt lower threshold, LSB*/
#define RGB381_THRLW1		 0x25  /*RGB interrupt lower threshold, intervening bits*/
#define RGB381_THRLW2		 0x26  /*RGB interrupt lower threshold, MSB*/
/*************************LTR381RGB function define******************/
#define ALS_MeasRes_20B      0x00   /**<  data 20 bit,conversion time 400ms */
#define ALS_MeasRes_19B      0x10   /**<  data 19 bit,conversion time 200ms */
#define ALS_MeasRes_18B      0x20   /**<  data 18 bit,conversion time 100ms */
#define ALS_MeasRes_17B      0x30   /**<  data 17 bit,conversion time 50ms */
#define ALS_MeasRes_16B      0x40   /**<  data 16 bit,conversion time 25ms */
/*********************************************************************/
#define ALS_MeasRate_25ms      0x00   /**<  measurement time 25ms */
#define ALS_MeasRate_50ms      0x01   /**<  measurement time 50ms */
#define ALS_MeasRate_100ms     0x02   /**<  measurement time 100ms */
#define ALS_MeasRate_200ms     0x03   /**<  measurement time 500ms */
#define ALS_MeasRate_500ms     0x04   /**<  measurement time 500ms */
#define ALS_MeasRate_1000ms    0x05   /**<  measurement time 1000ms */
#define ALS_MeasRate_2000ms    0x06   /**<  measurement time 2000ms */
/*******************************************************************/
#define ALS_Gain_1X       0x00   /**<  1x gain  */
#define ALS_Gain_3X       0x01   /**<  3x gain  */
#define ALS_Gain_6X       0x02   /**<  6x gain */
#define ALS_Gain_9X       0x03   /**<  9x gain */
#define ALS_Gain_18X      0x04   /**<  18x gain */

#define CS_Modle         0x02    /* RGB is active mode */
#define ALS_Active       0x04    /* RGB is active mode */
/****************************************************/
typedef struct
{
	uint32_t RDC; //color data clear raw data	原始值
	uint32_t RDR; //color data red   raw data	原始值带小数
//	uint32_t RDG; //color data green raw data
//	uint32_t RDB; //color data blue  raw data

	float CDR; //color data red   calibration data	校零值（原始值带小数）
//	float CDG; //color data green calibration data
//	float CDB; //color data blue  calibration data

	float RtR; //color data red   convert data	(CDR - RDR)*gain
//	float RtG; //color data green convert data
//	float RtB; //color data blue  convert data

	float Tt;
	float TtMin;
	float TtMax;   // original Tt
	uint8_t std_num;

	float gain;	//归一化值
	//float sum;
	float STD;		//标准差
	float average;	//均值
	float SAMP[SAMP_SIZE];
//	uint16_t CALCU_SAMP[CALCU_SAMP_SIZE];

	//float AccArea; //accumulation area

	uint8_t   Result;
	uint8_t  ID; //color sensor ID
}ColorSensor;
extern ColorSensor CS[CS_MAX];
extern uint8_t SensorState1,SensorState2,SensorState3,SensorState4,SensorState5,SensorState6,SensorState7,SensorState8,SensorState9,SensorState10;
//extern uint8_t SensorState1,SensorState2,SensorState3,SensorState4,SensorState5,SensorState6,SensorState7,SensorState8,SensorState9;
/**************************************************/
void SensorMeasurementFactor(uint8_t measfactor);
void SensorSetGain(uint8_t gain);
uint8_t SensorGetID(uint8_t ID);
void SensorGetRawData(ColorSensor *CS);
void SensorCalculatedCalibrationFactor(ColorSensor *CS);
void RGBtoHSV(ColorSensor *CS);
void SensorEnable(void);
uint8_t SensorInit(ColorSensor *CS);
int SensorSelfCheck(void);
void SensorRGBInit(ColorSensor *CS);
void SensorGetCalData(ColorSensor *CS);
void sample_first_zero(void);
void SensorInitial(void);
int check_first_zero(void);
void sample_calibrate_calc(uint32_t times);
void sample_calc_avg_std(void);
void sample_calc_tt(void);
void sample_calc_result(void);
/**************************************************/

#endif /* RGB381_H_ */
