Sample application to show ranging with interrupts support. Uses centre sensor on X-Nucleo-53L0A1 and ranging_continuous_interrupt mode.
Dependencies: mbed X_NUCLEO_53L0A1
Fork of Display_53L0A1 by
main.cpp
00001 #include "mbed.h" 00002 #include "XNucleo53L0A1.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 interrupt mode 00009 on the center embedded sensor. 00010 The measured data is displayed on the on-board 4-digit display, and sent to com port 00011 00012 User Blue button stops the current measurement and the entire program, releasing all resources. 00013 Reset button is used to restart the program. 00014 00015 Interrupt mode requires callback function which handles IRQ from given sensor. 00016 */ 00017 00018 #define VL53L0_I2C_SDA D14 00019 #define VL53L0_I2C_SCL D15 00020 00021 static XNucleo53L0A1 *board = NULL; 00022 VL53L0X_RangingMeasurementData_t data_sensor; 00023 OperatingMode operating_mode; 00024 00025 /* flags that handle interrupt request for sensor and user blue button*/ 00026 volatile bool int_sensor_centre = false; 00027 volatile bool int_measuring_stop = false; 00028 00029 00030 /* ISR callback function of the centre sensor */ 00031 void sensor_centre_irq(void) 00032 { 00033 int_sensor_centre = true; 00034 board->sensor_centre->disable_interrupt_measure_detection_irq(); 00035 } 00036 00037 /* ISR callback function of the user blue button to switch measuring sensor. */ 00038 void measuring_stop_irq(void) 00039 { 00040 int_measuring_stop = true; 00041 } 00042 00043 /* On board 4 digit local display refresh */ 00044 void display_refresh(const VL53L0X_RangingMeasurementData_t &data) 00045 { 00046 char str[4]; 00047 if (data.RangeStatus == 0) { // we have a valid range. 00048 printf("%4d; ", data.RangeMilliMeter); 00049 sprintf(str, "%4d", data.RangeMilliMeter); 00050 } else { 00051 sprintf(str, "%s", "----"); 00052 } 00053 board->display->display_string(str); 00054 } 00055 00056 void range_measure(DevI2C *device_i2c) 00057 { 00058 int status; 00059 00060 /* creates the 53L0A1 expansion board singleton obj */ 00061 board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2); 00062 00063 board->display->display_string("53L0"); 00064 00065 /* setting operating mode to continuous interrupt */ 00066 operating_mode = range_continuous_interrupt; 00067 00068 /* init the 53L0A1 expansion board with default values */ 00069 status = board->init_board(); 00070 if (status) { 00071 printf("Failed to init board!\n\r"); 00072 return; 00073 } 00074 //Stop any measurement before setting sensor 00075 status = board->sensor_centre->stop_measurement(operating_mode); 00076 status = board->sensor_centre->start_measurement(operating_mode, &sensor_centre_irq); 00077 00078 if (!status) { 00079 printf("Entering loop mode\r\n"); 00080 while (true) { 00081 if (int_sensor_centre) { 00082 int_sensor_centre = false; 00083 status = board->sensor_centre->handle_irq(operating_mode, &data_sensor); 00084 display_refresh(data_sensor); 00085 } 00086 00087 if (int_measuring_stop) { 00088 printf("\r\nEnding loop mode \r\n"); 00089 break; 00090 } 00091 } 00092 } 00093 board->display->display_string("BYE"); 00094 delete board; 00095 } 00096 00097 /*=================================== Main ================================== 00098 Press the blue user button to stop the measurements in progress 00099 =============================================================================*/ 00100 int main() 00101 { 00102 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 00103 InterruptIn stop_button(USER_BUTTON); 00104 stop_button.rise(&measuring_stop_irq); 00105 #endif 00106 DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL); 00107 range_measure(device_i2c); // start continuous measures 00108 } 00109 00110
Generated on Wed Jul 20 2022 08:57:31 by 1.7.2