Driver for Proximity / Ambient Light Sensor VCNL40xx from Vishay

Dependencies:   mbed

Committer:
kokisan2000
Date:
Sat Nov 30 19:44:13 2013 +0000
Revision:
0:1f3d09f0060a
Library for Proximity / Ambient Light Sensor VCNL40xx from Vishay

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kokisan2000 0:1f3d09f0060a 1 /*
kokisan2000 0:1f3d09f0060a 2 Copyright (c) 2012 Vishay GmbH, www.vishay.com
kokisan2000 0:1f3d09f0060a 3 author: DS, version 1.2
kokisan2000 0:1f3d09f0060a 4
kokisan2000 0:1f3d09f0060a 5 Permission is hereby granted, free of charge, to any person obtaining a copy
kokisan2000 0:1f3d09f0060a 6 of this software and associated documentation files (the "Software"), to deal
kokisan2000 0:1f3d09f0060a 7 in the Software without restriction, including without limitation the rights
kokisan2000 0:1f3d09f0060a 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
kokisan2000 0:1f3d09f0060a 9 copies of the Software, and to permit persons to whom the Software is
kokisan2000 0:1f3d09f0060a 10 furnished to do so, subject to the following conditions:
kokisan2000 0:1f3d09f0060a 11
kokisan2000 0:1f3d09f0060a 12 The above copyright notice and this permission notice shall be included in
kokisan2000 0:1f3d09f0060a 13 all copies or substantial portions of the Software.
kokisan2000 0:1f3d09f0060a 14
kokisan2000 0:1f3d09f0060a 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
kokisan2000 0:1f3d09f0060a 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
kokisan2000 0:1f3d09f0060a 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
kokisan2000 0:1f3d09f0060a 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
kokisan2000 0:1f3d09f0060a 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kokisan2000 0:1f3d09f0060a 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
kokisan2000 0:1f3d09f0060a 21 THE SOFTWARE.
kokisan2000 0:1f3d09f0060a 22 */
kokisan2000 0:1f3d09f0060a 23
kokisan2000 0:1f3d09f0060a 24 #ifndef VCNL40x0_H
kokisan2000 0:1f3d09f0060a 25 #define VCNL40x0_H
kokisan2000 0:1f3d09f0060a 26
kokisan2000 0:1f3d09f0060a 27 #include "mbed.h"
kokisan2000 0:1f3d09f0060a 28
kokisan2000 0:1f3d09f0060a 29 // Library for the Vishay Proximity/Ambient Light Sensor VCNL4010/4020
kokisan2000 0:1f3d09f0060a 30 // The VCNL4x00 is a I2C digital Proximity and Ambient Light Sensor in a small SMD package
kokisan2000 0:1f3d09f0060a 31
kokisan2000 0:1f3d09f0060a 32 #define VCNL40x0_ADDRESS (0x26) // 001 0011 shifted left 1 bit = 0x26
kokisan2000 0:1f3d09f0060a 33
kokisan2000 0:1f3d09f0060a 34 // registers
kokisan2000 0:1f3d09f0060a 35 #define REGISTER_COMMAND (0x80)
kokisan2000 0:1f3d09f0060a 36 #define REGISTER_ID (0x81)
kokisan2000 0:1f3d09f0060a 37 #define REGISTER_PROX_RATE (0x82)
kokisan2000 0:1f3d09f0060a 38 #define REGISTER_PROX_CURRENT (0x83)
kokisan2000 0:1f3d09f0060a 39 #define REGISTER_AMBI_PARAMETER (0x84)
kokisan2000 0:1f3d09f0060a 40 #define REGISTER_AMBI_VALUE (0x85)
kokisan2000 0:1f3d09f0060a 41 #define REGISTER_PROX_VALUE (0x87)
kokisan2000 0:1f3d09f0060a 42 #define REGISTER_INTERRUPT_CONTROL (0x89)
kokisan2000 0:1f3d09f0060a 43 #define REGISTER_INTERRUPT_LOW_THRES (0x8a)
kokisan2000 0:1f3d09f0060a 44 #define REGISTER_INTERRUPT_HIGH_THRES (0x8c)
kokisan2000 0:1f3d09f0060a 45 #define REGISTER_INTERRUPT_STATUS (0x8e)
kokisan2000 0:1f3d09f0060a 46 #define REGISTER_PROX_TIMING (0x8f)
kokisan2000 0:1f3d09f0060a 47 #define REGISTER_AMBI_IR_LIGHT_LEVEL (0x90) // This register is not intended to be use by customer
kokisan2000 0:1f3d09f0060a 48
kokisan2000 0:1f3d09f0060a 49 // Bits in Command register (0x80)
kokisan2000 0:1f3d09f0060a 50 #define COMMAND_ALL_DISABLE (0x00)
kokisan2000 0:1f3d09f0060a 51 #define COMMAND_SELFTIMED_MODE_ENABLE (0x01)
kokisan2000 0:1f3d09f0060a 52 #define COMMAND_PROX_ENABLE (0x02)
kokisan2000 0:1f3d09f0060a 53 #define COMMAND_AMBI_ENABLE (0x04)
kokisan2000 0:1f3d09f0060a 54 #define COMMAND_PROX_ON_DEMAND (0x08)
kokisan2000 0:1f3d09f0060a 55 #define COMMAND_AMBI_ON_DEMAND (0x10)
kokisan2000 0:1f3d09f0060a 56 #define COMMAND_MASK_PROX_DATA_READY (0x20)
kokisan2000 0:1f3d09f0060a 57 #define COMMAND_MASK_AMBI_DATA_READY (0x40)
kokisan2000 0:1f3d09f0060a 58 #define COMMAND_MASK_LOCK (0x80)
kokisan2000 0:1f3d09f0060a 59
kokisan2000 0:1f3d09f0060a 60 // Bits in Product ID Revision Register (0x81)
kokisan2000 0:1f3d09f0060a 61 #define PRODUCT_MASK_REVISION_ID (0x0f)
kokisan2000 0:1f3d09f0060a 62 #define PRODUCT_MASK_PRODUCT_ID (0xf0)
kokisan2000 0:1f3d09f0060a 63
kokisan2000 0:1f3d09f0060a 64 // Bits in Prox Measurement Rate register (0x82)
kokisan2000 0:1f3d09f0060a 65 #define PROX_MEASUREMENT_RATE_2 (0x00) // DEFAULT
kokisan2000 0:1f3d09f0060a 66 #define PROX_MEASUREMENT_RATE_4 (0x01)
kokisan2000 0:1f3d09f0060a 67 #define PROX_MEASUREMENT_RATE_8 (0x02)
kokisan2000 0:1f3d09f0060a 68 #define PROX_MEASUREMENT_RATE_16 (0x03)
kokisan2000 0:1f3d09f0060a 69 #define PROX_MEASUREMENT_RATE_31 (0x04)
kokisan2000 0:1f3d09f0060a 70 #define PROX_MEASUREMENT_RATE_62 (0x05)
kokisan2000 0:1f3d09f0060a 71 #define PROX_MEASUREMENT_RATE_125 (0x06)
kokisan2000 0:1f3d09f0060a 72 #define PROX_MEASUREMENT_RATE_250 (0x07)
kokisan2000 0:1f3d09f0060a 73 #define PROX_MASK_MEASUREMENT_RATE (0x07)
kokisan2000 0:1f3d09f0060a 74
kokisan2000 0:1f3d09f0060a 75 // Bits in Procimity LED current setting (0x83)
kokisan2000 0:1f3d09f0060a 76 #define PROX_MASK_LED_CURRENT (0x3f) // DEFAULT = 2
kokisan2000 0:1f3d09f0060a 77 #define PROX_MASK_FUSE_PROG_ID (0xc0)
kokisan2000 0:1f3d09f0060a 78
kokisan2000 0:1f3d09f0060a 79 // Bits in Ambient Light Parameter register (0x84)
kokisan2000 0:1f3d09f0060a 80 #define AMBI_PARA_AVERAGE_1 (0x00)
kokisan2000 0:1f3d09f0060a 81 #define AMBI_PARA_AVERAGE_2 (0x01)
kokisan2000 0:1f3d09f0060a 82 #define AMBI_PARA_AVERAGE_4 (0x02)
kokisan2000 0:1f3d09f0060a 83 #define AMBI_PARA_AVERAGE_8 (0x03)
kokisan2000 0:1f3d09f0060a 84 #define AMBI_PARA_AVERAGE_16 (0x04)
kokisan2000 0:1f3d09f0060a 85 #define AMBI_PARA_AVERAGE_32 (0x05) // DEFAULT
kokisan2000 0:1f3d09f0060a 86 #define AMBI_PARA_AVERAGE_64 (0x06)
kokisan2000 0:1f3d09f0060a 87 #define AMBI_PARA_AVERAGE_128 (0x07)
kokisan2000 0:1f3d09f0060a 88 #define AMBI_MASK_PARA_AVERAGE (0x07)
kokisan2000 0:1f3d09f0060a 89
kokisan2000 0:1f3d09f0060a 90 #define AMBI_PARA_AUTO_OFFSET_ENABLE (0x08) // DEFAULT enable
kokisan2000 0:1f3d09f0060a 91 #define AMBI_MASK_PARA_AUTO_OFFSET (0x08)
kokisan2000 0:1f3d09f0060a 92
kokisan2000 0:1f3d09f0060a 93 #define AMBI_PARA_MEAS_RATE_1 (0x00)
kokisan2000 0:1f3d09f0060a 94 #define AMBI_PARA_MEAS_RATE_2 (0x10) // DEFAULT
kokisan2000 0:1f3d09f0060a 95 #define AMBI_PARA_MEAS_RATE_3 (0x20)
kokisan2000 0:1f3d09f0060a 96 #define AMBI_PARA_MEAS_RATE_4 (0x30)
kokisan2000 0:1f3d09f0060a 97 #define AMBI_PARA_MEAS_RATE_5 (0x40)
kokisan2000 0:1f3d09f0060a 98 #define AMBI_PARA_MEAS_RATE_6 (0x50)
kokisan2000 0:1f3d09f0060a 99 #define AMBI_PARA_MEAS_RATE_8 (0x60)
kokisan2000 0:1f3d09f0060a 100 #define AMBI_PARA_MEAS_RATE_10 (0x70)
kokisan2000 0:1f3d09f0060a 101 #define AMBI_MASK_PARA_MEAS_RATE (0x70)
kokisan2000 0:1f3d09f0060a 102
kokisan2000 0:1f3d09f0060a 103 #define AMBI_PARA_CONT_CONV_ENABLE (0x80)
kokisan2000 0:1f3d09f0060a 104 #define AMBI_MASK_PARA_CONT_CONV (0x80) // DEFAULT disable
kokisan2000 0:1f3d09f0060a 105
kokisan2000 0:1f3d09f0060a 106 // Bits in Interrupt Control Register (x89)
kokisan2000 0:1f3d09f0060a 107 #define INTERRUPT_THRES_SEL_PROX (0x00)
kokisan2000 0:1f3d09f0060a 108 #define INTERRUPT_THRES_SEL_ALS (0x01)
kokisan2000 0:1f3d09f0060a 109
kokisan2000 0:1f3d09f0060a 110 #define INTERRUPT_THRES_ENABLE (0x02)
kokisan2000 0:1f3d09f0060a 111
kokisan2000 0:1f3d09f0060a 112 #define INTERRUPT_ALS_READY_ENABLE (0x04)
kokisan2000 0:1f3d09f0060a 113
kokisan2000 0:1f3d09f0060a 114 #define INTERRUPT_PROX_READY_ENABLE (0x08)
kokisan2000 0:1f3d09f0060a 115
kokisan2000 0:1f3d09f0060a 116 #define INTERRUPT_COUNT_EXCEED_1 (0x00) // DEFAULT
kokisan2000 0:1f3d09f0060a 117 #define INTERRUPT_COUNT_EXCEED_2 (0x20)
kokisan2000 0:1f3d09f0060a 118 #define INTERRUPT_COUNT_EXCEED_4 (0x40)
kokisan2000 0:1f3d09f0060a 119 #define INTERRUPT_COUNT_EXCEED_8 (0x60)
kokisan2000 0:1f3d09f0060a 120 #define INTERRUPT_COUNT_EXCEED_16 (0x80)
kokisan2000 0:1f3d09f0060a 121 #define INTERRUPT_COUNT_EXCEED_32 (0xa0)
kokisan2000 0:1f3d09f0060a 122 #define INTERRUPT_COUNT_EXCEED_64 (0xc0)
kokisan2000 0:1f3d09f0060a 123 #define INTERRUPT_COUNT_EXCEED_128 (0xe0)
kokisan2000 0:1f3d09f0060a 124 #define INTERRUPT_MASK_COUNT_EXCEED (0xe0)
kokisan2000 0:1f3d09f0060a 125
kokisan2000 0:1f3d09f0060a 126 // Bits in Interrupt Status Register (x8e)
kokisan2000 0:1f3d09f0060a 127 #define INTERRUPT_STATUS_THRES_HI (0x01)
kokisan2000 0:1f3d09f0060a 128 #define INTERRUPT_STATUS_THRES_LO (0x02)
kokisan2000 0:1f3d09f0060a 129 #define INTERRUPT_STATUS_ALS_READY (0x04)
kokisan2000 0:1f3d09f0060a 130 #define INTERRUPT_STATUS_PROX_READY (0x08)
kokisan2000 0:1f3d09f0060a 131 #define INTERRUPT_MASK_STATUS_THRES_HI (0x01)
kokisan2000 0:1f3d09f0060a 132 #define INTERRUPT_MASK_THRES_LO (0x02)
kokisan2000 0:1f3d09f0060a 133 #define INTERRUPT_MASK_ALS_READY (0x04)
kokisan2000 0:1f3d09f0060a 134 #define INTERRUPT_MASK_PROX_READY (0x08)
kokisan2000 0:1f3d09f0060a 135
kokisan2000 0:1f3d09f0060a 136 typedef enum {
kokisan2000 0:1f3d09f0060a 137 VCNL40x0_ERROR_OK = 0, // Everything executed normally
kokisan2000 0:1f3d09f0060a 138 VCNL40x0_ERROR_I2CINIT, // Unable to initialise I2C
kokisan2000 0:1f3d09f0060a 139 VCNL40x0_ERROR_I2CBUSY, // I2C already in use
kokisan2000 0:1f3d09f0060a 140 VCNL40x0_ERROR_LAST
kokisan2000 0:1f3d09f0060a 141 } VCNL40x0Error_e;
kokisan2000 0:1f3d09f0060a 142
kokisan2000 0:1f3d09f0060a 143
kokisan2000 0:1f3d09f0060a 144 class VCNL40x0 {
kokisan2000 0:1f3d09f0060a 145 public:
kokisan2000 0:1f3d09f0060a 146 // Creates an instance of the class.
kokisan2000 0:1f3d09f0060a 147 // Connect module at I2C address addr using I2C port pins sda and scl.
kokisan2000 0:1f3d09f0060a 148
kokisan2000 0:1f3d09f0060a 149 VCNL40x0 (PinName sda, PinName scl, unsigned char addr);
kokisan2000 0:1f3d09f0060a 150
kokisan2000 0:1f3d09f0060a 151 // Destroys instance
kokisan2000 0:1f3d09f0060a 152
kokisan2000 0:1f3d09f0060a 153 ~VCNL40x0();
kokisan2000 0:1f3d09f0060a 154
kokisan2000 0:1f3d09f0060a 155 // public functions
kokisan2000 0:1f3d09f0060a 156
kokisan2000 0:1f3d09f0060a 157 VCNL40x0Error_e Init (void);
kokisan2000 0:1f3d09f0060a 158
kokisan2000 0:1f3d09f0060a 159 VCNL40x0Error_e SetCommandRegister (unsigned char Command);
kokisan2000 0:1f3d09f0060a 160 VCNL40x0Error_e SetCurrent (unsigned char CurrentValue);
kokisan2000 0:1f3d09f0060a 161 VCNL40x0Error_e SetProximityRate (unsigned char ProximityRate);
kokisan2000 0:1f3d09f0060a 162 VCNL40x0Error_e SetAmbiConfiguration (unsigned char AmbiConfiguration);
kokisan2000 0:1f3d09f0060a 163 VCNL40x0Error_e SetLowThreshold (unsigned int LowThreshold);
kokisan2000 0:1f3d09f0060a 164 VCNL40x0Error_e SetHighThreshold (unsigned int HighThreshold);
kokisan2000 0:1f3d09f0060a 165 VCNL40x0Error_e SetInterruptControl (unsigned char InterruptControl);
kokisan2000 0:1f3d09f0060a 166 VCNL40x0Error_e SetInterruptStatus (unsigned char InterruptStatus);
kokisan2000 0:1f3d09f0060a 167 VCNL40x0Error_e SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment);
kokisan2000 0:1f3d09f0060a 168
kokisan2000 0:1f3d09f0060a 169 VCNL40x0Error_e ReadID (unsigned char *ID);
kokisan2000 0:1f3d09f0060a 170 VCNL40x0Error_e ReadCurrent (unsigned char *CurrentValue);
kokisan2000 0:1f3d09f0060a 171 VCNL40x0Error_e ReadCommandRegister (unsigned char *Command);
kokisan2000 0:1f3d09f0060a 172 VCNL40x0Error_e ReadProxiValue (unsigned int *ProxiValue);
kokisan2000 0:1f3d09f0060a 173 VCNL40x0Error_e ReadAmbiValue (unsigned int *AmbiValue);
kokisan2000 0:1f3d09f0060a 174 VCNL40x0Error_e ReadInterruptStatus (unsigned char *InterruptStatus);
kokisan2000 0:1f3d09f0060a 175 VCNL40x0Error_e ReadInterruptControl (unsigned char *InterruptControl);
kokisan2000 0:1f3d09f0060a 176
kokisan2000 0:1f3d09f0060a 177 VCNL40x0Error_e ReadProxiOnDemand (unsigned int *ProxiValue);
kokisan2000 0:1f3d09f0060a 178 VCNL40x0Error_e ReadAmbiOnDemand (unsigned int *AmbiValue);
kokisan2000 0:1f3d09f0060a 179
kokisan2000 0:1f3d09f0060a 180 private:
kokisan2000 0:1f3d09f0060a 181 I2C _i2c;
kokisan2000 0:1f3d09f0060a 182 int _addr;
kokisan2000 0:1f3d09f0060a 183 char _send[3];
kokisan2000 0:1f3d09f0060a 184 char _receive[2];
kokisan2000 0:1f3d09f0060a 185
kokisan2000 0:1f3d09f0060a 186 };
kokisan2000 0:1f3d09f0060a 187
kokisan2000 0:1f3d09f0060a 188 #endif
kokisan2000 0:1f3d09f0060a 189