A simple example of a ST VL53L3 ToF sensor being used to get range data polling for measurements. Mbed V5.15

Dependencies:   X_NUCLEO_53L3A2

Revision:
0:2a67f2b41fd7
Child:
1:9ea3593bac93
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Main.cpp	Thu Oct 15 12:14:48 2020 +0000
@@ -0,0 +1,132 @@
+/*
+ *  This VL53L1X Expansion board test application performs range measurements
+ *  using the onboard embedded centre sensor, in singleshot, polling mode.
+ *  Measured ranges are ouput on the Serial Port, running at 9600 baud.
+ *
+ *  The User Blue button stops the current measurement and entire program,
+ *  releasing all resources.
+ *
+ *  The Reset button can be used to restart the program.
+ */
+ 
+  //Main_single_polling.h
+ 
+#include <stdio.h>
+
+#include "mbed.h"
+#include "XNucleo53L1A1.h"
+#include "vl53L1x_I2c.h"
+#include <time.h>
+
+
+// define i2c mcu pins
+#define VL53L1_I2C_SDA   D14 
+#define VL53L1_I2C_SCL   D15 
+
+static XNucleo53L1A1 *board=NULL;
+Serial pc(SERIAL_TX, SERIAL_RX); 
+
+
+
+VL53LX_Dev_t                   devCentre;
+VL53LX_Dev_t                   devLeft;
+VL53LX_Dev_t                   devRight;
+VL53LX_DEV                     Dev = &devCentre;
+
+
+ 
+/*=================================== Main ==================================
+=============================================================================*/
+int main()
+{   
+    int status;
+    VL53LX * Sensor;
+    uint16_t wordData;
+
+    static VL53LX_MultiRangingData_t RangingData;
+
+
+    pc.baud(115200);  // baud rate is important as printf statements take a lot of time
+    printf("Hello world!\r\n");
+
+    vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
+
+ //   printf("I2C device created! %d %d\r\n",dev_I2C,*dev_I2C);
+    
+    /* creates the 53L1A1 expansion board singleton obj */
+    board = XNucleo53L1A1::instance(dev_I2C, A2, D8, D2);
+    printf("board created!\r\n");
+
+
+    /* init the 53L1A1 expansion board with default values */
+    status = board->init_board();
+    if (status) {
+        printf("Failed to init board!\r\n");
+        return 0;
+    }
+       
+   
+    printf("board initiated! \n");
+                                            
+    printf("configuring centre channel \n");
+    Dev=&devCentre;
+    Sensor=board->sensor_centre;
+    Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
+    printf("configured centre channel \n");
+  
+        
+   // configure the i2c connection     
+    Dev->comms_speed_khz = 400;
+    Dev->comms_type = 1;
+
+/* Device Initialization and setting */
+
+    status = Sensor->VL53LX_DataInit();
+    uint8_t NewDataReady=0;     
+
+    
+    status = Sensor->VL53LX_StartMeasurement();   
+    printf("VL53LX_StartMeasurement %d \n",status);         
+        
+    while(1) 
+    {     
+        status = Sensor->VL53LX_WaitMeasurementDataReady();
+
+        if(!status)
+        {
+
+            status = Sensor->VL53LX_GetMultiRangingData( &RangingData);
+
+            if ( status == 0)
+            {
+                int no_of_object_found=RangingData.NumberOfObjectsFound;
+                if ( no_of_object_found < 10 ) 
+                {
+                    for(int j=0;j<no_of_object_found;j++){
+                          if ((RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID) || 
+                            (RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
+                            {   // print data
+                                printf("centre \t object %d  \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
+                                j,
+                                RangingData.RangeData[j].RangeStatus,
+                                RangingData.RangeData[j].RangeMilliMeter,
+                                RangingData.RangeData[j].SignalRateRtnMegaCps/65536.0,
+                                RangingData.RangeData[j].AmbientRateRtnMegaCps/65536.0);
+                            } //if
+                    } //for
+                } // if  ( no_of_object_found < 10 ) 
+            }   // if status VL53LX_GetMultiRangingData                                                                          
+                                        
+            } // if !status VL53LX_WaitMeasurementDataReady
+            else
+            {
+                printf("VL53L1_WaitMeasurementDataReady failed %d \n",status);
+            }
+
+            status = Sensor->VL53LX_ClearInterruptAndStartMeasurement();
+        
+    } // while(1)
+    
+//       status = Sensor->VL53LX_StopMeasurement(); 
+    
+}