Vishay VCNL4010 Fully Integrated Proximity and Ambient Light Sensor with Infrared Emitter, I2C Interface, and Interrupt Function

Dependents:   test_VCNL4010

Committer:
Rhyme
Date:
Wed Apr 26 08:49:07 2017 +0000
Revision:
0:cf922a073b7f
Child:
1:238c76a37054
First commit, barely working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:cf922a073b7f 1 #ifndef _VCNL4010_H_
Rhyme 0:cf922a073b7f 2 #define _VCNL4010_H_
Rhyme 0:cf922a073b7f 3
Rhyme 0:cf922a073b7f 4 /**
Rhyme 0:cf922a073b7f 5 * Register #0 Command Register (0x80)
Rhyme 0:cf922a073b7f 6 * bit[7] config_lock
Rhyme 0:cf922a073b7f 7 * bit[6] als_data_rdy
Rhyme 0:cf922a073b7f 8 * bit[5] prox_data_rdy
Rhyme 0:cf922a073b7f 9 * bit[4] als_od
Rhyme 0:cf922a073b7f 10 * bit[3] prox_od
Rhyme 0:cf922a073b7f 11 * bit[2] als_en
Rhyme 0:cf922a073b7f 12 * bit[1] prox_en
Rhyme 0:cf922a073b7f 13 * bit[0] selftimed_en
Rhyme 0:cf922a073b7f 14 *
Rhyme 0:cf922a073b7f 15 * Register #1 Product ID Revision Register (0x81)
Rhyme 0:cf922a073b7f 16 * bit[7:4] Product ID
Rhyme 0:cf922a073b7f 17 * bit[3:0] Revision ID
Rhyme 0:cf922a073b7f 18 *
Rhyme 0:cf922a073b7f 19 * Register #2 Rate of Proximity Measurement (0x82)
Rhyme 0:cf922a073b7f 20 * bit[7:3] (n/a)
Rhyme 0:cf922a073b7f 21 * bit[2:0] Rate of Proximity Measurement
Rhyme 0:cf922a073b7f 22 * 000 - 1.95 measurements/s (DEFAULT)
Rhyme 0:cf922a073b7f 23 * 001 - 3.960625 measurements/s
Rhyme 0:cf922a073b7f 24 * 010 - 7.8125 measurements/s
Rhyme 0:cf922a073b7f 25 * 011 - 16.625 measurements/s
Rhyme 0:cf922a073b7f 26 * 100 - 31.25 measurements/s
Rhyme 0:cf922a073b7f 27 * 101 - 62.5 measurements/s
Rhyme 0:cf922a073b7f 28 * 110 - 125 measurements/s
Rhyme 0:cf922a073b7f 29 * 111 - 250 measurements/s
Rhyme 0:cf922a073b7f 30 *
Rhyme 0:cf922a073b7f 31 * Register #3 LED Current Setting for Proximity Mode (0x83)
Rhyme 0:cf922a073b7f 32 * bit[7:6] Fuse prog ID
Rhyme 0:cf922a073b7f 33 * bit[5:0] IR LED current value
Rhyme 0:cf922a073b7f 34 * IR LED current = value(dec.) x 10mA
Rhyme 0:cf922a073b7f 35 * Valid Range = 0 to 20d.
Rhyme 0:cf922a073b7f 36 * 0 = 0mA, 1 = 10mA .. 20 = 200mA ((DEFAULT: 2 = 20mA)
Rhyme 0:cf922a073b7f 37 *
Rhyme 0:cf922a073b7f 38 * Register #4 Ambient Light Parameter Register (0x84)
Rhyme 0:cf922a073b7f 39 * bit[7] Continuous conversion mode 0: Disable = DEFAULT 1: Enable
Rhyme 0:cf922a073b7f 40 * bit[6:4] als_rate
Rhyme 0:cf922a073b7f 41 * bit[3] Auto offset compensation 0: Disable 1: Enable = DEFAULT
Rhyme 0:cf922a073b7f 42 * bit[2:0] Averaging function (number of measurements per run)
Rhyme 0:cf922a073b7f 43 * als_rate Ambient light measurement rate
Rhyme 0:cf922a073b7f 44 * 000 - 1 samples/s
Rhyme 0:cf922a073b7f 45 * 001 - 2 samples/s = DEFUAULT
Rhyme 0:cf922a073b7f 46 * 010 - 3 samples/s
Rhyme 0:cf922a073b7f 47 * 011 - 4 samples/s
Rhyme 0:cf922a073b7f 48 * 100 - 5 samples/s
Rhyme 0:cf922a073b7f 49 * 101 - 6 samples/s
Rhyme 0:cf922a073b7f 50 * 110 - 8 samples/s
Rhyme 0:cf922a073b7f 51 * 111 - 10 samples/s
Rhyme 0:cf922a073b7f 52 * Averaging function
Rhyme 0:cf922a073b7f 53 * Number of conversions = 2^decimal_value
Rhyme 0:cf922a073b7f 54 * 0 = 1 conv, 1 = 2 conv, 2 = 4 conv, .. t = 32 conv (DEFAULT) .. 7 = 128 conv
Rhyme 0:cf922a073b7f 55 *
Rhyme 0:cf922a073b7f 56 * Register #5 and #6 Ambient Light Result Register (0x85, 0x86)
Rhyme 0:cf922a073b7f 57 * bit[15:8] MSB (address 0x85)
Rhyme 0:cf922a073b7f 58 * bit[7:0] LSB (address 0x86)
Rhyme 0:cf922a073b7f 59 *
Rhyme 0:cf922a073b7f 60 * Register #7 and #8 Proximity Measurement Result Register (0x87, 0x88)
Rhyme 0:cf922a073b7f 61 * bit[15:8] MSB (address 0x87)
Rhyme 0:cf922a073b7f 62 * bit[7:0] LSB (address 0x88)
Rhyme 0:cf922a073b7f 63 *
Rhyme 0:cf922a073b7f 64 * Register #9 Interrupt Control Register (0x89)
Rhyme 0:cf922a073b7f 65 * bit[7:5] Interrupt Count Exceed
Rhyme 0:cf922a073b7f 66 * bit[4] (n/a)
Rhyme 0:cf922a073b7f 67 * bit[3] INT_PROX_ready_EN Eneables interrupt generation at proximity data ready
Rhyme 0:cf922a073b7f 68 * bit[2] INT_ALS_ready_EN Enables interrupt generation at ambient data ready
Rhyme 0:cf922a073b7f 69 * bit[1] INT_THRES_EN Enables interrupt generation when high or low threshold is exceeded
Rhyme 0:cf922a073b7f 70 * bit[0] INT_THRES_SEL Thresholds are applied to 0:proximity 1:als measurement
Rhyme 0:cf922a073b7f 71 * Interrupt Count Exceed
Rhyme 0:cf922a073b7f 72 * 000 - 1 count = DEFAULT
Rhyme 0:cf922a073b7f 73 * 001 - 2 count
Rhyme 0:cf922a073b7f 74 * 010 - 4 count
Rhyme 0:cf922a073b7f 75 * 011 - 8 count
Rhyme 0:cf922a073b7f 76 * 100 - 16 count
Rhyme 0:cf922a073b7f 77 * 101 - 32 count
Rhyme 0:cf922a073b7f 78 * 110 - 64 count
Rhyme 0:cf922a073b7f 79 * 111 - 128 count
Rhyme 0:cf922a073b7f 80 *
Rhyme 0:cf922a073b7f 81 * Register #10 and #11 Low Threshold (0x8A, 0x8B)
Rhyme 0:cf922a073b7f 82 * bit[15:8] MSB (address 0x8A)
Rhyme 0:cf922a073b7f 83 * bit[7:0] LSB (address 0x8B)
Rhyme 0:cf922a073b7f 84 *
Rhyme 0:cf922a073b7f 85 * Register #12 and #13 High Threshold (0x8C, 0x8D)
Rhyme 0:cf922a073b7f 86 * bit[15:8] MSB (address 0x8C)
Rhyme 0:cf922a073b7f 87 * bit[7:0] LSB (address 0x8D)
Rhyme 0:cf922a073b7f 88 *
Rhyme 0:cf922a073b7f 89 * Register #14 Interrupt Status (0x8E)
Rhyme 0:cf922a073b7f 90 * bit[7:4] (n/a)
Rhyme 0:cf922a073b7f 91 * bit[3] int_prox_ready
Rhyme 0:cf922a073b7f 92 * bit[2] int_als_ready
Rhyme 0:cf922a073b7f 93 * bit[1] int_th_low
Rhyme 0:cf922a073b7f 94 * bit[0] int_th_hi
Rhyme 0:cf922a073b7f 95 * Once an interrupt is generated the corresponding status bit
Rhyme 0:cf922a073b7f 96 * goes to 1 and stays there unless it is cleared by writing a 1
Rhyme 0:cf922a073b7f 97 * in the corresponding bit.
Rhyme 0:cf922a073b7f 98 *
Rhyme 0:cf922a073b7f 99 * Register #15 Proximity Modulator Timing Adjustment (0x8F)
Rhyme 0:cf922a073b7f 100 * bit[7:5] Modulation delay time
Rhyme 0:cf922a073b7f 101 * bit[4:3] Proximity frequency
Rhyme 0:cf922a073b7f 102 * bit[2:0] Modulation deat time
Rhyme 0:cf922a073b7f 103 * The settings for best performance will be provided by Vishay.
Rhyme 0:cf922a073b7f 104 * With first samples this is evaluated to:
Rhyme 0:cf922a073b7f 105 * delay time = 0
Rhyme 0:cf922a073b7f 106 * frequency = 0
Rhyme 0:cf922a073b7f 107 * deat time = 1
Rhyme 0:cf922a073b7f 108 * With that Register #15 should be programmed with 1 (= default value).
Rhyme 0:cf922a073b7f 109 *
Rhyme 0:cf922a073b7f 110 */
Rhyme 0:cf922a073b7f 111
Rhyme 0:cf922a073b7f 112 class VCNL4010
Rhyme 0:cf922a073b7f 113 {
Rhyme 0:cf922a073b7f 114 public:
Rhyme 0:cf922a073b7f 115 /**
Rhyme 0:cf922a073b7f 116 * VCNL4010 constructor
Rhyme 0:cf922a073b7f 117 *
Rhyme 0:cf922a073b7f 118 * @param sda SDA pin
Rhyme 0:cf922a073b7f 119 * @param sdl SCL pin
Rhyme 0:cf922a073b7f 120 * @param addr addr of the I2C peripheral
Rhyme 0:cf922a073b7f 121 */
Rhyme 0:cf922a073b7f 122 VCNL4010(PinName sda, PinName scl, int addr);
Rhyme 0:cf922a073b7f 123
Rhyme 0:cf922a073b7f 124 /**
Rhyme 0:cf922a073b7f 125 * VCNL4010 destructor
Rhyme 0:cf922a073b7f 126 */
Rhyme 0:cf922a073b7f 127 ~VCNL4010();
Rhyme 0:cf922a073b7f 128
Rhyme 0:cf922a073b7f 129 uint8_t getCommandReg(void) ;
Rhyme 0:cf922a073b7f 130 void setCommandReg(uint8_t cmd) ;
Rhyme 0:cf922a073b7f 131 uint8_t getProductID(void) ;
Rhyme 0:cf922a073b7f 132 uint8_t getProxRate(void) ;
Rhyme 0:cf922a073b7f 133 void setProxRate(uint8_t rate) ;
Rhyme 0:cf922a073b7f 134 uint8_t getIrLedCurrent(void) ;
Rhyme 0:cf922a073b7f 135 void setIrLedCurrent(uint8_t IrLedCur) ;
Rhyme 0:cf922a073b7f 136 uint8_t getAlsParam(void) ;
Rhyme 0:cf922a073b7f 137 void setAlsParam(uint8_t param) ;
Rhyme 0:cf922a073b7f 138 uint16_t getAls(void) ;
Rhyme 0:cf922a073b7f 139 uint16_t getProx(void) ;
Rhyme 0:cf922a073b7f 140 uint8_t getIntCtrl(void) ;
Rhyme 0:cf922a073b7f 141 void setIntCtrl(uint8_t ctrl) ;
Rhyme 0:cf922a073b7f 142 uint16_t getLowThreshold(void) ;
Rhyme 0:cf922a073b7f 143 void setLowThreshold(uint16_t thr) ;
Rhyme 0:cf922a073b7f 144 uint16_t getHighThreshold(void) ;
Rhyme 0:cf922a073b7f 145 void setHighThreshold(uint16_t thr) ;
Rhyme 0:cf922a073b7f 146 uint8_t getIntStatus(void) ;
Rhyme 0:cf922a073b7f 147 void setIntStatus(uint8_t status) ;
Rhyme 0:cf922a073b7f 148 uint8_t getProxModTiming(void) ;
Rhyme 0:cf922a073b7f 149 void setProxModTiming(uint8_t timing) ;
Rhyme 0:cf922a073b7f 150
Rhyme 0:cf922a073b7f 151 private:
Rhyme 0:cf922a073b7f 152 I2C m_i2c;
Rhyme 0:cf922a073b7f 153 int m_addr;
Rhyme 0:cf922a073b7f 154 void readRegs(int addr, uint8_t * data, int len);
Rhyme 0:cf922a073b7f 155 void writeRegs(uint8_t * data, int len);
Rhyme 0:cf922a073b7f 156 };
Rhyme 0:cf922a073b7f 157
Rhyme 0:cf922a073b7f 158 #endif /* _VCNL4010_H_ */