u8 TSL2561_ReadOneByte(u16 ReadAddr); //指定地址读取一个字节
void TSL2561_WriteOneByte(u16 WriteAddr,u8 DataToWrite); //指定地址写入一个字节
unsigned int Calculate_ch0( u8 DataLow0, u8 DataHigh0);
unsigned int Calculate_ch1(u8 DataLow1, u8 DataHigh1);
unsigned int CalculateLux(unsigned int iGain, unsigned int tInt, unsigned int ch0, unsigned int ch1, int iType);
#endif
//----------------------------------------------------
// T, FN, and CL Package coefficients(系数)
//----------------------------------------------------
// For Ch1/Ch0=0.00 to 0.50
// Lux/Ch0=0.0304-0.062*((Ch1/Ch0)^1.4)
// piecewise approximation
// For Ch1/Ch0=0.00 to 0.125:
// Lux/Ch0=0.0304-0.0272*(Ch1/Ch0)
//
// For Ch1/Ch0=0.125 to 0.250:
// Lux/Ch0=0.0325-0.0440*(Ch1/Ch0)
//
// For Ch1/Ch0=0.250 to 0.375:
// Lux/Ch0=0.0351-0.0544*(Ch1/Ch0)
//
// For Ch1/Ch0=0.375 to 0.50:
// Lux/Ch0=0.0381-0.0624*(Ch1/Ch0)
//
// For Ch1/Ch0=0.50 to 0.61:
// Lux/Ch0=0.0224-0.031*(Ch1/Ch0)
//
// For Ch1/Ch0=0.61 to 0.80:
// Lux/Ch0=0.0128-0.0153*(Ch1/Ch0)
//
// For Ch1/Ch0=0.80 to 1.30:
// Lux/Ch0=0.00146-0.00112*(Ch1/Ch0)
//
// For Ch1/Ch0>1.3:
// Lux/Ch0=0
//----------------------------------------------------
// 无浮点计算的Lux近似计算方程----lux equation approximation without floating point calculations
//////////////////////////////////////////////////////////////////////////////
// Routine: unsigned int CalculateLux(unsigned int ch0, unsigned int ch0, int iType)
//
// Description: Calculate the approximate illuminance (lux) given the raw
// channel values of the TSL2560. The equation if implemented
// as a piece-wise linear approximation.----分段线性近似
//
// Arguments: unsigned int iGain - gain, where 0:1X, 1:16X -- 积分增益
// unsigned int tInt - integration time, where 0:13.7mS, 1:100mS, 2:402mS, 3:Manual -- 积分时间
// unsigned int ch0 - raw channel value from channel 0 of TSL2560 -- 通道0初值
// unsigned int ch1 - raw channel value from channel 1 of TSL2560 -- 通道1初值
// unsigned int iType - package type (T or CS) -- 封装类型
//
// Return: unsigned int - the approximate illuminance (lux) -- 返回近似的Lux
//
//////////////////////////////////////////////////////////////////////////////
unsigned int CalculateLux(unsigned int iGain, unsigned int tInt, unsigned int ch0, unsigned int ch1, int iType)
{
//----------------------------------------------------------------------------
// first, scale the channel values depending on the gain and integration time 16X, 402mS is nominal.
// scale if integration time is NOT 402 msec
// 首先,根据增益和积分时间,衡量通道值
unsigned long chScale;
unsigned long channel1;
unsigned long channel0;
// find the ratio of the channel values (Channel1/Channel0)
// protect against divide by zero
ratio1 = 0;
if (channel0 != 0) ratio1 = (channel1 << (RATIO_SCALE+1)) / channel0;
// round the ratio value
ratio = (ratio1 + 1) >> 1;
// is ratio <= eachBreak ?
//unsigned int b, m;
switch (iType)
{
case 0: // T, FN and CL package
if ((ratio >= 0) && (ratio <= K1T))
{b=B1T; m=M1T;}
else if (ratio <= K2T)
{b=B2T; m=M2T;}
else if (ratio <= K3T)
{b=B3T; m=M3T;}
else if (ratio <= K4T)
{b=B4T; m=M4T;}
else if (ratio <= K5T)
{b=B5T; m=M5T;}
else if (ratio <= K6T)
{b=B6T; m=M6T;}
else if (ratio <= K7T)
{ b=B7T; m=M7T;}
else if (ratio > K8T)
{b=B8T; m=M8T;}
break;
case 1:// CS package
if ((ratio >= 0) && (ratio <= K1C))
{b=B1C; m=M1C;}
else if (ratio <= K2C)
{b=B2C; m=M2C;}
else if (ratio <= K3C)
{b=B3C; m=M3C;}
else if (ratio <= K4C)
{b=B4C; m=M4C;}
else if (ratio <= K5C)
{b=B5C; m=M5C;}
else if (ratio <= K6C)
{b=B6C; m=M6C;}
else if (ratio <= K7C)
{b=B7C; m=M7C;}
else if (ratio > K8C)
{b=B8C; m=M8C;}
break;
}
//unsigned long temp;
temp = ((channel0 * b) - (channel1 * m));
// do not allow negative lux value
if (temp < 0) temp = 0;