John Alexander / Mbed OS J_53L3_shield_polling

Dependencies:   53L13ExpansionBoard

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  *  This VL53L1X Expansion board test application performs range measurements
00003  *  using the onboard embedded centre sensor, in singleshot, polling mode.
00004  *  Measured ranges are ouput on the Serial Port, running at 9600 baud.
00005  *
00006  *  The User Blue button stops the current measurement and entire program,
00007  *  releasing all resources.
00008  *
00009  *  The Reset button can be used to restart the program.
00010  */
00011  
00012   //Main_single_polling.h
00013  
00014 #include <stdio.h>
00015 
00016 #include "mbed.h"
00017 #include "XNucleo53L1A1.h"
00018 #include "vl53L1x_I2c.h"
00019 #include <time.h>
00020 
00021 
00022 // define i2c mcu pins
00023 #define VL53L1_I2C_SDA   D14 
00024 #define VL53L1_I2C_SCL   D15 
00025 
00026 static XNucleo53L1A1 *board=NULL;
00027 Serial pc(SERIAL_TX, SERIAL_RX); 
00028 
00029 
00030 
00031 VL53LX_Dev_t                   devCentre;
00032 VL53LX_Dev_t                   devLeft;
00033 VL53LX_Dev_t                   devRight;
00034 VL53LX_DEV                     Dev = &devCentre;
00035 
00036 
00037  
00038 /*=================================== Main ==================================
00039 =============================================================================*/
00040 int main()
00041 {   
00042     int status;
00043     VL53LX * Sensor;
00044     uint16_t wordData;
00045 
00046     static VL53LX_MultiRangingData_t RangingData;
00047 
00048 
00049     pc.baud(115200);  // baud rate is important as printf statements take a lot of time
00050     printf("Hello world!\r\n");
00051 
00052     vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
00053 
00054  //   printf("I2C device created! %d %d\r\n",dev_I2C,*dev_I2C);
00055     
00056     /* creates the 53L1A1 expansion board singleton obj */
00057     board = XNucleo53L1A1::instance(dev_I2C, A2, D8, D2);
00058     printf("board created!\r\n");
00059 
00060 
00061     /* init the 53L1A1 expansion board with default values */
00062     status = board->init_board();
00063     if (status) {
00064         printf("Failed to init board!\r\n");
00065         return 0;
00066     }
00067        
00068    
00069     printf("board initiated! \n");
00070                                             
00071     printf("configuring centre channel \n");
00072     Dev=&devCentre;
00073     Sensor=board->sensor_centre;
00074     Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
00075     printf("configured centre channel \n");
00076   
00077         
00078    // configure the i2c connection     
00079     Dev->comms_speed_khz = 400;
00080     Dev->comms_type = 1;
00081 
00082 /* Device Initialization and setting */
00083 
00084     status = Sensor->VL53LX_DataInit();
00085     uint8_t NewDataReady=0;     
00086 
00087     
00088     status = Sensor->VL53LX_StartMeasurement();   
00089     printf("VL53LX_StartMeasurement %d \n",status);         
00090         
00091     while(1) 
00092     {     
00093         status = Sensor->VL53LX_WaitMeasurementDataReady();
00094 
00095         if(!status)
00096         {
00097 
00098             status = Sensor->VL53LX_GetMultiRangingData( &RangingData);
00099 
00100             if ( status == 0)
00101             {
00102                 int no_of_object_found=RangingData.NumberOfObjectsFound;
00103                 if ( no_of_object_found < 10 ) 
00104                 {
00105                     for(int j=0;j<no_of_object_found;j++){
00106                           if ((RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID) || 
00107                             (RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
00108                             {   // print data
00109                                 printf("centre \t object %d  \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
00110                                 j,
00111                                 RangingData.RangeData[j].RangeStatus,
00112                                 RangingData.RangeData[j].RangeMilliMeter,
00113                                 RangingData.RangeData[j].SignalRateRtnMegaCps/65536.0,
00114                                 RangingData.RangeData[j].AmbientRateRtnMegaCps/65536.0);
00115                             } //if
00116                     } //for
00117                 } // if  ( no_of_object_found < 10 ) 
00118             }   // if status VL53LX_GetMultiRangingData                                                                          
00119                                         
00120             } // if !status VL53LX_WaitMeasurementDataReady
00121             else
00122             {
00123                 printf("VL53L1_WaitMeasurementDataReady failed %d \n",status);
00124             }
00125 
00126             status = Sensor->VL53LX_ClearInterruptAndStartMeasurement();
00127         
00128     } // while(1)
00129     
00130 //       status = Sensor->VL53LX_StopMeasurement(); 
00131     
00132 }