Vishay UV Sensor I2C library
Dependents: test_VEML6075 quattro_vishay_6075_171018 testSensor
VEML6075.cpp@3:d0d23d5abf34, 2017-05-19 (annotated)
- Committer:
- Rhyme
- Date:
- Fri May 19 05:20:22 2017 +0000
- Revision:
- 3:d0d23d5abf34
- Parent:
- 2:0f3bb80aba40
- Child:
- 4:9518befb9fd3
comment about UVD data added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shockey | 0:14ccc7ed49fa | 1 | /* |
shockey | 0:14ccc7ed49fa | 2 | * File description here |
shockey | 0:14ccc7ed49fa | 3 | */ |
shockey | 0:14ccc7ed49fa | 4 | #include "VEML6075.h" |
shockey | 0:14ccc7ed49fa | 5 | |
shockey | 0:14ccc7ed49fa | 6 | /* VEML6075 SLAVE ADDRESS AND FUNCTION DESCRIPTION */ |
shockey | 0:14ccc7ed49fa | 7 | #define REG_UV_CONF 0x00 |
Rhyme | 1:8bc12629d49b | 8 | #define REG_Reserved01 0x01 |
Rhyme | 1:8bc12629d49b | 9 | #define REG_Reserved02 0x02 |
Rhyme | 1:8bc12629d49b | 10 | #define REG_Reserved03 0x03 |
Rhyme | 1:8bc12629d49b | 11 | #define REG_Reserved04 0x04 |
Rhyme | 1:8bc12629d49b | 12 | #define REG_Reserved05 0x05 |
Rhyme | 1:8bc12629d49b | 13 | #define REG_Reserved06 0x06 |
Rhyme | 2:0f3bb80aba40 | 14 | #define REG_UVA_Data 0x07 |
Rhyme | 3:d0d23d5abf34 | 15 | #define REG_UVD_Data 0x08 |
Rhyme | 2:0f3bb80aba40 | 16 | #define REG_UVB_Data 0x09 |
shockey | 0:14ccc7ed49fa | 17 | #define REG_UVCOMP1_Data 0x0A |
shockey | 0:14ccc7ed49fa | 18 | #define REG_UVCOMP2_Data 0x0B |
shockey | 0:14ccc7ed49fa | 19 | #define REG_ID 0x0C |
shockey | 0:14ccc7ed49fa | 20 | |
shockey | 0:14ccc7ed49fa | 21 | // Following magic numbers are from |
shockey | 0:14ccc7ed49fa | 22 | // VISHAY veml6075 Application Note 84339 |
shockey | 0:14ccc7ed49fa | 23 | // Page 6 |
shockey | 0:14ccc7ed49fa | 24 | #define UVA_A_COEF (2.5) |
shockey | 0:14ccc7ed49fa | 25 | #define UVA_B_COEF (2.5) |
shockey | 0:14ccc7ed49fa | 26 | #define UVB_C_COEF (2.75) |
shockey | 0:14ccc7ed49fa | 27 | #define UVB_D_COEF (2.75) |
shockey | 0:14ccc7ed49fa | 28 | #define UVA_sensitivity (0.93) |
shockey | 0:14ccc7ed49fa | 29 | #define UVA_CIE_sensitivity (0.093) |
shockey | 0:14ccc7ed49fa | 30 | #define UVB_sensitivity (2.1) |
shockey | 0:14ccc7ed49fa | 31 | #define UVB_CIE_sensitivity (0.21) |
shockey | 0:14ccc7ed49fa | 32 | #define UVA_RESPONSIVITY (0.0011) |
shockey | 0:14ccc7ed49fa | 33 | #define UVB_RESPONSIVITY (0.00125) |
shockey | 0:14ccc7ed49fa | 34 | |
shockey | 0:14ccc7ed49fa | 35 | VEML6075::VEML6075(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) { |
shockey | 0:14ccc7ed49fa | 36 | // activate the peripheral |
shockey | 0:14ccc7ed49fa | 37 | } |
shockey | 0:14ccc7ed49fa | 38 | |
shockey | 0:14ccc7ed49fa | 39 | VEML6075::~VEML6075() { } |
shockey | 0:14ccc7ed49fa | 40 | |
shockey | 0:14ccc7ed49fa | 41 | #if 0 |
shockey | 0:14ccc7ed49fa | 42 | // |
shockey | 0:14ccc7ed49fa | 43 | // numdata is supposed to be 2 |
shockey | 0:14ccc7ed49fa | 44 | // |
shockey | 0:14ccc7ed49fa | 45 | void VEML6075::cmdWrite(uint8_t cmd, uint8_t *data, uint8_t numdata) |
shockey | 0:14ccc7ed49fa | 46 | { |
shockey | 0:14ccc7ed49fa | 47 | uint8_t buf[3] ; |
shockey | 0:14ccc7ed49fa | 48 | |
shockey | 0:14ccc7ed49fa | 49 | buf[0] = cmd ; |
shockey | 0:14ccc7ed49fa | 50 | buf[1] = data[0] ; |
shockey | 0:14ccc7ed49fa | 51 | buf[2] = data[1] ; |
Rhyme | 2:0f3bb80aba40 | 52 | writeRegs(buf, 3) ; |
shockey | 0:14ccc7ed49fa | 53 | } |
shockey | 0:14ccc7ed49fa | 54 | |
shockey | 0:14ccc7ed49fa | 55 | // |
shockey | 0:14ccc7ed49fa | 56 | // numdata is supposed to be 2 |
shockey | 0:14ccc7ed49fa | 57 | // |
shockey | 0:14ccc7ed49fa | 58 | |
shockey | 0:14ccc7ed49fa | 59 | void VEML6075::cmdRead(uint8_t cmd, uint8_t *data, uint8_t numdata) |
shockey | 0:14ccc7ed49fa | 60 | { |
shockey | 0:14ccc7ed49fa | 61 | uint8_t buf[1] ; |
shockey | 0:14ccc7ed49fa | 62 | buf[0] = cmd ; |
shockey | 0:14ccc7ed49fa | 63 | m_i2c.write(m_addr, buf, 1, true) ; // writing command |
shockey | 0:14ccc7ed49fa | 64 | m_i2c.read(m_addr, (char*)data, numdata) ; |
shockey | 0:14ccc7ed49fa | 65 | } |
shockey | 0:14ccc7ed49fa | 66 | #endif |
shockey | 0:14ccc7ed49fa | 67 | |
shockey | 0:14ccc7ed49fa | 68 | void VEML6075::getUVConf(uint8_t *uvconf) |
shockey | 0:14ccc7ed49fa | 69 | { |
shockey | 0:14ccc7ed49fa | 70 | uint8_t data[2] ; |
shockey | 0:14ccc7ed49fa | 71 | readRegs(REG_UV_CONF, data, 2) ; |
shockey | 0:14ccc7ed49fa | 72 | *uvconf = data[0] ; |
shockey | 0:14ccc7ed49fa | 73 | } |
shockey | 0:14ccc7ed49fa | 74 | |
shockey | 0:14ccc7ed49fa | 75 | void VEML6075::setUVConf(uint8_t uvconf) |
shockey | 0:14ccc7ed49fa | 76 | { |
shockey | 0:14ccc7ed49fa | 77 | uint8_t data[3] ; |
shockey | 0:14ccc7ed49fa | 78 | data[0] = REG_UV_CONF ; |
shockey | 0:14ccc7ed49fa | 79 | data[1] = uvconf ; |
shockey | 0:14ccc7ed49fa | 80 | data[2] = 0 ; |
shockey | 0:14ccc7ed49fa | 81 | writeRegs(data, 3) ; |
shockey | 0:14ccc7ed49fa | 82 | } |
shockey | 0:14ccc7ed49fa | 83 | |
shockey | 0:14ccc7ed49fa | 84 | |
shockey | 0:14ccc7ed49fa | 85 | void VEML6075::getUVAData(uint16_t *uvadata) |
shockey | 0:14ccc7ed49fa | 86 | { |
shockey | 0:14ccc7ed49fa | 87 | uint8_t data[2] ; |
shockey | 0:14ccc7ed49fa | 88 | readRegs(REG_UVA_Data, data, 2) ; |
shockey | 0:14ccc7ed49fa | 89 | *uvadata = (data[1]<<8) | data[0] ; |
shockey | 0:14ccc7ed49fa | 90 | } |
shockey | 0:14ccc7ed49fa | 91 | |
shockey | 0:14ccc7ed49fa | 92 | void VEML6075::getUVBData(uint16_t *uvbdata) |
shockey | 0:14ccc7ed49fa | 93 | { |
shockey | 0:14ccc7ed49fa | 94 | uint8_t data[2] ; |
shockey | 0:14ccc7ed49fa | 95 | readRegs(REG_UVB_Data, data, 2) ; |
shockey | 0:14ccc7ed49fa | 96 | *uvbdata = (data[1]<<8) | data[0] ; |
shockey | 0:14ccc7ed49fa | 97 | } |
shockey | 0:14ccc7ed49fa | 98 | |
shockey | 0:14ccc7ed49fa | 99 | void VEML6075::getUVDData(uint16_t *uvddata) |
shockey | 0:14ccc7ed49fa | 100 | { |
shockey | 0:14ccc7ed49fa | 101 | uint8_t data[2] ; |
shockey | 0:14ccc7ed49fa | 102 | readRegs(REG_UVD_Data, data, 2) ; |
shockey | 0:14ccc7ed49fa | 103 | *uvddata = (data[1]<<8) | data[0] ; |
shockey | 0:14ccc7ed49fa | 104 | } |
shockey | 0:14ccc7ed49fa | 105 | |
shockey | 0:14ccc7ed49fa | 106 | float VEML6075::UVI(void) |
shockey | 0:14ccc7ed49fa | 107 | { |
shockey | 0:14ccc7ed49fa | 108 | float uvi = 0.0 ; |
shockey | 0:14ccc7ed49fa | 109 | uint16_t uvacomp = 0.0 ; |
shockey | 0:14ccc7ed49fa | 110 | uint16_t uvbcomp = 0.0 ; |
shockey | 0:14ccc7ed49fa | 111 | uint16_t uva, uvb, uvd, uvcomp1, uvcomp2 ; |
shockey | 0:14ccc7ed49fa | 112 | getUVAData(&uva) ; |
shockey | 0:14ccc7ed49fa | 113 | getUVBData(&uvb) ; |
shockey | 0:14ccc7ed49fa | 114 | getUVDData(&uvd) ; |
shockey | 0:14ccc7ed49fa | 115 | getUVCOMP1Data(&uvcomp1) ; |
shockey | 0:14ccc7ed49fa | 116 | getUVCOMP2Data(&uvcomp2) ; |
shockey | 0:14ccc7ed49fa | 117 | uvacomp = (uva - uvd) - UVA_A_COEF * (uvcomp1 - uvd) - UVA_B_COEF * (uvcomp2 - uvd) ; |
shockey | 0:14ccc7ed49fa | 118 | uvbcomp = (uvb - uvd) - UVB_C_COEF * (uvcomp1 - uvd) - UVB_D_COEF * (uvcomp2 - uvd) ; |
shockey | 0:14ccc7ed49fa | 119 | // uvi = (((float)uvbcomp * (float)UVB_RESPONSIVITY) + ((float)uvacomp * (float)UVA_RESPONSIVITY)) / 2.0 * 15 ; // For demo |
shockey | 0:14ccc7ed49fa | 120 | uvi = (((float)uvbcomp * (float)UVB_RESPONSIVITY) + ((float)uvacomp * (float)UVA_RESPONSIVITY)) / 2.0 ; |
shockey | 0:14ccc7ed49fa | 121 | return( uvi ) ; |
shockey | 0:14ccc7ed49fa | 122 | } |
shockey | 0:14ccc7ed49fa | 123 | |
shockey | 0:14ccc7ed49fa | 124 | // usage |
shockey | 0:14ccc7ed49fa | 125 | // fvalue = veml->getUVA() ; |
shockey | 0:14ccc7ed49fa | 126 | // printf("%f", fvalue) ; |
shockey | 0:14ccc7ed49fa | 127 | float VEML6075::getUVA(void) |
shockey | 0:14ccc7ed49fa | 128 | { |
shockey | 0:14ccc7ed49fa | 129 | uint16_t data ; |
shockey | 0:14ccc7ed49fa | 130 | float value ; |
shockey | 0:14ccc7ed49fa | 131 | getUVAData(&data) ; |
shockey | 0:14ccc7ed49fa | 132 | value = (float)data / (float)UVA_sensitivity ; |
shockey | 0:14ccc7ed49fa | 133 | return( value ) ; |
shockey | 0:14ccc7ed49fa | 134 | } |
shockey | 0:14ccc7ed49fa | 135 | |
shockey | 0:14ccc7ed49fa | 136 | float VEML6075::getUVA_CIE(void) |
shockey | 0:14ccc7ed49fa | 137 | { |
shockey | 0:14ccc7ed49fa | 138 | uint16_t data ; |
shockey | 0:14ccc7ed49fa | 139 | float value ; |
shockey | 0:14ccc7ed49fa | 140 | getUVAData(&data) ; |
shockey | 0:14ccc7ed49fa | 141 | value = (float)data / (float)UVA_CIE_sensitivity ; |
shockey | 0:14ccc7ed49fa | 142 | return( value ) ; |
shockey | 0:14ccc7ed49fa | 143 | } |
shockey | 0:14ccc7ed49fa | 144 | |
shockey | 0:14ccc7ed49fa | 145 | float VEML6075::getUVB(void) |
shockey | 0:14ccc7ed49fa | 146 | { |
shockey | 0:14ccc7ed49fa | 147 | uint16_t data ; |
shockey | 0:14ccc7ed49fa | 148 | float value ; |
shockey | 0:14ccc7ed49fa | 149 | getUVBData(&data) ; |
shockey | 0:14ccc7ed49fa | 150 | value = (float)data / (float)UVB_sensitivity ; |
shockey | 0:14ccc7ed49fa | 151 | return( value ) ; |
shockey | 0:14ccc7ed49fa | 152 | } |
shockey | 0:14ccc7ed49fa | 153 | |
shockey | 0:14ccc7ed49fa | 154 | float VEML6075::getUVB_CIE(void) |
shockey | 0:14ccc7ed49fa | 155 | { |
shockey | 0:14ccc7ed49fa | 156 | uint16_t data ; |
shockey | 0:14ccc7ed49fa | 157 | float value ; |
shockey | 0:14ccc7ed49fa | 158 | getUVBData(&data) ; |
shockey | 0:14ccc7ed49fa | 159 | value = (float)data / (float)UVB_CIE_sensitivity ; |
shockey | 0:14ccc7ed49fa | 160 | return( value ) ; |
shockey | 0:14ccc7ed49fa | 161 | } |
shockey | 0:14ccc7ed49fa | 162 | |
shockey | 0:14ccc7ed49fa | 163 | void VEML6075::getUVCOMP1Data(uint16_t *uvcomp1data) |
shockey | 0:14ccc7ed49fa | 164 | { |
shockey | 0:14ccc7ed49fa | 165 | uint8_t data[2] ; |
shockey | 0:14ccc7ed49fa | 166 | readRegs(REG_UVCOMP1_Data, data, 2) ; |
shockey | 0:14ccc7ed49fa | 167 | *uvcomp1data = (data[1]<<8) | data[0] ; |
shockey | 0:14ccc7ed49fa | 168 | } |
shockey | 0:14ccc7ed49fa | 169 | |
shockey | 0:14ccc7ed49fa | 170 | void VEML6075::getUVCOMP2Data(uint16_t *uvcomp2data) |
shockey | 0:14ccc7ed49fa | 171 | { |
shockey | 0:14ccc7ed49fa | 172 | uint8_t data[2] ; |
shockey | 0:14ccc7ed49fa | 173 | readRegs(REG_UVCOMP2_Data, data, 2) ; |
shockey | 0:14ccc7ed49fa | 174 | *uvcomp2data = (data[1]<<8) | data[0] ; |
shockey | 0:14ccc7ed49fa | 175 | } |
shockey | 0:14ccc7ed49fa | 176 | |
shockey | 0:14ccc7ed49fa | 177 | void VEML6075::getID(uint16_t *id) |
shockey | 0:14ccc7ed49fa | 178 | { |
shockey | 0:14ccc7ed49fa | 179 | uint8_t data[2] ; |
shockey | 0:14ccc7ed49fa | 180 | readRegs(REG_ID, data, 2) ; |
shockey | 0:14ccc7ed49fa | 181 | *id = (data[1]<<8) | data[0] ; |
shockey | 0:14ccc7ed49fa | 182 | } |
shockey | 0:14ccc7ed49fa | 183 | |
shockey | 0:14ccc7ed49fa | 184 | void VEML6075::readRegs(int addr, uint8_t * data, int len) { |
shockey | 0:14ccc7ed49fa | 185 | char t[1] = {addr}; |
shockey | 0:14ccc7ed49fa | 186 | m_i2c.write(m_addr, t, 1, true); |
shockey | 0:14ccc7ed49fa | 187 | m_i2c.read(m_addr, (char *)data, len); |
shockey | 0:14ccc7ed49fa | 188 | } |
shockey | 0:14ccc7ed49fa | 189 | |
shockey | 0:14ccc7ed49fa | 190 | void VEML6075::writeRegs(uint8_t * data, int len) { |
shockey | 0:14ccc7ed49fa | 191 | m_i2c.write(m_addr, (char *)data, len); |
shockey | 0:14ccc7ed49fa | 192 | } |