Test an Adafruit single VL53L0X breakout board using the mbed LPC1768 and print distance measurement to PC

Dependencies:   X_NUCLEO_53L0A1 mbed

Fork of HelloWorld_53L0A1 by ST

https://os.mbed.com/users/kbeck8/notebook/alternative-control-for-remote-control-vehicle/ has a more complex example using three LIDAR breakouts for gesture detection on the same I2C bus using the shutdown pin (solves the issue of them having the same I2C address). The driver has support for three, but an AND gate is required unless the driver is changed. DigitalOut pins turn shutdown on/off on each LIDAR.

Committer:
johnAlexander
Date:
Mon Nov 28 11:26:38 2016 +0000
Revision:
0:ce8359133ae6
Child:
1:3483e701ec59
Simple, singleshot, polled ranging example using the central sensor of an X_NUCLEO_53L0A1 expansion board.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnAlexander 0:ce8359133ae6 1 #include "mbed.h"
johnAlexander 0:ce8359133ae6 2 #include "x_nucleo_53l0a1.h"
johnAlexander 0:ce8359133ae6 3 #include <string.h>
johnAlexander 0:ce8359133ae6 4 #include <stdlib.h>
johnAlexander 0:ce8359133ae6 5 #include <stdio.h>
johnAlexander 0:ce8359133ae6 6 #include <assert.h>
johnAlexander 0:ce8359133ae6 7
johnAlexander 0:ce8359133ae6 8 /* This VL6180X Expansion board test application performs a range measurement and an als measurement in interrupt mode
johnAlexander 0:ce8359133ae6 9 on the onboard embedded top sensor.
johnAlexander 0:ce8359133ae6 10 The board red slider select on the flight the measurement type as ALS or RANGE; the measured data is diplayed on the
johnAlexander 0:ce8359133ae6 11 on bord 4digits display.
johnAlexander 0:ce8359133ae6 12
johnAlexander 0:ce8359133ae6 13 User Blue button allows to stop current measurement and the entire program releasing all the resources.
johnAlexander 0:ce8359133ae6 14 Reset button is used to restart the program. */
johnAlexander 0:ce8359133ae6 15
johnAlexander 0:ce8359133ae6 16 /* Polling operating modes don`t require callback function that handles IRQ
johnAlexander 0:ce8359133ae6 17 Callback IRQ functions are used only for measure that require interrupt */
johnAlexander 0:ce8359133ae6 18
johnAlexander 0:ce8359133ae6 19 /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value
johnAlexander 0:ce8359133ae6 20 is not ready to be read from the corresponding register. So you need to wait
johnAlexander 0:ce8359133ae6 21 for the result to be ready */
johnAlexander 0:ce8359133ae6 22
johnAlexander 0:ce8359133ae6 23 #define VL53L0_I2C_SDA D14
johnAlexander 0:ce8359133ae6 24 #define VL53L0_I2C_SCL D15
johnAlexander 0:ce8359133ae6 25
johnAlexander 0:ce8359133ae6 26 #define RANGE 0
johnAlexander 0:ce8359133ae6 27
johnAlexander 0:ce8359133ae6 28 static X_NUCLEO_53L0A1 *board=NULL;
johnAlexander 0:ce8359133ae6 29 VL53L0X_RangingMeasurementData_t data_sensor_centre;
johnAlexander 0:ce8359133ae6 30 OperatingMode operating_mode, prev_operating_mode;
johnAlexander 0:ce8359133ae6 31
johnAlexander 0:ce8359133ae6 32 /* flags that handle interrupt request */
johnAlexander 0:ce8359133ae6 33 bool int_sensor_centre=false, int_stop_measure=false;
johnAlexander 0:ce8359133ae6 34
johnAlexander 0:ce8359133ae6 35 /* ISR callback function of the sensor_centre */
johnAlexander 0:ce8359133ae6 36 void SensorTopIRQ(void)
johnAlexander 0:ce8359133ae6 37 {
johnAlexander 0:ce8359133ae6 38 int_sensor_centre=true;
johnAlexander 0:ce8359133ae6 39 board->sensor_centre->DisableInterruptMeasureDetectionIRQ();
johnAlexander 0:ce8359133ae6 40 }
johnAlexander 0:ce8359133ae6 41
johnAlexander 0:ce8359133ae6 42 /* ISR callback function of the user blue button to stop program */
johnAlexander 0:ce8359133ae6 43 void StopMeasureIRQ(void)
johnAlexander 0:ce8359133ae6 44 {
johnAlexander 0:ce8359133ae6 45 int_stop_measure=true;
johnAlexander 0:ce8359133ae6 46 }
johnAlexander 0:ce8359133ae6 47 #define DELAY 100 // 100ms
johnAlexander 0:ce8359133ae6 48 /* On board 4 digit local display refresh */
johnAlexander 0:ce8359133ae6 49 void DisplayRefresh(OperatingMode op_mode)
johnAlexander 0:ce8359133ae6 50 {
johnAlexander 0:ce8359133ae6 51 Timer timer;
johnAlexander 0:ce8359133ae6 52 char str[5];
johnAlexander 0:ce8359133ae6 53
johnAlexander 0:ce8359133ae6 54 if (op_mode==range_single_shot_polling || op_mode==range_continuous_interrupt || op_mode==range_continuous_polling)
johnAlexander 0:ce8359133ae6 55 {
johnAlexander 0:ce8359133ae6 56 if (data_sensor_centre.RangeStatus == 0) // we have a valid range.
johnAlexander 0:ce8359133ae6 57 {
johnAlexander 0:ce8359133ae6 58 sprintf(str,"%d",data_sensor_centre.RangeMilliMeter);
johnAlexander 0:ce8359133ae6 59 }
johnAlexander 0:ce8359133ae6 60 else
johnAlexander 0:ce8359133ae6 61 {
johnAlexander 0:ce8359133ae6 62 sprintf(str,"%s","----");
johnAlexander 0:ce8359133ae6 63 }
johnAlexander 0:ce8359133ae6 64 }
johnAlexander 0:ce8359133ae6 65
johnAlexander 0:ce8359133ae6 66 board->display->DisplayString(str);
johnAlexander 0:ce8359133ae6 67 /*
johnAlexander 0:ce8359133ae6 68 timer.start();
johnAlexander 0:ce8359133ae6 69 for(int i=0; i<DELAY; i=timer.read_ms())
johnAlexander 0:ce8359133ae6 70 {
johnAlexander 0:ce8359133ae6 71 }
johnAlexander 0:ce8359133ae6 72 timer.stop();
johnAlexander 0:ce8359133ae6 73 */
johnAlexander 0:ce8359133ae6 74 }
johnAlexander 0:ce8359133ae6 75
johnAlexander 0:ce8359133ae6 76 /* Print on USB Serial the started OperatingMode */
johnAlexander 0:ce8359133ae6 77 /*
johnAlexander 0:ce8359133ae6 78 void PrintStartMessage(OperatingMode op_mode)
johnAlexander 0:ce8359133ae6 79 {
johnAlexander 0:ce8359133ae6 80 if(op_mode==range_continuous_interrupt)
johnAlexander 0:ce8359133ae6 81 printf("\nStarted range continuous interrupt measure\n\r");
johnAlexander 0:ce8359133ae6 82 }
johnAlexander 0:ce8359133ae6 83 */
johnAlexander 0:ce8359133ae6 84
johnAlexander 0:ce8359133ae6 85 /* Print on USB Serial the stopped OperatingMode */
johnAlexander 0:ce8359133ae6 86 /*
johnAlexander 0:ce8359133ae6 87 void PrintStopMessage(OperatingMode op_mode)
johnAlexander 0:ce8359133ae6 88 {
johnAlexander 0:ce8359133ae6 89 if(op_mode==range_continuous_interrupt)
johnAlexander 0:ce8359133ae6 90 printf("Stopped range continuous interrupt measure\n\r");
johnAlexander 0:ce8359133ae6 91 }
johnAlexander 0:ce8359133ae6 92 */
johnAlexander 0:ce8359133ae6 93 /* Print on board 4 Digit display the indicated message <= 4 char */
johnAlexander 0:ce8359133ae6 94 //#define DELAY 2000 // 2Sec
johnAlexander 0:ce8359133ae6 95 /*
johnAlexander 0:ce8359133ae6 96 void DisplayMsg(const char * msg)
johnAlexander 0:ce8359133ae6 97 {
johnAlexander 0:ce8359133ae6 98 Timer timer;
johnAlexander 0:ce8359133ae6 99 char str[5];
johnAlexander 0:ce8359133ae6 100
johnAlexander 0:ce8359133ae6 101 timer.start();
johnAlexander 0:ce8359133ae6 102 for(int i=0; i<DELAY; i=timer.read_ms())
johnAlexander 0:ce8359133ae6 103 {
johnAlexander 0:ce8359133ae6 104 sprintf(str,"%s",msg);
johnAlexander 0:ce8359133ae6 105 board->display->DisplayString(str);
johnAlexander 0:ce8359133ae6 106 }
johnAlexander 0:ce8359133ae6 107 timer.stop();
johnAlexander 0:ce8359133ae6 108 }
johnAlexander 0:ce8359133ae6 109 */
johnAlexander 0:ce8359133ae6 110
johnAlexander 0:ce8359133ae6 111 void ContinousRangeMeasure(DevI2C *device_i2c) {
johnAlexander 0:ce8359133ae6 112 int status;
johnAlexander 0:ce8359133ae6 113
johnAlexander 0:ce8359133ae6 114 /* creates the 53L0A1 expansion board singleton obj */
johnAlexander 0:ce8359133ae6 115 board=X_NUCLEO_53L0A1::Instance(device_i2c, A2, D8, D2);
johnAlexander 0:ce8359133ae6 116
johnAlexander 0:ce8359133ae6 117 board->display->DisplayString("53L0");
johnAlexander 0:ce8359133ae6 118 /* init the 53L0A1 expansion board with default values */
johnAlexander 0:ce8359133ae6 119 status=board->InitBoard();
johnAlexander 0:ce8359133ae6 120 if(status)
johnAlexander 0:ce8359133ae6 121 printf("Failed to init board!\n\r");
johnAlexander 0:ce8359133ae6 122 // operating_mode=range_continuous_polling;
johnAlexander 0:ce8359133ae6 123 operating_mode=range_single_shot_polling;
johnAlexander 0:ce8359133ae6 124 /* start the measure on sensor top */
johnAlexander 0:ce8359133ae6 125 status=board->sensor_centre->StartMeasurement(operating_mode, SensorTopIRQ);
johnAlexander 0:ce8359133ae6 126 if(!status)
johnAlexander 0:ce8359133ae6 127 {
johnAlexander 0:ce8359133ae6 128 while(1)
johnAlexander 0:ce8359133ae6 129 {
johnAlexander 0:ce8359133ae6 130 status=board->sensor_centre->GetMeasurement(operating_mode, &data_sensor_centre);
johnAlexander 0:ce8359133ae6 131 DisplayRefresh(operating_mode);
johnAlexander 0:ce8359133ae6 132 if(int_stop_measure) // Blue Button isr was triggered
johnAlexander 0:ce8359133ae6 133 {
johnAlexander 0:ce8359133ae6 134 status=board->sensor_centre->StopMeasurement(operating_mode); // stop the measure and exit
johnAlexander 0:ce8359133ae6 135 int_stop_measure = false;
johnAlexander 0:ce8359133ae6 136 printf("\nProgram stopped!\n\n\r");
johnAlexander 0:ce8359133ae6 137 break;
johnAlexander 0:ce8359133ae6 138 }
johnAlexander 0:ce8359133ae6 139 }
johnAlexander 0:ce8359133ae6 140 }
johnAlexander 0:ce8359133ae6 141 board->display->DisplayString("BYE");
johnAlexander 0:ce8359133ae6 142 delete board;
johnAlexander 0:ce8359133ae6 143 }
johnAlexander 0:ce8359133ae6 144
johnAlexander 0:ce8359133ae6 145 /*=================================== Main ==================================
johnAlexander 0:ce8359133ae6 146 Press the blue user button to stop the measurements in progress
johnAlexander 0:ce8359133ae6 147 =============================================================================*/
johnAlexander 0:ce8359133ae6 148 int main()
johnAlexander 0:ce8359133ae6 149 {
johnAlexander 0:ce8359133ae6 150 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401
johnAlexander 0:ce8359133ae6 151 InterruptIn stop_button (USER_BUTTON);
johnAlexander 0:ce8359133ae6 152 stop_button.rise (&StopMeasureIRQ);
johnAlexander 0:ce8359133ae6 153 #endif
johnAlexander 0:ce8359133ae6 154 DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
johnAlexander 0:ce8359133ae6 155
johnAlexander 0:ce8359133ae6 156 ContinousRangeMeasure(device_i2c); // start continuous measures
johnAlexander 0:ce8359133ae6 157 }
johnAlexander 0:ce8359133ae6 158
johnAlexander 0:ce8359133ae6 159
johnAlexander 0:ce8359133ae6 160