Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
VCNL4000.cpp
00001 #include "VCNL4000.h" 00002 00003 const int VCNL4000::VCNL4000address = 0x13; 00004 const int VCNL4000::VCNL4000regAddr = 0x80; 00005 const int VCNL4000::Command = 0x0; 00006 const int VCNL4000::ProdIdRevision = 0x1; 00007 const int VCNL4000::IRLedCurrent = 0x3; 00008 const int VCNL4000::AmbientLightParam = 0x4; 00009 const int VCNL4000::AmbientLightMsb = 0x5; 00010 const int VCNL4000::AmbientLightLsb = 0x6; 00011 const int VCNL4000::ProximityMsb = 0x7; 00012 const int VCNL4000::ProximityLsb = 0x8; 00013 const int VCNL4000::ProximitySigFreq = 0x9; 00014 const int VCNL4000::ProxymityModulationTime = 0xa; 00015 00016 // --------------------------------------------------- 00017 VCNL4000::VCNL4000( PinName sda, PinName scl ) : _i2c( sda, scl ) { 00018 00019 int prodId = getProductId(); 00020 int prodRev = getProductRevision(); 00021 00022 if( prodId == 1 && prodRev == 1 ) { 00023 _status = 0; 00024 } 00025 else { 00026 _status = 1; 00027 } 00028 } 00029 00030 // --------------------------------------------------- 00031 VCNL4000::~VCNL4000( void ) { 00032 } 00033 00034 // --------------------------------------------------- 00035 int VCNL4000::registerRead( int reg ) { 00036 00037 _bytes[0] = ( reg & 0xff ); 00038 _status = _i2c.write( ( VCNL4000address << 1 ), _bytes, 1 ); 00039 if( _status == 0 ) { 00040 _status = _i2c.read( ( ( VCNL4000address << 1 ) + 1 ), _bytes, 1 ); 00041 return( _bytes[0] ); 00042 } 00043 return( 0 ); 00044 } 00045 00046 // --------------------------------------------------- 00047 void VCNL4000::registerWrite( int reg, unsigned char data ) { 00048 00049 _bytes[0] = reg & 0xff; 00050 _bytes[1] = data & 0xff; 00051 _status = _i2c.write( VCNL4000address << 1, _bytes, 2 ); 00052 00053 } 00054 00055 // --------------------------------------------------- 00056 int VCNL4000::getProximity( void ) { 00057 00058 startProximityMeasurement(); 00059 while( !proximityDataReady() ) { 00060 wait(0.1); 00061 } 00062 _data = registerRead( VCNL4000regAddr + ProximityMsb ) << 8; 00063 int status = _status; 00064 _data += registerRead( VCNL4000regAddr + ProximityLsb ); 00065 _status = _status | status; 00066 return( _data ); 00067 } 00068 00069 // --------------------------------------------------- 00070 int VCNL4000::getAmbientLight( void ) { 00071 startAmbientLightMeasurement(); 00072 while( !ambientLightDataReady() ) { 00073 wait(0.1); 00074 } 00075 _data = registerRead( VCNL4000regAddr + AmbientLightMsb ) << 8; 00076 int status = _status; 00077 _data += registerRead( VCNL4000regAddr + AmbientLightLsb ); 00078 _status = _status | status; 00079 return( _data ); 00080 } 00081 00082 // --------------------------------------------------- 00083 int VCNL4000::getProductId( void ) { 00084 return( ( registerRead( VCNL4000regAddr + ProdIdRevision ) & 0xf0 ) >> 4 ); 00085 } 00086 00087 // --------------------------------------------------- 00088 int VCNL4000::getProductRevision( void ) { 00089 return( registerRead( VCNL4000regAddr + ProdIdRevision ) & 0x0f ) ; 00090 } 00091 00092 // --------------------------------------------------- 00093 bool VCNL4000::proximityDataReady( void ) { 00094 return( ( registerRead( VCNL4000regAddr + Command ) & 0x20 ) >> 5 ); 00095 } 00096 00097 // --------------------------------------------------- 00098 bool VCNL4000::ambientLightDataReady( void ) { 00099 return( ( registerRead( VCNL4000regAddr + Command ) & 0x40 ) >> 6 ); 00100 } 00101 00102 // --------------------------------------------------- 00103 void VCNL4000::startProximityMeasurement( void ) { 00104 _data = registerRead( VCNL4000regAddr + Command ); 00105 if( _status == 0 ) { 00106 _data = _data | 0x08; 00107 registerWrite( VCNL4000regAddr + Command, _data ); 00108 } 00109 } 00110 00111 // --------------------------------------------------- 00112 void VCNL4000::startAmbientLightMeasurement( void ) { 00113 _data = registerRead( VCNL4000regAddr + Command ); 00114 if( _status == 0 ) { 00115 _data = _data | 0x10; 00116 registerWrite( VCNL4000regAddr + Command, _data ); 00117 } 00118 } 00119 00120 // --------------------------------------------------- 00121 void VCNL4000::setIRLedCurrent( int milliAmps ) { 00122 milliAmps /= 10; 00123 if( milliAmps > 20 ) { 00124 _status = 1; 00125 return; 00126 } 00127 _data = registerRead( VCNL4000regAddr + IRLedCurrent ); 00128 if( _status == 0 ) { 00129 _data = ( _data & 0xc0 ) | milliAmps; 00130 registerWrite( VCNL4000regAddr + IRLedCurrent, _data ); 00131 } 00132 } 00133 00134 // --------------------------------------------------- 00135 void VCNL4000::enableALContinuousConversionMode( void ) { 00136 _data = registerRead( VCNL4000regAddr + AmbientLightParam ); 00137 if( _status == 0 ) { 00138 _data = _data | 0x80; 00139 registerWrite( VCNL4000regAddr + AmbientLightParam, _data ); 00140 } 00141 } 00142 00143 // --------------------------------------------------- 00144 void VCNL4000::disableALContinuousConversionMode( void ) { 00145 _data = registerRead( VCNL4000regAddr + AmbientLightParam ); 00146 if( _status == 0 ) { 00147 _data = _data & 0x7f; 00148 registerWrite( VCNL4000regAddr + AmbientLightParam, _data ); 00149 } 00150 } 00151 00152 // --------------------------------------------------- 00153 bool VCNL4000::isALContinuousConversionMode( void ) { 00154 _data = registerRead( VCNL4000regAddr + AmbientLightParam ); 00155 if( ( ( _data >> 7 ) & 1 ) == 1 ) { 00156 return( true ); 00157 } 00158 else { 00159 return( false ); 00160 } 00161 } 00162 00163 // --------------------------------------------------- 00164 void VCNL4000::enableALAutoOffsetCompensation( void ) { 00165 _data = registerRead( VCNL4000regAddr + AmbientLightParam ); 00166 if( _status == 0 ) { 00167 _data = _data | 0x08; 00168 registerWrite( VCNL4000regAddr + AmbientLightParam, _data ); 00169 } 00170 } 00171 00172 // --------------------------------------------------- 00173 void VCNL4000::disableALAutoOffsetCompensation( void ) { 00174 _data = registerRead( VCNL4000regAddr + AmbientLightParam ); 00175 if( _status == 0 ) { 00176 _data = _data & 0xf7; 00177 registerWrite( VCNL4000regAddr + AmbientLightParam, _data ); 00178 } 00179 } 00180 00181 // --------------------------------------------------- 00182 bool VCNL4000::iseALAutoOffsetCompensation( void ) { 00183 _data = registerRead( VCNL4000regAddr + AmbientLightParam ); 00184 if( ( ( _data >> 3 ) & 1 ) == 1 ) { 00185 return( true ); 00186 } 00187 else { 00188 return( false ); 00189 } 00190 } 00191 00192 // --------------------------------------------------- 00193 void VCNL4000::setALAveragingFunction( int measurements ) { 00194 if( measurements > 7 ) { 00195 _status = 1; 00196 return; 00197 } 00198 _data = registerRead( VCNL4000regAddr + AmbientLightParam ); 00199 if( _status == 0 ) { 00200 _data = ( _data & 0xf8 ) | measurements; 00201 registerWrite( VCNL4000regAddr + AmbientLightParam, _data ); 00202 } 00203 } 00204 00205 // --------------------------------------------------- 00206 int VCNL4000::gettALAveragingFunction( void ) { 00207 _data = registerRead( VCNL4000regAddr + AmbientLightParam ); 00208 if( _status == 0 ) { 00209 return( _data & 0xf ); 00210 } 00211 else { 00212 return( 0 ); 00213 } 00214 } 00215 00216 // --------------------------------------------------- 00217 void VCNL4000::setProximityMeasurementSigFreq( int frequency ) { 00218 if( frequency > 3 ) { 00219 _status = 1; 00220 return; 00221 } 00222 _data = registerRead( VCNL4000regAddr + ProximitySigFreq ); 00223 if( _status == 0 ) { 00224 _data = ( _data & 0xfc ) | frequency; 00225 registerWrite( VCNL4000regAddr + ProximitySigFreq, _data ); 00226 } 00227 } 00228 00229 // --------------------------------------------------- 00230 int VCNL4000::getProximityMeasurementSigFreq( void ) { 00231 _data = registerRead( VCNL4000regAddr + ProximitySigFreq ); 00232 if( _status == 0 ) { 00233 return( _data & 0x3 ); 00234 } 00235 else { 00236 return( 0 ); 00237 } 00238 } 00239 00240 // --------------------------------------------------- 00241 void VCNL4000::setProxiModulatorDelayTime( int delayTime ) { 00242 if( delayTime > 7 ) { 00243 _status = 1; 00244 return; 00245 } 00246 _data = registerRead( VCNL4000regAddr + ProxymityModulationTime ); 00247 if( _status == 0 ) { 00248 _data = ( _data & 0x1f ) | ( delayTime << 5 ); 00249 registerWrite( VCNL4000regAddr + ProxymityModulationTime, _data ); 00250 } 00251 } 00252 00253 // --------------------------------------------------- 00254 int VCNL4000::getProxiModulatorDelayTime( void ) { 00255 _data = registerRead( VCNL4000regAddr + ProxymityModulationTime ); 00256 if( _status == 0 ) { 00257 return( ( _data >> 5 ) & 0x7 ); 00258 } 00259 else { 00260 return( 0 ); 00261 } 00262 } 00263 00264 // --------------------------------------------------- 00265 void VCNL4000::setProxiModulatorDeadTime( int deadTime ) { 00266 if( deadTime > 7 ) { 00267 _status = 1; 00268 return; 00269 } 00270 _data = registerRead( VCNL4000regAddr + ProxymityModulationTime ); 00271 if( _status == 0 ) { 00272 _data = ( _data & 0x07 ) | deadTime; 00273 registerWrite( VCNL4000regAddr + ProxymityModulationTime, _data ); 00274 } 00275 } 00276 00277 // --------------------------------------------------- 00278 int VCNL4000::getProxiModulatorDeadTime( void ) { 00279 _data = registerRead( VCNL4000regAddr + ProxymityModulationTime ); 00280 if( _status == 0 ) { 00281 return( _data & 0x7 ); 00282 } 00283 else { 00284 return( 0 ); 00285 } 00286 } 00287 00288 // --------------------------------------------------- 00289
Generated on Wed Jul 13 2022 08:45:17 by
1.7.2