ff

Dependencies:   vl53l0x_api libxDot-mbed5

Revision:
3:83ec4984a7af
Parent:
2:8ac50bd2c7a1
--- a/main.cpp	Fri Apr 21 11:22:11 2017 +0000
+++ b/main.cpp	Tue Apr 23 14:27:14 2019 +0000
@@ -3,38 +3,17 @@
 #include "vl53l0x_platform.h"
 #include "vl53l0x_i2c_platform.h"
 
+#define USE_I2C_2V8
+
 Serial pc(USBTX, USBRX);
 DigitalOut led(LED1);
 
-void print_pal_error(VL53L0X_Error Status){
-    char buf[VL53L0X_MAX_STRING_LENGTH];
-    VL53L0X_GetPalErrorString(Status, buf);
-    printf("API Status: %i : %s\n", Status, buf);
-}
-
-void print_range_status(VL53L0X_RangingMeasurementData_t* pRangingMeasurementData){
-    char buf[VL53L0X_MAX_STRING_LENGTH];
-    uint8_t RangeStatus;
-
-    /*
-     * New Range Status: data is valid when pRangingMeasurementData->RangeStatus = 0
-     */
-
-    RangeStatus = pRangingMeasurementData->RangeStatus;
-
-    VL53L0X_GetRangeStatusString(RangeStatus, buf);
-    printf("Range Status: %i : %s\n", RangeStatus, buf);
-
-}
-
 
 VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     uint8_t NewDatReady=0;
     uint32_t LoopNb;
-
-    // Wait until it finished
-    // use timeout to avoid deadlock
+    
     if (Status == VL53L0X_ERROR_NONE) {
         LoopNb = 0;
         do {
@@ -59,8 +38,6 @@
     uint32_t StopCompleted=0;
     uint32_t LoopNb;
 
-    // Wait until it finished
-    // use timeout to avoid deadlock
     if (Status == VL53L0X_ERROR_NONE) {
         LoopNb = 0;
         do {
@@ -82,176 +59,65 @@
 }
 
 
+
 int main()
 {
-  
+    int x=1, measure=0;
+    int ave=0, sum=0;
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     VL53L0X_Dev_t MyDevice;
     VL53L0X_Dev_t *pMyDevice = &MyDevice;
+    VL53L0X_RangingMeasurementData_t    RangingMeasurementData;
+    VL53L0X_RangingMeasurementData_t   *pRangingMeasurementData    = &RangingMeasurementData;
     VL53L0X_Version_t                   Version;
-    VL53L0X_Version_t                  *pVersion   = &Version;
-    VL53L0X_DeviceInfo_t                DeviceInfo; 
-    
-    int32_t status_int;
-    
-    pc.printf("VL53L0X API Simple Ranging Example\r\n");
     
     // Initialize Comms
     pMyDevice->I2cDevAddr      = 0x52;
     pMyDevice->comms_type      =  1;
     pMyDevice->comms_speed_khz =  400;
     
-    pc.printf("Init comms\r\n");
+    
+    VL53L0X_ERROR_CONTROL_INTERFACE;
+    VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0);
+    VL53L0X_DataInit(&MyDevice); // Data initialization
+    Status = VL53L0X_ERROR_NONE;
+    uint32_t refSpadCount;
+    uint8_t isApertureSpads;
+    uint8_t VhvSettings;
+    uint8_t PhaseCal;
     
-    if(Status == VL53L0X_ERROR_NONE)
-    {
-        status_int = VL53L0X_GetVersion(pVersion);
-        if (status_int != 0)
-            Status = VL53L0X_ERROR_CONTROL_INTERFACE;
-    }
-    pc.printf("VL53L0X API Version: %d.%d.%d (revision %d)\r\n", pVersion->major, pVersion->minor ,pVersion->build, pVersion->revision);
-        
-        int addr;
-        
-        addr = VL53L0X_scan();
-        printf("Device found at: %i\r\n", addr);
-        //uint8_t data;
-        //data=0;
-        if(Status == VL53L0X_ERROR_NONE)
-    {
-            printf ("Call of VL53L0X_DataInit\n");
-            uint16_t osc_calibrate_val=0;
-            Status = VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,&osc_calibrate_val);
-            printf("%i\n",osc_calibrate_val);
-      Status = VL53L0X_DataInit(&MyDevice); // Data initialization
-      print_pal_error(Status);
-    }
+    VL53L0X_StaticInit(pMyDevice); // Device Initialization
+    VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization
+    VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization
+    VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
+    VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF
+    VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF
+    VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF
+    VL53L0X_StartMeasurement(pMyDevice);
 
-        if(Status == VL53L0X_ERROR_NONE)
-    {
-        Status = VL53L0X_GetDeviceInfo(&MyDevice, &DeviceInfo);
-        if(Status == VL53L0X_ERROR_NONE)
-        {
-            printf("VL53L0X_GetDeviceInfo:\n");
-            printf("Device Name : %s\n", DeviceInfo.Name);
-            printf("Device Type : %s\n", DeviceInfo.Type);
-            printf("Device ID : %s\n", DeviceInfo.ProductId);
-            printf("ProductRevisionMajor : %d\n", DeviceInfo.ProductRevisionMajor);
-        printf("ProductRevisionMinor : %d\n", DeviceInfo.ProductRevisionMinor);
-
-        if ((DeviceInfo.ProductRevisionMinor != 1) && (DeviceInfo.ProductRevisionMinor != 1)) {
-            printf("Error expected cut 1.1 but found cut %d.%d\n",
-                       DeviceInfo.ProductRevisionMajor, DeviceInfo.ProductRevisionMinor);
-                Status = VL53L0X_ERROR_NOT_SUPPORTED;
-            }
+    while (1){
+        while(x<=50){
+        WaitMeasurementDataReady(pMyDevice);
+        VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
+        measure=pRangingMeasurementData->RangeMilliMeter;
+        //printf("In loop measurement %d\n", mea);
+        sum=sum+measure;
+        led=!led;
+        // Clear the interrupt
+        VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
+        VL53L0X_PollingDelay(pMyDevice);
+        x++;
         }
-        print_pal_error(Status);
+        ave=sum/x;
+        pc.printf("Average is %d\n", ave);
+        x=1;
+        sum=0;
+        wait(20);
     }
 
-        VL53L0X_RangingMeasurementData_t    RangingMeasurementData;
-        VL53L0X_RangingMeasurementData_t   *pRangingMeasurementData    = &RangingMeasurementData;
-        Status = VL53L0X_ERROR_NONE;
-        uint32_t refSpadCount;
-        uint8_t isApertureSpads;
-        uint8_t VhvSettings;
-        uint8_t PhaseCal;
-
-    if(Status == VL53L0X_ERROR_NONE)
-        {
-            printf ("Call of VL53L0X_StaticInit\n");
-            Status = VL53L0X_StaticInit(pMyDevice); // Device Initialization
-            // StaticInit will set interrupt by default
-            print_pal_error(Status);
-        }
-
-        if(Status == VL53L0X_ERROR_NONE)
-        {
-            printf ("Call of VL53L0X_PerformRefCalibration\n");
-            Status = VL53L0X_PerformRefCalibration(pMyDevice,
-                    &VhvSettings, &PhaseCal); // Device Initialization
-            print_pal_error(Status);
-        }
-
-        if(Status == VL53L0X_ERROR_NONE)
-        {
-            printf ("Call of VL53L0X_PerformRefSpadManagement\n");
-            Status = VL53L0X_PerformRefSpadManagement(pMyDevice,
-                    &refSpadCount, &isApertureSpads); // Device Initialization
-            print_pal_error(Status);
-        }
-
-        if(Status == VL53L0X_ERROR_NONE)
-        {
-
-            printf ("Call of VL53L0X_SetDeviceMode\n");
-            Status = VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
-            print_pal_error(Status);
-        }
-
-        if(Status == VL53L0X_ERROR_NONE)
-        {
-            printf ("Call of VL53L0X_StartMeasurement\n");
-            Status = VL53L0X_StartMeasurement(pMyDevice);
-            print_pal_error(Status);
-        }
-
-        if(Status == VL53L0X_ERROR_NONE)
-        {
-            uint32_t measurement;
-            uint32_t no_of_measurements = 6000;
-
-            uint16_t* pResults = (uint16_t*)malloc(sizeof(uint16_t) * no_of_measurements);
-
-            for(measurement=0; measurement<no_of_measurements; measurement++)
-            {
-
-                Status = WaitMeasurementDataReady(pMyDevice);
-
-                if(Status == VL53L0X_ERROR_NONE)
-                {
-                    Status = VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
-
-                    *(pResults + measurement) = pRangingMeasurementData->RangeMilliMeter;
-                    printf("In loop measurement %lu: %d\n", measurement, pRangingMeasurementData->RangeMilliMeter);
-                    wait(1);
-                    led=!led;
-                    // Clear the interrupt
-                    VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
-                    VL53L0X_PollingDelay(pMyDevice);
-                } else {
-                    break;
-                }
-            }
-
-            if(Status == VL53L0X_ERROR_NONE)
-            {
-                for(measurement=0; measurement<no_of_measurements; measurement++)
-                {
-                    printf("measurement %lu: %d\n", measurement, *(pResults + measurement));
-                }
-            }
-
-            free(pResults);
-        }
-
-
-        if(Status == VL53L0X_ERROR_NONE)
-        {
-            printf ("Call of VL53L0X_StopMeasurement\n");
-            Status = VL53L0X_StopMeasurement(pMyDevice);
-        }
-
-        if(Status == VL53L0X_ERROR_NONE)
-        {
-            printf ("Wait Stop to be competed\n");
-            Status = WaitStopCompleted(pMyDevice);
-        }
-
-        if(Status == VL53L0X_ERROR_NONE)
-        Status = VL53L0X_ClearInterruptMask(pMyDevice,
-            VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
-    while (true)
-        if (pc.readable())
-            pc.putc(pc.getc());
+    VL53L0X_StopMeasurement(pMyDevice);
+    WaitStopCompleted(pMyDevice);
+    VL53L0X_ClearInterruptMask(pMyDevice,VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
+    
 }