Gas Pressure Display Updated Power control for Pressure sensor added

Dependencies:   UniGraphic mbed vt100

Committer:
Rhyme
Date:
Fri Feb 16 08:27:50 2018 +0000
Revision:
0:37c8ecde13c2
control PSE530 power via PTC5 (pse530_en)

Who changed what in which revision?

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