ST / Mbed 2 deprecated HelloWorld_6180XA1_AppExample

Dependencies:   X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1_AppExample by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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