ECE 4180 Lab 2 Part 16

Dependencies:   X_NUCLEO_53L0A1 mbed

Fork of HelloWorld_VL53L0X_LPC1768 by jim hamblen

Committer:
johnAlexander
Date:
Mon Nov 28 14:55:05 2016 +0000
Revision:
1:3483e701ec59
Parent:
0:ce8359133ae6
Child:
3:b3f70617a6b3
First release. Ready to go to st.com and mbed.

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 1:3483e701ec59 8 /* This VL53L0X Expansion board test application performs a range measurement in polling mode
johnAlexander 0:ce8359133ae6 9 on the onboard embedded top sensor.
johnAlexander 1:3483e701ec59 10 The measured data is displayed on the on-board 4-digit display.
johnAlexander 0:ce8359133ae6 11
johnAlexander 1:3483e701ec59 12 User Blue button stops the current measurement and the entire program, releasing all resources.
johnAlexander 0:ce8359133ae6 13 Reset button is used to restart the program. */
johnAlexander 0:ce8359133ae6 14
johnAlexander 0:ce8359133ae6 15 /* Polling operating modes don`t require callback function that handles IRQ
johnAlexander 0:ce8359133ae6 16 Callback IRQ functions are used only for measure that require interrupt */
johnAlexander 0:ce8359133ae6 17
johnAlexander 0:ce8359133ae6 18 /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value
johnAlexander 0:ce8359133ae6 19 is not ready to be read from the corresponding register. So you need to wait
johnAlexander 0:ce8359133ae6 20 for the result to be ready */
johnAlexander 0:ce8359133ae6 21
johnAlexander 0:ce8359133ae6 22 #define VL53L0_I2C_SDA D14
johnAlexander 0:ce8359133ae6 23 #define VL53L0_I2C_SCL D15
johnAlexander 0:ce8359133ae6 24
johnAlexander 0:ce8359133ae6 25 #define RANGE 0
johnAlexander 0:ce8359133ae6 26
johnAlexander 0:ce8359133ae6 27 static X_NUCLEO_53L0A1 *board=NULL;
johnAlexander 0:ce8359133ae6 28 VL53L0X_RangingMeasurementData_t data_sensor_centre;
johnAlexander 1:3483e701ec59 29 OperatingMode operating_mode;
johnAlexander 0:ce8359133ae6 30
johnAlexander 0:ce8359133ae6 31 /* flags that handle interrupt request */
johnAlexander 0:ce8359133ae6 32 bool int_sensor_centre=false, int_stop_measure=false;
johnAlexander 0:ce8359133ae6 33
johnAlexander 0:ce8359133ae6 34 /* ISR callback function of the sensor_centre */
johnAlexander 0:ce8359133ae6 35 void SensorTopIRQ(void)
johnAlexander 0:ce8359133ae6 36 {
johnAlexander 0:ce8359133ae6 37 int_sensor_centre=true;
johnAlexander 0:ce8359133ae6 38 board->sensor_centre->DisableInterruptMeasureDetectionIRQ();
johnAlexander 0:ce8359133ae6 39 }
johnAlexander 0:ce8359133ae6 40
johnAlexander 0:ce8359133ae6 41 /* ISR callback function of the user blue button to stop program */
johnAlexander 0:ce8359133ae6 42 void StopMeasureIRQ(void)
johnAlexander 0:ce8359133ae6 43 {
johnAlexander 0:ce8359133ae6 44 int_stop_measure=true;
johnAlexander 0:ce8359133ae6 45 }
johnAlexander 1:3483e701ec59 46
johnAlexander 0:ce8359133ae6 47 /* On board 4 digit local display refresh */
johnAlexander 0:ce8359133ae6 48 void DisplayRefresh(OperatingMode op_mode)
johnAlexander 0:ce8359133ae6 49 {
johnAlexander 0:ce8359133ae6 50 char str[5];
johnAlexander 0:ce8359133ae6 51
johnAlexander 0:ce8359133ae6 52 if (op_mode==range_single_shot_polling || op_mode==range_continuous_interrupt || op_mode==range_continuous_polling)
johnAlexander 0:ce8359133ae6 53 {
johnAlexander 0:ce8359133ae6 54 if (data_sensor_centre.RangeStatus == 0) // we have a valid range.
johnAlexander 0:ce8359133ae6 55 {
johnAlexander 0:ce8359133ae6 56 sprintf(str,"%d",data_sensor_centre.RangeMilliMeter);
johnAlexander 0:ce8359133ae6 57 }
johnAlexander 0:ce8359133ae6 58 else
johnAlexander 0:ce8359133ae6 59 {
johnAlexander 0:ce8359133ae6 60 sprintf(str,"%s","----");
johnAlexander 0:ce8359133ae6 61 }
johnAlexander 0:ce8359133ae6 62 }
johnAlexander 0:ce8359133ae6 63
johnAlexander 0:ce8359133ae6 64 board->display->DisplayString(str);
johnAlexander 0:ce8359133ae6 65 }
johnAlexander 0:ce8359133ae6 66
johnAlexander 1:3483e701ec59 67 void RangeMeasure(DevI2C *device_i2c) {
johnAlexander 0:ce8359133ae6 68 int status;
johnAlexander 0:ce8359133ae6 69
johnAlexander 0:ce8359133ae6 70 /* creates the 53L0A1 expansion board singleton obj */
johnAlexander 0:ce8359133ae6 71 board=X_NUCLEO_53L0A1::Instance(device_i2c, A2, D8, D2);
johnAlexander 0:ce8359133ae6 72
johnAlexander 0:ce8359133ae6 73 board->display->DisplayString("53L0");
johnAlexander 0:ce8359133ae6 74 /* init the 53L0A1 expansion board with default values */
johnAlexander 0:ce8359133ae6 75 status=board->InitBoard();
johnAlexander 0:ce8359133ae6 76 if(status)
johnAlexander 0:ce8359133ae6 77 printf("Failed to init board!\n\r");
johnAlexander 0:ce8359133ae6 78 // operating_mode=range_continuous_polling;
johnAlexander 0:ce8359133ae6 79 operating_mode=range_single_shot_polling;
johnAlexander 0:ce8359133ae6 80 /* start the measure on sensor top */
johnAlexander 0:ce8359133ae6 81 status=board->sensor_centre->StartMeasurement(operating_mode, SensorTopIRQ);
johnAlexander 0:ce8359133ae6 82 if(!status)
johnAlexander 0:ce8359133ae6 83 {
johnAlexander 0:ce8359133ae6 84 while(1)
johnAlexander 0:ce8359133ae6 85 {
johnAlexander 0:ce8359133ae6 86 status=board->sensor_centre->GetMeasurement(operating_mode, &data_sensor_centre);
johnAlexander 0:ce8359133ae6 87 DisplayRefresh(operating_mode);
johnAlexander 0:ce8359133ae6 88 if(int_stop_measure) // Blue Button isr was triggered
johnAlexander 0:ce8359133ae6 89 {
johnAlexander 0:ce8359133ae6 90 status=board->sensor_centre->StopMeasurement(operating_mode); // stop the measure and exit
johnAlexander 0:ce8359133ae6 91 int_stop_measure = false;
johnAlexander 0:ce8359133ae6 92 printf("\nProgram stopped!\n\n\r");
johnAlexander 0:ce8359133ae6 93 break;
johnAlexander 0:ce8359133ae6 94 }
johnAlexander 0:ce8359133ae6 95 }
johnAlexander 0:ce8359133ae6 96 }
johnAlexander 0:ce8359133ae6 97 board->display->DisplayString("BYE");
johnAlexander 0:ce8359133ae6 98 delete board;
johnAlexander 0:ce8359133ae6 99 }
johnAlexander 0:ce8359133ae6 100
johnAlexander 0:ce8359133ae6 101 /*=================================== Main ==================================
johnAlexander 0:ce8359133ae6 102 Press the blue user button to stop the measurements in progress
johnAlexander 0:ce8359133ae6 103 =============================================================================*/
johnAlexander 0:ce8359133ae6 104 int main()
johnAlexander 0:ce8359133ae6 105 {
johnAlexander 0:ce8359133ae6 106 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401
johnAlexander 0:ce8359133ae6 107 InterruptIn stop_button (USER_BUTTON);
johnAlexander 0:ce8359133ae6 108 stop_button.rise (&StopMeasureIRQ);
johnAlexander 0:ce8359133ae6 109 #endif
johnAlexander 0:ce8359133ae6 110 DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
johnAlexander 0:ce8359133ae6 111
johnAlexander 1:3483e701ec59 112 RangeMeasure(device_i2c); // start continuous measures
johnAlexander 0:ce8359133ae6 113 }
johnAlexander 0:ce8359133ae6 114
johnAlexander 0:ce8359133ae6 115
johnAlexander 0:ce8359133ae6 116