Sample ranging example. Suporting up to 3 sensors. Sending result from X-NUCLEO-53L0A1 both on com port and on digit display. User button IRQ used for switching displayed sensor

Dependencies:   X_NUCLEO_53L0A1 mbed

Fork of Display_53L0A1_WithSatelites by Jerry Szczurak

Files at this revision

API Documentation at this revision

Comitter:
JerrySzczurak
Date:
Wed Jun 14 09:14:13 2017 +0000
Parent:
0:eade49d47e3a
Commit message:
Sample ranging example. Suporting up to 3 sensors. Sending result from X-NUCLEO-53L0A1 both on com port and on digit display. User button IRQ used for switching displayed sensor

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r eade49d47e3a -r 9d5c48d409b6 main.cpp
--- a/main.cpp	Mon Jun 12 15:01:09 2017 +0000
+++ b/main.cpp	Wed Jun 14 09:14:13 2017 +0000
@@ -6,47 +6,65 @@
 #include <assert.h>
 
 /* This VL53L0X Expansion board test application performs a range measurement in polling mode
-   on the onboard embedded top sensor. 
-   The measured data is displayed on the on-board 4-digit display.
-
-   User Blue button stops the current measurement and the entire program, releasing all resources.
-   Reset button is used to restart the program. */
-
-/* Polling operating modes don`t require callback function that handles IRQ 
-   Callback IRQ functions are used only for measure that require interrupt */
-
-/* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value 
-   is not ready to be read from the corresponding register. So you need to wait
-   for the result to be ready */
+   on the sensors. The demo supports the centre sensor and up to 2 satelites boards. 
+   
+   The User Blue button controls the selected sensor. Measurements from this sensor are displayed 
+   on the 4-digit display.
+   Data collected from all sensors is also sent on the com port.
+   
+   The Reset button is used to restart the program. 
+ */
 
 #define VL53L0_I2C_SDA   D14 
 #define VL53L0_I2C_SCL   D15 
 
-#define RANGE   0
-
 static X_NUCLEO_53L0A1 *board=NULL;
 VL53L0X_RangingMeasurementData_t data_sensor;
 OperatingMode operating_mode;
     
-/* flags that handle interrupt request */
-volatile bool int_sensor_centre=false;
 /* Current sensor number*/
-int currentSensor = 0; 
+int currentSensor = 0;
+/* Installed sensors count */ 
 int sensorCnt = 0; 
 
+/* array storing pointers to the sensors and their prefixes for the display purpouse*/
 struct Sensors
 {
     char prefix;
     VL53L0X *sensorPtr;
 } installedSensors[3];
 
-
-/* ISR callback function of the sensor_centre */
-void SensorTopIRQ(void)
+int InitSensorsArray()
 {
-   int_sensor_centre=true;
-   board->sensor_centre->DisableInterruptMeasureDetectionIRQ();
-}   
+   int status = 1;
+   /* start the measure on sensor top */
+   if (NULL != board->sensor_centre)
+   {
+       installedSensors[sensorCnt].prefix = 'C';
+       installedSensors[sensorCnt].sensorPtr = board->sensor_centre;
+       ++sensorCnt;
+       status=board->sensor_centre->StartMeasurement(operating_mode, NULL);
+   }
+   /* start the measure on sensor left if present */
+   if (NULL != board->sensor_left)
+   {
+        installedSensors[sensorCnt].prefix='L';
+        installedSensors[sensorCnt].sensorPtr = board->sensor_left;
+        ++sensorCnt;    
+        status=board->sensor_left->StartMeasurement(operating_mode, NULL);
+   }
+   /* start the measure on sensor right if present */
+   if (NULL != board->sensor_right)
+   {
+        installedSensors[sensorCnt].prefix = 'R';
+        installedSensors[sensorCnt].sensorPtr = board->sensor_right;
+        ++sensorCnt;
+        status=board->sensor_right->StartMeasurement(operating_mode, NULL);
+   }
+   /* set first displayed sensor to embedded top */
+   currentSensor = 0;
+   return status;
+}
 
 /* ISR callback function of the user blue button to switch measuring sensor. */
 void SwitchMeasuringSensorIRQ(void)
@@ -57,36 +75,33 @@
     printf("Sensor changed to %c\r\n",installedSensors[currentSensor].prefix);
 }
 
-/* On board 4 digit local display refresh */
+/* On board 4 digit local display refresh*/
 void DisplayRefresh(OperatingMode op_mode)
 {   
-   char str[4];
+  char str[4];
    
-   if (op_mode==range_single_shot_polling || op_mode==range_continuous_interrupt || op_mode==range_continuous_polling)
-   {
-      for (int t=0; t < sensorCnt; t++)
+  for (int t=0; t < sensorCnt; t++)
+  {
+     installedSensors[t].sensorPtr->GetMeasurement(op_mode, &data_sensor);
+     if (data_sensor.RangeStatus == 0) // we have a valid range.
+     {
+      printf("%c %4d; ", installedSensors[t].prefix,data_sensor.RangeMilliMeter);
+      if (currentSensor == t)
       {
-         installedSensors[t].sensorPtr->GetMeasurement(operating_mode, &data_sensor);
-         if (data_sensor.RangeStatus == 0) // we have a valid range.
-         {
-          printf("%c %4d; ", installedSensors[t].prefix,data_sensor.RangeMilliMeter);
-          if (currentSensor == t)
-          {
-              sprintf(str,"%c%3d", installedSensors[t].prefix ,data_sensor.RangeMilliMeter);
-          }
-         }
-         else
-         {
-          printf("%c ----; ", installedSensors[t].prefix);
-          if (currentSensor == t)
-          {
-           sprintf(str,"%c%s", installedSensors[t].prefix, "---");   
-          }
-         } 
+          sprintf(str,"%c%3d", installedSensors[t].prefix ,data_sensor.RangeMilliMeter);
       }
-      printf("\r\n");
-   }
-   board->display->DisplayString(str);
+     }
+     else
+     {
+      printf("%c ----; ", installedSensors[t].prefix);
+      if (currentSensor == t)
+      {
+       sprintf(str,"%c%s", installedSensors[t].prefix, "---");   
+      }
+     } 
+  }
+  printf("\r\n");
+  board->display->DisplayString(str);
 }
 
 void RangeMeasure(DevI2C *device_i2c) {
@@ -101,30 +116,8 @@
    if(status)
       printf("Failed to init board!\n\r");   
    operating_mode=range_single_shot_polling;
-   /* start the measure on sensor top */
-   if (NULL != board->sensor_centre)
-   {
-       installedSensors[sensorCnt].prefix = 'C';
-       installedSensors[sensorCnt].sensorPtr = board->sensor_centre;
-       ++sensorCnt;
-       status=board->sensor_centre->StartMeasurement(operating_mode, SensorTopIRQ);
-   }
-   if (NULL != board->sensor_left)
-   {
-    installedSensors[sensorCnt].prefix='L';
-    installedSensors[sensorCnt].sensorPtr = board->sensor_left;
-    ++sensorCnt;    
-    status=board->sensor_left->StartMeasurement(operating_mode, SensorTopIRQ);
-   }
-   if (NULL != board->sensor_right)
-   {
-    installedSensors[sensorCnt].prefix = 'R';
-    installedSensors[sensorCnt].sensorPtr = board->sensor_right;
-    ++sensorCnt;
-    status=board->sensor_right->StartMeasurement(operating_mode, SensorTopIRQ);
-   }
-   
-   currentSensor = 0;
+
+   status = InitSensorsArray();
    
    if(!status)
    {
@@ -138,7 +131,7 @@
 }    
 
 /*=================================== Main ==================================
- Press the blue user button to stop the measurements in progress    
+ Press the blue user button to switch the sensor.    
 =============================================================================*/
 int main()
 {