VL6180x lib under optimization (add longer ranging and multiple devices on the same I2C)

Dependents:   Coupe Robotique FIP Coupe-Robotique-FIP-Main

Embed: (wiki syntax)

« Back to documentation index

VL6180x Class Reference

VL6180x Class Reference

VL6180x tof/als sensor example. More...

#include <VL6180x.h>

Public Member Functions

 VL6180x (PinName sda, PinName scl, uint8_t addr)
 VL6180x constructor.
 ~VL6180x ()
 VL6180x destructor.
int VL6180xInit (void)
 Send mandatory settings as stated in ST datasheet.
void VL6180xDefautSettings (void)
 Use default settings from ST data sheet section 9.
uint8_t getDistance ()
 Get Range distance in (mm)
float getAmbientLight (vl6180x_als_gain VL6180X_ALS_GAIN)
 Get ALS level in Lux.
void getIdentification (struct VL6180xIdentification *temp)
 Load structure provided by the user with identification info Structure example: struct VL6180xIdentification.
uint8_t changeAddress (uint8_t old_address, uint8_t new_address)
 Change the default address of the device to allow multiple sensors on the bus.

Detailed Description

VL6180x tof/als sensor example.

#include "mbed.h"
#include <VL6180x.h>

const float GAIN_1    = 1.01;  // Actual ALS Gain of 1.01
const float GAIN_1_25 = 1.28;  // Actual ALS Gain of 1.28
const float GAIN_1_67 = 1.72;  // Actual ALS Gain of 1.72
const float GAIN_2_5  = 2.6;   // Actual ALS Gain of 2.60
const float GAIN_5    = 5.21;  // Actual ALS Gain of 5.21
const float GAIN_10   = 10.32; // Actual ALS Gain of 10.32
const float GAIN_20   = 20;    // Actual ALS Gain of 20
const float GAIN_40   = 40;    // Actual ALS Gain of 40


#define VL6180X_ADDRESS 0x29

VL6180xIdentification identification;
// mbed uses 8bit addresses shift address by 1 bit left
VL6180x sensor(D14, D15, VL6180X_ADDRESS<<1);

void printIdentification(struct VL6180xIdentification *temp){
  printf("Model ID = ");
  printf("%d\n",temp->idModel);

  printf("Model Rev = ");
  printf("%d",temp->idModelRevMajor);
  printf(".");
  printf("%d\n",temp->idModelRevMinor);

  printf("Module Rev = ");
  printf("%d",temp->idModuleRevMajor);
  printf(".");
  printf("%d\n",temp->idModuleRevMinor);  

  printf("Manufacture Date = ");
  printf("%d",((temp->idDate >> 3) & 0x001F));
  printf("/");
  printf("%d",((temp->idDate >> 8) & 0x000F));
  printf("/1");
  printf("%d\n",((temp->idDate >> 12) & 0x000F));
  printf(" Phase: ");
  printf("%d\n",(temp->idDate & 0x0007));

  printf("Manufacture Time (s)= ");
  printf("%d\n",(temp->idTime * 2));
  printf("\n\n");
}
int main() {

  wait_ms(100); // delay .1s

  sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
  printIdentification(&identification); // Helper function to print all the Module information

  if(sensor.VL6180xInit() != 0){
        printf("FAILED TO INITALIZE\n"); //Initialize device and check for errors
  }; 

  sensor.VL6180xDefautSettings(); //Load default settings to get started.
  
    wait_ms(1000); // delay 1s

    
    
    while(1) {
  //Get Ambient Light level and report in LUX
      printf("Ambient Light Level (Lux) = ");
  
  //Input GAIN for light levels, 
  // GAIN_20     // Actual ALS Gain of 20
  // GAIN_10     // Actual ALS Gain of 10.32
  // GAIN_5      // Actual ALS Gain of 5.21
  // GAIN_2_5    // Actual ALS Gain of 2.60
  // GAIN_1_67   // Actual ALS Gain of 1.72
  // GAIN_1_25   // Actual ALS Gain of 1.28
  // GAIN_1      // Actual ALS Gain of 1.01
  // GAIN_40     // Actual ALS Gain of 40
  
      printf("%f\n",sensor.getAmbientLight(GAIN_1) );

  //Get Distance and report in mm
      printf("Distance measured (mm) = ");
      printf("%d\n", sensor.getDistance() ); 

      wait_ms(500);  
    }
}

Definition at line 230 of file VL6180x.h.


Constructor & Destructor Documentation

VL6180x ( PinName  sda,
PinName  scl,
uint8_t  addr 
)

VL6180x constructor.

Parameters:
sdaSDA pin
sdlSCL pin
addraddr of the I2C peripheral

Definition at line 32 of file VL6180x.cpp.

~VL6180x ( void   )

VL6180x destructor.

Definition at line 75 of file VL6180x.cpp.


Member Function Documentation

uint8_t changeAddress ( uint8_t  old_address,
uint8_t  new_address 
)

Change the default address of the device to allow multiple sensors on the bus.

Can use up to 127 sensors. New address is saved in non-volatile device memory.

Parameters:
old_addresscurrent address
new_addressdesired new address
Returns:
new address

Definition at line 120 of file VL6180x.cpp.

float getAmbientLight ( vl6180x_als_gain  VL6180X_ALS_GAIN )

Get ALS level in Lux.

Parameters:
vl6180x_als_gaingain setting for sensor
sdlSCL pin
addraddr of the I2C peripheral
Returns:
light level in Lux

Definition at line 144 of file VL6180x.cpp.

uint8_t getDistance (  )

Get Range distance in (mm)

Returns:
TOF distance in mm

Definition at line 135 of file VL6180x.cpp.

void getIdentification ( struct VL6180xIdentification temp )

Load structure provided by the user with identification info Structure example: struct VL6180xIdentification.

Definition at line 107 of file VL6180x.cpp.

void VL6180xDefautSettings ( void   )

Use default settings from ST data sheet section 9.

http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf

Definition at line 78 of file VL6180x.cpp.

int VL6180xInit ( void   )

Send mandatory settings as stated in ST datasheet.

http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf (Section 1.3)

Returns:
0 if ok
-1 on error

Definition at line 33 of file VL6180x.cpp.