Simple ranging example, using expansion and satellite board sensors, with mbed OS 5.x

Dependencies:   X_NUCLEO_53L1A1_mbed

Revision:
1:b7507ca3370f
Parent:
0:6b7696e7df5e
--- a/main.cpp	Fri May 17 10:44:38 2019 +0000
+++ b/main.cpp	Thu May 23 13:00:36 2019 +0000
@@ -2,7 +2,7 @@
  *  This VL53L1X Expansion board test application performs range measurements
  *  using the onboard embedded centre sensor.
  *
- *  Measured ranges are ouput on the Serial Port, running at 115200 baud.
+ *  Measured ranges are ouput on the Serial Port, running at 96000 baud.
  *
  *  The User Blue button stops the current measurement and entire program,
  *  releasing all resources.
@@ -36,14 +36,23 @@
 #define VL53L1_I2C_SCL   D15
 
 static XNucleo53L1A1 *board=NULL;
+Serial pc(SERIAL_TX, SERIAL_RX);
 
-Serial pc(SERIAL_TX, SERIAL_RX);
+volatile bool polling_stop = false;
+
+void stop_polling(void)
+{
+    polling_stop = true;
+}
 
 /*=================================== Main ==================================
 =============================================================================*/
 int main()
 {
-
+#if USER_BUTTON==PC_13  // we are cross compiling for Nucleo-f401
+    InterruptIn stop_button(USER_BUTTON);
+    stop_button.rise(&stop_polling);
+#endif
     int status = 0;
     uint8_t ready_centre = 0;
     uint8_t ready_left = 0;
@@ -64,7 +73,7 @@
 
     /* init the 53L1A1 expansion board with default values */
     status = board->init_board();
-    if (status != 0) {
+    if (status) {
         printf("Failed to init board!\r\n");
         return status;
     }
@@ -72,24 +81,30 @@
     printf("board initiated! - %d\r\n", status);
 
     /* Start ranging on the centre sensor */
-    status = board->sensor_centre->VL53L1X_StartRanging();
-    if (status != 0) {
-        printf("Centre sensor failed to start ranging!\r\n");
-        return status;
+    if (board->sensor_centre != NULL) {
+        status = board->sensor_centre->VL53L1X_StartRanging();
+        if (status != 0) {
+            printf("Centre sensor failed to start ranging!\r\n");
+            return status;
+        }
     }
 
     /* Start ranging on the left sensor */
-    status = board->sensor_left->VL53L1X_StartRanging();
-    if (status != 0) {
-        printf("Left sensor failed to start ranging!\r\n");
-        return status;
+    if (board->sensor_left != NULL) {
+        status = board->sensor_left->VL53L1X_StartRanging();
+        if (status != 0) {
+            printf("Left sensor failed to start ranging!\r\n");
+            return status;
+        }
     }
 
     /* Start ranging on the right sensor */
-    status = board->sensor_right->VL53L1X_StartRanging();
-    if (status != 0) {
-        printf("Right sensor failed to start ranging!\r\n");
-        return status;
+    if (board->sensor_right != NULL) {
+        status = board->sensor_right->VL53L1X_StartRanging();
+        if (status != 0) {
+            printf("Right sensor failed to start ranging!\r\n");
+            return status;
+        }
     }
 
     /* Ranging loop
@@ -97,26 +112,42 @@
      */
     while (1)
     {
-        board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre);
-        board->sensor_left->VL53L1X_CheckForDataReady(&ready_left);
-        board->sensor_right->VL53L1X_CheckForDataReady(&ready_right);
-
+        if (polling_stop)
+        {
+            printf("\r\nEnding loop mode \r\n");
+            break;
+        }
+        if (board->sensor_centre != NULL) {
+            board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre);
+        }
+        if (board->sensor_left != NULL) {
+            board->sensor_left->VL53L1X_CheckForDataReady(&ready_left);
+        }
+        if (board->sensor_right != NULL) {
+            board->sensor_right->VL53L1X_CheckForDataReady(&ready_right);
+        }
         if (ready_centre) {
             board->sensor_centre->VL53L1X_GetRangeStatus(&ready_centre);
             board->sensor_centre->VL53L1X_GetDistance(&distance_centre);
         }
         if (ready_left) {
-            board->sensor_centre->VL53L1X_GetRangeStatus(&ready_left);
-            board->sensor_centre->VL53L1X_GetDistance(&distance_left);
+            board->sensor_left->VL53L1X_GetRangeStatus(&ready_left);
+            board->sensor_left->VL53L1X_GetDistance(&distance_left);
         }
         if (ready_right) {
-            board->sensor_centre->VL53L1X_GetRangeStatus(&ready_right);
-            board->sensor_centre->VL53L1X_GetDistance(&distance_right);
+            board->sensor_right->VL53L1X_GetRangeStatus(&ready_right);
+            board->sensor_right->VL53L1X_GetDistance(&distance_right);
         }
 
-        printf("%d\t", distance_centre);
-        printf("%d\t", distance_left);
-        printf("%d\t", distance_right);
+        if (board->sensor_centre != NULL) {
+            printf("%d\t", distance_centre);
+        }
+        if (board->sensor_left != NULL) {
+            printf("%d\t", distance_left);
+        }
+        if (board->sensor_right != NULL) {
+            printf("%d\t", distance_right);
+        }
     }
 
     return status;