Simple Ranging Example, using Expansion Board Sensor, driven via Interrupts.

Dependencies:   mbed X_NUCLEO_53L1A1_mbed

Revision:
17:e7a6fd6d3c97
Parent:
14:d3904b05aad6
Child:
19:92972e54d45c
--- a/main.cpp	Tue Apr 09 12:19:35 2019 +0000
+++ b/main.cpp	Fri May 17 09:14:06 2019 +0000
@@ -1,81 +1,123 @@
+/*
+ *  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.
+ *
+ *  The User Blue button stops the current measurement and entire program,
+ *  releasing all resources.
+ *
+ *  The Black Reset button is used to restart the program.
+ *
+ *  *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
+ *            the X-NUCELO-53L0A1 expansion board are not made/OFF.
+ *            These links must be made to allow interrupts from the Satellite boards
+ *            to be received.
+ *            U11 and U18 must be made/ON to allow interrupts to be received from the
+ *            INT_L & INT_R positions; or
+ *            U10 and U15 must be made/ON to allow interrupts to be received from the
+ *            Alternate INT_L & INT_R positions.
+ *            The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R
+ *            positions.
+ *            INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9.
+ *            Alternate INT_L is on CN5 Connector pin 2 as D8.
+ *            INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4.
+ *            Alternate INT_R is on CN9 Connector pin 5 as D2.
+ *            The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/
+ */
+
+#include <stdio.h>
+
 #include "mbed.h"
 #include "XNucleo53L1A1.h"
 #include "vl53L1x_I2c.h"
-#include <stdio.h>
 
-/* This VL53L1X Expansion board test application performs a range measurement in polling mode
-   on the onboard embedded top sensor. */
-
-#define VL53L1_I2C_SDA   D14 
-#define VL53L1_I2C_SCL   D15 
-
-
-/**
- * Expander 0 i2c address[7..0] format
- */
-#define I2cExpAddr0 134 // (0x43*2)
-/**
- * Expander 1 i2c address[7..0] format
- */
-#define I2cExpAddr1 132 // (0x42*2)
-
-/**
- * GPIO monitor pin state register
- * 16 bit register LSB at lowest offset (little endian)
- */
-#define GPMR    0x10
-/**
- * STMPE1600 GPIO set pin state register
- * 16 bit register LSB at lowest offset (little endian)
- */
-#define GPSR    0x12
-/**
- * STMPE1600 GPIO set pin direction register
- * 16 bit register LSB at lowest offset
- */
-#define GPDR    0x14
+#define VL53L1_I2C_SDA   D14
+#define VL53L1_I2C_SCL   D15
 
 static XNucleo53L1A1 *board=NULL;
-Serial pc(SERIAL_TX, SERIAL_RX); 
- 
+
+Serial pc(SERIAL_TX, SERIAL_RX);
+
 /*=================================== Main ==================================
 =============================================================================*/
 int main()
-{   
-    int status;
+{
+
+    int status = 0;
+    uint8_t ready_centre = 0;
+    uint8_t ready_left = 0;
+    uint8_t ready_right = 0;
+    uint16_t distance_centre = 0;
+    uint16_t distance_left = 0;
+    uint16_t distance_right = 0;
 
     printf("Hello world!\r\n");
 
     vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
 
     printf("I2C device created!\r\n");
-    
-    /* creates the 53L0A1 expansion board singleton obj */
-    board = XNucleo53L1A1::instance(dev_I2C, A2, D8, D2);
+
+    /* creates the 53L1A1 expansion board singleton obj */
+    board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2);
     printf("board created!\r\n");
 
-    /* init the 53L0A1 expansion board with default values */
+    /* init the 53L1A1 expansion board with default values */
     status = board->init_board();
-    if (status) {
+    if (status != 0) {
         printf("Failed to init board!\r\n");
-    //    return 0;
+        return status;
     }
+
     printf("board initiated! - %d\r\n", status);
-    
+
+    /* Start ranging on the centre sensor */
     status = board->sensor_centre->VL53L1X_StartRanging();
-    printf("start ranging! - %d\r\n", status);
-    uint8_t ready = 0;
-    uint16_t distance_centre = 0;
-    
+    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;
+    }
+
+    /* 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;
+    }
+
+    /* Ranging loop
+     * Checks each sensor for data ready
+     */
     while (1)
-    {   
-        do {
-            board->sensor_centre->VL53L1X_CheckForDataReady(&ready);
-        } while (!ready);
+    {
+        board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre);
+        board->sensor_left->VL53L1X_CheckForDataReady(&ready_left);
+        board->sensor_right->VL53L1X_CheckForDataReady(&ready_right);
 
-        board->sensor_centre->VL53L1X_GetRangeStatus(&ready);
-        board->sensor_centre->VL53L1X_GetDistance(&distance_centre);
-        //board->sensor_centre->VL53L1X_ClearInterrupt();
+        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);
+        }
+        if (ready_right) {
+            board->sensor_centre->VL53L1X_GetRangeStatus(&ready_right);
+            board->sensor_centre->VL53L1X_GetDistance(&distance_right);
+        }
+
         printf("%d\t", distance_centre);
-    }   
+        printf("%d\t", distance_left);
+        printf("%d\t", distance_right);
+    }
+
+    return status;
 }