Sample application, using the X_Nucleo_53L0A1 library, to display range results from the X-NUCLEO-53L0A1 central sensor on the 4-digit LED display.

Dependencies:   mbed X_NUCLEO_53L0A1

Fork of Display_53L0A1 by ST Expansion SW Team

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