David Siorpaes / Mbed 2 deprecated HelloWorld_6180XA1

Dependencies:   X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

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_6180xa1.h"
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <stdio.h>
00006 #include <assert.h>
00007 
00008 /* This VL6180X Expansion board test application performs a range measurement and an als measurement in interrupt mode
00009    on the onboard embedded top sensor. 
00010    The board red slider select on the flight the measurement type as ALS or RANGE; the measured data is diplayed on the 
00011    on bord 4digits display.
00012 
00013    User Blue button allows to stop current measurement and the entire program releasing all the resources.
00014    Reset button is used to restart the program. */
00015 
00016 /* Polling operating modes don`t require callback function that handles IRQ 
00017    Callback IRQ functions are used only for measure that require interrupt */
00018 
00019 /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value 
00020    is not ready to be read from the corresponding register. So you need to wait
00021    for the result to be ready */
00022 
00023 #define VL6180X_I2C_SDA   D14 
00024 #define VL6180X_I2C_SCL   D15 
00025 
00026 #define RANGE   0
00027 #define ALS     1
00028 
00029 static X_NUCLEO_6180XA1 *board=NULL;
00030 MeasureData_t data_sensor_top;
00031 OperatingMode operating_mode, prev_operating_mode;
00032     
00033 /* flags that handle interrupt request */
00034 bool int_sensor_top=false, int_stop_measure=false;  
00035 
00036 /* ISR callback function of the sensor_top */
00037 void SensorTopIRQ(void)
00038 {
00039    int_sensor_top=true;
00040    board->sensor_top->DisableInterruptMeasureDetectionIRQ();
00041 }   
00042 
00043 /* ISR callback function of the user blue button to stop program */
00044 void StopMeasureIRQ(void)
00045 {
00046    int_stop_measure=true;
00047 }
00048 
00049 /* On board 4 digit local display refresh */
00050 void DisplayRefresh(OperatingMode op_mode)
00051 {   
00052    char str[5];
00053    
00054    if(op_mode==range_continuous_interrupt || op_mode==range_continuous_polling)
00055    {
00056       if(data_sensor_top.range_mm!=0xFFFFFFFF)
00057       {
00058          sprintf(str,"%d",data_sensor_top.range_mm);
00059       }
00060       else
00061       {
00062          sprintf(str,"%s","----");
00063       }
00064    }
00065    else if(op_mode==als_continuous_interrupt || op_mode==als_continuous_polling)
00066    {
00067       if(data_sensor_top.lux!=0xFFFFFFFF)
00068       {
00069          sprintf(str,"%d",data_sensor_top.lux);
00070       }
00071       else
00072       {
00073          sprintf(str,"%s","----");
00074       }
00075    }
00076    board->display->DisplayString(str, strlen(str));       
00077 }
00078 
00079 /* On board red slider position check */
00080 enum OpModeIntPoll_t{ PollMeasure, IntMeasure };
00081 
00082 OperatingMode CheckSlider(enum OpModeIntPoll_t OpMode) {
00083     
00084 OperatingMode ret;
00085 int measure= board->RdSwitch();
00086 
00087    switch (OpMode) {
00088     case PollMeasure:
00089       if(measure==RANGE)
00090         ret = range_continuous_polling;
00091       else if(measure==ALS)
00092         ret = als_continuous_polling;           
00093     break;
00094     
00095     case IntMeasure:
00096       if(measure==RANGE)
00097         ret = range_continuous_interrupt;
00098       else if(measure==ALS)
00099         ret = als_continuous_interrupt;     
00100     break;
00101    }
00102      return ret;      
00103 }
00104 
00105 /* Print on USB Serial the started OperatingMode */
00106 void PrintStartMessage(OperatingMode op_mode)
00107 {
00108    if(op_mode==range_continuous_interrupt)
00109       printf("\nStarted range continuous interrupt measure\n\r");
00110    else if(prev_operating_mode==als_continuous_interrupt)
00111       printf("\nStarted als continuous interrupt measure\n\r");
00112 }
00113 
00114 /* Print on USB Serial the stopped OperatingMode */
00115 void PrintStopMessage(OperatingMode op_mode)
00116 {
00117    if(op_mode==range_continuous_interrupt)
00118       printf("Stopped range continuous interrupt measure\n\r");
00119    else if(prev_operating_mode==als_continuous_interrupt)
00120       printf("Stopped als continuous interrupt measure\n\r");
00121 }
00122 
00123 /* Print on board 4 Digit display the indicated message <= 4 char */
00124 #define DELAY 2000  // 2Sec
00125 void DisplayMsg(const char * msg)
00126 {
00127    Timer timer;
00128    char str[5];
00129    
00130    timer.start();
00131    for(int i=0; i<DELAY; i=timer.read_ms())
00132    {
00133       sprintf(str,"%s",msg);
00134       board->display->DisplayString(str, strlen(str));
00135    }
00136    timer.stop();
00137 }
00138 
00139 
00140 void IntContinousALSorRangeMeasure (DevI2C *device_i2c) {
00141    int status;   
00142    /* creates the 6180XA1 expansion board singleton obj */
00143    board=X_NUCLEO_6180XA1::Instance(device_i2c, A3, A2, D13, D2);
00144    DisplayMsg  ("INT");
00145    /* init the 6180XA1 expansion board with default values */
00146    status=board->InitBoard();
00147    if(status)
00148       printf("Failed to init board!\n\r");   
00149    /* check the red slider position for ALS/Range measure */
00150    operating_mode=CheckSlider(IntMeasure);   
00151    /* start the measure on sensor top */
00152    status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL);
00153    if(!status)
00154    {
00155       prev_operating_mode=operating_mode;
00156       PrintStartMessage(operating_mode);
00157       while(1)
00158       {
00159          if(int_sensor_top)  /* 6180 isr was triggered */
00160          {
00161             int_sensor_top=false;
00162             status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top); /* handle the isr and read the meaure */
00163             DisplayRefresh(operating_mode);
00164          }
00165          if(int_stop_measure) /* Blue Button isr was triggered */
00166          {
00167             status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the measure and exit */
00168             if(!status)
00169                PrintStopMessage(prev_operating_mode);
00170             int_stop_measure = false;
00171             printf("\nProgram stopped!\n\n\r");
00172             break;
00173          }
00174          operating_mode=CheckSlider(IntMeasure); /* check if red slider was moved */
00175          if(operating_mode!=prev_operating_mode)
00176          {
00177             DisplayRefresh(prev_operating_mode);
00178             status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the running measure */
00179             if(!status)
00180                PrintStopMessage(prev_operating_mode);
00181             prev_operating_mode=operating_mode;
00182             status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); /* start the new measure */
00183             if(!status)
00184                PrintStartMessage(operating_mode);
00185          } else
00186             DisplayRefresh(operating_mode);           
00187       }
00188    }
00189    DisplayMsg("BYE");
00190    delete board;        
00191 }    
00192 
00193 /*=================================== Main ==================================
00194  Move the VL6180X Expansion board red slider to switch between ALS or Range
00195  measures.
00196  Press the blue user button to stop the measurements in progress    
00197 =============================================================================*/
00198 int main()
00199 {   
00200 #if USER_BUTTON==PC_13  // we are cross compiling for Nucleo-f401 
00201    InterruptIn stop_button (USER_BUTTON);
00202    stop_button.rise (&StopMeasureIRQ);  
00203 #endif   
00204    DevI2C *device_i2c =new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL);     
00205         
00206    IntContinousALSorRangeMeasure (device_i2c);  // start continous measures Interrupt based
00207 }
00208