Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed X_NUCLEO_53L1A1_mbed WS2812
main.cpp
00001 #include <stdio.h> 00002 00003 #include "mbed.h" 00004 #include "VL53L1X_I2C.h" 00005 #include "VL53L1X_Class.h" 00006 #include "LED_WS2812.h" 00007 00008 #define VL53L1_I2C_SDA D14 00009 #define VL53L1_I2C_SCL D15 00010 00011 static VL53L1X *sensor = NULL; 00012 Serial pc(SERIAL_TX, SERIAL_RX); 00013 00014 /* flags that handle interrupt request for sensor and user blue button*/ 00015 volatile bool int_sensor = false; 00016 volatile bool int_stop = false; 00017 00018 00019 /* ISR callback function of the sensor */ 00020 void sensor_irq(void) 00021 { 00022 int_sensor = true; 00023 sensor->disable_interrupt_measure_detection_irq(); 00024 } 00025 00026 /* ISR callback function of the user blue button to switch measuring sensor. */ 00027 void measuring_stop_irq(void) 00028 { 00029 int_stop = true; 00030 } 00031 00032 /* Start the sensor ranging */ 00033 int start_ranging() 00034 { 00035 int status = 0; 00036 /* start the measure on the sensor */ 00037 if (NULL != sensor) { 00038 status = sensor->stop_measurement(); 00039 if (status != 0) { 00040 return status; 00041 } 00042 00043 status = sensor->start_measurement(&sensor_irq); 00044 if (status != 0) { 00045 return status; 00046 } 00047 } 00048 return status; 00049 } 00050 00051 int range_measure(VL53L1X_DevI2C *device_i2c) 00052 { 00053 int status = 0; 00054 uint16_t distance = 0; 00055 int distance_close = 0; 00056 00057 LED_WS2812 LED(A0,12); 00058 LED.SetIntensity(5); 00059 LED.SetColor(BLACK); 00060 00061 /* Create a xshutdown pin */ 00062 DigitalOut xshutdown(D10); 00063 00064 /* create instance of sensor class */ 00065 sensor = new VL53L1X(device_i2c, &xshutdown, D11); 00066 00067 sensor->vl53l1_off(); 00068 /* initialise sensor */ 00069 sensor->init_sensor(0x52); 00070 00071 if (status) { 00072 delete sensor; 00073 sensor= NULL; 00074 printf("Sensor centre not present\n\r"); 00075 } 00076 status = sensor->vl53l1x_set_timing_budget_in_ms(100); // good trade off between accuracy and power consumption 00077 status = sensor->vl53l1x_set_inter_measurement_in_ms(500); // perform one ranging every second 00078 status = sensor->vl53l1x_set_distance_threshold(1900, 1900, 0, 0); // interrupt if distance < 1.9 m (~6 feet) 00079 status = start_ranging(); 00080 if (status != 0) { 00081 printf("Failed to start ranging!\r\n"); 00082 return status; 00083 } 00084 if (NULL != sensor) { 00085 printf("Entering loop mode\r\n"); 00086 /* Main ranging interrupt loop */ 00087 while (true) { 00088 if (int_sensor) { 00089 distance_close = 1; 00090 int_sensor = false; 00091 status = sensor->handle_irq(&distance); 00092 // printf("distance: %d\r\n", distance); 00093 } 00094 if (distance_close) 00095 { 00096 LED.InsertColor(RED); 00097 LED.StartBlink(0.2); 00098 wait_ms(500); 00099 LED.StopBlink(); 00100 distance_close = 0; 00101 } 00102 else 00103 { 00104 LED.SetColor(GREEN); 00105 wait_ms(500); 00106 } 00107 } 00108 } 00109 00110 return status; 00111 00112 } 00113 00114 00115 /*=================================== Main ================================== 00116 =============================================================================*/ 00117 int main() 00118 { 00119 #if TARGET_STM // we are cross compiling for an STM32-Nucleo 00120 InterruptIn stop_button(USER_BUTTON); 00121 stop_button.rise(&measuring_stop_irq); 00122 #endif 00123 #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards. 00124 InterruptIn stop_button(SW2); 00125 stop_button.rise(&measuring_stop_irq); 00126 #endif 00127 VL53L1X_DevI2C *device_i2c = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); 00128 range_measure(device_i2c); // start continuous measures 00129 }
Generated on Mon Jul 18 2022 05:09:59 by
1.7.2