ST Expansion SW Team / Mbed OS VL6180_MB63_1sensor_singleshot_poll

Dependencies:   X_NUCLEO_6180

main.cpp

Committer:
charlesmn
Date:
2021-05-03
Revision:
1:43a9bc2f05b5
Parent:
0:d96995e87b29

File content as of revision 1:43a9bc2f05b5:

/**
  ******************************************************************************
  * File Name          : main.c
  * Date               : 29/10/2020
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2020 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
    This program controls a VL6180 ToF sensor running on an STM32F401 card with the
  VL6180 shield.  This program works in single shot mode where the program polls for
  the measurement finishing. This allows for the display to be updated while the
  measurement occurs.
  
  The display is very crude with no storing of the last digits, each digit is written
  and then a wait occurs and then the next digit is written. This means that a lot of time is 
  taken writing the display and any long period when the display is not serviced 
  will cause the display to flicker.
  
  In this program access to the VL6180 api is through wrapper functions in
  vl6180_class.cpp. It is also possible to access the api directly. E.G both the lines below
  do the same thing. 
  
status = VL6180_ClearInterrupt(dev,INTERRUPT_CLEAR_ERROR|INTERRUPT_CLEAR_RANGING);
status = sensor->vl6180_ClearInterrupt(dev,INTERRUPT_CLEAR_ERROR|INTERRUPT_CLEAR_RANGING);
  */
  

/* Includes ------------------------------------------------------------------*/
//#include "stm32xxx_hal.h"

/* Private variables ---------------------------------------------------------*/

#include <stdio.h>

#include "mbed.h"
#include "XNucleo6810.h"
#include <time.h>

#include "spi_interface.h"


//VL6180_SINGLE_DEVICE_DRIVER


#define VL6180_I2C_SDA   D14 
#define VL6180_I2C_SCL   D15 

/* USER CODE BEGIN 0 */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "vl6180_api.h"
#include "6180a1.h"

#ifdef DEBUG
    //TODO
#include "diag/trace.h"
    #define debug(msg, ...)   trace_printf(msg,__VA_ARGS__)
    #define trace_warn(msg,...) trace_printf("W %s %d" msg "\n", __func__, __LINE__, __VA_ARGS__)
#else
    #define debug(msg, ...)  (void)0
#endif

#define DigitDisplay_ms     1 /* ms each digit is kept on */
#if VL6180_HAVE_DMAX_RANGING
#define DMaxDispTime     0 /* Set to 1000 to display Dmax during 1 sec when no target is detected */
#else
#define DMaxDispTime     0
#endif
    

MyVL6180Dev_t                   devCentre;  //data  for each of the vl6180
MyVL6180Dev_t                   devLeft;
MyVL6180Dev_t                   devRight;
MyVL6180Dev_t                   devBottom;
VL6180Dev_t                     Dev = &devCentre; // the currently used vl6180

volatile int IntrFired=0;

VL6180 *Sensor;  

static XNucleo53L1A1 *board=NULL;

// MBed V6.4 has renamed wait_ms and UnbufferedSerial replaces Serial
#if (MBED_VERSION  > 60300) 
UnbufferedSerial  pc(SERIAL_TX, SERIAL_RX); 
extern "C" void wait_ms(int ms);
#else
Serial pc(SERIAL_TX, SERIAL_RX); 
#endif

vl6180_DevI2C *dev_I2C = new vl6180_DevI2C(VL6180_I2C_SDA, VL6180_I2C_SCL);

#define theVL6180Dev   0x52    // what we use as "API device

#define i2c_bus      (&hi2c1)
#define def_i2c_time_out 100

// Define interrupt pins
// The interrupt pins depend on solder blobs on the back of the shield.
// Each interupt can have two possible pins, this allows all interrupts to be on the same gpio or different gpio
// The first values are those an unmodified board has
// see ST document UM2657 for more details
PinName CentreIntPin = A3;
PinName LeftIntPin = D13;
PinName RightIntPin = D2;
PinName BottomIntPin = A2;
// alternate set
//PinName CentreIntPin = A5;
//PinName LeftIntPin = D8;
//PinName RightIntPin = D4;
//PinName BottomIntPin = A4;



static char DISP_CurString[10]; // the strin g to be displayed on the screen

VL6180_RangeData_t Range;  /* Range measurmeent  */
uint16_t range;             /* range average distance */

int main(void)
{
    int status;
        
    pc.baud(115200);  // baud rate is important as printf statements take a lot of time
    devCentre.i2c_addr = theVL6180Dev;  // set to default adress for now
    printf("vl6180_ss_and_poll_ranging mbed = %d \r\n",MBED_VERSION);
    
    // create instances for the sensors
    board = XNucleo53L1A1::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin ,BottomIntPin);  
    
        // find the sensors we have and initialise
    status = board->init_board();
    if (status) {
        printf("Failed to init board!\r\n");
        return 0;
    }
    
    //select centre sensor
    devCentre.i2c_addr = NEW_SENSOR_CENTRE_ADDRESS;
    Dev = &devCentre;
    Sensor=board->sensor_centre;
    if ( board->sensor_centre ==NULL)
        printf("board->sensor_centre = Null\n");

    /* Note that as we waited  1msec we could bypass VL6180_WaitDeviceBooted(&Dev); */
    Sensor->vl6180_WaitDeviceBooted(Dev);
    Sensor->vl6180_InitData(Dev);

    printf("started interrupt centre %d\n",status);
    
    /* Enable Dmax calculation only if value is displayed (to save computation power) */
    Sensor->vl6180_DMaxSetState(Dev, DMaxDispTime>0);
    status = Sensor->vl6180_Prepare(Dev);
    
    VL6180_ClearInterrupt(Dev, INTERRUPT_CLEAR_RANGING);

    /* kick off the first measurement */
    status = Sensor->vl6180_RangeStartSingleShot(Dev);
    
    printf("start loop \n");
    /* Infinite loop */
    while (1) {
        status = Sensor->vl6180_RangeGetMeasurementIfReady(Dev, &Range);
        if( status == 0 ){         
            // Application must check Range.errorStatus before accessing the other data
            //    If Range.errorStatus is DataNotReady, application knows that it has to wait a bit before getting a new data
            //    If Range.errorStatus is 0, application knows it is a valid distance
            //    If Range.errorStatus is not 0, application knows that reported distance is invalid so may take some decisions depending on the errorStatus
            if (Range.errorStatus == DataNotReady)
                continue;
            
           if((Range.errorStatus == 0) && (status == 0))
           {
      //           printf("range = %d\n",Range.range_mm); //output result to serial port
                 sprintf(DISP_CurString, " %d", (int)Range.range_mm);
           } 
           else
           {
                 sprintf(DISP_CurString, " %d", 0);
           }               
                
            /* re-arm next measurement */ 
           status = Sensor->vl6180_RangeStartSingleShot(Dev);   
        }
        else{
            // it is an critical error
 //           HandleError("critical error on VL6180x_RangeCheckAndGetMeasurement");
        }
             
       
       // the display is very simple and requires written to frequently so
       // we are writing to the display when we would normally sleep.
       // when we are not writing to the display it is blank
       for (int i = 0 ;i < 20;i++)
       {
            XNUCLEO6180XA1_DisplayString(DISP_CurString, DigitDisplay_ms* 5);
       }

    }

}

 // wait_ms was removed in MBed V6.4          
#if (MBED_VERSION  > 60300) 
void wait_ms(int ms)
 {
    thread_sleep_for(ms);
 }
#endif
  


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/