class to readout the VEML7700 light sensor via i2c
Diff: veml7700.cpp
- Revision:
- 1:d6eb62dc0a1e
- Parent:
- 0:e71d3ecdd257
--- a/veml7700.cpp Tue Sep 10 11:11:19 2019 +0000 +++ b/veml7700.cpp Tue Sep 10 13:33:10 2019 +0000 @@ -11,6 +11,7 @@ * version 0.30 : start with error reporting * version 0.34 : correction * version 0.46 : corrections for correct working , more methods added + * version 0.50 : added readout in lux * This file make part of the PeriperalDevice package see repository * https://github.com/wimbeaumont/PeripheralDevices * @@ -20,7 +21,7 @@ * **/ -#define VERSION_VEML7700_SRC "0.49" +#define VERSION_VEML7700_SRC "0.52" namespace VEML7700_CONST { // the VEML7700 support only 1 I2C address @@ -60,7 +61,9 @@ u16 IntTSets[NrIntT]={ 0b1100,0b1000,0b0000,0b0001,0b0010,0b0011 }; int IntTs[NrIntT] = { 25 ,50,100,200,400, 800 }; - +const float luxref=.0576 ; // lux per bit for gain = 1 and IT =100 ms +const int RefIntT= 100; +// min resolution gain =2 IT =800 , 0.0036 (als ch) // R1 and R2 are 16 words value // bit mask R3 and values const u16 RESERVED_BIT_MSK_POW_SET = 0xFFF8; // these bits should be 0 so use inverse with AND @@ -257,6 +260,22 @@ u16 VEML7700::get_white_ch_bits(void){ return read_cmd(WHITE ); } + + +float VEML7700::get_lux(bool ALSreg , bool verify_reg ) { + u16 reg= ALSreg ? ALS: WHITE; + u16 res = read_cmd(reg); + if ( verify_reg ) { + reg= ALS_CONF_0; + reg =read_cmd(reg); + (void) decodeIntTbits( reg,IntTnr ); // just set this correct + (void) decodeIntTbits( reg,gain_nr ); // just set gain_nr correct + } + + return (float)res *luxref / gains[gain_nr]*RefIntT /(float) IntTs[IntTnr] ; +} + + // below are not necessay but useful fpr debugging u16 VEML7700::get_bits(u16 regvalue, u16 lsb ,u16 bsize ) { u16 mask = 1; @@ -270,34 +289,43 @@ } float VEML7700::decodeGainBits( u16 gb) { - int g_nr ; float rv=0; + int dummy; + return decodeGainBits( gb, dummy ) ; +} + +float VEML7700::decodeGainBits( u16 gb, int& g_nr) { + // this function doesn't set the gain_nr as it can be used for checking only for ( g_nr = 0; g_nr < NrGains; g_nr++ ){ if ( gb == GainSets[g_nr] ) { break;} } - if( g_nr < NrGains) { rv = gains[gain_nr];}; - // else not valid gain setting , return 0 - return rv; + if( g_nr >= NrGains) { g_nr = NrGains-1;}; + return gains[g_nr]; } -int VEML7700::decodeIntTbits( u16 ib ) { - int In_nr, rv=0; +int VEML7700::decodeIntTbits( u16 ib) { + int dummy; + return decodeIntTbits( ib,dummy ); +} + +int VEML7700::decodeIntTbits( u16 ib, int &In_nr ) { + // this function doesn't set the IntTnr as it can be used for checking only for ( In_nr= 0;In_nr < NrIntT ;In_nr++){ if( ib == IntTSets[In_nr] ){ break;} } - if ( In_nr < NrIntT) { rv =IntTs[In_nr]; }; - //else not valid Integration bit code return 0 - return rv ; + if ( In_nr >= NrIntT) { In_nr= NrIntT-1; }; + return IntTs[In_nr]; } -void VEML7700::decode_Reg0( bool& sd ,bool& ie, u16& pers_protect,int& IntT, float& gain ) { +u16 VEML7700::decode_Reg0( bool& sd ,bool& ie, u16& pers_protect,int& IntT, float& gain ) { u16 reg= read_cmd(ALS_CONF_0); u16 res= get_bits(reg, ALS_SD_LSB , ALS_SD_SIZE ) ; - sd = reg ? false : true; + sd = res ? false : true; res= get_bits(reg,ALS_INT_EN_LSB , ALS_INT_EN_SIZE ) ; - ie = reg ? false : true; + ie = res ? false : true; pers_protect= get_bits(reg,ALS_PERS_LSB , ALS_PERS_SIZE ) ; res= get_bits(reg,ALS_IT_LSB , ALS_IT_SIZE ) ; IntT=decodeIntTbits(res); res= get_bits(reg,ALS_GAIN_LSB , ALS_GAIN_SIZE ) ; gain=decodeGainBits(res); + return reg; }