ST Expansion SW Team / Mbed OS Display_53L0A1_OS5

Dependencies:   X_NUCLEO_53L0A1_mbed_sdk

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* 
00002  *  This VL53L0X Expansion board test application performs range measurements
00003  *  using the onboard embedded centre sensor, in singleshot, polling mode. 
00004  *  Measured ranges are displayed on the on-board 4-digit display.
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 #include <string.h>
00013 #include <stdio.h>
00014  
00015 #include "mbed.h"
00016 #include "x_nucleo_53l0a1.h"
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_centre;
00023 OperatingMode operating_mode;
00024     
00025 /* flags that handle interrupt request */
00026 bool int_stop_measure = false;   
00027  
00028 /* ISR callback function of the user blue button to stop program */
00029 void StopMeasureIRQ(void)
00030 {
00031    int_stop_measure = true;
00032 }
00033  
00034 /* On board 4 digit local display refresh */
00035 void DisplayRefresh(VL53L0X_RangingMeasurementData_t sensor_range_data)
00036 {   
00037    char str[5];
00038    
00039    if (sensor_range_data.RangeStatus == 0) // we have a valid range.
00040    {
00041       sprintf(str,"%d", sensor_range_data.RangeMilliMeter);
00042    }
00043    else
00044    {
00045       sprintf(str,"%s","----");
00046    }
00047      
00048    board->display->display_string(str);
00049 }
00050  
00051 /*=================================== Main ==================================
00052  Press the blue user button to stop measurements in progress    
00053 =============================================================================*/
00054 int main()
00055 {
00056 #if USER_BUTTON == PC_13  // we are cross compiling for Nucleo-f401 
00057    InterruptIn stop_button(USER_BUTTON);
00058    stop_button.rise(&StopMeasureIRQ);  
00059 #endif   
00060  
00061    DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);     
00062    int status;
00063         
00064    /* creates the 53L0A1 expansion board singleton obj */
00065    board=X_NUCLEO_53L0A1::instance(device_i2c, A2, D8, D2);
00066     
00067    board->display->display_string("53L0");
00068    /* init the 53L0A1 expansion board with default values */
00069    status = board->init_board();
00070    if (status) printf("Failed to init board!\n\r");   
00071    operating_mode = range_single_shot_polling;
00072    /* start the measure on sensor centre */
00073    status = board->sensor_centre->start_measurement(operating_mode, NULL);
00074    if (!status)
00075    {
00076       while (1) // infinite loop. can be broken by pressing Blue (User) button.
00077       {
00078          status = board->sensor_centre->get_measurement(operating_mode, &data_sensor_centre);
00079          DisplayRefresh(data_sensor_centre);
00080          if (int_stop_measure) // Blue Button isr was triggered
00081          {
00082             status = board->sensor_centre->stop_measurement(operating_mode); // stop the measure and exit
00083             int_stop_measure = false;
00084             printf("\nProgram stopped!\n\n\r");
00085             break;
00086          }
00087       }
00088    }
00089    board->display->display_string("BYE");
00090    delete board;        
00091 }
00092  
00093