Energy harvesting mobile robot. Developed at Institute of Systems and Robotics — University of Coimbra.

Dependencies:   RF24

Dependents:   Mapping VirtualForces_debug OneFileToRuleThemAll VirtualForces_with_class ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers VCNL40x0.cpp Source File

VCNL40x0.cpp

00001 /*
00002 Copyright (c) 2012 Vishay GmbH, www.vishay.com
00003 author: DS, version 1.21
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 #include "VCNL40x0.h"
00025 
00026 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00027 
00028 VCNL40x0::VCNL40x0(PinName sda, PinName scl, unsigned char addr) : _i2c(sda, scl), _addr(addr) {
00029     _i2c.frequency(1000000);                                // set I2C frequency to 1MHz
00030 }
00031 
00032 VCNL40x0::~VCNL40x0() {
00033 }
00034 
00035 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00036 
00037 VCNL40x0Error_e VCNL40x0::SetCommandRegister (unsigned char Command) {
00038 
00039     _send[0] = REGISTER_COMMAND;                            // VCNL40x0 Configuration reister
00040     _send[1] = Command;
00041     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00042 
00043     return VCNL40x0_ERROR_OK;
00044 }
00045 
00046 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00047 
00048 VCNL40x0Error_e VCNL40x0::ReadCommandRegister (unsigned char *Command) {
00049 
00050     _send[0] = REGISTER_COMMAND;                            // VCNL40x0 Configuration register
00051     _i2c.write(VCNL40x0_ADDRESS,_send, 1);                  // Write 1 byte on I2C
00052     _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1);              // Read 1 byte on I2C
00053 
00054     *Command = (unsigned char)(_receive[0]);
00055 
00056     return VCNL40x0_ERROR_OK;
00057 }
00058 
00059 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00060 
00061 VCNL40x0Error_e VCNL40x0::ReadID (unsigned char *ID) {
00062 
00063     _send[0] = REGISTER_ID;                                 // VCNL40x0 product ID revision register
00064     _i2c.write(VCNL40x0_ADDRESS, _send, 1);                 // Write 1 byte on I2C
00065     _i2c.read(VCNL40x0_ADDRESS+1, _receive, 1);             // Read 1 byte on I2C
00066 
00067     *ID = (unsigned char)(_receive[0]);
00068 
00069     return VCNL40x0_ERROR_OK;
00070 }
00071 
00072 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00073 
00074 VCNL40x0Error_e VCNL40x0::SetCurrent (unsigned char Current) {
00075 
00076     _send[0] = REGISTER_PROX_CURRENT;                       // VCNL40x0 IR LED Current register
00077     _send[1] = Current;
00078     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00079 
00080     return VCNL40x0_ERROR_OK;
00081 }
00082 
00083 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00084 
00085 VCNL40x0Error_e VCNL40x0::ReadCurrent (unsigned char *Current) {
00086 
00087     _send[0] = REGISTER_PROX_CURRENT;                       // VCNL40x0 IR LED current register
00088     _i2c.write(VCNL40x0_ADDRESS,_send, 1);                  // Write 1 byte on I2C
00089     _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1);              // Read 1 byte on I2C
00090 
00091     *Current = (unsigned char)(_receive[0]);
00092 
00093     return VCNL40x0_ERROR_OK;
00094 }
00095 
00096 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00097 
00098 VCNL40x0Error_e VCNL40x0::SetProximityRate (unsigned char ProximityRate) {
00099 
00100     _send[0] = REGISTER_PROX_RATE;                          // VCNL40x0 Proximity rate register
00101     _send[1] = ProximityRate;
00102     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00103 
00104     return VCNL40x0_ERROR_OK;
00105 }
00106 
00107 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00108 
00109 VCNL40x0Error_e VCNL40x0::SetAmbiConfiguration (unsigned char AmbiConfiguration) {
00110 
00111     _send[0] = REGISTER_AMBI_PARAMETER;                     // VCNL40x0 Ambilight configuration
00112     _send[1] = AmbiConfiguration;
00113     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00114 
00115     return VCNL40x0_ERROR_OK;
00116 }
00117 
00118 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00119 
00120 VCNL40x0Error_e VCNL40x0::SetInterruptControl (unsigned char InterruptControl) {
00121 
00122     _send[0] = REGISTER_INTERRUPT_CONTROL;                  // VCNL40x0 Interrupt Control register
00123     _send[1] = InterruptControl;
00124     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00125 
00126     return VCNL40x0_ERROR_OK;
00127 }
00128 
00129 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00130 
00131 VCNL40x0Error_e VCNL40x0::ReadInterruptControl (unsigned char *InterruptControl) {
00132 
00133     _send[0] = REGISTER_INTERRUPT_CONTROL;                  // VCNL40x0 Interrupt Control register
00134     _i2c.write(VCNL40x0_ADDRESS,_send, 1);                  // Write 1 byte on I2C
00135     _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1);              // Read 1 byte on I2C
00136 
00137     *InterruptControl = (unsigned char)(_receive[0]);
00138 
00139     return VCNL40x0_ERROR_OK;
00140 }
00141 
00142 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00143 
00144 VCNL40x0Error_e VCNL40x0::SetInterruptStatus (unsigned char InterruptStatus) {
00145 
00146     _send[0] = REGISTER_INTERRUPT_STATUS;                   // VCNL40x0 Interrupt Status register
00147     _send[1] = InterruptStatus;
00148     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00149 
00150     return VCNL40x0_ERROR_OK;
00151 }
00152 
00153 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00154 
00155 VCNL40x0Error_e VCNL40x0::SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment) {
00156 
00157     _send[0] = REGISTER_PROX_TIMING;                        // VCNL40x0 Modulator Timing Adjustment register
00158     _send[1] = ModulatorTimingAdjustment;
00159     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00160 
00161     return VCNL40x0_ERROR_OK;
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00165 
00166 VCNL40x0Error_e VCNL40x0::ReadInterruptStatus (unsigned char *InterruptStatus) {
00167 
00168     _send[0] = REGISTER_INTERRUPT_STATUS;                   // VCNL40x0 Interrupt Status register
00169     _i2c.write(VCNL40x0_ADDRESS,_send, 1);                  // Write 1 byte on I2C
00170     _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1);              // Read 1 byte on I2C
00171 
00172     *InterruptStatus = (unsigned char)(_receive[0]);
00173 
00174     return VCNL40x0_ERROR_OK;
00175 }
00176 
00177 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00178 
00179 VCNL40x0Error_e VCNL40x0::ReadProxiValue (unsigned int *ProxiValue) {
00180 
00181     _send[0] = REGISTER_PROX_VALUE;                         // VCNL40x0 Proximity Value register
00182     _i2c.write(VCNL40x0_ADDRESS, _send, 1);                 // Write 1 byte on I2C
00183     _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2);             // Read 2 bytes on I2C
00184     *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
00185 
00186     return VCNL40x0_ERROR_OK;
00187 
00188 }
00189 
00190 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00191 
00192 VCNL40x0Error_e VCNL40x0::ReadAmbiValue (unsigned int *AmbiValue) {
00193 
00194     _send[0] = REGISTER_AMBI_VALUE;                          // VCNL40x0 Ambient Light Value register
00195     _i2c.write(VCNL40x0_ADDRESS, _send, 1);                  // Write 1 byte on I2C
00196     _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2);              // Read 2 bytes on I2C
00197     *AmbiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
00198 
00199     return VCNL40x0_ERROR_OK;
00200 
00201 }
00202 
00203 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00204 
00205 VCNL40x0Error_e VCNL40x0::SetLowThreshold (unsigned int LowThreshold) {
00206 
00207     unsigned char LoByte=0, HiByte=0;
00208     
00209     LoByte = (unsigned char)(LowThreshold & 0x00ff);
00210     HiByte = (unsigned char)((LowThreshold & 0xff00)>>8);
00211     
00212     _send[0] = REGISTER_INTERRUPT_LOW_THRES;                // VCNL40x0 Low Threshold Register, Hi Byte
00213     _send[1] = HiByte;
00214     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00215     
00216     _send[0] = REGISTER_INTERRUPT_LOW_THRES+1;              // VCNL40x0 Low Threshold Register, Lo Byte
00217     _send[1] = LoByte;
00218     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00219 
00220     return VCNL40x0_ERROR_OK;
00221 
00222 }
00223 
00224 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00225 
00226 VCNL40x0Error_e VCNL40x0::SetHighThreshold (unsigned int HighThreshold) {
00227 
00228     unsigned char LoByte=0, HiByte=0;
00229     
00230     LoByte = (unsigned char)(HighThreshold & 0x00ff);
00231     HiByte = (unsigned char)((HighThreshold & 0xff00)>>8);
00232     
00233     _send[0] = REGISTER_INTERRUPT_HIGH_THRES;               // VCNL40x0 High Threshold Register, Hi Byte
00234     _send[1] = HiByte;
00235     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00236     
00237     _send[0] = REGISTER_INTERRUPT_HIGH_THRES+1;             // VCNL40x0 High Threshold Register, Lo Byte
00238     _send[1] = LoByte;
00239     _i2c.write(VCNL40x0_ADDRESS,_send, 2);                  // Write 2 bytes on I2C
00240 
00241     return VCNL40x0_ERROR_OK;
00242 
00243 }
00244 
00245 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00246 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00247 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00248 
00249 VCNL40x0Error_e VCNL40x0::ReadProxiOnDemand (unsigned int *ProxiValue) {
00250 
00251     unsigned char Command=0;
00252 
00253     // enable prox value on demand
00254     SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND);
00255  
00256     // wait on prox data ready bit
00257     do {
00258         ReadCommandRegister (&Command);                     // read command register
00259     } while (!(Command & COMMAND_MASK_PROX_DATA_READY));
00260 
00261     ReadProxiValue (ProxiValue);                            // read prox value
00262 
00263     SetCommandRegister (COMMAND_ALL_DISABLE);               // stop prox value on demand
00264     
00265     return VCNL40x0_ERROR_OK;
00266 }
00267 
00268 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00269 
00270 VCNL40x0Error_e VCNL40x0::ReadAmbiOnDemand (unsigned int *AmbiValue) {
00271 
00272     unsigned char Command=0;
00273 
00274     // enable ambi value on demand
00275     SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_AMBI_ON_DEMAND);
00276 
00277     // wait on ambi data ready bit
00278     do {
00279         ReadCommandRegister (&Command);                     // read command register
00280     } while (!(Command & COMMAND_MASK_AMBI_DATA_READY));
00281 
00282     ReadAmbiValue (AmbiValue);                              // read ambi value    
00283 
00284     SetCommandRegister (COMMAND_ALL_DISABLE);               // stop ambi value on demand    
00285 
00286     return VCNL40x0_ERROR_OK;
00287 }
00288