John Alexander / Mbed 2 deprecated Display_53L0A1

Dependencies:   X_NUCLEO_53L0A1 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "x_nucleo_53l0a1.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 polling mode
00009    on the onboard embedded top sensor. 
00010    The measured data is displayed on the on-board 4-digit display.
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 /* Polling operating modes don`t require callback function that handles IRQ 
00016    Callback IRQ functions are used only for measure that require interrupt */
00017 
00018 /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value 
00019    is not ready to be read from the corresponding register. So you need to wait
00020    for the result to be ready */
00021 
00022 #define VL53L0_I2C_SDA   D14 
00023 #define VL53L0_I2C_SCL   D15 
00024 
00025 #define RANGE   0
00026 
00027 static X_NUCLEO_53L0A1 *board=NULL;
00028 VL53L0X_RangingMeasurementData_t data_sensor_centre;
00029 OperatingMode operating_mode;
00030     
00031 /* flags that handle interrupt request */
00032 bool int_sensor_centre=false, int_stop_measure=false;   
00033 
00034 /* ISR callback function of the sensor_centre */
00035 void SensorTopIRQ(void)
00036 {
00037    int_sensor_centre=true;
00038    board->sensor_centre->DisableInterruptMeasureDetectionIRQ();
00039 }   
00040 
00041 /* ISR callback function of the user blue button to stop program */
00042 void StopMeasureIRQ(void)
00043 {
00044    int_stop_measure=true;
00045 }
00046 
00047 /* On board 4 digit local display refresh */
00048 void DisplayRefresh(OperatingMode op_mode)
00049 {   
00050    char str[5];
00051    
00052    if (op_mode==range_single_shot_polling || op_mode==range_continuous_interrupt || op_mode==range_continuous_polling)
00053    {
00054       if (data_sensor_centre.RangeStatus == 0) // we have a valid range.
00055       {
00056          sprintf(str,"%d",data_sensor_centre.RangeMilliMeter);
00057       }
00058       else
00059       {
00060          sprintf(str,"%s","----");
00061       }
00062    }
00063      
00064    board->display->DisplayString(str);
00065 }
00066 
00067 void RangeMeasure(DevI2C *device_i2c) {
00068    int status;
00069 
00070    /* creates the 53L0A1 expansion board singleton obj */
00071    board=X_NUCLEO_53L0A1::Instance(device_i2c, A2, D8, D2);
00072     
00073    board->display->DisplayString("53L0");
00074    /* init the 53L0A1 expansion board with default values */
00075    status=board->InitBoard();
00076    if(status)
00077       printf("Failed to init board!\n\r");   
00078    operating_mode=range_single_shot_polling;
00079    /* start the measure on sensor top */
00080    status=board->sensor_centre->StartMeasurement(operating_mode, SensorTopIRQ);
00081    if(!status)
00082    {
00083       while(1)
00084       {
00085          status=board->sensor_centre->GetMeasurement(operating_mode, &data_sensor_centre);
00086          DisplayRefresh(operating_mode);
00087          if(int_stop_measure) // Blue Button isr was triggered
00088          {
00089             status=board->sensor_centre->StopMeasurement(operating_mode); // stop the measure and exit
00090             int_stop_measure = false;
00091             printf("\nProgram stopped!\n\n\r");
00092             break;
00093          }
00094       }
00095    }
00096    board->display->DisplayString("BYE");
00097    delete board;        
00098 }    
00099 
00100 /*=================================== Main ==================================
00101  Press the blue user button to stop the measurements in progress    
00102 =============================================================================*/
00103 int main()
00104 {   
00105 #if USER_BUTTON==PC_13  // we are cross compiling for Nucleo-f401 
00106    InterruptIn stop_button (USER_BUTTON);
00107    stop_button.rise (&StopMeasureIRQ);  
00108 #endif   
00109    DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);     
00110         
00111    RangeMeasure(device_i2c);  // start continuous measures
00112 }
00113