Librairie adaptée au laboratoire 2

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_6180XA1 by ST

Files at this revision

API Documentation at this revision

Comitter:
gallonm
Date:
Thu Sep 17 10:41:21 2015 +0200
Parent:
3:454541a079f4
Child:
5:40d1bdb706e0
Commit message:
First compile of Init function and ReadID function

Changed in this revision

Components/Interfaces/GenericSensor.h Show annotated file Show diff for this revision Revisions of this file
Components/Interfaces/LightSensor.h Show annotated file Show diff for this revision Revisions of this file
Components/Interfaces/RangeSensor.h Show annotated file Show diff for this revision Revisions of this file
Components/VL6180X/vl6180x_class.cpp Show annotated file Show diff for this revision Revisions of this file
Components/VL6180X/vl6180x_class.h Show annotated file Show diff for this revision Revisions of this file
Components/VL6180X/vl6180x_def.h Show annotated file Show diff for this revision Revisions of this file
x_nucleo_6180xa1.h Show annotated file Show diff for this revision Revisions of this file
--- a/Components/Interfaces/GenericSensor.h	Fri Sep 11 14:39:00 2015 +0200
+++ b/Components/Interfaces/GenericSensor.h	Thu Sep 17 10:41:21 2015 +0200
@@ -54,14 +54,14 @@
      * @param[out]  ptr Pointer to device specific initalization structure
      * @return      0 in case of success, an error code otherwise
      */
-    virtual int Init(void *ptr) = 0;
+    virtual int Init() = 0;
 
     /**
      * @brief       Get ID of sensor
      * @param[out]  id Pointer to where to store the ID to
      * @return      0 in case of success, an error code otherwise
      */
-    virtual int ReadID(uint8_t *id) = 0;
+    virtual int ReadID() = 0;
 };
 
-#endif /* __GENERIC_SENSOR_CLASS_H */
\ No newline at end of file
+#endif /* __GENERIC_SENSOR_CLASS_H */
--- a/Components/Interfaces/LightSensor.h	Fri Sep 11 14:39:00 2015 +0200
+++ b/Components/Interfaces/LightSensor.h	Thu Sep 17 10:41:21 2015 +0200
@@ -54,7 +54,7 @@
      * @param[out]  piData Pointer to where to store light to
      * @return      0 in case of success, an error code otherwise
      */
-    virtual int GetLight(int *piData) = 0;
+    //virtual int GetLight(int *piData) = 0;
 };
 
-#endif /* __LIGHT_SENSOR_CLASS_H */
\ No newline at end of file
+#endif /* __LIGHT_SENSOR_CLASS_H */
--- a/Components/Interfaces/RangeSensor.h	Fri Sep 11 14:39:00 2015 +0200
+++ b/Components/Interfaces/RangeSensor.h	Thu Sep 17 10:41:21 2015 +0200
@@ -54,7 +54,7 @@
      * @param[out]  piData Pointer to where to store range to
      * @return      0 in case of success, an error code otherwise
      */
-    virtual int GetRange(int *piData) = 0;
+    //virtual int GetRange(int *piData) = 0;
 };
 
-#endif /* __RANGE_SENSOR_CLASS_H */
\ No newline at end of file
+#endif /* __RANGE_SENSOR_CLASS_H */
--- a/Components/VL6180X/vl6180x_class.cpp	Fri Sep 11 14:39:00 2015 +0200
+++ b/Components/VL6180X/vl6180x_class.cpp	Thu Sep 17 10:41:21 2015 +0200
@@ -38,8 +38,104 @@
 /* Includes ------------------------------------------------------------------*/
 #include "vl6180x_class.h"
 
+/* define for inizialization -------------------------------------------------*/
+
+#if VL6180x_UPSCALE_SUPPORT == 1
+    #define _GetUpscale(dev, ... )  1
+    #define _SetUpscale(...) -1
+    #define DEF_UPSCALE 1
+#elif VL6180x_UPSCALE_SUPPORT == 2
+    #define _GetUpscale(dev, ... )  2
+    #define _SetUpscale(...)
+    #define DEF_UPSCALE 2
+#elif  VL6180x_UPSCALE_SUPPORT == 3
+    #define _GetUpscale(dev, ... )  3
+    #define _SetUpscale(...)
+    #define DEF_UPSCALE 3
+#else
+    #define DEF_UPSCALE (-(VL6180x_UPSCALE_SUPPORT))
+    #define _GetUpscale(dev, ... ) VL6180xDevDataGet(dev, UpscaleFactor)
+    #define _SetUpscale(dev, Scaling ) VL6180xDevDataSet(dev, UpscaleFactor, Scaling)
+#endif
+
+
+#if VL6180x_SINGLE_DEVICE_DRIVER
+extern  struct VL6180xDevData_t SingleVL6180xDevData;
+#define VL6180xDevDataGet(dev, field) (SingleVL6180xDevData.field)
+#define VL6180xDevDataSet(dev, field, data) SingleVL6180xDevData.field=(data)
+#endif
+
+
+#if VL6180x_UPSCALE_SUPPORT == 1
+    #define _GetUpscale(dev, ... )  1
+    #define _SetUpscale(...) -1
+    #define DEF_UPSCALE 1
+#elif VL6180x_UPSCALE_SUPPORT == 2
+    #define _GetUpscale(dev, ... )  2
+    #define _SetUpscale(...)
+    #define DEF_UPSCALE 2
+#elif  VL6180x_UPSCALE_SUPPORT == 3
+    #define _GetUpscale(dev, ... )  3
+    #define _SetUpscale(...)
+    #define DEF_UPSCALE 3
+#else
+    #define DEF_UPSCALE (-(VL6180x_UPSCALE_SUPPORT))
+    #define _GetUpscale(dev, ... ) VL6180xDevDataGet(dev, UpscaleFactor)
+    #define _SetUpscale(dev, Scaling ) VL6180xDevDataSet(dev, UpscaleFactor, Scaling)
+#endif
+
+
+struct VL6180xDevData_t VL6180x_DEV_DATA_ATTR  SingleVL6180xDevData={
+        .EceFactorM        = DEF_ECE_FACTOR_M,
+        .EceFactorD        = DEF_ECE_FACTOR_D,
+#ifdef VL6180x_HAVE_UPSCALE_DATA
+        .UpscaleFactor     = DEF_UPSCALE,
+#endif
+#ifdef VL6180x_HAVE_ALS_DATA
+        .IntegrationPeriod = DEF_INT_PEFRIOD,
+        .AlsGainCode       = DEF_ALS_GAIN,
+        .AlsScaler         = DEF_ALS_SCALER,
+#endif
+#ifdef VL6180x_HAVE_DMAX_RANGING
+        .DMaxEnable =   DEF_DMAX_ENABLE,
+#endif
+};
+
+
 #define Fix7_2_KCPs(x) ((((uint32_t)(x))*1000)>>7)
 
+/* define for i2c configuration ----------------------------------------------*/
+
+#define I2C_BUFFER_CONFIG 1
+#define VL6180x_I2C_USER_VAR
+#define TEMP_BUF_SIZE	32
+
+#ifndef I2C_BUFFER_CONFIG
+#error "I2C_BUFFER_CONFIG not defined"
+/* TODO you must define value for  I2C_BUFFER_CONFIG in configuration or platform h */
+#endif
+
+
+#if I2C_BUFFER_CONFIG == 0
+    /* GLOBAL config buffer */
+    uint8_t i2c_global_buffer[VL6180x_MAX_I2C_XFER_SIZE];
+
+    #define DECL_I2C_BUFFER
+    #define VL6180x_GetI2cBuffer(dev, n_byte)  i2c_global_buffer
+
+#elif I2C_BUFFER_CONFIG == 1
+    /* ON STACK */
+    #define DECL_I2C_BUFFER  uint8_t LocBuffer[VL6180x_MAX_I2C_XFER_SIZE];
+    #define VL6180x_GetI2cBuffer(dev, n_byte)  LocBuffer
+#elif I2C_BUFFER_CONFIG == 2
+    /* user define buffer type declare DECL_I2C_BUFFER  as access  via VL6180x_GetI2cBuffer */
+    #define DECL_I2C_BUFFER
+#else
+#error "invalid I2C_BUFFER_CONFIG "
+#endif
+
+/* Initialization functions --------------------------------------------------*/
+
 int VL6180X::VL6180x_InitData(VL6180xDev_t dev)
 {
     int status, dmax_status ;
@@ -73,14 +169,14 @@
     do{
 
         /* backup offset initial value from nvm these must be done prior any over call that use offset */
-        status = VL6180x_RdByte(dev,SYSRANGE_PART_TO_PART_RANGE_OFFSET, (uint8_t*)&offset);
+        status = VL6180x_RdByte(MyDevice,SYSRANGE_PART_TO_PART_RANGE_OFFSET, (uint8_t*)&offset);
         if( status ){
             VL6180x_ErrLog("SYSRANGE_PART_TO_PART_RANGE_OFFSET rd fail");
             break;
         }
         VL6180xDevDataSet(dev, Part2PartOffsetNVM, offset);
 
-        status=VL6180x_RdDWord( dev, SYSRANGE_RANGE_IGNORE_THRESHOLD, &CalValue);
+        status=VL6180x_RdDWord(MyDevice, SYSRANGE_RANGE_IGNORE_THRESHOLD, &CalValue);
         if( status ){
             VL6180x_ErrLog("Part2PartAmbNVM rd fail");
             break;
@@ -90,7 +186,7 @@
         }
         VL6180xDevDataSet(dev, Part2PartAmbNVM, CalValue);
 
-        status = VL6180x_RdWord(dev, SYSRANGE_CROSSTALK_COMPENSATION_RATE ,&u16);
+        status = VL6180x_RdWord(MyDevice, SYSRANGE_CROSSTALK_COMPENSATION_RATE ,&u16);
         if( status){
             VL6180x_ErrLog("SYSRANGE_CROSSTALK_COMPENSATION_RATE rd fail ");
             break;
@@ -105,7 +201,7 @@
         }
 
         /* Read or wait for fresh out of reset  */
-        status = VL6180x_RdByte(dev,SYSTEM_FRESH_OUT_OF_RESET, &FreshOutReset);
+        status = VL6180x_RdByte(MyDevice,SYSTEM_FRESH_OUT_OF_RESET, &FreshOutReset);
         if( status )  {
             VL6180x_ErrLog("SYSTEM_FRESH_OUT_OF_RESET rd fail");
             break;
@@ -121,34 +217,73 @@
 }
 
 
-int VL6180X::VL6180XReadID(uint8_t *rl_id)
-{
-	  if(!rl_id)
-	  {
-	  	 return API_ERROR;
-	  }
-	  return VL6180X_IO_Read(rl_id, IDENTIFICATION_MODEL_ID, 1);
-}
+#define _DMaxData(field) VL6180xDevDataGet(dev, DMaxData.field)
+
+
+#ifndef VL6180x_PLATFORM_PROVIDE_SQRT
+
+uint32_t VL6180X::VL6180x_SqrtUint32(uint32_t num) {
+    uint32_t res = 0;
+    uint32_t bit = 1 << 30; /* The second-to-top bit is set: 1 << 30 for 32 bits */
+
+    /* "bit" starts at the highest power of four <= the argument. */
+    while (bit > num)
+        bit >>= 2;
 
-int VL6180X::VL6180X_IO_Read(uint8_t *pBuffer, uint8_t RegisterAddress, uint16 NumByteToRead)
-{
-    int lecture
-    
-    lecture=dev_i2c.i2c_read(pBuffer, DevAddress, RegisterAddress, NumByteToRead);
-    if(lecture!=0)
-    {
-    	  return API_ERROR;
+    while (bit != 0) {
+        if (num >= res + bit) {
+            num -= res + bit;
+            res = (res >> 1) + bit;
+        }
+        else
+            res >>= 1;
+        bit >>= 2;
     }
-    return API_NO_ERROR;
+    return res;
+}
+#endif
+
+
+void VL6180X::_DMax_OneTimeInit(VL6180xDev_t dev){
+    _DMaxData(ambTuningWindowFactor_K)=DEF_AMBIENT_TUNING;
 }
 
 
-
+uint32_t VL6180X::_DMax_RawValueAtRateKCps(VL6180xDev_t dev, int32_t rate){
+    uint32_t snrLimit_K;
+    int32_t DMaxSq;
+    uint32_t RawDMax;
+    DMaxFix_t retSignalAt400mm;
+    uint32_t ambTuningWindowFactor_K;
 
 
+    ambTuningWindowFactor_K = _DMaxData(ambTuningWindowFactor_K);
+    snrLimit_K              = _DMaxData(snrLimit_K);
+    retSignalAt400mm        = _DMaxData(retSignalAt400mm); /* 12 to 18 bits Kcps */
+    if( rate > 0 ){
+        DMaxSq = 400*400*1000 / rate -(400*400/330); /* K of (1/RtnAmb -1/330 )=> 30bit- (12-18)bit  => 12-18 bits*/
+        if( DMaxSq<= 0){
+            RawDMax = 0;
+        }
+        else{
+            /* value can be more 32 bit so base on raneg apply *retSignalAt400mm before or after division to presevr accuracy */
+            if( DMaxSq< (2<<12)  ){
+                DMaxSq = DMaxSq*retSignalAt400mm/(snrLimit_K+ambTuningWindowFactor_K);       /* max 12 + 12 to 18 -10 => 12-26 bit */
+            }else{
+                DMaxSq = DMaxSq/(snrLimit_K+ambTuningWindowFactor_K)*retSignalAt400mm;       /* 12 to 18 -10 + 12 to 18 *=> 12-26 bit */
+            }
+            RawDMax=VL6180x_SqrtUint32(DMaxSq);
+        }
+    }
+    else{
+        RawDMax = 0x7FFFFFFF; /* bigest possibmle 32bit signed value */
+    }
+    return RawDMax;
+}
 
 
-static int _DMax_InitData(VL6180xDev_t dev){
+int VL6180X::_DMax_InitData(VL6180xDev_t dev)
+{
     int status, warning;
     uint8_t u8;
     uint16_t u16;
@@ -167,7 +302,7 @@
 
     LOG_FUNCTION_START("");
     do{
-        status = VL6180x_RdByte(dev, 0x02A ,&u8);
+        status = VL6180x_RdByte(MyDevice, 0x02A ,&u8);
         if( status ){
             VL6180x_ErrLog("Reg 0x02A rd fail");
             break;
@@ -179,25 +314,25 @@
         }
         Reg2A_KCps = Fix7_2_KCPs(u8); /* convert to KCPs */
 
-        status = VL6180x_RdByte(dev, SYSRANGE_RANGE_CHECK_ENABLES, &SysRangeCheckEn);
+        status = VL6180x_RdByte(MyDevice, SYSRANGE_RANGE_CHECK_ENABLES, &SysRangeCheckEn);
         if (status) {
             VL6180x_ErrLog("SYSRANGE_RANGE_CHECK_ENABLES rd fail ");
             break;
         }
 
-        status = VL6180x_RdByte(dev, SYSRANGE_MAX_CONVERGENCE_TIME, &MaxConvTime);
+        status = VL6180x_RdByte(MyDevice, SYSRANGE_MAX_CONVERGENCE_TIME, &MaxConvTime);
         if( status){
             VL6180x_ErrLog("SYSRANGE_MAX_CONVERGENCE_TIME rd fail ");
             break;
         }
 
-        status = VL6180x_RdDWord(dev, 0x0B8, &RegB8);
+        status = VL6180x_RdDWord(MyDevice, 0x0B8, &RegB8);
         if( status ){
             VL6180x_ErrLog("reg 0x0B8 rd fail ");
             break;
         }
 
-        status = VL6180x_RdByte(dev, SYSRANGE_MAX_AMBIENT_LEVEL_MULT, &snrLimit);
+        status = VL6180x_RdByte(MyDevice, SYSRANGE_MAX_AMBIENT_LEVEL_MULT, &snrLimit);
         if( status){
             VL6180x_ErrLog("SYSRANGE_MAX_AMBIENT_LEVEL_MULT rd fail ");
             break;
@@ -214,7 +349,7 @@
 
         /* if xtalk range check is off omit it in snr clipping */
         if( SysRangeCheckEn&RANGE_CHECK_RANGE_ENABLE_MASK ){
-            status = VL6180x_RdWord(dev, SYSRANGE_RANGE_IGNORE_THRESHOLD, &u16);
+            status = VL6180x_RdWord(MyDevice, SYSRANGE_RANGE_IGNORE_THRESHOLD, &u16);
             if( status){
                 VL6180x_ErrLog("SYSRANGE_RANGE_IGNORE_THRESHOLD rd fail ");
                 break;
@@ -240,6 +375,160 @@
     return status;
 }
 
+
 #undef Fix7_2_KCPs
 
+/* Write and read functions from I2C -----------------------------------------*/
+
+int VL6180X::VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data)
+{
+    int  status;
+    DECL_I2C_BUFFER
+    VL6180x_I2C_USER_VAR
+
+    status=VL6180x_I2CWrite(dev, index, &data,(uint8_t)3);
+    return status;
+}
+
+int VL6180X::VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data)
+{
+    int  status;
+    DECL_I2C_BUFFER
+    VL6180x_I2C_USER_VAR
+
+    status=VL6180x_I2CWrite(dev, index, (uint8_t *)&data,(uint8_t)4);
+    return status;
+}
+
+int VL6180X::VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data)
+{
+    VL6180x_I2C_USER_VAR
+    DECL_I2C_BUFFER
+    int  status;
+
+	  status=VL6180x_I2CWrite(dev, index, (uint8_t *)&data,(uint8_t)6);
+    return status;
+}
+
+int VL6180X::VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data)
+{
+    VL6180x_I2C_USER_VAR
+    int  status;
+    uint8_t buffer;
+    DECL_I2C_BUFFER
+
+    status=VL6180x_I2CRead(dev, index, &buffer,1);
+    if( !status ){
+        *data=buffer;
+    }
+    return status;
+}
+
+int VL6180X::VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data)
+{
+    VL6180x_I2C_USER_VAR
+    int  status;
+    uint8_t *buffer;
+    DECL_I2C_BUFFER
+
+    status=VL6180x_I2CRead(dev, index, buffer,2);
+    if( !status ){
+       /* VL6180x register are Big endian if cpu is be direct read direct into *data is possible */
+       *data=((uint16_t)buffer[0]<<8)|(uint16_t)buffer[1];
+    }
+    return status;
+}
+
+int VL6180X::VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data)
+{
+    VL6180x_I2C_USER_VAR
+    int status;
+    uint8_t *buffer;
+    DECL_I2C_BUFFER
+
+    
+    status=VL6180x_I2CRead(dev, index, buffer,4);
+    if( !status ){
+       /* VL6180x register are Big endian if cpu is be direct read direct into data is possible */
+       *data=((uint32_t)buffer[0]<<24)|((uint32_t)buffer[1]<<16)|((uint32_t)buffer[2]<<8)|((uint32_t)buffer[3]);
+    }
+    return status;
+}
+
+int VL6180X::VL6180x_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToWrite)
+{
+    int ret;
+	  int i;
+    uint8_t tmp[TEMP_BUF_SIZE];
+    uint16_t myRegisterAddr = RegisterAddr;
+    uint8_t *array;
+    
+    if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
+        
+    /* First, send 8 bits device address and 16 bits register address in BE format. Then, send data and STOP condition */
+    tmp[0] = *(((uint8_t*)&myRegisterAddr)+1);  
+    tmp[1] = (uint8_t)RegisterAddr;
+    
+    if(NumByteToWrite>1)
+    {
+    	  array=new uint8_t[NumByteToWrite];
+    	  for(i=0;i<NumByteToWrite;i++)
+    	  {
+    	  	 array[NumByteToWrite-1-i]=pBuffer[i];
+    	  }
+    }
+    	  	 
+    memcpy(tmp+2, array, NumByteToWrite);
+ 
+    ret = dev_i2c.write(DeviceAddr, (const char*)tmp, NumByteToWrite+sizeof(RegisterAddr), false);
+ 
+    if(ret) return -1;
+    return 0;
+}
+
+int VL6180X::VL6180x_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
+{
+    int ret;
+    uint16_t myRegisterAddr = RegisterAddr;
+    uint16_t myRegisterAddrBE;
+        
+    myRegisterAddrBE = *(((uint8_t*)&myRegisterAddr)+1);
+    *(((uint8_t*)&myRegisterAddrBE)+1) = (uint8_t)myRegisterAddr;
+    
+    /* Send 8 bits device address and 16 bits register address in BE format, with no STOP condition */
+    ret = dev_i2c.write(DeviceAddr, (const char*)&myRegisterAddrBE, sizeof(RegisterAddr), true);
+    if(!ret) {
+        /* Read data, with STOP condition  */
+        ret = dev_i2c.read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
+    }
+    
+    if(ret) return -1;
+    return 0;
+} 
+
+/* IO read funcitons ---------------------------------------------------------*/
+
+int VL6180X::VL6180X_ReadID(uint8_t *rl_id)
+{
+	  if(!rl_id)
+	  {
+	  	 return API_ERROR; // DA DEFINIRE IL TIPO DI ERRORE!!
+	  }
+	  return VL6180X_IO_Read(rl_id, IDENTIFICATION_MODEL_ID, 1);
+}
+
+
+int VL6180X::VL6180X_IO_Read(uint8_t *pBuffer, uint8_t RegisterAddress, uint16_t NumByteToRead)
+{
+    int lecture;
+    
+    lecture=dev_i2c.i2c_read(pBuffer, MyDeviceAddress, RegisterAddress, NumByteToRead);
+    if(lecture!=0)
+    {
+    	  return INVALID_PARAMS; // DA DEFINIRE IL TIPO DI ERRORE!!
+    }
+    return API_NO_ERROR;  // DA DEFINIRE IL TIPO DI ERRORE!!
+}
+
+
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- a/Components/VL6180X/vl6180x_class.h	Fri Sep 11 14:39:00 2015 +0200
+++ b/Components/VL6180X/vl6180x_class.h	Thu Sep 17 10:41:21 2015 +0200
@@ -41,14 +41,43 @@
 /* Includes ------------------------------------------------------------------*/
 #include "RangeSensor.h"
 #include "LightSensor.h"
+#include "DevI2C.h" //temporaneo fino alla creazione della classe x_nucleo_6180xa1
 #include "vl6180x_api.h"
 #include "vl6180x_cfg.h"
 #include "vl6180x_def.h"
-#include "vl6180x_i2c.h"
 #include "vl6180x_types.h"
 #include "vl6180x_platform.h"
 #include "vl6180x_appcfg.h"
 
+/** default device address */
+#define DEFAULT_DEVICE_ADDRESS		0x29
+/** default value ECE factor Molecular */
+#define DEF_ECE_FACTOR_M    85
+/** default value ECE factor Denominator */
+#define DEF_ECE_FACTOR_D    100
+/** default value ALS integration time */
+#define DEF_INT_PEFRIOD     100
+/** default value ALS gain */
+#define DEF_ALS_GAIN        1
+/** default value ALS scaler */
+#define DEF_ALS_SCALER      1
+/** default value for DMAX Enbale */
+#define DEF_DMAX_ENABLE     1
+/** default ambient tuning factor %x1000 */
+#define DEF_AMBIENT_TUNING  80
+
+/*
+typedef struct
+{
+		struct VL6180xDevData_t Data;
+		uint8_t I2cAddr;
+		uint8_t DevID;
+	
+    unsigned Present;
+    unsigned Ready;
+}MyVL6180xDev_t;
+*/
+
 /* Classes -------------------------------------------------------------------*/
 /** Class representing a VL6180X sensor component
  */
@@ -56,21 +85,47 @@
  public:
     /** Constructor
      * @param[in] i2c device I2C to be used for communication
+     * @param[in] device address, 0x29 by default 
      */
-        VL6180X(DevI2C &i2c) : RangeSensor(), LightSensor(), dev_i2c(i2c) {}
+     VL6180X(DevI2C &i2c, uint8_t DevAddr=DEFAULT_DEVICE_ADDRESS) : RangeSensor(), LightSensor(), dev_i2c(i2c)
+     {
+        MyDeviceAddress=DevAddr;
+			  RangeLight_id=0;
+        //MyDevice.I2cAddr=DevAddr;
+        //MyDevice.DeviID=0;
+        //MyDevice.Present=0;
+        //MyDevice.Ready=0;
+        //oppure MyVL6180xDev_t MyDevice={DevAddr, 0, 0, 0};
+     }
     
     /** Destructor
      */
-        virtual ~VL6180X() {}
+    /*virtual ~VL6180X() 
+    {
+        // delete[] memory allocated
+        VL6180x_SetupGPIOx(MyDevice, 0, 0, 0);	
+    } */
+    
+    // funzione che setta il paremetro DevID: SetDevID(int n)
+    // funzione che setta il parametro Present: SetPresent(int n)
+    // funzione che setta il parametro Ready: SetReady(int n)
+    // funzione che modifica lo slave address: SetI2CAddress(VL6180xDev_t MyDevice, uint8_t NewAddress))
     
     /*** Interface Methods ***/
-    virtual int Init(void *init_struct) {
-        return VL6180x_InitData(VL6180xDev_t dev);
+    // nel momento in cui creo la classe x_nucleo_6180xa1, all'interno definisco un oggetto VL6180xDev_t MyDevice
+    // definisco una funzione Init() che chiama a sua volta la seguente funzione Init() e gli passo MyDevice
+    /*virtual*/ int Init() //par:void *init_struct
+    {
+        return VL6180x_InitData(MyDevice); // a questa funzione devo passare il parametro di Init()
     }
     
-    virtual int ReadID(uint8_t *rl_id) {
-        return VL6180X_ReadID(rl_id);
+    // nel momento in cui creo la classe x_nucleo_6180xa1, all'interno definisco un oggetto uint_t *RangeLight_id
+    // definisco una funzione ReadID() che chiama a sua volta la seguente funzione ReadID() e gli passo RangeLight_id
+    /*virtual*/ int ReadID() //par:uint8_t *rl_id
+    {
+        return VL6180X_ReadID(&RangeLight_id); // a questa funzione devo passare il parametro di ReadID()
     }
+    
     /*
     virtual int GetRange(int *piData) {
         return VL6180X_GetRange(piData);
@@ -81,18 +136,47 @@
     }
     */
 
+		/* Initialization functions */
+    int VL6180x_InitData(VL6180xDev_t dev);
+    /*static*/ int _DMax_InitData(VL6180xDev_t dev);
+    void _DMax_OneTimeInit(VL6180xDev_t dev);
+    /*static*/ uint32_t _DMax_RawValueAtRateKCps(VL6180xDev_t dev, int32_t rate);
+    uint32_t VL6180x_SqrtUint32(uint32_t num);
+    
+    /* IO read funcitons */
+    int VL6180X_ReadID(uint8_t *rl_id);
+    int VL6180X_IO_Read(uint8_t *pBuffer, uint8_t RegisterAddress, uint16_t NumByteToRead);
+    
+    /* 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_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:
     /*** Methods ***/
-    int VL6180X_IO_Read(uint8_t *pBuffer, uint8_t RegisterAddress, uint16 NumByteToRead);
+    
     
     
     
     /*** Instance Variables ***/
     /* IO Device */
+    //temporaneo
+    DevI2C &dev_i2c; // creo questa variabile in x_nucleo_6180xa1.h allo stesso modo e poi passo al costruttore del MyDevice il suo indirizzo
+    //MyVL6180xDev_t MyDevice; in  sostituzione di VL6180xDev_t sotto 
     
-    DevI2C &dev_i2c;
-    
+    /* VL6180X Device */
+    uint8_t MyDeviceAddress;
+    //temporanei
+    VL6180xDev_t MyDevice; // creo questa variabile in x_nucleo_6180xa1.h allo stesso modo e poi passo alla funzione Init()
+    uint8_t RangeLight_id; // creo questa variabile in x_nucleo_6180xa1.h allo stesso modo e poi passo alla funzione ReadID() il suo indirizzo
     
 };
 
-#endif // __VL6180X_CLASS_H
\ No newline at end of file
+#endif // __VL6180X_CLASS_H
--- a/Components/VL6180X/vl6180x_def.h	Fri Sep 11 14:39:00 2015 +0200
+++ b/Components/VL6180X/vl6180x_def.h	Thu Sep 17 10:41:21 2015 +0200
@@ -334,15 +334,15 @@
 /**
  * The device model ID
  */
-#define IDENTIFICATION_MODEL_ID                 0x000
+#define IDENTIFICATION_MODEL_ID                 (uint16_t) 0x000
 /**
  * Revision identifier of the Device for major change.
  */
-#define IDENTIFICATION_MODULE_REV_MAJOR         0x003
+#define IDENTIFICATION_MODULE_REV_MAJOR         (uint16_t) 0x003
 /**
  * Revision identifier of the Device for minor change.
  */
-#define IDENTIFICATION_MODULE_REV_MINOR         0x004
+#define IDENTIFICATION_MODULE_REV_MINOR         (uint16_t) 0x004
 
 
 /**
@@ -352,7 +352,7 @@
  *  Same definition as #SYSTEM_MODE_GPIO1
  * @ingroup device_regdef
  */
-#define SYSTEM_MODE_GPIO0                       0x010
+#define SYSTEM_MODE_GPIO0                       (uint16_t) 0x010
 /**
  * @def SYSTEM_MODE_GPIO1
  * @brief Configures polarity and select what als or ranging functionality gpio pin serves.
@@ -361,7 +361,7 @@
  * Same definition apply to register GPIO0 that is used as chip enable at power up.
  * @ingroup device_regdef
  */
-#define SYSTEM_MODE_GPIO1                       0x011
+#define SYSTEM_MODE_GPIO1                       (uint16_t) 0x011
     /** gpio pad POLARITY mask in #SYSTEM_MODE_GPIO1 (and/or 0) write  1  to set active high polarity (positive edge) */
     #define GPIOx_POLARITY_SELECT_MASK              0x20
     /** gpio pad Function select shift in #SYSTEM_MODE_GPIO1 or 0 */
@@ -397,7 +397,7 @@
  * \sa GPIO use for interrupt #SYSTEM_MODE_GPIO0 or #SYSTEM_MODE_GPIO1\n
  * @ingroup device_regdef
  */
-#define SYSTEM_INTERRUPT_CONFIG_GPIO           0x014
+#define SYSTEM_INTERRUPT_CONFIG_GPIO           (uint16_t) 0x014
     /** RANGE bits shift in #SYSTEM_INTERRUPT_CONFIG_GPIO */
     #define CONFIG_GPIO_RANGE_SHIFT            0
     /** RANGE bits mask in #SYSTEM_INTERRUPT_CONFIG_GPIO  (unshifted)*/
@@ -424,7 +424,7 @@
  *  Use or combination of any #INTERRUPT_CLEAR_RANGING , #INTERRUPT_CLEAR_ALS , #INTERRUPT_CLEAR_ERROR
  *  @ingroup device_regdef
  */
-#define SYSTEM_INTERRUPT_CLEAR                0x015
+#define SYSTEM_INTERRUPT_CLEAR                (uint16_t) 0x015
     /** clear ranging interrupt in write to #SYSTEM_INTERRUPT_CLEAR */
     #define INTERRUPT_CLEAR_RANGING                0x01
     /** clear als interrupt  in write to #SYSTEM_INTERRUPT_CLEAR */
@@ -433,7 +433,7 @@
     #define INTERRUPT_CLEAR_ERROR                  0x04
 
 /** After power up or reset this register will start reading 1 when device is ready */
-#define SYSTEM_FRESH_OUT_OF_RESET             0x016
+#define SYSTEM_FRESH_OUT_OF_RESET             (uint16_t) 0x016
 
 /**
  * @def SYSTEM_GROUPED_PARAMETER_HOLD
@@ -450,7 +450,7 @@
  * #SYSALS_THRESH_LOW
  * @ingroup device_regdef
  */
-#define SYSTEM_GROUPED_PARAMETER_HOLD         0x017
+#define SYSTEM_GROUPED_PARAMETER_HOLD         (uint16_t) 0x017
 
 
 /**
@@ -461,7 +461,7 @@
  *
  * @ingroup device_regdef
  */
-#define SYSRANGE_START                        0x018
+#define SYSRANGE_START                        (uint16_t) 0x018
     /** mask existing bit in #SYSRANGE_START*/
     #define MODE_MASK          0x03
     /** bit 0 in #SYSRANGE_START write 1 toggle state in continuous mode and arm next shot in single shot mode */
@@ -476,14 +476,14 @@
  * High level range  threshold (must be scaled)
  * @ingroup device_regdef
  */
-#define SYSRANGE_THRESH_HIGH                  0x019
+#define SYSRANGE_THRESH_HIGH                  (uint16_t) 0x019
 
 /**
  * @def SYSRANGE_THRESH_LOW
  * Low level range  threshold (must be scaled)
  * @ingroup device_regdef
  */
-#define SYSRANGE_THRESH_LOW                   0x01A
+#define SYSRANGE_THRESH_LOW                   (uint16_t) 0x01A
 
 /**
  * @def SYSRANGE_INTERMEASUREMENT_PERIOD
@@ -494,7 +494,7 @@
  *
  * @ingroup device_regdef
  */
-#define SYSRANGE_INTERMEASUREMENT_PERIOD      0x01B
+#define SYSRANGE_INTERMEASUREMENT_PERIOD      (uint16_t) 0x01B
 
 /**
  * @brief Maximum time to run measurement in Ranging modes.
@@ -506,33 +506,33 @@
  *
  * @ingroup device_regdef
  */
-#define SYSRANGE_MAX_CONVERGENCE_TIME         0x01C
+#define SYSRANGE_MAX_CONVERGENCE_TIME         (uint16_t) 0x01C
 /**@brief Cross talk compensation rate
  * @warning  never write register directly use @a VL6180x_SetXTalkCompensationRate()
  * refer to manual for calibration procedure and computation
  * @ingroup device_regdef
  */
-#define SYSRANGE_CROSSTALK_COMPENSATION_RATE  0x01E
+#define SYSRANGE_CROSSTALK_COMPENSATION_RATE  (uint16_t) 0x01E
 /**
  * @brief Minimum range value in mm to qualify for crosstalk compensation
  */
-#define SYSRANGE_CROSSTALK_VALID_HEIGHT       0x021
-#define SYSRANGE_EARLY_CONVERGENCE_ESTIMATE   0x022
-#define SYSRANGE_PART_TO_PART_RANGE_OFFSET    0x024
-#define SYSRANGE_RANGE_IGNORE_VALID_HEIGHT    0x025
-#define SYSRANGE_RANGE_IGNORE_THRESHOLD       0x026
-#define SYSRANGE_EMITTER_BLOCK_THRESHOLD      0x028
-#define SYSRANGE_MAX_AMBIENT_LEVEL_THRESH     0x02A
-#define SYSRANGE_MAX_AMBIENT_LEVEL_MULT       0x02C
+#define SYSRANGE_CROSSTALK_VALID_HEIGHT       (uint16_t) 0x021
+#define SYSRANGE_EARLY_CONVERGENCE_ESTIMATE   (uint16_t) 0x022
+#define SYSRANGE_PART_TO_PART_RANGE_OFFSET    (uint16_t) 0x024
+#define SYSRANGE_RANGE_IGNORE_VALID_HEIGHT    (uint16_t) 0x025
+#define SYSRANGE_RANGE_IGNORE_THRESHOLD       (uint16_t) 0x026
+#define SYSRANGE_EMITTER_BLOCK_THRESHOLD      (uint16_t) 0x028
+#define SYSRANGE_MAX_AMBIENT_LEVEL_THRESH     (uint16_t) 0x02A
+#define SYSRANGE_MAX_AMBIENT_LEVEL_MULT       (uint16_t) 0x02C
 /** @brief  various Enable check enabel register
  *  @a VL6180x_RangeSetEceState()
  */
-#define SYSRANGE_RANGE_CHECK_ENABLES          0x02D
+#define SYSRANGE_RANGE_CHECK_ENABLES          (uint16_t) 0x02D
     #define RANGE_CHECK_ECE_ENABLE_MASK      0x01
     #define RANGE_CHECK_RANGE_ENABLE_MASK    0x02
     #define RANGE_CHECK_SNR_ENABLKE          0x10
 
-#define SYSRANGE_VHV_RECALIBRATE              0x02E
+#define SYSRANGE_VHV_RECALIBRATE              (uint16_t) 0x02E
 #define SYSRANGE_VHV_REPEAT_RATE              0x031
 
 /**
@@ -541,26 +541,26 @@
  *
  * same bit definition as range \a #SYSRANGE_START \n
  */
-#define SYSALS_START                          0x038
+#define SYSALS_START                          (uint16_t) 0x038
 
 /** ALS low Threshold high */
-#define SYSALS_THRESH_HIGH                    0x03A
+#define SYSALS_THRESH_HIGH                    (uint16_t) 0x03A
 /** ALS low Threshold low */
-#define SYSALS_THRESH_LOW                     0x03C
+#define SYSALS_THRESH_LOW                     (uint16_t) 0x03C
 /** ALS intermeasurement period */
-#define SYSALS_INTERMEASUREMENT_PERIOD        0x03E
+#define SYSALS_INTERMEASUREMENT_PERIOD        (uint16_t) 0x03E
 /** 
  * @warning or value with 0x40 when writing to these register*/
-#define SYSALS_ANALOGUE_GAIN                  0x03F
+#define SYSALS_ANALOGUE_GAIN                  (uint16_t) 0x03F
 /** ALS integration period */
-#define SYSALS_INTEGRATION_PERIOD             0x040
+#define SYSALS_INTEGRATION_PERIOD             (uint16_t) 0x040
 
 /**
  * @brief Result range status
  *
  *  Hold the various range interrupt flags and error Specific error codes
  */
-#define RESULT_RANGE_STATUS                   0x04D
+#define RESULT_RANGE_STATUS                   (uint16_t) 0x04D
     /** Device ready for new command bit 0*/
     #define RANGE_DEVICE_READY_MASK       0x01
     /** mask for error status covers bits [7:4]  in #RESULT_RANGE_STATUS @a ::RangeError_u */
@@ -573,7 +573,7 @@
  * @brief Result  als status \n
  *  Hold the various als interrupt flags and Specific error codes
  */
-#define RESULT_ALS_STATUS                     0x4E
+#define RESULT_ALS_STATUS                     (uint16_t) 0x4E
     /** Device ready for new command bit 0*/
    #define ALS_DEVICE_READY_MASK       0x01
 
@@ -584,7 +584,7 @@
  * Lux value depends on Gain and integration settings and calibrated lux/count setting
  * \a VL6180x_AlsGetLux() \a VL6180x_AlsGetMeasurement()
  */
-#define RESULT_ALS_VAL                        0x50
+#define RESULT_ALS_VAL                        (uint16_t) 0x50
 
 /**
  * @def FW_ALS_RESULT_SCALER
@@ -592,7 +592,7 @@
  * these register content is cached by API in \a VL6180xDevData_t::AlsScaler
  * for lux computation acceleration
  */
-#define FW_ALS_RESULT_SCALER                  0x120
+#define FW_ALS_RESULT_SCALER                  (uint16_t) 0x120
 
 
 /**
@@ -624,7 +624,7 @@
  *
  * @ingroup device_regdef
  */
-#define RESULT_INTERRUPT_STATUS_GPIO          0x4F
+#define RESULT_INTERRUPT_STATUS_GPIO          (uint16_t) 0x4F
     /** ranging interrupt 1st bit position in #RESULT_INTERRUPT_STATUS_GPIO */
     #define RES_INT_RANGE_SHIFT  0
     /** ALS interrupt 1st bit position in #RESULT_INTERRUPT_STATUS_GPIO */
@@ -654,33 +654,33 @@
 /**
  * Final range result value presented to the user for use. Unit is in mm.
  */
-#define RESULT_RANGE_VAL                        0x062
+#define RESULT_RANGE_VAL                        (uint16_t) 0x062
 
 /**
  * Raw Range result value with offset applied (no cross talk compensation applied). Unit is in mm.
  */
-#define RESULT_RANGE_RAW                        0x064
+#define RESULT_RANGE_RAW                        (uint16_t) 0x064
 
 /**
  * @brief Sensor count rate of signal returns correlated to IR emitter.
  *
  * Computed from RETURN_SIGNAL_COUNT / RETURN_CONV_TIME. Mcps 9.7 format
  */
-#define RESULT_RANGE_SIGNAL_RATE                0x066
+#define RESULT_RANGE_SIGNAL_RATE                (uint16_t) 0x066
 
 /**
  * @brief Return signal count
  *
  *  Sensor count output value attributed to signal correlated to IR emitter on the Return array.
  */
-#define RESULT_RANGE_RETURN_SIGNAL_COUNT        0x06C
+#define RESULT_RANGE_RETURN_SIGNAL_COUNT        (uint16_t) 0x06C
 
 /**
  * @brief Reference signal count
  *
  * sensor count output value attributed to signal correlated to IR emitter on the Reference array.
  */
-#define RESULT_RANGE_REFERENCE_SIGNAL_COUNT     0x070
+#define RESULT_RANGE_REFERENCE_SIGNAL_COUNT     (uint16_t) 0x070
 
 /**
  * @brief Return ambient count
@@ -688,24 +688,24 @@
  * sensor count output value attributed to uncorrelated ambient signal on the Return array.
  * Must be multiplied by 6 if used to calculate the ambient to signal threshold
  */
-#define RESULT_RANGE_RETURN_AMB_COUNT           0x074
+#define RESULT_RANGE_RETURN_AMB_COUNT           (uint16_t) 0x074
 
 /**
  * @brief   Reference ambient count
  *
  * Sensor count output value attributed to uncorrelated ambient signal on the Reference array.
  */
-#define RESULT_RANGE_REFERENCE_AMB_COUNT        0x078
+#define RESULT_RANGE_REFERENCE_AMB_COUNT        (uint16_t) 0x078
 
 /**
  * sensor count output value attributed to signal on the Return array.
  */
-#define RESULT_RANGE_RETURN_CONV_TIME           0x07C
+#define RESULT_RANGE_RETURN_CONV_TIME           (uint16_t) 0x07C
 
 /**
  * sensor count output value attributed to signal on the Reference array.
  */
-#define RESULT_RANGE_REFERENCE_CONV_TIME        0x080
+#define RESULT_RANGE_REFERENCE_CONV_TIME        (uint16_t) 0x080
 
 
 /**
@@ -714,7 +714,7 @@
  *
  * Never should  user write directly onto that register directly \a VL6180x_UpscaleSetScaling()
  */
-#define RANGE_SCALER                            0x096
+#define RANGE_SCALER                            (uint16_t) 0x096
 
 /**
  * @def READOUT_AVERAGING_SAMPLE_PERIOD
@@ -729,7 +729,7 @@
  *
  * see datasheet for more detail
  */
-#define READOUT_AVERAGING_SAMPLE_PERIOD     0x10A
+#define READOUT_AVERAGING_SAMPLE_PERIOD     (uint16_t) 0x10A
 
 /**
  * @def I2C_SLAVE_DEVICE_ADDRESS
@@ -738,6 +738,6 @@
  * so what prohamd is commonly whar ergfer as adrerss /2
  * @sa VL6180x_SetI2CAddress()
  */
-#define I2C_SLAVE_DEVICE_ADDRESS               0x212
+#define I2C_SLAVE_DEVICE_ADDRESS               (uint16_t) 0x212
 
 #endif /* _VL6180x_DEF */
--- a/x_nucleo_6180xa1.h	Fri Sep 11 14:39:00 2015 +0200
+++ b/x_nucleo_6180xa1.h	Thu Sep 17 10:41:21 2015 +0200
@@ -47,9 +47,32 @@
 #include "GPIO_expander_class.h"
 #include "DevI2C.h"
 
-
+/* Classes--------------------------------------------------------------------*/
 
-
+/*class X_NUCLEO_6180XA1
+{
+	protected:
+	
+	public:
+		X_NUCLEO_VL6180XA1(); // verificare se necessario passare come argomento un dev_i2c
+		bool VL6180X_Init(); // controlla la presenza del sensore , chiamo Prepare() e setto ready
+		// modificare l'indirizzo i2c dei sensori (slave address)
+		
+			
+		VL6180X *vl6180x_sensor;
+		Display *display;
+		GPIO_expander *gpio_expander;
+		DevI2C *dev_i2c;
+		
+		//posso creare 4 puntatori a istanze di tipo VL6180X, con i nomi della board(S1, left, bottom, right)
+		//oppure posso creare un vettore di VL6180X di dimensione 4 per contenere tutti i sensori
+		//dopo tengo traccia di quali sensori sono presenti e funzionanti 
+		
+	private:
+		static X_NUCLEO_IKS01A1 *instance;
+		
+}
+*/