John Alexander / Mbed OS VL53L3CX_Shield_1Sensor_Interrupt_MbOS6x

Dependencies:   X_NUCLEO_53L3A2

Revision:
7:bd1ddfbee3ef
Parent:
6:4bc839979c55
--- a/main.cpp	Fri May 07 15:51:29 2021 +0000
+++ b/main.cpp	Mon May 10 12:48:51 2021 +0000
@@ -28,7 +28,7 @@
 #define I2C_SDA   D14
 #define I2C_SCL   D15
 
-#define MEASUREMENTTIMING  55
+#define MEASUREMENTTIMING  40 //55
 
 static XNucleo53L3A2 *board=NULL;
 #if (MBED_VERSION  > 60300)
@@ -58,7 +58,7 @@
     }
 
 
-    // constructor - Sensor is not used and can be removed
+    // constructor
     WaitForMeasurement(PinName pin,VL53LX_DEV Dev) : _interrupt(pin)          // create the InterruptIn on the pin specified to Counter
     {
         Devlocal = Dev;
@@ -102,8 +102,8 @@
 
 
 VL53LX_Dev_t devCentre;
-VL53LX_Dev_t devLeft;
-VL53LX_Dev_t devRight;
+//VL53LX_Dev_t devLeft;
+//VL53LX_Dev_t devRight;
 VL53LX_DEV Dev = &devCentre;
 
 
@@ -115,23 +115,17 @@
 int main()
 {
     int status;
-    VL53L3 * Sensor;
-    uint16_t wordData;
-    uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right
+    VL53L3 *Sensor;
+    WaitForMeasurement *int1;
 
-
-    WaitForMeasurement* int1;
+    vl53L3_DevI2C *dev_I2C = new vl53L3_DevI2C(I2C_SDA, I2C_SCL);
+    /* creates the 53L1A1 expansion board singleton obj */
+    board = XNucleo53L3A2::instance(dev_I2C, A2, D8, D2);
+    
+    printf("board created!\r\n");
 
     pc.baud(115200);  // baud rate is important as printf statements take a lot of time
 
-    printf("Hello world!\r\n");
-
-    vl53L3_DevI2C *dev_I2C = new vl53L3_DevI2C(I2C_SDA, I2C_SCL);
-
-    /* creates the 53L1A1 expansion board singleton obj */
-    board = XNucleo53L3A2::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) {
@@ -139,65 +133,51 @@
         return 0;
     }
 
-
     printf("board initiated! - %d\r\n", status);
 
-    for (ToFSensor=0; ToFSensor<3; ToFSensor++) {
-        wait_ms(15);
-        switch(ToFSensor) {
-            case 0:
-                if (board->sensor_centre== NULL ) continue;
-                Dev=&devCentre;
-                Sensor=board->sensor_centre;
-                Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
-                printf("configuring centre channel \n");
-                break;
-            default:
-                printf(" error in switch, invalid ToF sensor \n");
-        }
+    Dev = &devCentre;
+    Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
+    Dev->comms_speed_khz = 400;
+    Dev->comms_type = 1;
+    
+    Sensor = board->sensor_centre;
+    
+    printf("configuring centre channel \n");
 
-// configure the sensors
-        Dev->comms_speed_khz = 400;
-
-        Dev->comms_type = 1;
-
-        Sensor->VL53LX_RdWord(Dev, 0x01, &wordData);
-        printf("VL53L1X: %02X   %d\n\r", wordData,Dev->I2cDevAddr);
-        /* Device Initialization and setting */
-
-        status = Sensor->VL53LX_DataInit();
-        status = Sensor->VL53LX_SetDistanceMode(VL53LX_DISTANCEMODE_LONG);
-        status = Sensor->VL53LX_SetMeasurementTimingBudgetMicroSeconds( MEASUREMENTTIMING * 1000);
-        status = Sensor->VL53LX_SmudgeCorrectionEnable(VL53LX_SMUDGE_CORRECTION_SINGLE);
-        status = Sensor->VL53LX_SetXTalkCompensationEnable(1);
-
-    }
+    /* Device Initialization and setting */
+    status = Sensor->VL53LX_DataInit();
+    status = Sensor->VL53LX_SetDistanceMode(VL53LX_DISTANCEMODE_LONG);
+    status = Sensor->VL53LX_SetMeasurementTimingBudgetMicroSeconds( MEASUREMENTTIMING * 1000);
+    status = Sensor->VL53LX_SmudgeCorrectionEnable(VL53LX_SMUDGE_CORRECTION_SINGLE);
+    status = Sensor->VL53LX_SetXTalkCompensationEnable(1);
 
     if (board->sensor_centre!= NULL ) {
         printf("starting interrupt centre\n");
-        Sensor=board->sensor_centre;
-        devCentre.I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
-        int1 =  new WaitForMeasurement(A2,&devCentre);
+        int1 =  new WaitForMeasurement(A2, &devCentre);
+
         status = Sensor->VL53LX_StartMeasurement();
         printf("VL53L1_StartMeasurement %d \n",status);
-        status = board->sensor_centre->VL53LX_ClearInterruptAndStartMeasurement();
+        status = Sensor->VL53LX_ClearInterruptAndStartMeasurement();
     }
 
-    // loop waiting for interrupts to happen. This is signaled by   int_centre_result,int_left_result or int_right_result
-    // being non zero. The are set back to zero when processing is completed
+
+    VL53LX_MultiRangingData_t MultiRangingData;
+    VL53LX_MultiRangingData_t *pMultiRangingData;
+
+    // infinite loop, waiting for interrupts to happen.
+    // an interrupt being received is signaled by int_centre_result being non zero.
+    // int_centre_result is set back to zero when interrupt processing is completed
     while (1) {
 
-        VL53LX_MultiRangingData_t MultiRangingData;
-        VL53LX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
+        pMultiRangingData = &MultiRangingData;
 
         wait_ms(10);
 
         if (int_centre_result != 0) {
-            int_centre_result = 0;
-            status = board->sensor_centre->VL53LX_GetMultiRangingData( pMultiRangingData);
-            int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
+            status = Sensor->VL53LX_GetMultiRangingData( pMultiRangingData);
+            int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
             if (( no_of_object_found < 10 ) && ( no_of_object_found != 0 )) {
-                for(int j=0; j<no_of_object_found; j++) {
+                for(int j = 0; j < no_of_object_found; j++) {
                     if (pMultiRangingData->RangeData[j].RangeStatus == 0) {
                         printf("centre\t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
                                pMultiRangingData->RangeData[j].RangeStatus,
@@ -209,15 +189,16 @@
             }
             int_centre_result = 0;
             wait_ms( MEASUREMENTTIMING );
-            status = board->sensor_centre->VL53LX_ClearInterruptAndStartMeasurement();
+            status = Sensor->VL53LX_ClearInterruptAndStartMeasurement();
         }
+    }
 
-        printf("terminated");
-    }
+    printf("terminated");
 }
 
 #if (MBED_VERSION  > 60300)
-    extern "C" void wait_ms(int ms) {
-        thread_sleep_for(ms);
-    }
+extern "C" void wait_ms(int ms)
+{
+    thread_sleep_for(ms);
+}
 #endif