Color sensor reset at the end of calibration added. sensor id auto assignment was changed to be a fixed value assignment to avoid sensor id shift when some sensor is absent.

Dependencies:   UniGraphic mbed vt100

Committer:
Rhyme
Date:
Fri Feb 23 07:51:55 2018 +0000
Revision:
1:8818b793d147
Parent:
0:ce97f6d34336
Wrong behavior when one of color sensor is missing has been fixed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:ce97f6d34336 1 /*
Rhyme 0:ce97f6d34336 2 * File description here
Rhyme 0:ce97f6d34336 3 */
Rhyme 0:ce97f6d34336 4 #include "VEML6040.h"
Rhyme 0:ce97f6d34336 5 #include "af_mgr.h"
Rhyme 0:ce97f6d34336 6
Rhyme 0:ce97f6d34336 7 /* VEML6075 SLAVE ADDRESS AND FUNCTION DESCRIPTION */
Rhyme 0:ce97f6d34336 8 #define REG_COLOR_CONF 0x00
Rhyme 0:ce97f6d34336 9 #define REG_Reserved1 0x01
Rhyme 0:ce97f6d34336 10 #define REG_Reserved2 0x02
Rhyme 0:ce97f6d34336 11 #define REG_Reserved3 0x03
Rhyme 0:ce97f6d34336 12 #define REG_Reserved4 0x04
Rhyme 0:ce97f6d34336 13 #define REG_Reserved5 0x05
Rhyme 0:ce97f6d34336 14 #define REG_Reserved6 0x06
Rhyme 0:ce97f6d34336 15 #define REG_Reserved7 0x07
Rhyme 0:ce97f6d34336 16 #define REG_R_Data 0x08
Rhyme 0:ce97f6d34336 17 #define REG_G_Data 0x09
Rhyme 0:ce97f6d34336 18 #define REG_B_Data 0x0A
Rhyme 0:ce97f6d34336 19 #define REG_W_Data 0x0B
Rhyme 0:ce97f6d34336 20
Rhyme 0:ce97f6d34336 21 // Following magic numbers are from
Rhyme 0:ce97f6d34336 22 // VISHAY VEML6040 Application Note 84331
Rhyme 0:ce97f6d34336 23 // Page 4
Rhyme 0:ce97f6d34336 24 #define LUX_RESOLUTION_0 (0.25168)
Rhyme 0:ce97f6d34336 25 #define LUX_RESOLUTION_1 (0.12584)
Rhyme 0:ce97f6d34336 26 #define LUX_RESOLUTION_2 (0.06292)
Rhyme 0:ce97f6d34336 27 #define LUX_RESOLUTION_3 (0.03146)
Rhyme 0:ce97f6d34336 28 #define LUX_RESOLUTION_4 (0.01573)
Rhyme 0:ce97f6d34336 29 #define LUX_RESOLUTION_5 (0.007865)
Rhyme 0:ce97f6d34336 30
Rhyme 0:ce97f6d34336 31 // Following magic numbers are from
Rhyme 0:ce97f6d34336 32 // VISHAY VEML6040 Application Note 84331
Rhyme 0:ce97f6d34336 33 // Page 9
Rhyme 0:ce97f6d34336 34 #define CORR_COEFF_M0 (0.048403)
Rhyme 0:ce97f6d34336 35 #define CORR_COEFF_M1 (0.183633)
Rhyme 0:ce97f6d34336 36 #define CORR_COEFF_M2 (-0.253589)
Rhyme 0:ce97f6d34336 37 #define CORR_COEFF_M3 (0.022916)
Rhyme 0:ce97f6d34336 38 #define CORR_COEFF_M4 (0.176388)
Rhyme 0:ce97f6d34336 39 #define CORR_COEFF_M5 (-0.183205)
Rhyme 0:ce97f6d34336 40 #define CORR_COEFF_M6 (-0.077436)
Rhyme 0:ce97f6d34336 41 #define CORR_COEFF_M7 (0.124541)
Rhyme 0:ce97f6d34336 42 #define CORR_COEFF_M8 (0.032081)
Rhyme 0:ce97f6d34336 43
Rhyme 0:ce97f6d34336 44 // Following magic numbers are from
Rhyme 0:ce97f6d34336 45 // VISHAY VEML6040 Application Note 84331
Rhyme 0:ce97f6d34336 46 // Page 10
Rhyme 0:ce97f6d34336 47 #define CCT_CONST (4278.6)
Rhyme 0:ce97f6d34336 48 #define OFFSET_OPEN_AIR (0.5)
Rhyme 0:ce97f6d34336 49
Rhyme 0:ce97f6d34336 50 VEML6040::VEML6040(I2C *i2c, int addr) : m_addr(addr<<1) {
Rhyme 0:ce97f6d34336 51 p_i2c = i2c ;
Rhyme 0:ce97f6d34336 52 p_i2c->frequency(100000); /* 100kHz */
Rhyme 0:ce97f6d34336 53 // activate the peripheral
Rhyme 0:ce97f6d34336 54 }
Rhyme 0:ce97f6d34336 55
Rhyme 0:ce97f6d34336 56 VEML6040::~VEML6040() { }
Rhyme 0:ce97f6d34336 57
Rhyme 0:ce97f6d34336 58 /**
Rhyme 0:ce97f6d34336 59 * set COLOR Config
Rhyme 0:ce97f6d34336 60 * @param colorconf uint8_t 8bit register value
Rhyme 0:ce97f6d34336 61 * @returns 0: success non-0: failure
Rhyme 0:ce97f6d34336 62 * @note Command Code 0x00 is used to access CONF register
Rhyme 0:ce97f6d34336 63 * @note bit[7] (reserved)
Rhyme 0:ce97f6d34336 64 * @note bit[6:4] = IT[2:0] Integration Time Selector
Rhyme 0:ce97f6d34336 65 * @note bit[3] (reserved)
Rhyme 0:ce97f6d34336 66 * @note bit[2] TRIG Proceed one detcting cycle at manual force mode
Rhyme 0:ce97f6d34336 67 * @note bit[1] AF 0: Auto mode 1: manual force mode
Rhyme 0:ce97f6d34336 68 * @note bit[0] SD 0: normal 1: chip shutdown setting
Rhyme 0:ce97f6d34336 69 *
Rhyme 0:ce97f6d34336 70 * @note IT[2:0] 0=40ms, 1=80ms, 2=160ms, 3=320ms, 4=640ms, 5=1280ms
Rhyme 0:ce97f6d34336 71 * @note as our WatchDog is set to 1sec, 1280ms is invalid
Rhyme 0:ce97f6d34336 72 * @note and 640ms may not be practical
Rhyme 0:ce97f6d34336 73 */
Rhyme 0:ce97f6d34336 74 int VEML6040::setCOLORConf(uint8_t colorconf)
Rhyme 0:ce97f6d34336 75 {
Rhyme 0:ce97f6d34336 76 int result ;
Rhyme 0:ce97f6d34336 77 uint8_t data[3] ;
Rhyme 0:ce97f6d34336 78 data[0] = REG_COLOR_CONF ;
Rhyme 0:ce97f6d34336 79 data[1] = colorconf ;
Rhyme 0:ce97f6d34336 80 data[2] = 0 ;
Rhyme 0:ce97f6d34336 81 result = writeRegs(data, 3) ;
Rhyme 0:ce97f6d34336 82 return( result ) ;
Rhyme 0:ce97f6d34336 83 }
Rhyme 0:ce97f6d34336 84
Rhyme 0:ce97f6d34336 85 /**
Rhyme 0:ce97f6d34336 86 * get COLOR Config
Rhyme 0:ce97f6d34336 87 * @param *colorconf uint8_t refer to setCOLORConf for the value
Rhyme 0:ce97f6d34336 88 * @returns 0: success non-0: failure
Rhyme 0:ce97f6d34336 89 */
Rhyme 0:ce97f6d34336 90 int VEML6040::getCOLORConf(uint8_t *colorconf)
Rhyme 0:ce97f6d34336 91 {
Rhyme 0:ce97f6d34336 92 int result ;
Rhyme 0:ce97f6d34336 93 uint8_t data[2] ;
Rhyme 0:ce97f6d34336 94 result = readRegs(REG_COLOR_CONF, data, 2) ;
Rhyme 0:ce97f6d34336 95 if (result == 0) {
Rhyme 0:ce97f6d34336 96 *colorconf = data[0] ;
Rhyme 0:ce97f6d34336 97 }
Rhyme 0:ce97f6d34336 98 return( result ) ;
Rhyme 0:ce97f6d34336 99 }
Rhyme 0:ce97f6d34336 100
Rhyme 0:ce97f6d34336 101
Rhyme 0:ce97f6d34336 102
Rhyme 0:ce97f6d34336 103 int VEML6040::getRData(uint16_t *rdata)
Rhyme 0:ce97f6d34336 104 {
Rhyme 0:ce97f6d34336 105 uint8_t data[2] ;
Rhyme 0:ce97f6d34336 106 int result ;
Rhyme 0:ce97f6d34336 107 result = readRegs(REG_R_Data, data, 2) ;
Rhyme 0:ce97f6d34336 108 *rdata = (data[1]<<8) | data[0] ;
Rhyme 0:ce97f6d34336 109 return( result ) ;
Rhyme 0:ce97f6d34336 110 }
Rhyme 0:ce97f6d34336 111
Rhyme 0:ce97f6d34336 112 int VEML6040::getGData(uint16_t *gdata)
Rhyme 0:ce97f6d34336 113 {
Rhyme 0:ce97f6d34336 114 uint8_t data[2] ;
Rhyme 0:ce97f6d34336 115 int result ;
Rhyme 0:ce97f6d34336 116 result = readRegs(REG_G_Data, data, 2) ;
Rhyme 0:ce97f6d34336 117 *gdata = (data[1]<<8) | data[0] ;
Rhyme 0:ce97f6d34336 118 return( result ) ;
Rhyme 0:ce97f6d34336 119 }
Rhyme 0:ce97f6d34336 120
Rhyme 0:ce97f6d34336 121 int VEML6040::getBData(uint16_t *bdata)
Rhyme 0:ce97f6d34336 122 {
Rhyme 0:ce97f6d34336 123 uint8_t data[2] ;
Rhyme 0:ce97f6d34336 124 int result ;
Rhyme 0:ce97f6d34336 125 result = readRegs(REG_B_Data, data, 2) ;
Rhyme 0:ce97f6d34336 126 *bdata = (data[1]<<8) | data[0] ;
Rhyme 0:ce97f6d34336 127 return( result ) ;
Rhyme 0:ce97f6d34336 128 }
Rhyme 0:ce97f6d34336 129
Rhyme 0:ce97f6d34336 130 int VEML6040::getWData(uint16_t *wdata)
Rhyme 0:ce97f6d34336 131 {
Rhyme 0:ce97f6d34336 132 uint8_t data[2] ;
Rhyme 0:ce97f6d34336 133 int result ;
Rhyme 0:ce97f6d34336 134 result = readRegs(REG_W_Data, data, 2) ;
Rhyme 0:ce97f6d34336 135 *wdata = (data[1]<<8) | data[0] ;
Rhyme 0:ce97f6d34336 136 return( result ) ;
Rhyme 0:ce97f6d34336 137 }
Rhyme 0:ce97f6d34336 138
Rhyme 0:ce97f6d34336 139 // usage
Rhyme 0:ce97f6d34336 140 // fvalue = veml->getUVA() ;
Rhyme 0:ce97f6d34336 141 // printf("%f", fvalue) ;
Rhyme 0:ce97f6d34336 142 float VEML6040::getR(void)
Rhyme 0:ce97f6d34336 143 {
Rhyme 0:ce97f6d34336 144 uint16_t data ;
Rhyme 0:ce97f6d34336 145 float value ;
Rhyme 0:ce97f6d34336 146 getRData(&data) ;
Rhyme 0:ce97f6d34336 147 value = (float)LUX_RESOLUTION_0 * (float)data ;
Rhyme 0:ce97f6d34336 148 return( value ) ;
Rhyme 0:ce97f6d34336 149 }
Rhyme 0:ce97f6d34336 150
Rhyme 0:ce97f6d34336 151 float VEML6040::getG(void)
Rhyme 0:ce97f6d34336 152 {
Rhyme 0:ce97f6d34336 153 uint16_t data ;
Rhyme 0:ce97f6d34336 154 float value ;
Rhyme 0:ce97f6d34336 155 getGData(&data) ;
Rhyme 0:ce97f6d34336 156 value = (float)LUX_RESOLUTION_0 * (float)data ;
Rhyme 0:ce97f6d34336 157 return( value ) ;
Rhyme 0:ce97f6d34336 158 }
Rhyme 0:ce97f6d34336 159
Rhyme 0:ce97f6d34336 160 float VEML6040::getB(void)
Rhyme 0:ce97f6d34336 161 {
Rhyme 0:ce97f6d34336 162 uint16_t data ;
Rhyme 0:ce97f6d34336 163 float value ;
Rhyme 0:ce97f6d34336 164 getBData(&data) ;
Rhyme 0:ce97f6d34336 165 value = (float)LUX_RESOLUTION_0 * (float)data ;
Rhyme 0:ce97f6d34336 166 return( value ) ;
Rhyme 0:ce97f6d34336 167 }
Rhyme 0:ce97f6d34336 168
Rhyme 0:ce97f6d34336 169 float VEML6040::getW(void)
Rhyme 0:ce97f6d34336 170 {
Rhyme 0:ce97f6d34336 171 uint16_t data ;
Rhyme 0:ce97f6d34336 172 float value ;
Rhyme 0:ce97f6d34336 173 getWData(&data) ;
Rhyme 0:ce97f6d34336 174 value = (float)LUX_RESOLUTION_0 * (float)data ;
Rhyme 0:ce97f6d34336 175 return( value ) ;
Rhyme 0:ce97f6d34336 176 }
Rhyme 0:ce97f6d34336 177
Rhyme 0:ce97f6d34336 178 float VEML6040::getX(void)
Rhyme 0:ce97f6d34336 179 {
Rhyme 0:ce97f6d34336 180 uint16_t R ;
Rhyme 0:ce97f6d34336 181 uint16_t G ;
Rhyme 0:ce97f6d34336 182 uint16_t B ;
Rhyme 0:ce97f6d34336 183 float value ;
Rhyme 0:ce97f6d34336 184 getRData(&R) ;
Rhyme 0:ce97f6d34336 185 getGData(&G) ;
Rhyme 0:ce97f6d34336 186 getBData(&B) ;
Rhyme 0:ce97f6d34336 187 value = (float)CORR_COEFF_M0 * (float)R + (float)CORR_COEFF_M1 * (float)G + (float)CORR_COEFF_M2 * (float)B ;
Rhyme 0:ce97f6d34336 188 return( value ) ;
Rhyme 0:ce97f6d34336 189 }
Rhyme 0:ce97f6d34336 190
Rhyme 0:ce97f6d34336 191 float VEML6040::getY(void)
Rhyme 0:ce97f6d34336 192 {
Rhyme 0:ce97f6d34336 193 uint16_t R ;
Rhyme 0:ce97f6d34336 194 uint16_t G ;
Rhyme 0:ce97f6d34336 195 uint16_t B ;
Rhyme 0:ce97f6d34336 196 float value ;
Rhyme 0:ce97f6d34336 197 getRData(&R) ;
Rhyme 0:ce97f6d34336 198 getGData(&G) ;
Rhyme 0:ce97f6d34336 199 getBData(&B) ;
Rhyme 0:ce97f6d34336 200 value = (float)CORR_COEFF_M3 * (float)R + (float)CORR_COEFF_M4 * (float)G + (float)CORR_COEFF_M5 * (float)B ;
Rhyme 0:ce97f6d34336 201 return( value ) ;
Rhyme 0:ce97f6d34336 202 }
Rhyme 0:ce97f6d34336 203
Rhyme 0:ce97f6d34336 204 float VEML6040::getZ(void)
Rhyme 0:ce97f6d34336 205 {
Rhyme 0:ce97f6d34336 206 uint16_t R ;
Rhyme 0:ce97f6d34336 207 uint16_t G ;
Rhyme 0:ce97f6d34336 208 uint16_t B ;
Rhyme 0:ce97f6d34336 209 float value ;
Rhyme 0:ce97f6d34336 210 getRData(&R) ;
Rhyme 0:ce97f6d34336 211 getGData(&G) ;
Rhyme 0:ce97f6d34336 212 getBData(&B) ;
Rhyme 0:ce97f6d34336 213 value = (float)CORR_COEFF_M6 * (float)R + (float)CORR_COEFF_M7 * (float)G + (float)CORR_COEFF_M8 * (float)B ;
Rhyme 0:ce97f6d34336 214 return( value ) ;
Rhyme 0:ce97f6d34336 215 }
Rhyme 0:ce97f6d34336 216
Rhyme 0:ce97f6d34336 217 float VEML6040::getCCTiData(void)
Rhyme 0:ce97f6d34336 218 {
Rhyme 0:ce97f6d34336 219 uint16_t rdata ;
Rhyme 0:ce97f6d34336 220 uint16_t gdata ;
Rhyme 0:ce97f6d34336 221 uint16_t bdata ;
Rhyme 0:ce97f6d34336 222 float value ;
Rhyme 0:ce97f6d34336 223 getRData(&rdata) ;
Rhyme 0:ce97f6d34336 224 getGData(&gdata) ;
Rhyme 0:ce97f6d34336 225 getBData(&bdata) ;
Rhyme 0:ce97f6d34336 226 value = ((float)rdata - (float)bdata) / (float)gdata + (float)OFFSET_OPEN_AIR ;
Rhyme 0:ce97f6d34336 227 return( value ) ;
Rhyme 0:ce97f6d34336 228 }
Rhyme 0:ce97f6d34336 229
Rhyme 0:ce97f6d34336 230 float VEML6040::getCCTData(void)
Rhyme 0:ce97f6d34336 231 {
Rhyme 0:ce97f6d34336 232 // uint16_t cctidata ;
Rhyme 0:ce97f6d34336 233 float cctidata ;
Rhyme 0:ce97f6d34336 234 float value ;
Rhyme 0:ce97f6d34336 235 cctidata = getCCTiData() ;
Rhyme 0:ce97f6d34336 236 // getCCTiData(&cctidata) ;
Rhyme 0:ce97f6d34336 237 value = (float)CCT_CONST * powf( cctidata, -1.2455 ) ;
Rhyme 0:ce97f6d34336 238 return( value ) ;
Rhyme 0:ce97f6d34336 239 }
Rhyme 0:ce97f6d34336 240
Rhyme 0:ce97f6d34336 241 float VEML6040::getCIEX(void)
Rhyme 0:ce97f6d34336 242 {
Rhyme 0:ce97f6d34336 243 float X ;
Rhyme 0:ce97f6d34336 244 float Y ;
Rhyme 0:ce97f6d34336 245 float Z ;
Rhyme 0:ce97f6d34336 246 float value ;
Rhyme 0:ce97f6d34336 247 X = getX() ;
Rhyme 0:ce97f6d34336 248 Y = getY() ;
Rhyme 0:ce97f6d34336 249 Z = getZ() ;
Rhyme 0:ce97f6d34336 250 value = (float)X / ((float)X + (float)Y + (float)Z) ;
Rhyme 0:ce97f6d34336 251 return( value ) ;
Rhyme 0:ce97f6d34336 252 }
Rhyme 0:ce97f6d34336 253
Rhyme 0:ce97f6d34336 254 float VEML6040::getCIEY(void)
Rhyme 0:ce97f6d34336 255 {
Rhyme 0:ce97f6d34336 256 float X ;
Rhyme 0:ce97f6d34336 257 float Y ;
Rhyme 0:ce97f6d34336 258 float Z ;
Rhyme 0:ce97f6d34336 259 float value ;
Rhyme 0:ce97f6d34336 260 X = getX() ;
Rhyme 0:ce97f6d34336 261 Y = getY() ;
Rhyme 0:ce97f6d34336 262 Z = getZ() ;
Rhyme 0:ce97f6d34336 263 value = (float)Y / ((float)X + (float)Y + (float)Z) ;
Rhyme 0:ce97f6d34336 264 return( value ) ;
Rhyme 0:ce97f6d34336 265 }
Rhyme 0:ce97f6d34336 266
Rhyme 0:ce97f6d34336 267 int VEML6040::readRegs(int addr, uint8_t * data, int len) {
Rhyme 0:ce97f6d34336 268 char t[1] = {addr};
Rhyme 0:ce97f6d34336 269 int result ;
Rhyme 0:ce97f6d34336 270 __disable_irq() ; // Disable Interrupts
Rhyme 0:ce97f6d34336 271 result = p_i2c->write(m_addr, t, 1, true);
Rhyme 0:ce97f6d34336 272 if (result == 0) { // write success
Rhyme 0:ce97f6d34336 273 result = p_i2c->read(m_addr, (char *)data, len, false);
Rhyme 0:ce97f6d34336 274 }
Rhyme 0:ce97f6d34336 275 __enable_irq() ; // Enable Interrupts
Rhyme 0:ce97f6d34336 276 return(result) ;
Rhyme 0:ce97f6d34336 277 }
Rhyme 0:ce97f6d34336 278
Rhyme 0:ce97f6d34336 279 int VEML6040::writeRegs(uint8_t * data, int len) {
Rhyme 0:ce97f6d34336 280 int result ;
Rhyme 0:ce97f6d34336 281 __disable_irq() ; // Disable Interrupts
Rhyme 0:ce97f6d34336 282 result = p_i2c->write(m_addr, (char *)data, len);
Rhyme 0:ce97f6d34336 283 __enable_irq() ; // Enable Interrupts
Rhyme 0:ce97f6d34336 284 return(result) ;
Rhyme 0:ce97f6d34336 285 }