Vishay VCNL4010 Fully Integrated Proximity and Ambient Light Sensor with Infrared Emitter, I2C Interface, and Interrupt Function
VCNL4010.cpp@1:238c76a37054, 2017-04-26 (annotated)
- Committer:
- Rhyme
- Date:
- Wed Apr 26 09:39:24 2017 +0000
- Revision:
- 1:238c76a37054
- Parent:
- 0:cf922a073b7f
Documentation added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:cf922a073b7f | 1 | #include "mbed.h" |
Rhyme | 0:cf922a073b7f | 2 | #include "VCNL4010.h" |
Rhyme | 0:cf922a073b7f | 3 | |
Rhyme | 0:cf922a073b7f | 4 | /* I2C 7bit address is 0x13 */ |
Rhyme | 0:cf922a073b7f | 5 | /* Register Definitions */ |
Rhyme | 0:cf922a073b7f | 6 | #define REG_COMMAND 0x80 |
Rhyme | 0:cf922a073b7f | 7 | #define REG_PRODUCT_ID 0x81 |
Rhyme | 0:cf922a073b7f | 8 | #define REG_PROXIMITY_RATE 0x82 |
Rhyme | 0:cf922a073b7f | 9 | #define REG_IR_LED_CURRENT 0x83 |
Rhyme | 0:cf922a073b7f | 10 | #define REG_AMBIENT_LIGHT_PARAMETER 0x84 |
Rhyme | 0:cf922a073b7f | 11 | #define REG_AMBIENT_LIGHT_RESULT_MSB 0x85 |
Rhyme | 0:cf922a073b7f | 12 | #define REG_AMBIENT_LIGHT_RESULT_LSB 0x86 |
Rhyme | 0:cf922a073b7f | 13 | #define REG_PROXIMITY_RESULT_MSB 0x87 |
Rhyme | 0:cf922a073b7f | 14 | #define REG_PROXIMITY_RESULT_LSB 0x88 |
Rhyme | 0:cf922a073b7f | 15 | #define REG_INTERRUPT_CONTROL 0x89 |
Rhyme | 0:cf922a073b7f | 16 | #define REG_LOW_THRESHOLD_MSB 0x8A |
Rhyme | 0:cf922a073b7f | 17 | #define REG_LOW_THRESHOLD_LSB 0x8B |
Rhyme | 0:cf922a073b7f | 18 | #define REG_HIGH_THRESHOLD_MSB 0x8C |
Rhyme | 0:cf922a073b7f | 19 | #define REG_HIGH_THRESHOLD_LSB 0x8D |
Rhyme | 0:cf922a073b7f | 20 | #define REG_INTERRUPT_STATUS 0x8E |
Rhyme | 0:cf922a073b7f | 21 | #define REG_PROXIMITY_MOD_TIMING 0x8F |
Rhyme | 0:cf922a073b7f | 22 | |
Rhyme | 0:cf922a073b7f | 23 | /* Bits in Register */ |
Rhyme | 0:cf922a073b7f | 24 | /* COMMAND REGISTER (0x80) */ |
Rhyme | 0:cf922a073b7f | 25 | |
Rhyme | 0:cf922a073b7f | 26 | /* mandatory functions */ |
Rhyme | 0:cf922a073b7f | 27 | VCNL4010::VCNL4010(PinName sda, PinName scl, int addr): m_i2c(sda, scl), m_addr(addr<<1) { |
Rhyme | 0:cf922a073b7f | 28 | // activate the peripheral |
Rhyme | 0:cf922a073b7f | 29 | } |
Rhyme | 0:cf922a073b7f | 30 | |
Rhyme | 0:cf922a073b7f | 31 | VCNL4010::~VCNL4010() |
Rhyme | 0:cf922a073b7f | 32 | { |
Rhyme | 0:cf922a073b7f | 33 | } |
Rhyme | 0:cf922a073b7f | 34 | |
Rhyme | 0:cf922a073b7f | 35 | void VCNL4010::readRegs(int addr, uint8_t * data, int len) { |
Rhyme | 0:cf922a073b7f | 36 | char t[1] = {addr}; |
Rhyme | 0:cf922a073b7f | 37 | m_i2c.write(m_addr, t, 1, true); |
Rhyme | 0:cf922a073b7f | 38 | m_i2c.read(m_addr, (char *)data, len); |
Rhyme | 0:cf922a073b7f | 39 | } |
Rhyme | 0:cf922a073b7f | 40 | |
Rhyme | 0:cf922a073b7f | 41 | void VCNL4010::writeRegs(uint8_t * data, int len) { |
Rhyme | 0:cf922a073b7f | 42 | m_i2c.write(m_addr, (char *)data, len); |
Rhyme | 0:cf922a073b7f | 43 | } |
Rhyme | 0:cf922a073b7f | 44 | |
Rhyme | 0:cf922a073b7f | 45 | /* device specific member functions */ |
Rhyme | 0:cf922a073b7f | 46 | uint8_t VCNL4010::getCommandReg(void) |
Rhyme | 0:cf922a073b7f | 47 | { |
Rhyme | 0:cf922a073b7f | 48 | uint8_t cmd ; |
Rhyme | 0:cf922a073b7f | 49 | readRegs(REG_COMMAND, &cmd, 1) ; |
Rhyme | 0:cf922a073b7f | 50 | return( cmd ) ; |
Rhyme | 0:cf922a073b7f | 51 | } |
Rhyme | 0:cf922a073b7f | 52 | |
Rhyme | 0:cf922a073b7f | 53 | void VCNL4010::setCommandReg(uint8_t cmd) |
Rhyme | 0:cf922a073b7f | 54 | { |
Rhyme | 0:cf922a073b7f | 55 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 56 | data[0] = REG_COMMAND ; |
Rhyme | 0:cf922a073b7f | 57 | data[1] = cmd ; |
Rhyme | 0:cf922a073b7f | 58 | writeRegs(data, 2) ; |
Rhyme | 0:cf922a073b7f | 59 | } |
Rhyme | 0:cf922a073b7f | 60 | |
Rhyme | 0:cf922a073b7f | 61 | uint8_t VCNL4010::getProductID(void) |
Rhyme | 0:cf922a073b7f | 62 | { |
Rhyme | 0:cf922a073b7f | 63 | uint8_t id ; |
Rhyme | 0:cf922a073b7f | 64 | readRegs(REG_PRODUCT_ID, &id, 1) ; |
Rhyme | 0:cf922a073b7f | 65 | return( id ) ; |
Rhyme | 0:cf922a073b7f | 66 | } |
Rhyme | 0:cf922a073b7f | 67 | |
Rhyme | 0:cf922a073b7f | 68 | uint8_t VCNL4010::getProxRate(void) |
Rhyme | 0:cf922a073b7f | 69 | { |
Rhyme | 0:cf922a073b7f | 70 | uint8_t rate ; |
Rhyme | 0:cf922a073b7f | 71 | readRegs(REG_PROXIMITY_RATE, &rate, 1) ; |
Rhyme | 0:cf922a073b7f | 72 | return( rate ) ; |
Rhyme | 0:cf922a073b7f | 73 | } |
Rhyme | 0:cf922a073b7f | 74 | |
Rhyme | 0:cf922a073b7f | 75 | void VCNL4010::setProxRate(uint8_t rate) |
Rhyme | 0:cf922a073b7f | 76 | { |
Rhyme | 0:cf922a073b7f | 77 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 78 | data[0] = REG_PROXIMITY_RATE ; |
Rhyme | 0:cf922a073b7f | 79 | data[1] = rate ; |
Rhyme | 0:cf922a073b7f | 80 | writeRegs(data, 2) ; |
Rhyme | 0:cf922a073b7f | 81 | } |
Rhyme | 0:cf922a073b7f | 82 | |
Rhyme | 0:cf922a073b7f | 83 | uint8_t VCNL4010::getIrLedCurrent(void) |
Rhyme | 0:cf922a073b7f | 84 | { |
Rhyme | 0:cf922a073b7f | 85 | uint8_t IrLedCur ; |
Rhyme | 0:cf922a073b7f | 86 | readRegs(REG_IR_LED_CURRENT, &IrLedCur, 1) ; |
Rhyme | 0:cf922a073b7f | 87 | return( IrLedCur ) ; |
Rhyme | 0:cf922a073b7f | 88 | } |
Rhyme | 0:cf922a073b7f | 89 | |
Rhyme | 0:cf922a073b7f | 90 | void VCNL4010::setIrLedCurrent(uint8_t IrLedCur) |
Rhyme | 0:cf922a073b7f | 91 | { |
Rhyme | 0:cf922a073b7f | 92 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 93 | data[0] = REG_IR_LED_CURRENT ; |
Rhyme | 0:cf922a073b7f | 94 | data[1] = IrLedCur ; |
Rhyme | 0:cf922a073b7f | 95 | writeRegs(data, 2) ; |
Rhyme | 0:cf922a073b7f | 96 | } |
Rhyme | 0:cf922a073b7f | 97 | |
Rhyme | 0:cf922a073b7f | 98 | uint8_t VCNL4010::getAlsParam(void) |
Rhyme | 0:cf922a073b7f | 99 | { |
Rhyme | 0:cf922a073b7f | 100 | uint8_t param ; |
Rhyme | 0:cf922a073b7f | 101 | readRegs(REG_AMBIENT_LIGHT_PARAMETER, ¶m, 1) ; |
Rhyme | 0:cf922a073b7f | 102 | return( param ) ; |
Rhyme | 0:cf922a073b7f | 103 | } |
Rhyme | 0:cf922a073b7f | 104 | |
Rhyme | 0:cf922a073b7f | 105 | void VCNL4010::setAlsParam(uint8_t param) |
Rhyme | 0:cf922a073b7f | 106 | { |
Rhyme | 0:cf922a073b7f | 107 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 108 | data[0] = REG_AMBIENT_LIGHT_PARAMETER ; |
Rhyme | 0:cf922a073b7f | 109 | data[1] = param ; |
Rhyme | 0:cf922a073b7f | 110 | writeRegs(data, 2) ; |
Rhyme | 0:cf922a073b7f | 111 | } |
Rhyme | 0:cf922a073b7f | 112 | |
Rhyme | 0:cf922a073b7f | 113 | uint16_t VCNL4010::getAls(void) |
Rhyme | 0:cf922a073b7f | 114 | { |
Rhyme | 0:cf922a073b7f | 115 | uint16_t als ; |
Rhyme | 0:cf922a073b7f | 116 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 117 | readRegs(REG_AMBIENT_LIGHT_RESULT_MSB, data, 2) ; |
Rhyme | 0:cf922a073b7f | 118 | als = (data[0] << 8) | data[1] ; |
Rhyme | 0:cf922a073b7f | 119 | return( als ) ; |
Rhyme | 0:cf922a073b7f | 120 | } |
Rhyme | 0:cf922a073b7f | 121 | |
Rhyme | 0:cf922a073b7f | 122 | uint16_t VCNL4010::getProx(void) |
Rhyme | 0:cf922a073b7f | 123 | { |
Rhyme | 0:cf922a073b7f | 124 | uint16_t prox ; |
Rhyme | 0:cf922a073b7f | 125 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 126 | readRegs(REG_PROXIMITY_RESULT_MSB, data, 2) ; |
Rhyme | 0:cf922a073b7f | 127 | prox = (data[0] << 8) | data[1] ; |
Rhyme | 0:cf922a073b7f | 128 | return( prox ) ; |
Rhyme | 0:cf922a073b7f | 129 | } |
Rhyme | 0:cf922a073b7f | 130 | |
Rhyme | 0:cf922a073b7f | 131 | uint8_t VCNL4010::getIntCtrl(void) |
Rhyme | 0:cf922a073b7f | 132 | { |
Rhyme | 0:cf922a073b7f | 133 | uint8_t ctrl ; |
Rhyme | 0:cf922a073b7f | 134 | readRegs(REG_INTERRUPT_CONTROL, &ctrl, 1) ; |
Rhyme | 0:cf922a073b7f | 135 | return( ctrl ) ; |
Rhyme | 0:cf922a073b7f | 136 | } |
Rhyme | 0:cf922a073b7f | 137 | |
Rhyme | 0:cf922a073b7f | 138 | void VCNL4010::setIntCtrl(uint8_t ctrl) |
Rhyme | 0:cf922a073b7f | 139 | { |
Rhyme | 0:cf922a073b7f | 140 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 141 | data[0] = REG_INTERRUPT_CONTROL ; |
Rhyme | 0:cf922a073b7f | 142 | data[1] = ctrl ; |
Rhyme | 0:cf922a073b7f | 143 | writeRegs(data, 2) ; |
Rhyme | 0:cf922a073b7f | 144 | } |
Rhyme | 0:cf922a073b7f | 145 | |
Rhyme | 0:cf922a073b7f | 146 | uint16_t VCNL4010::getLowThreshold(void) |
Rhyme | 0:cf922a073b7f | 147 | { |
Rhyme | 0:cf922a073b7f | 148 | uint16_t thr ; |
Rhyme | 0:cf922a073b7f | 149 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 150 | readRegs(REG_LOW_THRESHOLD_MSB, data, 2) ; |
Rhyme | 0:cf922a073b7f | 151 | thr = (data[0] << 8) | data[1] ; |
Rhyme | 0:cf922a073b7f | 152 | return( thr ) ; |
Rhyme | 0:cf922a073b7f | 153 | } |
Rhyme | 0:cf922a073b7f | 154 | |
Rhyme | 0:cf922a073b7f | 155 | void VCNL4010::setLowThreshold(uint16_t thr) |
Rhyme | 0:cf922a073b7f | 156 | { |
Rhyme | 0:cf922a073b7f | 157 | uint8_t data[3] ; |
Rhyme | 0:cf922a073b7f | 158 | data[0] = REG_LOW_THRESHOLD_MSB ; |
Rhyme | 0:cf922a073b7f | 159 | data[1] = (thr >> 8) & 0xFF ; |
Rhyme | 0:cf922a073b7f | 160 | data[2] = thr & 0xFF ; |
Rhyme | 0:cf922a073b7f | 161 | writeRegs(data, 3) ; |
Rhyme | 0:cf922a073b7f | 162 | } |
Rhyme | 0:cf922a073b7f | 163 | |
Rhyme | 0:cf922a073b7f | 164 | uint16_t VCNL4010::getHighThreshold(void) |
Rhyme | 0:cf922a073b7f | 165 | { |
Rhyme | 0:cf922a073b7f | 166 | uint16_t thr ; |
Rhyme | 0:cf922a073b7f | 167 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 168 | readRegs(REG_HIGH_THRESHOLD_MSB, data, 2) ; |
Rhyme | 0:cf922a073b7f | 169 | thr = (data[0] << 8) | data[1] ; |
Rhyme | 0:cf922a073b7f | 170 | return( thr ) ; |
Rhyme | 0:cf922a073b7f | 171 | } |
Rhyme | 0:cf922a073b7f | 172 | |
Rhyme | 0:cf922a073b7f | 173 | void VCNL4010::setHighThreshold(uint16_t thr) |
Rhyme | 0:cf922a073b7f | 174 | { |
Rhyme | 0:cf922a073b7f | 175 | uint8_t data[3] ; |
Rhyme | 0:cf922a073b7f | 176 | data[0] = REG_HIGH_THRESHOLD_MSB ; |
Rhyme | 0:cf922a073b7f | 177 | data[1] = (thr >> 8) & 0xFF ; |
Rhyme | 0:cf922a073b7f | 178 | data[2] = thr & 0xFF ; |
Rhyme | 0:cf922a073b7f | 179 | writeRegs(data, 3) ; |
Rhyme | 0:cf922a073b7f | 180 | } |
Rhyme | 0:cf922a073b7f | 181 | |
Rhyme | 0:cf922a073b7f | 182 | uint8_t VCNL4010::getIntStatus(void) |
Rhyme | 0:cf922a073b7f | 183 | { |
Rhyme | 0:cf922a073b7f | 184 | uint8_t status ; |
Rhyme | 0:cf922a073b7f | 185 | readRegs(REG_INTERRUPT_STATUS, &status, 1) ; |
Rhyme | 0:cf922a073b7f | 186 | return( status ) ; |
Rhyme | 0:cf922a073b7f | 187 | } |
Rhyme | 0:cf922a073b7f | 188 | |
Rhyme | 0:cf922a073b7f | 189 | void VCNL4010::setIntStatus(uint8_t status) |
Rhyme | 0:cf922a073b7f | 190 | { |
Rhyme | 0:cf922a073b7f | 191 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 192 | data[0] = REG_INTERRUPT_STATUS ; |
Rhyme | 0:cf922a073b7f | 193 | data[1] = status ; |
Rhyme | 0:cf922a073b7f | 194 | writeRegs(data, 2) ; |
Rhyme | 0:cf922a073b7f | 195 | } |
Rhyme | 0:cf922a073b7f | 196 | |
Rhyme | 0:cf922a073b7f | 197 | uint8_t VCNL4010::getProxModTiming(void) |
Rhyme | 0:cf922a073b7f | 198 | { |
Rhyme | 0:cf922a073b7f | 199 | uint8_t timing ; |
Rhyme | 0:cf922a073b7f | 200 | readRegs(REG_PROXIMITY_MOD_TIMING, &timing, 1) ; |
Rhyme | 0:cf922a073b7f | 201 | return( timing ) ; |
Rhyme | 0:cf922a073b7f | 202 | } |
Rhyme | 0:cf922a073b7f | 203 | |
Rhyme | 0:cf922a073b7f | 204 | void VCNL4010::setProxModTiming(uint8_t timing) |
Rhyme | 0:cf922a073b7f | 205 | { |
Rhyme | 0:cf922a073b7f | 206 | uint8_t data[2] ; |
Rhyme | 0:cf922a073b7f | 207 | data[0] = REG_PROXIMITY_MOD_TIMING ; |
Rhyme | 0:cf922a073b7f | 208 | data[1] = timing ; |
Rhyme | 0:cf922a073b7f | 209 | writeRegs(data, 2) ; |
Rhyme | 0:cf922a073b7f | 210 | } |