This lib allows control of VISHAY VCNL4000 distance/ambient light sensor via I2C bus.

Dependents:   Major_dHome

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers VCNL4000.h Source File

VCNL4000.h

00001 /* mbed VCNL4000 Library version 0beta1
00002  * Copyright (c) 2012 bengo
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #ifndef VCNL4000_H
00024 #define VCNL4000_H
00025 
00026 #include "mbed.h"
00027 
00028 /** VCNL4000 distance/luminosity sensor controller library
00029  *
00030  * Example:
00031  * @code
00032  * #include "mbed.h"
00033  * #include "VCNL4000.h"
00034  *
00035  * Serial pc(USBTX, USBRX); // tx, rx
00036  *
00037  * // -----------------------------------
00038  * int main() {
00039  *
00040  *    VCNL4000 vcnl( p9, p10 );
00041  *
00042  *    int status = vcnl.getStatus();
00043  *    
00044  *    if( status != 0 ) {
00045  *        pc.printf( "\x1b[2J\x1b[f Something went wrong with VCNL4000 access, status = %d.\n\r", status );
00046  *        exit( 1 );
00047  *    }
00048  *  
00049  *    vcnl.setALAveragingFunction( 0x0f );
00050  *    vcnl.setIRLedCurrent( 2000 );
00051  *    vcnl.setProximityMeasurementSigFreq( 2 );
00052  *    vcnl.setProxiModulatorDelayTime( 0x04 );
00053  *    vcnl.setProxiModulatorDeadTime( 0x01 );
00054  *    
00055  *    while( 1 ) {
00056  *       int prox = vcnl.getProximity();
00057  *       int ambl = vcnl.getAmbientLight();
00058  *       pc.printf("\x1b[2J\x1b[f proxy: %d\n\r amb.l: %d\n\r", prox, ambl ); 
00059  *       wait( 0.1 );
00060  *    }     
00061  * }
00062  * @endcode
00063  */
00064 
00065 class VCNL4000 {
00066 
00067  public:
00068  
00069    /**
00070     *  Create a VCNL4000 object connected to the specified I2C pins
00071     * @param sda I2C SDA pin
00072     * @param scl I2C SDL pin  
00073     */
00074    VCNL4000( PinName sda, PinName scl );
00075    
00076    ~VCNL4000( void );
00077    
00078    /**
00079     * Return status code of previos function call
00080     */
00081    inline int getStatus( void ) { return( _status ); }
00082    
00083    /**
00084     * Read VCNL4000 internal register content
00085     * @param reg register address 
00086     */
00087    int registerRead( int reg );
00088    
00089    /**
00090     * Send data to VCNL4000 internal register
00091     * @param reg register address
00092     * @param data value to be written
00093     */
00094    void registerWrite( int reg, unsigned char data );
00095    
00096    /**
00097     * Read VCNL4000 proximity measurement
00098     */
00099    int getProximity( void );
00100    
00101    /**
00102     * Read VCNL4000 ambient light measurement
00103     */
00104    int getAmbientLight( void );
00105    
00106    /**
00107     * Get VCNL 4000 product id (should be 1)
00108     */
00109    int getProductId( void );
00110    
00111    /**
00112     * Get VCNL 4000 product revision (should be 1)
00113     */
00114    int getProductRevision( void );
00115 
00116    /**
00117     * Return True when a proximity data measurement completed
00118     */ 
00119    bool proximityDataReady( void );
00120    
00121    /**
00122     * Return True when an ambient light measurement completed
00123     */
00124    bool ambientLightDataReady( void );
00125 
00126    /**
00127     * Tell VCNL4000 to start a proximity measurement
00128     */
00129    void startProximityMeasurement( void );
00130    
00131    /**
00132     * Tell VCNL4000 to start an ambient light measurement
00133     */
00134    void startAmbientLightMeasurement( void );
00135    
00136    /** 
00137     * Set VCNL4000 infrared led current
00138     * @param milliAmps current in mA
00139     */
00140    void setIRLedCurrent( int milliAmps );
00141    
00142    /*
00143     * Enable VCNL4000 continuous conversion mode
00144     */
00145    void enableALContinuousConversionMode( void );
00146 
00147    /*
00148     * Disable VCNL4000 continuous conversion mode
00149     */
00150    void disableALContinuousConversionMode( void );
00151    
00152    /*
00153     * Return True if VCNL4000 is set in continuous conversion mode
00154     */ 
00155    bool isALContinuousConversionMode( void );
00156 
00157    /*
00158     * Enable ambient light auto offset compensation
00159     */
00160    void enableALAutoOffsetCompensation( void );
00161    
00162    /*
00163     * Disable ambient light auto offset compensation
00164     */
00165    void disableALAutoOffsetCompensation( void );
00166    
00167    /*
00168     * Return True if ambient light auto offset compensation is set
00169     */
00170    bool iseALAutoOffsetCompensation( void );
00171 
00172    /*
00173     * Set ambient light averaging function
00174     * @param measurements [0-7] 2^(measurements) = number of conversions 
00175     */
00176    void setALAveragingFunction( int measurements );
00177    
00178    /*
00179     * Return the averaging functions. Number of conversions = 2^value
00180     */
00181    int gettALAveragingFunction( void );
00182 
00183    /*
00184     * Set proximity measurement signal frequency
00185     * @param frequency [0-3] IR signal frequency 3.125/(frequency+1) 
00186     */
00187    void setProximityMeasurementSigFreq( int frequency );
00188    
00189    /*
00190     * Return IR signal frequency. Frequency = 3.125/(value+1) MHz
00191     */
00192    int getProximityMeasurementSigFreq( void );
00193 
00194    /*
00195     * Set proximity modulator delay time 
00196     * @param delayTime delay time. This value should be provided by Vishay 
00197     */ 
00198    void setProxiModulatorDelayTime( int delayTime );
00199    
00200    /*
00201     * Return proximity modulator delay time 
00202     */   
00203    int getProxiModulatorDelayTime( void );
00204    
00205    /*
00206     * Set proximity modulator dead time 
00207     * @param deadTime dead time. This value should be provided by Vishay 
00208     */ 
00209    void setProxiModulatorDeadTime( int deadTime );
00210 
00211    /*
00212     * Return proximity modulator dead time 
00213     */
00214    int getProxiModulatorDeadTime( void );   
00215 
00216  private:
00217 
00218    int _status;
00219    char _bytes[2];
00220    int _data;
00221    I2C _i2c;
00222    
00223    static const int VCNL4000address;
00224    static const int VCNL4000regAddr;
00225    static const int Command;
00226    static const int ProdIdRevision;
00227    static const int IRLedCurrent;
00228    static const int AmbientLightParam;
00229    static const int AmbientLightMsb;
00230    static const int AmbientLightLsb;
00231    static const int ProximityMsb;
00232    static const int ProximityLsb;
00233    static const int ProximitySigFreq;
00234    static const int ProxymityModulationTime;
00235           
00236  };
00237 
00238 #endif