class to readout the VEML7700 light sensor via i2c
veml7700.cpp@1:d6eb62dc0a1e, 2019-09-10 (annotated)
- Committer:
- wbeaumont
- Date:
- Tue Sep 10 13:33:10 2019 +0000
- Revision:
- 1:d6eb62dc0a1e
- Parent:
- 0:e71d3ecdd257
corrections added lux output
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbeaumont | 0:e71d3ecdd257 | 1 | #include "veml7700.h" |
wbeaumont | 0:e71d3ecdd257 | 2 | //nclude <stdio.h> |
wbeaumont | 0:e71d3ecdd257 | 3 | /** |
wbeaumont | 0:e71d3ecdd257 | 4 | * veml7700.cpp |
wbeaumont | 0:e71d3ecdd257 | 5 | * implementation of the veml7700 class , see veml7700.h |
wbeaumont | 0:e71d3ecdd257 | 6 | * |
wbeaumont | 0:e71d3ecdd257 | 7 | * version history |
wbeaumont | 0:e71d3ecdd257 | 8 | * version 0.20 : initial value not tested with the sensor |
wbeaumont | 0:e71d3ecdd257 | 9 | * version 0.21 : just change the name from VEML770 VEML7700 |
wbeaumont | 0:e71d3ecdd257 | 10 | corrected address |
wbeaumont | 0:e71d3ecdd257 | 11 | * version 0.30 : start with error reporting |
wbeaumont | 0:e71d3ecdd257 | 12 | * version 0.34 : correction |
wbeaumont | 0:e71d3ecdd257 | 13 | * version 0.46 : corrections for correct working , more methods added |
wbeaumont | 1:d6eb62dc0a1e | 14 | * version 0.50 : added readout in lux |
wbeaumont | 0:e71d3ecdd257 | 15 | * This file make part of the PeriperalDevice package see repository |
wbeaumont | 0:e71d3ecdd257 | 16 | * https://github.com/wimbeaumont/PeripheralDevices |
wbeaumont | 0:e71d3ecdd257 | 17 | * |
wbeaumont | 0:e71d3ecdd257 | 18 | * (C) Wim Beaumont Universiteit Antwerpen 2015 2019 |
wbeaumont | 0:e71d3ecdd257 | 19 | * License see |
wbeaumont | 0:e71d3ecdd257 | 20 | * https://github.com/wimbeaumont/PeripheralDevices/blob/master/LICENSE |
wbeaumont | 0:e71d3ecdd257 | 21 | * |
wbeaumont | 0:e71d3ecdd257 | 22 | **/ |
wbeaumont | 0:e71d3ecdd257 | 23 | |
wbeaumont | 1:d6eb62dc0a1e | 24 | #define VERSION_VEML7700_SRC "0.52" |
wbeaumont | 0:e71d3ecdd257 | 25 | |
wbeaumont | 0:e71d3ecdd257 | 26 | namespace VEML7700_CONST { |
wbeaumont | 0:e71d3ecdd257 | 27 | // the VEML7700 support only 1 I2C address |
wbeaumont | 0:e71d3ecdd257 | 28 | const int i2caddr = 0x20; |
wbeaumont | 0:e71d3ecdd257 | 29 | |
wbeaumont | 0:e71d3ecdd257 | 30 | // registers |
wbeaumont | 0:e71d3ecdd257 | 31 | |
wbeaumont | 0:e71d3ecdd257 | 32 | const u8 ALS_CONF_0 =0x0 ; |
wbeaumont | 0:e71d3ecdd257 | 33 | const u8 ALS_WH =0x1 ; |
wbeaumont | 0:e71d3ecdd257 | 34 | const u8 ALS_WL =0x2 ; |
wbeaumont | 0:e71d3ecdd257 | 35 | const u8 POW_SET =0x3 ; |
wbeaumont | 0:e71d3ecdd257 | 36 | const u8 ALS =0x4 ; |
wbeaumont | 0:e71d3ecdd257 | 37 | const u8 WHITE =0x5 ; |
wbeaumont | 0:e71d3ecdd257 | 38 | const u8 ALS_INT =0x6 ; |
wbeaumont | 0:e71d3ecdd257 | 39 | |
wbeaumont | 0:e71d3ecdd257 | 40 | |
wbeaumont | 0:e71d3ecdd257 | 41 | // bit masks R0 and values |
wbeaumont | 0:e71d3ecdd257 | 42 | const u16 ALS_SD_SIZE =0x1 ; //shutdown =1 |
wbeaumont | 0:e71d3ecdd257 | 43 | const u16 ALS_SD_LSB =0x0 ; |
wbeaumont | 0:e71d3ecdd257 | 44 | const u16 ALS_INT_EN_SIZE =0x1 ; //enable =1 |
wbeaumont | 0:e71d3ecdd257 | 45 | const u16 ALS_INT_EN_LSB =0x1 ; |
wbeaumont | 0:e71d3ecdd257 | 46 | const u16 ALS_PERS_SIZE =0x2 ; |
wbeaumont | 0:e71d3ecdd257 | 47 | const u16 ALS_PERS_LSB =4 ; |
wbeaumont | 0:e71d3ecdd257 | 48 | const u16 ALS_GAIN_SIZE =0x2 ; |
wbeaumont | 0:e71d3ecdd257 | 49 | const u16 ALS_GAIN_LSB = 11; |
wbeaumont | 0:e71d3ecdd257 | 50 | const u16 ALS_IT_SIZE =0x4; |
wbeaumont | 0:e71d3ecdd257 | 51 | const u16 ALS_IT_LSB =6; |
wbeaumont | 0:e71d3ecdd257 | 52 | const u16 RESERVED_BIT_MSK_ALS_CONF_0 = 0xE40C; // these bits should be 0 so use inverse with AND |
wbeaumont | 0:e71d3ecdd257 | 53 | |
wbeaumont | 0:e71d3ecdd257 | 54 | const int NrGains =4; |
wbeaumont | 0:e71d3ecdd257 | 55 | |
wbeaumont | 0:e71d3ecdd257 | 56 | // make array of struct for the next 2 ? |
wbeaumont | 0:e71d3ecdd257 | 57 | u16 GainSets[NrGains] = { 0x2, 0x3 , 0x0 , 0x1 }; |
wbeaumont | 0:e71d3ecdd257 | 58 | float gains[NrGains] = {.125, .25 , 1, 2 }; |
wbeaumont | 0:e71d3ecdd257 | 59 | |
wbeaumont | 0:e71d3ecdd257 | 60 | const int NrIntT =6; |
wbeaumont | 0:e71d3ecdd257 | 61 | u16 IntTSets[NrIntT]={ 0b1100,0b1000,0b0000,0b0001,0b0010,0b0011 }; |
wbeaumont | 0:e71d3ecdd257 | 62 | int IntTs[NrIntT] = { 25 ,50,100,200,400, 800 }; |
wbeaumont | 0:e71d3ecdd257 | 63 | |
wbeaumont | 1:d6eb62dc0a1e | 64 | const float luxref=.0576 ; // lux per bit for gain = 1 and IT =100 ms |
wbeaumont | 1:d6eb62dc0a1e | 65 | const int RefIntT= 100; |
wbeaumont | 1:d6eb62dc0a1e | 66 | // min resolution gain =2 IT =800 , 0.0036 (als ch) |
wbeaumont | 0:e71d3ecdd257 | 67 | // R1 and R2 are 16 words value |
wbeaumont | 0:e71d3ecdd257 | 68 | // bit mask R3 and values |
wbeaumont | 0:e71d3ecdd257 | 69 | const u16 RESERVED_BIT_MSK_POW_SET = 0xFFF8; // these bits should be 0 so use inverse with AND |
wbeaumont | 0:e71d3ecdd257 | 70 | const u16 PSM_EN_SIZE =0x1 ; |
wbeaumont | 0:e71d3ecdd257 | 71 | const u16 PSM_EN_LSB =0x0 ; |
wbeaumont | 0:e71d3ecdd257 | 72 | const u16 PSM_SIZE =0x6 ; |
wbeaumont | 0:e71d3ecdd257 | 73 | const u16 PSM_LSB =0x1 ; |
wbeaumont | 0:e71d3ecdd257 | 74 | |
wbeaumont | 0:e71d3ecdd257 | 75 | // R4 and R5 are 16 words value |
wbeaumont | 0:e71d3ecdd257 | 76 | // bit mask R6 and values status |
wbeaumont | 0:e71d3ecdd257 | 77 | |
wbeaumont | 0:e71d3ecdd257 | 78 | const u16 INT_TH_HIGH_MSK =0x4000 ; |
wbeaumont | 0:e71d3ecdd257 | 79 | const u16 INT_TH_LOW_MSK =0x8000 ; |
wbeaumont | 0:e71d3ecdd257 | 80 | } |
wbeaumont | 0:e71d3ecdd257 | 81 | |
wbeaumont | 0:e71d3ecdd257 | 82 | using namespace VEML7700_CONST; |
wbeaumont | 0:e71d3ecdd257 | 83 | |
wbeaumont | 0:e71d3ecdd257 | 84 | |
wbeaumont | 0:e71d3ecdd257 | 85 | |
wbeaumont | 0:e71d3ecdd257 | 86 | VEML7700::VEML7700(I2CInterface* i2c , bool init ) |
wbeaumont | 0:e71d3ecdd257 | 87 | :getVersion( VERSION_VEML7700_HDR,VERSION_VEML7700_SRC, __TIME__, __DATE__), |
wbeaumont | 0:e71d3ecdd257 | 88 | i2cdev(i2c) { |
wbeaumont | 0:e71d3ecdd257 | 89 | i2cdev=i2c; |
wbeaumont | 0:e71d3ecdd257 | 90 | gain_nr=0; |
wbeaumont | 0:e71d3ecdd257 | 91 | IntTnr =0; |
wbeaumont | 0:e71d3ecdd257 | 92 | if( init) { |
wbeaumont | 0:e71d3ecdd257 | 93 | set_default_als_config(); |
wbeaumont | 0:e71d3ecdd257 | 94 | set_default_powermode(); |
wbeaumont | 0:e71d3ecdd257 | 95 | // don't care about the threshold settings are interrupt not used by default |
wbeaumont | 0:e71d3ecdd257 | 96 | } |
wbeaumont | 0:e71d3ecdd257 | 97 | |
wbeaumont | 0:e71d3ecdd257 | 98 | } |
wbeaumont | 0:e71d3ecdd257 | 99 | |
wbeaumont | 0:e71d3ecdd257 | 100 | int VEML7700::get_status(void) { |
wbeaumont | 0:e71d3ecdd257 | 101 | return i2cdev-> getLastComError() ; |
wbeaumont | 0:e71d3ecdd257 | 102 | |
wbeaumont | 0:e71d3ecdd257 | 103 | } |
wbeaumont | 0:e71d3ecdd257 | 104 | |
wbeaumont | 0:e71d3ecdd257 | 105 | |
wbeaumont | 0:e71d3ecdd257 | 106 | void VEML7700::setHighWarningLevel( int lvl){ |
wbeaumont | 0:e71d3ecdd257 | 107 | u16 llvl=(u16) lvl & 0xFF; |
wbeaumont | 0:e71d3ecdd257 | 108 | write_cmd(ALS_WH, llvl); |
wbeaumont | 0:e71d3ecdd257 | 109 | } |
wbeaumont | 0:e71d3ecdd257 | 110 | |
wbeaumont | 0:e71d3ecdd257 | 111 | void VEML7700::setLowWarningLevel( int lvl){ |
wbeaumont | 0:e71d3ecdd257 | 112 | u16 llvl=(u16) lvl & 0xFF; |
wbeaumont | 0:e71d3ecdd257 | 113 | write_cmd(ALS_WL, llvl); |
wbeaumont | 0:e71d3ecdd257 | 114 | } |
wbeaumont | 0:e71d3ecdd257 | 115 | |
wbeaumont | 0:e71d3ecdd257 | 116 | bool VEML7700::HighThresoldExeed(void) { |
wbeaumont | 0:e71d3ecdd257 | 117 | return (INT_TH_HIGH_MSK & read_cmd(ALS_INT )) ? true:false; |
wbeaumont | 0:e71d3ecdd257 | 118 | } |
wbeaumont | 0:e71d3ecdd257 | 119 | |
wbeaumont | 0:e71d3ecdd257 | 120 | bool VEML7700::LowThresoldExeed(void){ |
wbeaumont | 0:e71d3ecdd257 | 121 | return (INT_TH_LOW_MSK & read_cmd(ALS_INT )) ? true:false; |
wbeaumont | 0:e71d3ecdd257 | 122 | } |
wbeaumont | 0:e71d3ecdd257 | 123 | |
wbeaumont | 0:e71d3ecdd257 | 124 | |
wbeaumont | 0:e71d3ecdd257 | 125 | void VEML7700::set_bits_reg ( u8 reg , u16 value, u16 lsb ,u16 bsize ) { |
wbeaumont | 0:e71d3ecdd257 | 126 | u16 regvalue =read_cmd(reg ); |
wbeaumont | 0:e71d3ecdd257 | 127 | regvalue = set_bits(regvalue, value, lsb, bsize) ; |
wbeaumont | 0:e71d3ecdd257 | 128 | if (reg == ALS_CONF_0) {regvalue &= ~RESERVED_BIT_MSK_ALS_CONF_0; } |
wbeaumont | 0:e71d3ecdd257 | 129 | if (reg == POW_SET) {regvalue &= ~RESERVED_BIT_MSK_POW_SET; } |
wbeaumont | 0:e71d3ecdd257 | 130 | write_cmd( reg, regvalue ) ; |
wbeaumont | 0:e71d3ecdd257 | 131 | } |
wbeaumont | 0:e71d3ecdd257 | 132 | |
wbeaumont | 0:e71d3ecdd257 | 133 | u16 VEML7700::set_bits(u16 regvalue, u16 value, u16 lsb ,u16 bsize ) { |
wbeaumont | 0:e71d3ecdd257 | 134 | u16 mask = 1; |
wbeaumont | 0:e71d3ecdd257 | 135 | mask = mask << bsize ; mask = mask -1; // so bsize=3 give now mask 0 0111 |
wbeaumont | 0:e71d3ecdd257 | 136 | mask = mask << lsb; // put the bits in the correct place |
wbeaumont | 0:e71d3ecdd257 | 137 | regvalue &= ~mask; // set bits to 0 |
wbeaumont | 0:e71d3ecdd257 | 138 | value = value << lsb; // set the value bits in the correct place. |
wbeaumont | 0:e71d3ecdd257 | 139 | value &= mask ; //only set these bits |
wbeaumont | 0:e71d3ecdd257 | 140 | return value | regvalue; |
wbeaumont | 0:e71d3ecdd257 | 141 | } |
wbeaumont | 0:e71d3ecdd257 | 142 | |
wbeaumont | 0:e71d3ecdd257 | 143 | void VEML7700::shutdown( bool enable) { |
wbeaumont | 0:e71d3ecdd257 | 144 | u16 ie= enable ? 0 :1 ; |
wbeaumont | 0:e71d3ecdd257 | 145 | set_bits_reg( ALS_CONF_0,ie, ALS_SD_LSB,ALS_SD_SIZE ); |
wbeaumont | 0:e71d3ecdd257 | 146 | } |
wbeaumont | 0:e71d3ecdd257 | 147 | |
wbeaumont | 0:e71d3ecdd257 | 148 | void VEML7700::set_int_enable( bool int_enable) { |
wbeaumont | 0:e71d3ecdd257 | 149 | u16 ie= int_enable ? 0 :1 ; |
wbeaumont | 0:e71d3ecdd257 | 150 | set_bits_reg( ALS_CONF_0,ie, ALS_INT_EN_LSB,ALS_INT_EN_SIZE ); |
wbeaumont | 0:e71d3ecdd257 | 151 | } |
wbeaumont | 0:e71d3ecdd257 | 152 | |
wbeaumont | 0:e71d3ecdd257 | 153 | void VEML7700::set_gain_bits( u16 gbits ) { |
wbeaumont | 0:e71d3ecdd257 | 154 | set_bits_reg ( ALS_CONF_0 , gbits , ALS_GAIN_LSB,ALS_GAIN_SIZE ); |
wbeaumont | 0:e71d3ecdd257 | 155 | } |
wbeaumont | 0:e71d3ecdd257 | 156 | |
wbeaumont | 0:e71d3ecdd257 | 157 | void VEML7700::set_IntT_bits( u16 InTgbits ) { |
wbeaumont | 0:e71d3ecdd257 | 158 | set_bits_reg ( ALS_CONF_0 , InTgbits , ALS_IT_LSB,ALS_IT_SIZE ); |
wbeaumont | 0:e71d3ecdd257 | 159 | } |
wbeaumont | 0:e71d3ecdd257 | 160 | |
wbeaumont | 0:e71d3ecdd257 | 161 | |
wbeaumont | 0:e71d3ecdd257 | 162 | void VEML7700::set_default_als_config ( bool shutdown , bool int_enable ,u16 pres, u16 integrationtime, u16 gain ) { |
wbeaumont | 0:e71d3ecdd257 | 163 | u16 setvalue = shutdown? 1:0; //set shutdown |
wbeaumont | 0:e71d3ecdd257 | 164 | u16 uinterrupt= int_enable ?1:0; |
wbeaumont | 0:e71d3ecdd257 | 165 | setvalue=set_bits( setvalue , uinterrupt , ALS_INT_EN_LSB,ALS_INT_EN_SIZE); |
wbeaumont | 0:e71d3ecdd257 | 166 | setvalue=set_bits( setvalue , pres , ALS_PERS_LSB,ALS_PERS_SIZE); |
wbeaumont | 0:e71d3ecdd257 | 167 | setvalue=set_bits( setvalue , integrationtime , ALS_IT_LSB,ALS_IT_SIZE); |
wbeaumont | 0:e71d3ecdd257 | 168 | setvalue=set_bits( setvalue , gain , ALS_GAIN_LSB,ALS_GAIN_SIZE); |
wbeaumont | 0:e71d3ecdd257 | 169 | setvalue &= ~RESERVED_BIT_MSK_ALS_CONF_0; |
wbeaumont | 0:e71d3ecdd257 | 170 | write_cmd( ALS_CONF_0, setvalue ) ;// it will et all the bits so no need to read first |
wbeaumont | 0:e71d3ecdd257 | 171 | } |
wbeaumont | 0:e71d3ecdd257 | 172 | |
wbeaumont | 0:e71d3ecdd257 | 173 | void VEML7700::set_power_saving_enable( bool ps_enable) { |
wbeaumont | 0:e71d3ecdd257 | 174 | u16 pse=ps_enable ? 0 :1 ; |
wbeaumont | 0:e71d3ecdd257 | 175 | set_bits_reg( POW_SET ,pse, PSM_EN_LSB, PSM_EN_SIZE ); |
wbeaumont | 0:e71d3ecdd257 | 176 | } |
wbeaumont | 0:e71d3ecdd257 | 177 | |
wbeaumont | 0:e71d3ecdd257 | 178 | void VEML7700::set_power_saving_mode( u16 psmode) { |
wbeaumont | 0:e71d3ecdd257 | 179 | set_bits_reg( POW_SET ,psmode, PSM_LSB, PSM_SIZE ); |
wbeaumont | 0:e71d3ecdd257 | 180 | } |
wbeaumont | 0:e71d3ecdd257 | 181 | |
wbeaumont | 0:e71d3ecdd257 | 182 | void VEML7700::set_default_powermode ( bool ps_enable , u16 psmode) { |
wbeaumont | 0:e71d3ecdd257 | 183 | u16 setvalue = ps_enable ? 0:1; |
wbeaumont | 0:e71d3ecdd257 | 184 | setvalue = set_bits( setvalue, psmode,PSM_LSB, PSM_SIZE ); |
wbeaumont | 0:e71d3ecdd257 | 185 | setvalue &= ~ ~RESERVED_BIT_MSK_POW_SET; |
wbeaumont | 0:e71d3ecdd257 | 186 | write_cmd( POW_SET , setvalue ) ; |
wbeaumont | 0:e71d3ecdd257 | 187 | } |
wbeaumont | 0:e71d3ecdd257 | 188 | |
wbeaumont | 0:e71d3ecdd257 | 189 | // error handling via DevErrorReporter |
wbeaumont | 0:e71d3ecdd257 | 190 | u16 VEML7700::read_cmd ( u8 reg) { |
wbeaumont | 0:e71d3ecdd257 | 191 | /* seems this doesn work returns always zero |
wbeaumont | 0:e71d3ecdd257 | 192 | printf("call read_cmd "); |
wbeaumont | 0:e71d3ecdd257 | 193 | char readvalue[2]; // max 2 values read |
wbeaumont | 0:e71d3ecdd257 | 194 | // first set the reg pointer . not sure if this works |
wbeaumont | 0:e71d3ecdd257 | 195 | readvalue[0] = (char) reg; |
wbeaumont | 0:e71d3ecdd257 | 196 | int err= i2cdev->write (i2caddr,readvalue, 1,false) ; // Write reg to an I2C slave. 3 words with stop |
wbeaumont | 0:e71d3ecdd257 | 197 | if (err) { printf("VELM7700 %d i2c err %d in %d \n\r",err, __LINE__); } |
wbeaumont | 0:e71d3ecdd257 | 198 | //now read |
wbeaumont | 0:e71d3ecdd257 | 199 | err = i2cdev->read( i2caddr, readvalue , 2, false) ;// read from the i2c dev with stop |
wbeaumont | 0:e71d3ecdd257 | 200 | if (err) {printf("VELM7700 %d i2c err %d in %d \n\r",err, __LINE__); } |
wbeaumont | 0:e71d3ecdd257 | 201 | printf(" %x %x \n\r", (int) readvalue[0], (int) readvalue[1]); |
wbeaumont | 0:e71d3ecdd257 | 202 | u16 rv= readvalue[1]; rv=rv<<8; |
wbeaumont | 0:e71d3ecdd257 | 203 | rv|= readvalue[0]; |
wbeaumont | 0:e71d3ecdd257 | 204 | return rv; |
wbeaumont | 0:e71d3ecdd257 | 205 | */ |
wbeaumont | 0:e71d3ecdd257 | 206 | char readvalue[2]; |
wbeaumont | 0:e71d3ecdd257 | 207 | i2cdev-> read_reg( i2caddr , readvalue , 2, (int) reg, 1); |
wbeaumont | 0:e71d3ecdd257 | 208 | u16 rv= readvalue[1]; rv=rv<<8; |
wbeaumont | 0:e71d3ecdd257 | 209 | rv|= readvalue[0]; |
wbeaumont | 0:e71d3ecdd257 | 210 | return rv; |
wbeaumont | 0:e71d3ecdd257 | 211 | |
wbeaumont | 0:e71d3ecdd257 | 212 | } |
wbeaumont | 0:e71d3ecdd257 | 213 | |
wbeaumont | 0:e71d3ecdd257 | 214 | |
wbeaumont | 0:e71d3ecdd257 | 215 | |
wbeaumont | 0:e71d3ecdd257 | 216 | // error handling via DevErrorReporter |
wbeaumont | 0:e71d3ecdd257 | 217 | void VEML7700::write_cmd( u8 reg , u16 value ){ |
wbeaumont | 0:e71d3ecdd257 | 218 | char writevalue[3]; // max 3 values to write |
wbeaumont | 0:e71d3ecdd257 | 219 | writevalue[0] = (char) reg; |
wbeaumont | 0:e71d3ecdd257 | 220 | writevalue[1] = (char)( value & 0xFF); //Write LSByte first see data I2C interface |
wbeaumont | 0:e71d3ecdd257 | 221 | value =value >>8; |
wbeaumont | 0:e71d3ecdd257 | 222 | writevalue[2] = (char)( value & 0xFF); //MSByte |
wbeaumont | 0:e71d3ecdd257 | 223 | i2cdev->write(i2caddr,writevalue, 3,false) ; // Write to an I2C slave. 3 words with stop |
wbeaumont | 0:e71d3ecdd257 | 224 | } |
wbeaumont | 0:e71d3ecdd257 | 225 | |
wbeaumont | 0:e71d3ecdd257 | 226 | |
wbeaumont | 0:e71d3ecdd257 | 227 | u16 VEML7700::get_IntT_in_set_bit( int time_ms) { |
wbeaumont | 0:e71d3ecdd257 | 228 | for ( IntTnr= 0;IntTnr <NrIntT ;IntTnr++){ |
wbeaumont | 0:e71d3ecdd257 | 229 | if( time_ms < IntTs[IntTnr] ) break; |
wbeaumont | 0:e71d3ecdd257 | 230 | } |
wbeaumont | 0:e71d3ecdd257 | 231 | if( IntTnr == NrIntT ) { IntTnr--;} |
wbeaumont | 0:e71d3ecdd257 | 232 | return IntTSets[IntTnr]; |
wbeaumont | 0:e71d3ecdd257 | 233 | } |
wbeaumont | 0:e71d3ecdd257 | 234 | |
wbeaumont | 0:e71d3ecdd257 | 235 | void VEML7700::set_integrationtime( int time_ms ) { |
wbeaumont | 0:e71d3ecdd257 | 236 | u16 gb=get_IntT_in_set_bit( time_ms); |
wbeaumont | 0:e71d3ecdd257 | 237 | set_IntT_bits( gb); |
wbeaumont | 0:e71d3ecdd257 | 238 | } |
wbeaumont | 0:e71d3ecdd257 | 239 | |
wbeaumont | 0:e71d3ecdd257 | 240 | |
wbeaumont | 0:e71d3ecdd257 | 241 | void VEML7700::set_gain( float gain ) { |
wbeaumont | 0:e71d3ecdd257 | 242 | u16 gb= get_gain_in_set_bit ( gain ); |
wbeaumont | 0:e71d3ecdd257 | 243 | set_gain_bits( gb); |
wbeaumont | 0:e71d3ecdd257 | 244 | } |
wbeaumont | 0:e71d3ecdd257 | 245 | |
wbeaumont | 0:e71d3ecdd257 | 246 | |
wbeaumont | 0:e71d3ecdd257 | 247 | u16 VEML7700::get_gain_in_set_bit (float gainsel ) { |
wbeaumont | 0:e71d3ecdd257 | 248 | for ( gain_nr = 0; gain_nr < NrGains; gain_nr++ ){ |
wbeaumont | 0:e71d3ecdd257 | 249 | if ( gainsel <= gains[gain_nr] ) { break;} |
wbeaumont | 0:e71d3ecdd257 | 250 | } |
wbeaumont | 0:e71d3ecdd257 | 251 | if( gain_nr == NrGains) { gain_nr--;}; |
wbeaumont | 0:e71d3ecdd257 | 252 | return GainSets[gain_nr]; |
wbeaumont | 0:e71d3ecdd257 | 253 | } |
wbeaumont | 0:e71d3ecdd257 | 254 | |
wbeaumont | 0:e71d3ecdd257 | 255 | |
wbeaumont | 0:e71d3ecdd257 | 256 | u16 VEML7700::get_als_bits(void){ |
wbeaumont | 0:e71d3ecdd257 | 257 | return read_cmd(ALS ); |
wbeaumont | 0:e71d3ecdd257 | 258 | } |
wbeaumont | 0:e71d3ecdd257 | 259 | |
wbeaumont | 0:e71d3ecdd257 | 260 | u16 VEML7700::get_white_ch_bits(void){ |
wbeaumont | 0:e71d3ecdd257 | 261 | return read_cmd(WHITE ); |
wbeaumont | 0:e71d3ecdd257 | 262 | } |
wbeaumont | 1:d6eb62dc0a1e | 263 | |
wbeaumont | 1:d6eb62dc0a1e | 264 | |
wbeaumont | 1:d6eb62dc0a1e | 265 | float VEML7700::get_lux(bool ALSreg , bool verify_reg ) { |
wbeaumont | 1:d6eb62dc0a1e | 266 | u16 reg= ALSreg ? ALS: WHITE; |
wbeaumont | 1:d6eb62dc0a1e | 267 | u16 res = read_cmd(reg); |
wbeaumont | 1:d6eb62dc0a1e | 268 | if ( verify_reg ) { |
wbeaumont | 1:d6eb62dc0a1e | 269 | reg= ALS_CONF_0; |
wbeaumont | 1:d6eb62dc0a1e | 270 | reg =read_cmd(reg); |
wbeaumont | 1:d6eb62dc0a1e | 271 | (void) decodeIntTbits( reg,IntTnr ); // just set this correct |
wbeaumont | 1:d6eb62dc0a1e | 272 | (void) decodeIntTbits( reg,gain_nr ); // just set gain_nr correct |
wbeaumont | 1:d6eb62dc0a1e | 273 | } |
wbeaumont | 1:d6eb62dc0a1e | 274 | |
wbeaumont | 1:d6eb62dc0a1e | 275 | return (float)res *luxref / gains[gain_nr]*RefIntT /(float) IntTs[IntTnr] ; |
wbeaumont | 1:d6eb62dc0a1e | 276 | } |
wbeaumont | 1:d6eb62dc0a1e | 277 | |
wbeaumont | 1:d6eb62dc0a1e | 278 | |
wbeaumont | 0:e71d3ecdd257 | 279 | // below are not necessay but useful fpr debugging |
wbeaumont | 0:e71d3ecdd257 | 280 | u16 VEML7700::get_bits(u16 regvalue, u16 lsb ,u16 bsize ) { |
wbeaumont | 0:e71d3ecdd257 | 281 | u16 mask = 1; |
wbeaumont | 0:e71d3ecdd257 | 282 | mask = mask << bsize ; mask = mask -1; // so bsize=3 give now mask 0 0111 |
wbeaumont | 0:e71d3ecdd257 | 283 | mask = mask << lsb; // put the bits in the correct place |
wbeaumont | 0:e71d3ecdd257 | 284 | regvalue &= mask ; //truncate if to big |
wbeaumont | 0:e71d3ecdd257 | 285 | mask = 1; |
wbeaumont | 0:e71d3ecdd257 | 286 | mask = mask << bsize ; mask = mask -1; // so bsize=3 give now mask 0 0111 |
wbeaumont | 0:e71d3ecdd257 | 287 | regvalue = regvalue >> lsb; // set the value bits in the correct place. |
wbeaumont | 0:e71d3ecdd257 | 288 | return regvalue & mask; |
wbeaumont | 0:e71d3ecdd257 | 289 | } |
wbeaumont | 0:e71d3ecdd257 | 290 | |
wbeaumont | 0:e71d3ecdd257 | 291 | float VEML7700::decodeGainBits( u16 gb) { |
wbeaumont | 1:d6eb62dc0a1e | 292 | int dummy; |
wbeaumont | 1:d6eb62dc0a1e | 293 | return decodeGainBits( gb, dummy ) ; |
wbeaumont | 1:d6eb62dc0a1e | 294 | } |
wbeaumont | 1:d6eb62dc0a1e | 295 | |
wbeaumont | 1:d6eb62dc0a1e | 296 | float VEML7700::decodeGainBits( u16 gb, int& g_nr) { |
wbeaumont | 1:d6eb62dc0a1e | 297 | // this function doesn't set the gain_nr as it can be used for checking only |
wbeaumont | 0:e71d3ecdd257 | 298 | for ( g_nr = 0; g_nr < NrGains; g_nr++ ){ |
wbeaumont | 0:e71d3ecdd257 | 299 | if ( gb == GainSets[g_nr] ) { break;} |
wbeaumont | 0:e71d3ecdd257 | 300 | } |
wbeaumont | 1:d6eb62dc0a1e | 301 | if( g_nr >= NrGains) { g_nr = NrGains-1;}; |
wbeaumont | 1:d6eb62dc0a1e | 302 | return gains[g_nr]; |
wbeaumont | 0:e71d3ecdd257 | 303 | } |
wbeaumont | 0:e71d3ecdd257 | 304 | |
wbeaumont | 1:d6eb62dc0a1e | 305 | int VEML7700::decodeIntTbits( u16 ib) { |
wbeaumont | 1:d6eb62dc0a1e | 306 | int dummy; |
wbeaumont | 1:d6eb62dc0a1e | 307 | return decodeIntTbits( ib,dummy ); |
wbeaumont | 1:d6eb62dc0a1e | 308 | } |
wbeaumont | 1:d6eb62dc0a1e | 309 | |
wbeaumont | 1:d6eb62dc0a1e | 310 | int VEML7700::decodeIntTbits( u16 ib, int &In_nr ) { |
wbeaumont | 1:d6eb62dc0a1e | 311 | // this function doesn't set the IntTnr as it can be used for checking only |
wbeaumont | 0:e71d3ecdd257 | 312 | for ( In_nr= 0;In_nr < NrIntT ;In_nr++){ |
wbeaumont | 0:e71d3ecdd257 | 313 | if( ib == IntTSets[In_nr] ){ break;} |
wbeaumont | 0:e71d3ecdd257 | 314 | } |
wbeaumont | 1:d6eb62dc0a1e | 315 | if ( In_nr >= NrIntT) { In_nr= NrIntT-1; }; |
wbeaumont | 1:d6eb62dc0a1e | 316 | return IntTs[In_nr]; |
wbeaumont | 0:e71d3ecdd257 | 317 | } |
wbeaumont | 0:e71d3ecdd257 | 318 | |
wbeaumont | 1:d6eb62dc0a1e | 319 | u16 VEML7700::decode_Reg0( bool& sd ,bool& ie, u16& pers_protect,int& IntT, float& gain ) { |
wbeaumont | 0:e71d3ecdd257 | 320 | u16 reg= read_cmd(ALS_CONF_0); |
wbeaumont | 0:e71d3ecdd257 | 321 | u16 res= get_bits(reg, ALS_SD_LSB , ALS_SD_SIZE ) ; |
wbeaumont | 1:d6eb62dc0a1e | 322 | sd = res ? false : true; |
wbeaumont | 0:e71d3ecdd257 | 323 | res= get_bits(reg,ALS_INT_EN_LSB , ALS_INT_EN_SIZE ) ; |
wbeaumont | 1:d6eb62dc0a1e | 324 | ie = res ? false : true; |
wbeaumont | 0:e71d3ecdd257 | 325 | pers_protect= get_bits(reg,ALS_PERS_LSB , ALS_PERS_SIZE ) ; |
wbeaumont | 0:e71d3ecdd257 | 326 | res= get_bits(reg,ALS_IT_LSB , ALS_IT_SIZE ) ; |
wbeaumont | 0:e71d3ecdd257 | 327 | IntT=decodeIntTbits(res); |
wbeaumont | 0:e71d3ecdd257 | 328 | res= get_bits(reg,ALS_GAIN_LSB , ALS_GAIN_SIZE ) ; |
wbeaumont | 0:e71d3ecdd257 | 329 | gain=decodeGainBits(res); |
wbeaumont | 1:d6eb62dc0a1e | 330 | return reg; |
wbeaumont | 0:e71d3ecdd257 | 331 | } |