class to readout the VEML7700 light sensor via i2c

Dependents:   veml7700_tst

Files at this revision

API Documentation at this revision

Comitter:
wbeaumont
Date:
Tue Sep 10 13:33:10 2019 +0000
Parent:
0:e71d3ecdd257
Commit message:
corrections added lux output

Changed in this revision

veml7700.cpp Show annotated file Show diff for this revision Revisions of this file
veml7700.h Show annotated file Show diff for this revision Revisions of this file
diff -r e71d3ecdd257 -r d6eb62dc0a1e veml7700.cpp
--- 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;
 }
diff -r e71d3ecdd257 -r d6eb62dc0a1e veml7700.h
--- a/veml7700.h	Tue Sep 10 11:11:19 2019 +0000
+++ b/veml7700.h	Tue Sep 10 13:33:10 2019 +0000
@@ -12,6 +12,7 @@
  * version 0.21  : just change the name from VEML770 VEML7700
  * version 0.30  : start with error reporting 
  * version 0.42  : more methodes 
+ * version 0.50  : added readout in lux 
  * This file make part of the PeriperalDevice package see repository  
  * https://github.com/wimbeaumont/PeripheralDevices
  *
@@ -21,7 +22,7 @@
  *
  **/
 
-#define VERSION_VEML7700_HDR "0.42"
+#define VERSION_VEML7700_HDR "0.51"
 
 
 class VEML7700 : public virtual getVersion {
@@ -59,11 +60,6 @@
 //  set interrupt enable    
 void set_int_enable( bool int_enable);
 
-// write the gain bits to reg 0 
-void set_gain_bits( u16 gbits ) ;
-// write the integration time  bits to reg 0 
-void set_IntT_bits( u16 InTgbits );
-
 // set the bits for the ALS config register (0) in one write cycle 
 void set_default_als_config ( bool shutdown=false  , bool int_enable=false ,u16 pres=0, u16 integationtime=0, u16 gain =1  );
 
@@ -85,12 +81,22 @@
 void set_gain( float gain );
 void set_integrationtime( int time_ms );
 
-// return convert the value readed from the als register, taking in acount the gain and integration factor in to lux 
-float get_lux_als(void) { return 0;}
+// returns convert  the value readed from the als or white  register, taking in acount the gain and integration time  in to lux 
+// if als   is true  the ALS register is read, else the white register is read 
+// if verify is flase it  doesn't reads the config register assumes the stored gain ant integration time are corrected stored in the program 
+float get_lux(bool als=true  ,bool verify_reg =false );
+
+
 
-float get_lux_white(void) { return 0;}
 // these could be private but could be useful for debugging 
 
+
+// write the gain bits to reg 0 
+void set_gain_bits( u16 gbits ) ;
+// write the integration time  bits to reg 0 
+void set_IntT_bits( u16 InTgbits );
+
+
 // get the gain bits depending on the value of gainsel, setgain <= gainsel  
 // so 0 -> .125 ,  .126 -> .25 ,  4 -> 2  1.1 -> 2 
 u16 get_gain_in_set_bit (float  gainsel );
@@ -101,9 +107,11 @@
 u16 get_bits(u16 regvalue,   u16 lsb ,u16 bsize );
 void set_bits_in_reg ( u8 reg , u16 value, u16 lsb ,u16 bsize ) {  set_bits_reg  ( reg ,  value,  lsb ,bsize ) ;}
 
-float decodeGainBits( u16 gb) ;
-int  decodeIntTbits( u16 ib ) ;    
-void decode_Reg0(  bool& sd ,bool& ie, u16& pers_protect,int& IntT, float& gain  ) ;
+float decodeGainBits( u16 gb , int& gainnr) ;
+float decodeGainBits( u16 gb);
+int  decodeIntTbits( u16 ib, int& InTnr ) ; 
+int  decodeIntTbits( u16 ib);
+u16  decode_Reg0(  bool& sd ,bool& ie, u16& pers_protect,int& IntT, float& gain  ) ;
 
 }; //end class