Line 48 commented out
Dependents: UPAS_MicroBLE ENV_NODE_REQUEST_POC ENV_F303K8
Fork of SI1145 by
SI1145.cpp@6:642ece0fad39, 2015-06-14 (annotated)
- Committer:
- caseyquinn
- Date:
- Sun Jun 14 01:33:47 2015 +0000
- Revision:
- 6:642ece0fad39
- Parent:
- 5:2260652f0296
- Child:
- 7:3e3fe1852990
addtional changes to value types
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ngood | 0:0490a77adbc5 | 1 | /** |
ngood | 0:0490a77adbc5 | 2 | * SI1145 Digital UV Index / IR / Visible Light Sensor library |
ngood | 0:0490a77adbc5 | 3 | * @author N Good |
ngood | 0:0490a77adbc5 | 4 | * @date 5-May-2015 |
ngood | 0:0490a77adbc5 | 5 | * Library for "SI1145 Digital UV Index / IR / Visible Light Sensor" from adafruit |
ngood | 0:0490a77adbc5 | 6 | * https://www.adafruit.com/products/1777 |
ngood | 0:0490a77adbc5 | 7 | */ |
ngood | 0:0490a77adbc5 | 8 | |
ngood | 0:0490a77adbc5 | 9 | #include "mbed.h" |
ngood | 0:0490a77adbc5 | 10 | #include "SI1145.h" |
ngood | 0:0490a77adbc5 | 11 | |
ngood | 0:0490a77adbc5 | 12 | SI1145::SI1145(PinName sda, PinName scl, char slave_adr) |
ngood | 0:0490a77adbc5 | 13 | : |
ngood | 0:0490a77adbc5 | 14 | i2c_p(new I2C(sda, scl)), |
ngood | 0:0490a77adbc5 | 15 | i2c(*i2c_p), |
ngood | 0:0490a77adbc5 | 16 | address(slave_adr), |
ngood | 0:0490a77adbc5 | 17 | t_fine(0) |
ngood | 0:0490a77adbc5 | 18 | { |
ngood | 0:0490a77adbc5 | 19 | initalize(); |
ngood | 0:0490a77adbc5 | 20 | } |
ngood | 0:0490a77adbc5 | 21 | |
ngood | 0:0490a77adbc5 | 22 | SI1145::SI1145(I2C &i2c_obj, char slave_adr) |
ngood | 0:0490a77adbc5 | 23 | : |
ngood | 0:0490a77adbc5 | 24 | i2c_p(NULL), |
ngood | 0:0490a77adbc5 | 25 | i2c(i2c_obj), |
ngood | 0:0490a77adbc5 | 26 | address(slave_adr), |
ngood | 0:0490a77adbc5 | 27 | t_fine(0) |
ngood | 0:0490a77adbc5 | 28 | { |
ngood | 0:0490a77adbc5 | 29 | initalize(); |
ngood | 0:0490a77adbc5 | 30 | } |
ngood | 0:0490a77adbc5 | 31 | |
ngood | 0:0490a77adbc5 | 32 | //SI1145::~SI1145() |
ngood | 0:0490a77adbc5 | 33 | //{ |
ngood | 0:0490a77adbc5 | 34 | // if (NULL != i2c_p) |
ngood | 0:0490a77adbc5 | 35 | // delete i2c_p; |
ngood | 0:0490a77adbc5 | 36 | //} |
ngood | 0:0490a77adbc5 | 37 | |
ngood | 0:0490a77adbc5 | 38 | void SI1145::initalize(void) { |
ngood | 0:0490a77adbc5 | 39 | uint16_t idu; |
ngood | 0:0490a77adbc5 | 40 | //i2c.frequency(100000); |
ngood | 0:0490a77adbc5 | 41 | char id[1] = {0x00}; |
ngood | 0:0490a77adbc5 | 42 | i2c.write(address,id,1); |
ngood | 0:0490a77adbc5 | 43 | char data[1] = {0}; |
ngood | 0:0490a77adbc5 | 44 | i2c.read(address,data,1); |
ngood | 0:0490a77adbc5 | 45 | float idf; |
ngood | 0:0490a77adbc5 | 46 | idu = data[0]; |
ngood | 0:0490a77adbc5 | 47 | idf = (float)idu; |
ngood | 0:0490a77adbc5 | 48 | pc.printf("%4.0f ID\n",idf); |
ngood | 0:0490a77adbc5 | 49 | // reset______________________________________________ |
ngood | 0:0490a77adbc5 | 50 | //write MEASRATE0 |
ngood | 0:0490a77adbc5 | 51 | char MEASRATE0[2] = {0x08,0x0}; |
ngood | 0:0490a77adbc5 | 52 | i2c.write(address,MEASRATE0,2); |
ngood | 0:0490a77adbc5 | 53 | //write MEASRATE1 |
ngood | 0:0490a77adbc5 | 54 | char MEASRATE1[2] = {0x09,0x0}; |
ngood | 0:0490a77adbc5 | 55 | i2c.write(address,MEASRATE1,2); |
ngood | 0:0490a77adbc5 | 56 | //write visQEN |
ngood | 0:0490a77adbc5 | 57 | char visQEN[2] = {0x04,0x0}; |
ngood | 0:0490a77adbc5 | 58 | i2c.write(address,visQEN,2); |
ngood | 0:0490a77adbc5 | 59 | // visQMODE1 |
ngood | 0:0490a77adbc5 | 60 | char visQMODE1[2] = {0x05,0x0}; |
ngood | 0:0490a77adbc5 | 61 | i2c.write(address,visQMODE1,2); |
ngood | 0:0490a77adbc5 | 62 | //write visQMODE2 |
ngood | 0:0490a77adbc5 | 63 | char visQMODE2[2] = {0x06,0x0}; |
ngood | 0:0490a77adbc5 | 64 | i2c.write(address,visQMODE2,2); |
ngood | 0:0490a77adbc5 | 65 | //write INTCFG |
ngood | 0:0490a77adbc5 | 66 | char INTCFG[2] = {0x03,0x0}; |
ngood | 0:0490a77adbc5 | 67 | i2c.write(address,INTCFG,2); |
ngood | 0:0490a77adbc5 | 68 | //write visQSTAT |
ngood | 0:0490a77adbc5 | 69 | char visQSTAT[2] = {0x21,0xFF}; |
ngood | 0:0490a77adbc5 | 70 | i2c.write(address,visQSTAT,2); |
ngood | 0:0490a77adbc5 | 71 | //write COMMAND |
ngood | 0:0490a77adbc5 | 72 | char COMMAND[2] = {0x18,0x01}; |
ngood | 0:0490a77adbc5 | 73 | i2c.write(address,COMMAND,2); |
ngood | 0:0490a77adbc5 | 74 | // wait |
ngood | 0:0490a77adbc5 | 75 | wait(100/1000); |
ngood | 0:0490a77adbc5 | 76 | //write HWKEY |
ngood | 0:0490a77adbc5 | 77 | char HWKEY[2] = {0x07, 0x17}; |
ngood | 0:0490a77adbc5 | 78 | i2c.write(address,HWKEY,2); |
ngood | 0:0490a77adbc5 | 79 | // wait |
ngood | 0:0490a77adbc5 | 80 | wait(100/1000); |
ngood | 0:0490a77adbc5 | 81 | //__________________________________________________________ |
ngood | 0:0490a77adbc5 | 82 | // enable UVindex measurement coefficients! |
ngood | 0:0490a77adbc5 | 83 | char REG_UCOEFF0[2] = {0x13,0x29}; |
ngood | 0:0490a77adbc5 | 84 | i2c.write(address, REG_UCOEFF0, 2); |
ngood | 0:0490a77adbc5 | 85 | char REG_UCOEFF1[2] = {0x14,0x89}; |
ngood | 0:0490a77adbc5 | 86 | i2c.write(address, REG_UCOEFF1, 2); |
ngood | 0:0490a77adbc5 | 87 | char REG_UCOEFF2[2] = {0x15,0x02}; |
ngood | 0:0490a77adbc5 | 88 | i2c.write(address, REG_UCOEFF2, 2); |
ngood | 0:0490a77adbc5 | 89 | char REG_UCOEFF3[2] = {0x16,0x00}; |
ngood | 0:0490a77adbc5 | 90 | i2c.write(address, REG_UCOEFF3, 2); |
ngood | 0:0490a77adbc5 | 91 | //__________________________________________________________ |
ngood | 0:0490a77adbc5 | 92 | |
ngood | 0:0490a77adbc5 | 93 | // enable UV sensor |
ngood | 0:0490a77adbc5 | 94 | char PARAM_CHLIST[2] = {0x17, 0x80 | 0x20 | 0x10 | 0x01}; |
ngood | 0:0490a77adbc5 | 95 | i2c.write(address, PARAM_CHLIST, 2); |
ngood | 0:0490a77adbc5 | 96 | char COMMD_CHLIST[2] = {0x18, 0x01 | 0xA0}; |
ngood | 0:0490a77adbc5 | 97 | i2c.write(address, COMMD_CHLIST, 2); |
ngood | 0:0490a77adbc5 | 98 | // enable interrupt on every sample |
ngood | 0:0490a77adbc5 | 99 | char REG_INTCFG[2] = {0x03,0x01}; |
ngood | 0:0490a77adbc5 | 100 | i2c.write(address, REG_INTCFG, 2); |
ngood | 0:0490a77adbc5 | 101 | char REG_visQEN[2] = {0x04,0x01}; |
ngood | 0:0490a77adbc5 | 102 | i2c.write(address, REG_visQEN, 2); |
ngood | 0:0490a77adbc5 | 103 | // program proximity sensor LED current |
ngood | 0:0490a77adbc5 | 104 | char REG_PSLED21[2] = {0x0F,0x03}; |
ngood | 0:0490a77adbc5 | 105 | i2c.write(address, REG_PSLED21, 2); // 20mA for LED 1 only |
ngood | 0:0490a77adbc5 | 106 | char PARAM_ADCMUX_LARGEvis[2] = {0x17, 0x03}; |
ngood | 0:0490a77adbc5 | 107 | i2c.write(address, PARAM_ADCMUX_LARGEvis, 2); |
ngood | 0:0490a77adbc5 | 108 | char PARAM_PS1ADCMUX[2] = {0x18, 0x07 | 0xA0}; |
ngood | 0:0490a77adbc5 | 109 | i2c.write(address, PARAM_PS1ADCMUX, 2); |
ngood | 0:0490a77adbc5 | 110 | // prox sensor #1 uses LED #1 |
ngood | 0:0490a77adbc5 | 111 | char PARAM_PSLED12SEL_PS1LED1[2] = {0x17, 0x01}; |
ngood | 0:0490a77adbc5 | 112 | i2c.write(address, PARAM_PSLED12SEL_PS1LED1, 2); |
ngood | 0:0490a77adbc5 | 113 | char PARAM_PSLED12SEL[2] = {0x18, 0x02 | 0xA0}; |
ngood | 0:0490a77adbc5 | 114 | i2c.write(address, PARAM_PSLED12SEL, 2); |
ngood | 0:0490a77adbc5 | 115 | // fastest clocks, clock div 1 |
ngood | 0:0490a77adbc5 | 116 | char PARAM_0[2] = {0x17, 0x0 | 0xA0}; |
ngood | 0:0490a77adbc5 | 117 | i2c.write(address, PARAM_0, 2); |
ngood | 0:0490a77adbc5 | 118 | char PARAM_PSADCGAIN[2] = {0x18, 0x0B}; |
ngood | 0:0490a77adbc5 | 119 | i2c.write(address, PARAM_PSADCGAIN, 2); |
ngood | 0:0490a77adbc5 | 120 | // take 511 clocks to measure |
ngood | 0:0490a77adbc5 | 121 | char PARAM_ADCCOUNTER_511CLK[2] = {0x17, 0x70}; |
ngood | 0:0490a77adbc5 | 122 | i2c.write(address, PARAM_ADCCOUNTER_511CLK, 2); |
ngood | 0:0490a77adbc5 | 123 | char PARAM_PSADCOUNTER[2] = {0x18, 0x0A | 0xA0}; |
ngood | 0:0490a77adbc5 | 124 | i2c.write(address, PARAM_PSADCOUNTER, 2); |
ngood | 0:0490a77adbc5 | 125 | // in prox mode, high range |
ngood | 0:0490a77adbc5 | 126 | char PARAM_PSADCMISC_RANGE_PARAM_PSADCMISC_PSMODE[2] = {0x17, 0x20 | 0x04}; |
ngood | 0:0490a77adbc5 | 127 | i2c.write(address, PARAM_PSADCMISC_RANGE_PARAM_PSADCMISC_PSMODE, 2); |
ngood | 0:0490a77adbc5 | 128 | char PARAM_PSADCMISC[2] = {0x18, 0x0C | 0xA0}; |
ngood | 0:0490a77adbc5 | 129 | i2c.write(address, PARAM_PSADCMISC, 2); |
ngood | 0:0490a77adbc5 | 130 | // |
ngood | 0:0490a77adbc5 | 131 | char PARAM_ADCMUX_SMALLvis[2] = {0x17, 0x00}; |
ngood | 0:0490a77adbc5 | 132 | i2c.write(address, PARAM_ADCMUX_SMALLvis, 2); |
ngood | 0:0490a77adbc5 | 133 | char PARAM_ALSvisADCMUX[2] = {0x18, 0x0E | 0xA0}; |
ngood | 0:0490a77adbc5 | 134 | i2c.write(address, PARAM_ALSvisADCMUX, 2); |
ngood | 0:0490a77adbc5 | 135 | // fastest clocks, clock div 1 |
ngood | 0:0490a77adbc5 | 136 | i2c.write(address, PARAM_0, 2); |
ngood | 0:0490a77adbc5 | 137 | char PARAM_ALSvisADCGAIN[2] = {0x18, 0x1E | 0xA0}; |
ngood | 0:0490a77adbc5 | 138 | i2c.write(address, PARAM_ALSvisADCGAIN, 2); |
ngood | 0:0490a77adbc5 | 139 | // take 511 clocks to measure |
ngood | 0:0490a77adbc5 | 140 | i2c.write(address, PARAM_ADCCOUNTER_511CLK, 2); |
ngood | 0:0490a77adbc5 | 141 | char PARAM_ALSvisADCOUNTER[2] = {0x18, 0x1D | 0xA0}; |
ngood | 0:0490a77adbc5 | 142 | i2c.write(address, PARAM_ALSvisADCOUNTER, 2); |
ngood | 0:0490a77adbc5 | 143 | // in high range mode |
ngood | 0:0490a77adbc5 | 144 | char PARAM_ALSvisADCMISC_RANGE[2] = {0x17, 0x20}; |
ngood | 0:0490a77adbc5 | 145 | i2c.write(address, PARAM_ALSvisADCMISC_RANGE, 2); |
ngood | 0:0490a77adbc5 | 146 | char PARAM_ALSvisADCMISC[2] = {0x18, 0x1F | 0xA0}; |
ngood | 0:0490a77adbc5 | 147 | i2c.write(address, PARAM_ALSvisADCMISC, 2); |
ngood | 0:0490a77adbc5 | 148 | // fastest clocks, clock div 1 |
ngood | 0:0490a77adbc5 | 149 | char PARAM_000[2] = {0x17, 0x0}; |
ngood | 0:0490a77adbc5 | 150 | i2c.write(address, PARAM_000, 2); |
ngood | 0:0490a77adbc5 | 151 | char PARAM_ALSuvADCGAIN[2] = {0x18, 0x11 | 0xA0}; |
ngood | 0:0490a77adbc5 | 152 | i2c.write(address, PARAM_ALSuvADCGAIN, 2); |
ngood | 0:0490a77adbc5 | 153 | // take 511 clocks to measure |
ngood | 0:0490a77adbc5 | 154 | i2c.write(address, PARAM_ADCCOUNTER_511CLK, 2); |
ngood | 0:0490a77adbc5 | 155 | char PARAM_ALSuvADCOUNTER[2] = {0x18, 0x10 | 0xA0}; |
ngood | 0:0490a77adbc5 | 156 | i2c.write(address, PARAM_ALSuvADCOUNTER, 2); |
ngood | 0:0490a77adbc5 | 157 | // in high range mode (not normal signal) |
ngood | 0:0490a77adbc5 | 158 | char PARAM_ALSuvADCMISC_uvRANGE[2] = {0x17, 0x20}; |
ngood | 0:0490a77adbc5 | 159 | i2c.write(address, PARAM_ALSuvADCMISC_uvRANGE, 2); |
ngood | 0:0490a77adbc5 | 160 | char PARAM_ALSuvADCMISC[2] = {0x18, 0x12 | 0xA0}; |
ngood | 0:0490a77adbc5 | 161 | i2c.write(address, PARAM_ALSuvADCMISC, 2); |
ngood | 0:0490a77adbc5 | 162 | // measurement rate for auto |
ngood | 0:0490a77adbc5 | 163 | char REG_MEASRATE0[2] = {0x08,0xFF}; |
ngood | 0:0490a77adbc5 | 164 | i2c.write(address, REG_MEASRATE0, 2);// 255 * 31.25uS = 8ms |
ngood | 0:0490a77adbc5 | 165 | // auto run |
ngood | 0:0490a77adbc5 | 166 | char REG_COMMAND[2] = {0x18,0x0F}; |
ngood | 0:0490a77adbc5 | 167 | i2c.write(address, REG_COMMAND, 2); |
ngood | 0:0490a77adbc5 | 168 | } |
ngood | 0:0490a77adbc5 | 169 | |
caseyquinn | 4:27844262d40f | 170 | float SI1145::getUV() |
ngood | 0:0490a77adbc5 | 171 | { |
ngood | 0:0490a77adbc5 | 172 | // Variables |
caseyquinn | 1:8587b5583343 | 173 | //float valf; // uv value |
ngood | 0:0490a77adbc5 | 174 | char reg[1]; // register |
ngood | 0:0490a77adbc5 | 175 | char data[2] = {0,0}; // data 2 x 8 bits |
ngood | 0:0490a77adbc5 | 176 | uint16_t vali; // uv value |
ngood | 0:0490a77adbc5 | 177 | // Set register |
ngood | 0:0490a77adbc5 | 178 | reg[0] = 0x2C; |
ngood | 0:0490a77adbc5 | 179 | // Read registers |
ngood | 0:0490a77adbc5 | 180 | i2c.write(address, reg, 1); |
ngood | 0:0490a77adbc5 | 181 | i2c.read(address, data, 2); |
ngood | 0:0490a77adbc5 | 182 | // Merge bytes |
ngood | 0:0490a77adbc5 | 183 | vali = data[0] | (data[1] << 8); // int |
caseyquinn | 1:8587b5583343 | 184 | //valf = (float)vali; // convert to float |
ngood | 0:0490a77adbc5 | 185 | // Return value |
caseyquinn | 1:8587b5583343 | 186 | return vali; |
ngood | 0:0490a77adbc5 | 187 | } |
ngood | 0:0490a77adbc5 | 188 | |
caseyquinn | 6:642ece0fad39 | 189 | int SI1145::getVIS() |
ngood | 0:0490a77adbc5 | 190 | { |
ngood | 0:0490a77adbc5 | 191 | // Variables |
caseyquinn | 6:642ece0fad39 | 192 | //float valf; // vis (IR+UV) value |
ngood | 0:0490a77adbc5 | 193 | char reg[1]; // register |
ngood | 0:0490a77adbc5 | 194 | char data[2] = {0,0}; // data 2 x 8 bits |
ngood | 0:0490a77adbc5 | 195 | uint16_t vali; // vis value |
ngood | 0:0490a77adbc5 | 196 | // Set register |
ngood | 0:0490a77adbc5 | 197 | reg[0] = 0x22; |
ngood | 0:0490a77adbc5 | 198 | // Read registers |
ngood | 0:0490a77adbc5 | 199 | i2c.write(address, reg, 1); |
ngood | 0:0490a77adbc5 | 200 | i2c.read(address, data, 2); |
ngood | 0:0490a77adbc5 | 201 | // Merge bytes |
ngood | 0:0490a77adbc5 | 202 | vali = data[0] | (data[1] << 8); // int |
caseyquinn | 6:642ece0fad39 | 203 | //valf = (float)vali; // convert to float |
ngood | 0:0490a77adbc5 | 204 | // Return value |
caseyquinn | 6:642ece0fad39 | 205 | return vali; |
ngood | 0:0490a77adbc5 | 206 | } |
ngood | 0:0490a77adbc5 | 207 | |
caseyquinn | 6:642ece0fad39 | 208 | int SI1145::getIR() |
ngood | 0:0490a77adbc5 | 209 | { |
ngood | 0:0490a77adbc5 | 210 | // Variables |
caseyquinn | 1:8587b5583343 | 211 | //float valf; // ir value |
ngood | 0:0490a77adbc5 | 212 | char reg[1]; // register |
ngood | 0:0490a77adbc5 | 213 | char data[2] = {0,0}; // data 2 x 8 bits |
caseyquinn | 6:642ece0fad39 | 214 | int vali; // ir value |
ngood | 0:0490a77adbc5 | 215 | // Set register |
ngood | 0:0490a77adbc5 | 216 | reg[0] = 0x24; |
ngood | 0:0490a77adbc5 | 217 | // Read registers |
ngood | 0:0490a77adbc5 | 218 | i2c.write(address, reg, 1); |
ngood | 0:0490a77adbc5 | 219 | i2c.read(address, data, 2); |
ngood | 0:0490a77adbc5 | 220 | // Merge bytes |
ngood | 0:0490a77adbc5 | 221 | vali = data[0] | (data[1] << 8); // int |
caseyquinn | 1:8587b5583343 | 222 | //valf = (float)vali; // convert to float |
ngood | 0:0490a77adbc5 | 223 | // Return value |
caseyquinn | 1:8587b5583343 | 224 | return vali; |
ngood | 0:0490a77adbc5 | 225 | } |
ngood | 0:0490a77adbc5 | 226 | |
caseyquinn | 6:642ece0fad39 | 227 | int SI1145::getPROX() |
ngood | 0:0490a77adbc5 | 228 | { |
ngood | 0:0490a77adbc5 | 229 | // Variables |
caseyquinn | 1:8587b5583343 | 230 | //float valf; // prox value |
ngood | 0:0490a77adbc5 | 231 | char reg[1]; // register |
ngood | 0:0490a77adbc5 | 232 | char data[2] = {0,0}; // data 2 x 8 bits |
caseyquinn | 6:642ece0fad39 | 233 | int vali; // prox value |
ngood | 0:0490a77adbc5 | 234 | // Set register |
ngood | 0:0490a77adbc5 | 235 | reg[0] = 0x26; |
ngood | 0:0490a77adbc5 | 236 | // Read registers |
ngood | 0:0490a77adbc5 | 237 | i2c.write(address, reg, 1); |
ngood | 0:0490a77adbc5 | 238 | i2c.read(address, data, 2); |
ngood | 0:0490a77adbc5 | 239 | // Merge bytes |
ngood | 0:0490a77adbc5 | 240 | vali = data[0] | (data[1] << 8); // int |
caseyquinn | 1:8587b5583343 | 241 | //valf = (float)vali; // convert to float |
ngood | 0:0490a77adbc5 | 242 | // Return value |
caseyquinn | 1:8587b5583343 | 243 | return vali; |
ngood | 0:0490a77adbc5 | 244 | } |
ngood | 0:0490a77adbc5 | 245 |