Hugo Pristauz / Mbed 2 deprecated S16_Blue_ToF

Dependencies:   BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers xn6180.cpp Source File

xn6180.cpp

00001 // xn6180.cpp - X-NUCLEO 6180XA1 expansion board functionality
00002 //
00003 // This VL6180X Expansion board test application performs a range measurement and an als measurement in interrupt mode
00004 // on the onboard embedded top sensor. 
00005 // The board red slider select on the flight the measurement type as ALS or RANGE; the measured data is diplayed on the 
00006 // on board 4digits display.
00007 //
00008 // User Blue button allows to stop current measurement and the entire program 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 // Callback IRQ functions are used only for measure that require interrupt
00013 //
00014 // GetMeasurement is asynchronous! It returns NOT_READY if the measurement value 
00015 // is not ready to be read from the corresponding register. So you need to wait
00016 // for the result to be ready
00017 //
00018 
00019 #include "mbed.h"
00020 #include "shields/xn6180.h"
00021 
00022 #define VL6180X_I2C_SDA   D14 
00023 #define VL6180X_I2C_SCL   D15 
00024 
00025 #define RANGE   0
00026 #define ALS     1
00027 
00028 #define DELAY 2000  // 2Sec
00029    
00030 // static DisplayShield *pShield = 0;    // used by IRQ callback to access data
00031 
00032    void DisplayShield::init(const char *msg, int msec)
00033    {
00034       if (msg)
00035          display(msg,msec);         // display "INI" for 1 second
00036    
00037          // now init the 6180XA1 expansion board with default values
00038       
00039       int err = pBoard->InitBoard();
00040       if (err)
00041          printf("Failed to init board!\n\r");
00042                
00043          // read the red slider position for ALS/Range measure
00044          
00045       slider(IntMeasure);              // update curMode due to slider position
00046       clear();                         // clear service handling request flag    
00047    }
00048 
00049    
00050    DisplayShield::DisplayShield(const char *msg, int msec)
00051    {
00052          // first we need an I2C device
00053          
00054       pDevice = new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL);     
00055       
00056          // next we create the 6180XA1 expansion board singleton obj 
00057          // after that we are already able to display the init message
00058       
00059 //    pBoard = X_NUCLEO_6180XA1::Instance(pDevice, A3, A2, D13, D2);
00060       pBoard = X_NUCLEO_6180XA1::Instance(pDevice, NC, NC, NC,  NC);
00061 
00062       init(msg,msec);                    // initialize shield
00063    }
00064 
00065 //==============================================================================
00066 // Displaying Messages or Values
00067 //==============================================================================
00068 
00069   
00070    void DisplayShield::display(const char * msg)  // display max 4 digits
00071    {
00072       char buf[5];
00073       int nmax = sizeof(buf) - 1;
00074       int len = strlen(msg);
00075 
00076       memset(buf,0,sizeof(buf));         // clear buffer, provide terminator
00077       for (int j=0; j < nmax; j++)
00078       {  if (msg[j] == 0)
00079             break;
00080          buf[j] = msg[j];
00081       }
00082 
00083       pBoard->display->DisplayString(buf, strlen(buf));
00084    }
00085 
00086    
00087    void DisplayShield::display(const char * msg, int msec)  // display & wait
00088    {
00089       Timer timer;
00090    
00091       timer.start();
00092       for(int i=0; i < msec; i = timer.read_ms())
00093          display(msg);
00094       timer.stop();
00095    }
00096 
00097 
00098 // On board 4 digit local display refresh
00099 
00100    void DisplayShield::refresh(OpMode mode)
00101    {   
00102       char str[5];
00103    
00104       if(mode == range_continuous_interrupt || mode == range_continuous_polling)
00105       {
00106          if (data.range_mm != 0xFFFFFFFF)
00107          {
00108             sprintf(str,"%d",data.range_mm);
00109          }
00110          else
00111          {
00112             sprintf(str,"%s","----");
00113          }
00114       }
00115       else if(mode == als_continuous_interrupt || mode == als_continuous_polling)
00116       {
00117          if(data.lux != 0xFFFFFFFF)
00118          {
00119             sprintf(str,"%d",data.lux);
00120          }
00121          else
00122          {
00123             sprintf(str,"%s","----");
00124          }
00125       }
00126       pBoard->display->DisplayString(str, strlen(str));       
00127    }
00128 
00129    void DisplayShield::refresh()       // refresh display in current mode
00130    {
00131        refresh(curMode);
00132    }
00133 
00134 
00135 //==============================================================================
00136 // IRQ Functionality
00137 //==============================================================================
00138 
00139           
00140    void DisplayShield::set()           // set data read service request
00141    {
00142       flagService = true;
00143    }
00144    
00145    void DisplayShield::clear()         // clear data read service request
00146    {
00147       flagService = false;
00148    }
00149    
00150    bool DisplayShield::request()       // return data read service request
00151    {
00152       return flagService;
00153    }
00154 /*
00155    static void cbDone(void)            // measurement done callback
00156    {
00157       if (pShield)
00158       {
00159           pShield->set();              // set service request
00160           pShield->disable();          // disable further interrupts
00161           pShield = 0;                 // free-up shield pointer, no more in use
00162       }
00163    } 
00164 */
00165 
00166    int DisplayShield::handle()         // handle ISR and read the measurement
00167    {
00168        clear();                        // clear data read service request
00169        return pBoard->sensor_top->HandleIRQ(curMode, &data); 
00170    }
00171 
00172    
00173    void DisplayShield::disable()       // disable interrupt measure detection
00174    {
00175       pBoard->sensor_top->DisableInterruptMeasureDetectionIRQ();
00176    }
00177 
00178 
00179    void DisplayShield::ready()         // set data ready status 
00180    {
00181        disable();                      // disable interrupts
00182        set();                          // set data service request flag
00183    }
00184 
00185    
00186    int DisplayShield::start(void (*callback)(void))
00187    {
00188       int err;                         // error code
00189       
00190 //    if (pShield != 0)
00191 //       return 0x99;                  // pShield is already booked!
00192 //
00193 //    pShield = this;                  // make access available to cbDone()         
00194       err = pBoard->sensor_top->StartMeasurement(curMode, callback, NULL, NULL); 
00195 
00196       if (err == 0)                    // if no errors occured
00197       {
00198          prvMode = curMode;
00199          startMessage();               // then report about successful start
00200       }
00201         
00202       return err;
00203    }
00204 
00205 
00206    int DisplayShield::stop()
00207    {
00208       int err;                         // error code
00209 
00210 //    pShield = 0;                     // free-up pShield pointer  
00211       err = pBoard->sensor_top->StopMeasurement(prvMode); // stop measurement
00212 
00213       if (err == 0)                    // if no errors occured
00214          stopMessage();                // then report about successful stop
00215          
00216       return err;                      // return error code
00217    }
00218 
00219 
00220 //==============================================================================
00221 // Examine Red Slider Position
00222 //==============================================================================
00223    
00224    bool DisplayShield::red()
00225    {
00226       return (pBoard->RdSwitch() == RANGE);
00227    }
00228    
00229    bool DisplayShield::slider(enum OpModeIntPoll_t mode)
00230    {
00231       int measure = pBoard->RdSwitch();
00232 
00233       switch (mode)
00234       {
00235          case PollMeasure:
00236             if (measure == RANGE)
00237                curMode = range_continuous_polling;
00238             else if(measure==ALS)
00239                curMode = als_continuous_polling;           
00240             break;
00241     
00242          case IntMeasure:
00243             if(measure == RANGE)
00244                curMode = range_continuous_interrupt;
00245             else if(measure == ALS)
00246                curMode = als_continuous_interrupt;     
00247             break;
00248       }
00249       
00250       return (curMode != prvMode);     // slider position changed?
00251    }
00252 
00253    bool DisplayShield::setup(OpMode mode)
00254    {
00255       switch (mode)
00256       {
00257          case range_continuous_polling:
00258          case als_continuous_polling:           
00259          case range_continuous_interrupt:
00260          case als_continuous_interrupt:
00261             curMode = mode;
00262             prvMode = mode;
00263             return 0;                  // no errors
00264       }
00265       return 1;                        // return with error code 1      
00266    }
00267 
00268 
00269 //==============================================================================
00270 // Trace Message Printing
00271 //==============================================================================
00272 
00273    void DisplayShield::startMessage(OpMode mode)
00274    {
00275       if (mode == range_continuous_interrupt)
00276          printf("\nStarted range continuous interrupt measure\n\r");
00277       else if(prvMode == als_continuous_interrupt)
00278          printf("\nStarted als continuous interrupt measure\n\r");
00279    }
00280 
00281    void DisplayShield::startMessage()
00282    {
00283        startMessage(curMode);
00284    }
00285 
00286    void DisplayShield::stopMessage(OpMode mode)
00287    {
00288       if (mode == range_continuous_interrupt)
00289          printf("Stopped range continuous interrupt measure\n\r");
00290       else if(prvMode == als_continuous_interrupt)
00291          printf("Stopped als continuous interrupt measure\n\r");
00292    }
00293 
00294    void DisplayShield::stopMessage()
00295    {
00296        stopMessage(prvMode);
00297    }
00298 
00299 // eof