Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)

Dependencies:   UniGraphic mbed vt100

18-Jun-2018 外部センサの電源オン・オフ機能は下位互換の為に無効になっていました。 この版で再度有効にしました。

Committer:
Rhyme
Date:
Mon Jun 18 01:56:00 2018 +0000
Revision:
1:8d65cfc3a2e2
Parent:
0:846e2321c637
External sensor power on/off function enabled. (Previously disabled)

Who changed what in which revision?

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