This program demonstrates ambient light sensing on the QW Shields.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers VCNL4010.h Source File

VCNL4010.h

00001 /*
00002 Copyright (c) 2012 Vishay GmbH, www.vishay.com
00003 author: DS, version 1.2
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #ifndef VCNL40x0_H
00025 #define VCNL40x0_H
00026 
00027 #include "mbed.h"
00028 
00029 // Library for the Vishay Proximity/Ambient Light Sensor VCNL4010/4020
00030 // The VCNL4x00 is a I2C digital Proximity and Ambient Light Sensor in a small SMD package
00031 
00032 #define VCNL40x0_ADDRESS               (0x26) // 001 0011 shifted left 1 bit = 0x26
00033 
00034 // registers 
00035 #define REGISTER_COMMAND               (0x80)
00036 #define REGISTER_ID                    (0x81)
00037 #define REGISTER_PROX_RATE             (0x82)
00038 #define REGISTER_PROX_CURRENT          (0x83)
00039 #define REGISTER_AMBI_PARAMETER        (0x84)
00040 #define REGISTER_AMBI_VALUE            (0x85)
00041 #define REGISTER_PROX_VALUE            (0x87)
00042 #define REGISTER_INTERRUPT_CONTROL     (0x89)
00043 #define REGISTER_INTERRUPT_LOW_THRES   (0x8a)
00044 #define REGISTER_INTERRUPT_HIGH_THRES  (0x8c)
00045 #define REGISTER_INTERRUPT_STATUS      (0x8e)
00046 #define REGISTER_PROX_TIMING           (0x8f)
00047 #define REGISTER_AMBI_IR_LIGHT_LEVEL   (0x90)   // This register is not intended to be use by customer
00048 
00049 // Bits in Command register (0x80)
00050 #define COMMAND_ALL_DISABLE            (0x00)
00051 #define COMMAND_SELFTIMED_MODE_ENABLE  (0x01)
00052 #define COMMAND_PROX_ENABLE            (0x02)
00053 #define COMMAND_AMBI_ENABLE            (0x04)
00054 #define COMMAND_PROX_ON_DEMAND         (0x08)
00055 #define COMMAND_AMBI_ON_DEMAND         (0x10)
00056 #define COMMAND_MASK_PROX_DATA_READY   (0x20)
00057 #define COMMAND_MASK_AMBI_DATA_READY   (0x40)
00058 #define COMMAND_MASK_LOCK              (0x80)
00059 
00060 // Bits in Product ID Revision Register (0x81)
00061 #define PRODUCT_MASK_REVISION_ID       (0x0f)
00062 #define PRODUCT_MASK_PRODUCT_ID        (0xf0)
00063 
00064 // Bits in Prox Measurement Rate register (0x82)
00065 #define PROX_MEASUREMENT_RATE_2        (0x00)   // DEFAULT
00066 #define PROX_MEASUREMENT_RATE_4        (0x01)
00067 #define PROX_MEASUREMENT_RATE_8        (0x02)
00068 #define PROX_MEASUREMENT_RATE_16       (0x03)
00069 #define PROX_MEASUREMENT_RATE_31       (0x04)
00070 #define PROX_MEASUREMENT_RATE_62       (0x05)
00071 #define PROX_MEASUREMENT_RATE_125      (0x06)
00072 #define PROX_MEASUREMENT_RATE_250      (0x07)
00073 #define PROX_MASK_MEASUREMENT_RATE     (0x07)
00074 
00075 // Bits in Procimity LED current setting (0x83)
00076 #define PROX_MASK_LED_CURRENT          (0x3f)   // DEFAULT = 2
00077 #define PROX_MASK_FUSE_PROG_ID         (0xc0)
00078 
00079 // Bits in Ambient Light Parameter register (0x84)
00080 #define AMBI_PARA_AVERAGE_1            (0x00)
00081 #define AMBI_PARA_AVERAGE_2            (0x01)
00082 #define AMBI_PARA_AVERAGE_4            (0x02)
00083 #define AMBI_PARA_AVERAGE_8            (0x03)
00084 #define AMBI_PARA_AVERAGE_16           (0x04)
00085 #define AMBI_PARA_AVERAGE_32           (0x05)   // DEFAULT
00086 #define AMBI_PARA_AVERAGE_64           (0x06)
00087 #define AMBI_PARA_AVERAGE_128          (0x07)
00088 #define AMBI_MASK_PARA_AVERAGE         (0x07)
00089 
00090 #define AMBI_PARA_AUTO_OFFSET_ENABLE   (0x08)   // DEFAULT enable
00091 #define AMBI_MASK_PARA_AUTO_OFFSET     (0x08)
00092 
00093 #define AMBI_PARA_MEAS_RATE_1          (0x00)
00094 #define AMBI_PARA_MEAS_RATE_2          (0x10)   // DEFAULT
00095 #define AMBI_PARA_MEAS_RATE_3          (0x20)
00096 #define AMBI_PARA_MEAS_RATE_4          (0x30)
00097 #define AMBI_PARA_MEAS_RATE_5          (0x40)
00098 #define AMBI_PARA_MEAS_RATE_6          (0x50)
00099 #define AMBI_PARA_MEAS_RATE_8          (0x60)
00100 #define AMBI_PARA_MEAS_RATE_10         (0x70)
00101 #define AMBI_MASK_PARA_MEAS_RATE       (0x70)
00102 
00103 #define AMBI_PARA_CONT_CONV_ENABLE     (0x80)
00104 #define AMBI_MASK_PARA_CONT_CONV       (0x80)   // DEFAULT disable
00105 
00106 // Bits in Interrupt Control Register (x89)
00107 #define INTERRUPT_THRES_SEL_PROX       (0x00)
00108 #define INTERRUPT_THRES_SEL_ALS        (0x01)
00109 
00110 #define INTERRUPT_THRES_ENABLE         (0x02)
00111 
00112 #define INTERRUPT_ALS_READY_ENABLE     (0x04)
00113 
00114 #define INTERRUPT_PROX_READY_ENABLE    (0x08)
00115 
00116 #define INTERRUPT_COUNT_EXCEED_1       (0x00)   // DEFAULT
00117 #define INTERRUPT_COUNT_EXCEED_2       (0x20)
00118 #define INTERRUPT_COUNT_EXCEED_4       (0x40)
00119 #define INTERRUPT_COUNT_EXCEED_8       (0x60)
00120 #define INTERRUPT_COUNT_EXCEED_16      (0x80)
00121 #define INTERRUPT_COUNT_EXCEED_32      (0xa0)
00122 #define INTERRUPT_COUNT_EXCEED_64      (0xc0)
00123 #define INTERRUPT_COUNT_EXCEED_128     (0xe0)
00124 #define INTERRUPT_MASK_COUNT_EXCEED    (0xe0) 
00125 
00126 // Bits in Interrupt Status Register (x8e)
00127 #define INTERRUPT_STATUS_THRES_HI      (0x01)
00128 #define INTERRUPT_STATUS_THRES_LO      (0x02)
00129 #define INTERRUPT_STATUS_ALS_READY     (0x04)
00130 #define INTERRUPT_STATUS_PROX_READY    (0x08)
00131 #define INTERRUPT_MASK_STATUS_THRES_HI (0x01)
00132 #define INTERRUPT_MASK_THRES_LO        (0x02)
00133 #define INTERRUPT_MASK_ALS_READY       (0x04)
00134 #define INTERRUPT_MASK_PROX_READY      (0x08)
00135 
00136 typedef enum {
00137     VCNL40x0_ERROR_OK = 0,               // Everything executed normally
00138     VCNL40x0_ERROR_I2CINIT,              // Unable to initialise I2C
00139     VCNL40x0_ERROR_I2CBUSY,              // I2C already in use
00140     VCNL40x0_ERROR_LAST
00141 } VCNL40x0Error_e;
00142 
00143 
00144 class VCNL40x0 {
00145 public:
00146 // Creates an instance of the class.
00147 // Connect module at I2C address addr using I2C port pins sda and scl.
00148 
00149     VCNL40x0 (PinName sda, PinName scl, unsigned char addr);
00150 
00151 // Destroys instance
00152 
00153     ~VCNL40x0();
00154 
00155 // public functions
00156 
00157     VCNL40x0Error_e Init (void);
00158     
00159     VCNL40x0Error_e SetCommandRegister (unsigned char Command);
00160     VCNL40x0Error_e SetCurrent (unsigned char CurrentValue);
00161     VCNL40x0Error_e SetProximityRate (unsigned char ProximityRate);
00162     VCNL40x0Error_e SetAmbiConfiguration (unsigned char AmbiConfiguration);
00163     VCNL40x0Error_e SetLowThreshold (unsigned int LowThreshold);
00164     VCNL40x0Error_e SetHighThreshold (unsigned int HighThreshold);
00165     VCNL40x0Error_e SetInterruptControl (unsigned char InterruptControl);
00166     VCNL40x0Error_e SetInterruptStatus (unsigned char InterruptStatus);
00167     VCNL40x0Error_e SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment);
00168 
00169     VCNL40x0Error_e ReadID (unsigned char *ID);
00170     VCNL40x0Error_e ReadCurrent (unsigned char *CurrentValue);
00171     VCNL40x0Error_e ReadCommandRegister (unsigned char *Command);
00172     VCNL40x0Error_e ReadProxiValue (unsigned int *ProxiValue);
00173     VCNL40x0Error_e ReadAmbiValue (unsigned int *AmbiValue);
00174     VCNL40x0Error_e ReadInterruptStatus (unsigned char *InterruptStatus);
00175     VCNL40x0Error_e ReadInterruptControl (unsigned char *InterruptControl);
00176     
00177     VCNL40x0Error_e ReadProxiOnDemand (unsigned int *ProxiValue);
00178     VCNL40x0Error_e ReadAmbiOnDemand (unsigned int *AmbiValue);
00179  
00180 private:
00181     I2C _i2c;
00182     int _addr;
00183     char _send[3];
00184     char _receive[2];
00185     
00186 };
00187 
00188 #endif