INSAT Mini Project

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_6180XA1 by ST

Components/VL6180X/vl6180x_class.h

Committer:
gallonm
Date:
2015-10-06
Revision:
10:4954b09b72d8
Parent:
9:9cbc1736f148
Child:
11:88541229403e

File content as of revision 10:4954b09b72d8:

/**
 ******************************************************************************
 * @file    vl6180x_class.h
 * @author  AST / EST
 * @version V0.0.1
 * @date    14-April-2015
 * @brief   Header file for component VL6180X
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
 *
 * 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.
 *
 ******************************************************************************
*/

#ifndef __VL6180X_CLASS_H
#define __VL6180X_CLASS_H

/* Includes ------------------------------------------------------------------*/
#include "RangeSensor.h"
#include "LightSensor.h"
#include "DevI2C.h" 
#include "vl6180x_api.h"
#include "vl6180x_cfg.h"
#include "vl6180x_def.h"
#include "vl6180x_types.h"
#include "vl6180x_platform.h"
//#include "vl6180x_appcfg.h"
#include "STMPE1600_class.h"

/** default device address */
#define DEFAULT_DEVICE_ADDRESS		0x29

/* Classes -------------------------------------------------------------------*/
/** Class representing a VL6180X sensor component
 */
 //FIXME verficare se impostare le funzioni come virtual come nel caso IKS
class VL6180X : public RangeSensor, public LightSensor 
{
 public:
    /** Constructor
     * @param[in] i2c device I2C to be used for communication
     * @param[in] digital out pin/STMPE1600DigiOut pin to be used for GPIO expander communication
     * @param[in] device address, 0x29 by default 
     * @param[in] device id, 0 by default
     */
		 VL6180X(DevI2C &i2c, DigitalOut &pin, /*PinName pin_gpio1,*/ uint8_t DevAddr=DEFAULT_DEVICE_ADDRESS) : RangeSensor(), LightSensor(), dev_i2c(i2c), gpio0(&pin)
		 {
			  MyDevice.I2cAddr=DevAddr;		 
        MyDevice.Present=0;
        MyDevice.Ready=0;
        Device=&MyDevice;;
				expgpio0=NULL;
			  //free_fall=new InterruptIn(pin_gpio1);
		 }  
		
     VL6180X(DevI2C &i2c, STMPE1600DigiOut &pin, /*PinName pin_gpio1,*/ uint8_t DevAddr=DEFAULT_DEVICE_ADDRESS) : RangeSensor(), LightSensor(), dev_i2c(i2c), expgpio0(&pin)
		 {
			  MyDevice.I2cAddr=DevAddr;		 
        MyDevice.Present=0;
        MyDevice.Ready=0;
        Device=&MyDevice;
			  gpio0=NULL;			 
        //free_fall=new InterruptIn(pin_gpio1);			 
		 }  
		 
    
   /** Destructor
    */
    //~VL6180X(){} 
		 
		 
		 void VL6180x_On(void) {
			if (gpio0) *gpio0=1;
			else if (expgpio0) *expgpio0=1;
		} 
		
    void VL6180x_Off(void) {
			if (gpio0) *gpio0=0;
			else if (expgpio0) *expgpio0=0;			
		}
    
		int InitSensor(uint8_t NewAddr) 
		{
		   int status;
	  
       VL6180x_Off();
       VL6180x_On();
	     status=IsPresent();
	     if(!status)
	     {
		     Device->Present=1;
		     status=Init();
         if(status)
			      error("Failed to init VL6180X sensor!\n");
         status=Prepare();
         if(status)
			      error("Failed to prepare VL6180X!\n");
	       status=SetI2CAddress(NewAddr);
		     if(status)
			      error("Failed to change I2C address!\n");
		     Device->Ready=1;
	     }
			 return status;
		}
    
    int Init() 
    {
       return VL6180x_InitData(Device); // ritorna 0 se corretto 
    }
		
		int IsPresent()
		{
			 int status;
			
			 status=ReadID();
       if(!status)
				 error("Failed to read ID device! Device not present!\n");
			 return status;
		}
		
		void SetPresent()
		{
			 Device->Present=1;
		}
		
		void SetReady()
		{
			 Device->Ready=1;
		}
		
		int Prepare()
		{
			 return VL6180x_Prepare(Device);
		}
		
		int SetI2CAddress(int NewAddr)
		{
			 int status;
			
			 status=VL6180x_SetI2CAddress(Device, NewAddr);
			 if(!status)
				  Device->I2cAddr;
			 return status;
		}
		
		int StartMeasurement(int operating_mode)
		{
		  int status;
	
	    switch(operating_mode)
	    {
		     case(1):
			     return VL6180x_RangePollMeasurement(Device, &RangeData);
         case(2):
			     return VL6180x_AlsPollMeasurement(Device, &AlsData);
         case(3): break;		
		     case(4): break;
	       case(5): break;
	       case(6): break;
		     case(7): break;
		     default:
			     return INVALID_PARAMS;
	    }
	  }
    /*
    int GetRange(int *piData) {
        return VL6180X_RangeGetMeasurement(piData);
    }

    int GetLight(int *piData) {
        return VL6180X_AlsGetMeasurement(piData);
    }*/
		
		int RangePollMeasurement()
		{
			 return VL6180x_RangePollMeasurement(Device, &RangeData);
		}
		
		int AlsPollMeasurement()
		{
			 return VL6180x_AlsPollMeasurement(Device, &AlsData);
		}
		
		    
		/* api.c functions */
		int VL6180x_WaitDeviceBooted(VL6180xDev_t dev);
		int VL6180x_InitData(VL6180xDev_t dev );
		int VL6180x_SetupGPIO1(VL6180xDev_t dev, uint8_t IntFunction, int ActiveHigh);
		int VL6180x_Prepare(VL6180xDev_t dev);
		int VL6180x_RangeStartContinuousMode(VL6180xDev_t dev);
		int VL6180x_RangeStartSingleShot(VL6180xDev_t dev);
		int VL6180x_RangeSetMaxConvergenceTime(VL6180xDev_t dev, uint8_t  MaxConTime_msec);
		int VL6180x_RangePollMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
		int VL6180x_RangeGetMeasurementIfReady(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
		int VL6180x_RangeGetMeasurement(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
		int VL6180x_RangeGetResult(VL6180xDev_t dev, int32_t *pRange_mm);
		int VL6180x_RangeConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt);
		int VL6180x_RangeGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus);
		int VL6180x_AlsPollMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData);
		int VL6180x_AlsGetMeasurement(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData);
		int VL6180x_AlsConfigInterrupt(VL6180xDev_t dev, uint8_t ConfigGpioInt);
		int VL6180x_AlsSetIntegrationPeriod(VL6180xDev_t dev, uint16_t period_ms);
		int VL6180x_AlsSetInterMeasurementPeriod(VL6180xDev_t dev,  uint16_t intermeasurement_period_ms);
		int VL6180x_AlsSetAnalogueGain(VL6180xDev_t dev, uint8_t gain);
		int VL6180x_AlsSetThresholds(VL6180xDev_t dev, uint8_t low, uint8_t high);
		int VL6180x_AlsGetInterruptStatus(VL6180xDev_t dev, uint8_t *pIntStatus);
		int VL6180x_StaticInit(VL6180xDev_t dev);
		int VL6180x_RangeWaitDeviceReady(VL6180xDev_t dev, int MaxLoop );
		int VL6180x_RangeSetInterMeasPeriod(VL6180xDev_t dev, uint32_t  InterMeasTime_msec);
		int VL6180x_UpscaleSetScaling(VL6180xDev_t dev, uint8_t scaling);
		int VL6180x_UpscaleGetScaling(VL6180xDev_t dev);
		uint16_t VL6180x_GetUpperLimit(VL6180xDev_t dev);
		int VL6180x_RangeSetThresholds(VL6180xDev_t dev, uint16_t low, uint16_t high, int SafeHold);
		int VL6180x_RangeGetThresholds(VL6180xDev_t dev, uint16_t *low, uint16_t *high);
		int VL6180x_RangeSetRawThresholds(VL6180xDev_t dev, uint8_t low, uint8_t high);
		int VL6180x_RangeSetEceFactor(VL6180xDev_t dev, uint16_t  FactorM, uint16_t FactorD);
		int VL6180x_RangeSetEceState(VL6180xDev_t dev, int enable );
		int VL6180x_FilterSetState(VL6180xDev_t dev, int state);
		int VL6180x_FilterGetState(VL6180xDev_t dev);
		int VL6180x_DMaxSetState(VL6180xDev_t dev, int state);
		int VL6180x_DMaxGetState(VL6180xDev_t dev);
		int VL6180x_RangeSetSystemMode(VL6180xDev_t dev, uint8_t mode);
		int8_t VL6180x_GetOffsetCalibrationData(VL6180xDev_t dev);
		void VL6180x_SetOffsetCalibrationData(VL6180xDev_t dev, int8_t offset);
		int VL6180x_SetXTalkCompensationRate(VL6180xDev_t dev, FixPoint97_t Rate);
		int VL6180x_AlsWaitDeviceReady(VL6180xDev_t dev, int MaxLoop );
		int VL6180x_AlsSetSystemMode(VL6180xDev_t dev, uint8_t mode); 
		int VL6180x_SetGroupParamHold(VL6180xDev_t dev, int Hold);
		int VL6180x_SetI2CAddress(VL6180xDev_t dev, uint8_t NewAddr);
		int VL6180x_SetupGPIOx(VL6180xDev_t dev, int pin, uint8_t IntFunction, int ActiveHigh);
		int VL6180x_SetGPIOxPolarity(VL6180xDev_t dev, int pin, int active_high);
		int VL6180x_SetGPIOxFunctionality(VL6180xDev_t dev, int pin, uint8_t functionality);
		int VL6180x_DisableGPIOxOut(VL6180xDev_t dev, int pin);
		int VL6180x_GetInterruptStatus(VL6180xDev_t dev, uint8_t *status);
		int VL6180x_ClearInterrupt(VL6180xDev_t dev, uint8_t IntClear );
		int VL6180x_RangeStaticInit(VL6180xDev_t dev); 
		int VL6180x_UpscaleRegInit(VL6180xDev_t dev);
		int VL6180x_UpscaleStaticInit(VL6180xDev_t dev); 
		int VL6180x_AlsGetLux(VL6180xDev_t dev, lux_t *pLux);
		int _UpscaleInitPatch0(VL6180xDev_t dev); 
		int VL6180x_RangeGetDeviceReady(VL6180xDev_t dev, int * Ready);
		int VL6180x_RangeSetEarlyConvergenceEestimateThreshold(VL6180xDev_t dev);
		int32_t _GetAveTotalTime(VL6180xDev_t dev); 
		int32_t _filter_Start(VL6180xDev_t dev, uint16_t m_trueRange_mm, uint16_t m_rawRange_mm, uint32_t m_rtnSignalRate, uint32_t m_rtnAmbientRate, uint16_t errorCode);
		int _filter_GetResult(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData);
		int _GetRateResult(VL6180xDev_t dev, VL6180x_RangeData_t *pRangeData); 
		int _DMax_InitData(VL6180xDev_t dev);
		
    /* Read function of the ID device */
    int ReadID();
    
    /* Write and read functions from I2C */
    int VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data);
    int VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data);
    int VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data);
    int VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data);
    int VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data);
    int VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data);
		int VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData);
    int VL6180x_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToWrite);
    int VL6180x_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToRead);

		

private:
    /* IO Device */
    DevI2C &dev_i2c;
    /* Digital out pin */
    DigitalOut *gpio0;
    /* GPIO expander */
    STMPE1600DigiOut *expgpio0;
    /* Input interrupt */
    //InterruptIn *free_fall;
    /* Device data */
    MyVL6180Dev_t MyDevice;
    VL6180xDev_t Device;  
    /* Device range data */
    VL6180x_RangeData_t RangeData;
    /* Device als data */
    VL6180x_AlsData_t AlsData;
};

#endif // __VL6180X_CLASS_H