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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "x_nucleo_53l0a1.h"
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <stdio.h>
00006 #include <assert.h>
00007 
00008 /* This VL53L0X Expansion board test application performs a range measurement in polling mode
00009    on the sensors. The demo supports the centre sensor and up to 2 satelites boards. 
00010    
00011    The User Blue button controls the selected sensor. Measurements from this sensor are displayed 
00012    on the 4-digit display.
00013    Data collected from all sensors is also sent on the com port.
00014    
00015    The Reset button is used to restart the program. 
00016  */
00017 
00018 #define VL53L0_I2C_SDA   D14 
00019 #define VL53L0_I2C_SCL   D15 
00020 
00021 static X_NUCLEO_53L0A1 *board=NULL;
00022 VL53L0X_RangingMeasurementData_t data_sensor;
00023 OperatingMode operating_mode;
00024     
00025 /* Current sensor number*/
00026 int currentSensor = 0;
00027 /* Installed sensors count */ 
00028 int sensorCnt = 0; 
00029 
00030 /* array storing pointers to the sensors and their prefixes for the display purpouse*/
00031 struct Sensors
00032 {
00033     char prefix;
00034     VL53L0X *sensorPtr;
00035 } installedSensors[3];
00036 
00037 int InitSensorsArray()
00038 {
00039    int status = 1;
00040    /* start the measure on sensor top */
00041    if (NULL != board->sensor_centre)
00042    {
00043        installedSensors[sensorCnt].prefix = 'C';
00044        installedSensors[sensorCnt].sensorPtr = board->sensor_centre;
00045        ++sensorCnt;
00046        status=board->sensor_centre->StartMeasurement(operating_mode, NULL);
00047    }
00048    /* start the measure on sensor left if present */
00049    if (NULL != board->sensor_left)
00050    {
00051         installedSensors[sensorCnt].prefix='L';
00052         installedSensors[sensorCnt].sensorPtr = board->sensor_left;
00053         ++sensorCnt;    
00054         status=board->sensor_left->StartMeasurement(operating_mode, NULL);
00055    }
00056    /* start the measure on sensor right if present */
00057    if (NULL != board->sensor_right)
00058    {
00059         installedSensors[sensorCnt].prefix = 'R';
00060         installedSensors[sensorCnt].sensorPtr = board->sensor_right;
00061         ++sensorCnt;
00062         status=board->sensor_right->StartMeasurement(operating_mode, NULL);
00063    }
00064    /* set first displayed sensor to embedded top */
00065    currentSensor = 0;
00066    return status;
00067 }
00068 
00069 /* ISR callback function of the user blue button to switch measuring sensor. */
00070 void SwitchMeasuringSensorIRQ(void)
00071 {
00072     ++currentSensor;
00073     if (currentSensor == sensorCnt)
00074         currentSensor = 0;
00075     printf("Sensor changed to %c\r\n",installedSensors[currentSensor].prefix);
00076 }
00077 
00078 /* On board 4 digit local display refresh*/
00079 void DisplayRefresh(OperatingMode op_mode)
00080 {   
00081   char str[4];
00082    
00083   for (int t=0; t < sensorCnt; t++)
00084   {
00085      installedSensors[t].sensorPtr->GetMeasurement(op_mode, &data_sensor);
00086      if (data_sensor.RangeStatus == 0) // we have a valid range.
00087      {
00088       printf("%c %4d; ", installedSensors[t].prefix,data_sensor.RangeMilliMeter);
00089       if (currentSensor == t)
00090       {
00091           sprintf(str,"%c%3d", installedSensors[t].prefix ,data_sensor.RangeMilliMeter);
00092       }
00093      }
00094      else
00095      {
00096       printf("%c ----; ", installedSensors[t].prefix);
00097       if (currentSensor == t)
00098       {
00099        sprintf(str,"%c%s", installedSensors[t].prefix, "---");   
00100       }
00101      } 
00102   }
00103   printf("\r\n");
00104   board->display->DisplayString(str);
00105 }
00106 
00107 void RangeMeasure(DevI2C *device_i2c) {
00108    int status;
00109 
00110    /* creates the 53L0A1 expansion board singleton obj */
00111    board=X_NUCLEO_53L0A1::Instance(device_i2c, A2, D8, D2);
00112     
00113    board->display->DisplayString("53L0");
00114    /* init the 53L0A1 expansion board with default values */
00115    status=board->InitBoard();
00116    if(status)
00117       printf("Failed to init board!\n\r");   
00118    operating_mode=range_single_shot_polling;
00119 
00120    status = InitSensorsArray();
00121    
00122    if(!status)
00123    {
00124       while(1)
00125       {
00126          DisplayRefresh(operating_mode);
00127       }
00128    }
00129    board->display->DisplayString("BYE");
00130    delete board;        
00131 }    
00132 
00133 /*=================================== Main ==================================
00134  Press the blue user button to switch the sensor.    
00135 =============================================================================*/
00136 int main()
00137 {   
00138 #if USER_BUTTON==PC_13  // we are cross compiling for Nucleo-f401 
00139    InterruptIn stop_button (USER_BUTTON);
00140    stop_button.rise (&SwitchMeasuringSensorIRQ);  
00141 #endif   
00142    DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);        
00143    RangeMeasure(device_i2c);  // start continuous measures
00144 }
00145