ST / IIS2DLPC

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IIS2DLPCSensor.cpp Source File

IIS2DLPCSensor.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    IIS2DLPCSensor.cpp
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    15 November 2018
00007  * @brief   Implementation of an IIS2DLPC Inertial Measurement Unit (IMU) 3 axes
00008  *          sensor.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 
00042 #include "IIS2DLPCSensor.h"
00043 
00044 
00045 /* Class Implementation ------------------------------------------------------*/
00046 
00047 /** Constructor
00048  * @param i2c object of an helper class which handles the I2C peripheral
00049  * @param address the address of the component's instance
00050  * @param int1_pin the interrupt 1 pin
00051  * @param int2_pin the interrupt 2 pin
00052  */
00053 IIS2DLPCSensor::IIS2DLPCSensor(DevI2C *i2c, uint8_t address, PinName int1_pin, PinName int2_pin) :
00054                              _dev_i2c(i2c), _address(address), _cs_pin(NC), _int1_irq(int1_pin), _int2_irq(int2_pin)
00055 {
00056   assert (i2c);
00057   _dev_spi = NULL;
00058   _reg_ctx.write_reg = IIS2DLPC_io_write;
00059   _reg_ctx.read_reg = IIS2DLPC_io_read;
00060   _reg_ctx.handle = (void *)this;
00061 }
00062 
00063 /** Constructor
00064  * @param spi object of an helper class which handles the SPI peripheral
00065  * @param cs_pin the chip select pin
00066  * @param int1_pin the interrupt 1 pin
00067  * @param int2_pin the interrupt 2 pin
00068  * @param spi_type the SPI type (4-Wires or 3-Wires)
00069  */
00070 IIS2DLPCSensor::IIS2DLPCSensor(SPI *spi, PinName cs_pin, PinName int1_pin, PinName int2_pin, SPI_type_t spi_type ) : 
00071                              _dev_spi(spi), _cs_pin(cs_pin), _int1_irq(int1_pin), _int2_irq(int2_pin), _spi_type(spi_type)
00072 {
00073   assert (spi);
00074   if (cs_pin == NC) 
00075   {
00076     printf ("ERROR LPS22HBSensor CS MUST NOT BE NC\n\r");       
00077     _dev_spi = NULL;
00078     _dev_i2c = NULL;
00079     return;
00080   }
00081   _reg_ctx.write_reg = IIS2DLPC_io_write;
00082   _reg_ctx.read_reg = IIS2DLPC_io_read;
00083   _reg_ctx.handle = (void *)this;
00084   _cs_pin = 1;    
00085   _dev_i2c = NULL;
00086   _address = 0;
00087     
00088   if (_spi_type == SPI3W)
00089   {
00090     /* Enable SPI 3-Wires on the component */
00091     uint8_t data = 0x05;
00092     iis2dlpc_write_reg(&_reg_ctx, IIS2DLPC_CTRL2, &data, 1);
00093   }
00094 
00095   /* Disable I2C on the component */
00096   iis2dlpc_i2c_interface_set(&_reg_ctx, IIS2DLPC_I2C_DISABLE);    
00097 }
00098 
00099 /**
00100  * @brief     Initializing the component.
00101  * @param[in] init pointer to device specific initalization structure.
00102  * @retval    "0" in case of success, an error code otherwise.
00103  */
00104 int IIS2DLPCSensor::init(void *init)
00105 {
00106   /* Enable register address automatically incremented during a multiple byte
00107   access with a serial interface. */
00108   if (iis2dlpc_auto_increment_set(&_reg_ctx, PROPERTY_ENABLE) != 0)
00109   {
00110     return 1;
00111   }
00112 
00113   /* Enable BDU */
00114   if (iis2dlpc_block_data_update_set(&_reg_ctx, PROPERTY_ENABLE) != 0)
00115   {
00116     return 1;
00117   }
00118 
00119   /* FIFO mode selection */
00120   if (iis2dlpc_fifo_mode_set(&_reg_ctx, IIS2DLPC_BYPASS_MODE) != 0)
00121   {
00122     return 1;
00123   }
00124 
00125   /* Power mode selection */
00126   if (iis2dlpc_power_mode_set(&_reg_ctx, IIS2DLPC_HIGH_PERFORMANCE) != 0)
00127   {
00128     return 1;
00129   }
00130 
00131   /* Output data rate selection - power down. */
00132   if (iis2dlpc_data_rate_set(&_reg_ctx, IIS2DLPC_XL_ODR_OFF) != 0)
00133   {
00134     return 1;
00135   }
00136 
00137   /* Full scale selection. */
00138   if (iis2dlpc_full_scale_set(&_reg_ctx, IIS2DLPC_2g) != 0)
00139   {
00140     return 1;
00141   }
00142 
00143   /* Select default output data rate. */
00144   _x_last_odr = 100.0f;
00145 
00146   _x_last_operating_mode = IIS2DLPC_HIGH_PERFORMANCE_MODE;
00147 
00148   _x_last_noise = IIS2DLPC_LOW_NOISE_DISABLE;
00149 
00150   _x_is_enabled = 0;
00151 
00152   return 0;
00153 }
00154 
00155 /**
00156  * @brief  Enable IIS2DLPC Accelerator
00157  * @retval 0 in case of success, an error code otherwise
00158  */
00159 int IIS2DLPCSensor::enable_x(void)
00160 { 
00161   /* Check if the component is already enabled */
00162   if ( _x_is_enabled == 1 )
00163   {
00164     return 0;
00165   }
00166   
00167   /* Output data rate selection. */
00168   if ( set_x_odr_when_enabled( _x_last_odr, _x_last_operating_mode, _x_last_noise ) == 1 )
00169   {
00170     return 1;
00171   }
00172   
00173   _x_is_enabled = 1;
00174   
00175   return 0;
00176 }
00177 
00178 /**
00179  * @brief  Disable IIS2DLPC Accelerator
00180  * @retval 0 in case of success, an error code otherwise
00181  */
00182 int IIS2DLPCSensor::disable_x(void)
00183 { 
00184   /* Check if the component is already disabled */
00185   if ( _x_is_enabled == 0 )
00186   {
00187     return 0;
00188   }
00189 
00190   /* Output data rate selection - power down. */
00191   if (iis2dlpc_data_rate_set(&_reg_ctx, IIS2DLPC_XL_ODR_OFF) != 0)
00192   {
00193     return 1;
00194   }
00195   
00196   _x_is_enabled = 0;
00197   
00198   return 0;
00199 }
00200 
00201 /**
00202  * @brief  Read ID of IIS2DLPC Accelerometer and Gyroscope
00203  * @param  p_id the pointer where the ID of the device is stored
00204  * @retval 0 in case of success, an error code otherwise
00205  */
00206 int IIS2DLPCSensor::read_id(uint8_t *id)
00207 {
00208   if(!id)
00209   { 
00210     return 1;
00211   }
00212 
00213   /* Read WHO AM I register */
00214   if (iis2dlpc_device_id_get(&_reg_ctx, id) != 0)
00215   {
00216     return 1;
00217   }
00218 
00219   return 0;
00220 }
00221 
00222 /**
00223  * @brief  Read data from IIS2DLPC Accelerometer
00224  * @param  acceleration the pointer where the accelerometer data are stored
00225  * @retval 0 in case of success, an error code otherwise
00226  */
00227 int IIS2DLPCSensor::get_x_axes(int32_t *acceleration)
00228 {
00229   int16_t data_raw[3];
00230   float sensitivity = 0;
00231   
00232   /* Read raw data from IIS2DLPC output register. */
00233   if ( get_x_axes_raw( data_raw ) == 1 )
00234   {
00235     return 1;
00236   }
00237   
00238   /* Get IIS2DLPC actual sensitivity. */
00239   if ( get_x_sensitivity( &sensitivity ) == 1 )
00240   {
00241     return 1;
00242   }
00243   
00244   /* Calculate the data. */
00245   acceleration[0] = ( int32_t )( data_raw[0] * sensitivity );
00246   acceleration[1] = ( int32_t )( data_raw[1] * sensitivity );
00247   acceleration[2] = ( int32_t )( data_raw[2] * sensitivity );
00248   
00249   return 0;
00250 }
00251 
00252 /**
00253  * @brief  Read Accelerometer Sensitivity
00254  * @param  sensitivity the pointer where the accelerometer sensitivity is stored
00255  * @retval 0 in case of success, an error code otherwise
00256  */
00257 int IIS2DLPCSensor::get_x_sensitivity(float *sensitivity)
00258 {
00259   int32_t ret = 0;
00260   iis2dlpc_fs_t full_scale;
00261   iis2dlpc_mode_t mode;
00262 
00263   /* Read actual full scale selection from sensor. */
00264   if (iis2dlpc_full_scale_get(&_reg_ctx, &full_scale) != 0)
00265   {
00266     return 1;
00267   }
00268 
00269   /* Read actual power mode selection from sensor. */
00270   if (iis2dlpc_power_mode_get(&_reg_ctx, &mode) != 0)
00271   {
00272     return 1;
00273   }
00274 
00275   switch(mode)
00276   {
00277     case IIS2DLPC_CONT_LOW_PWR_12bit:
00278     case IIS2DLPC_SINGLE_LOW_PWR_12bit:
00279     case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_12bit:
00280     case IIS2DLPC_SINGLE_LOW_LOW_NOISE_PWR_12bit:
00281       switch (full_scale)
00282       {
00283         case IIS2DLPC_2g:
00284           *sensitivity = IIS2DLPC_ACC_SENSITIVITY_FOR_FS_2G_LOPOW1_MODE;
00285            break;
00286 
00287         case IIS2DLPC_4g:
00288           *sensitivity = IIS2DLPC_ACC_SENSITIVITY_FOR_FS_4G_LOPOW1_MODE;
00289           break;
00290 
00291         case IIS2DLPC_8g:
00292           *sensitivity = IIS2DLPC_ACC_SENSITIVITY_FOR_FS_8G_LOPOW1_MODE;
00293            break;
00294 
00295         case IIS2DLPC_16g:
00296           *sensitivity = IIS2DLPC_ACC_SENSITIVITY_FOR_FS_16G_LOPOW1_MODE;
00297           break;
00298 
00299         default:
00300           *sensitivity = -1.0f;
00301           ret = 1;
00302           break;
00303       }
00304       break;
00305 
00306     case IIS2DLPC_HIGH_PERFORMANCE:
00307     case IIS2DLPC_CONT_LOW_PWR_4:
00308     case IIS2DLPC_CONT_LOW_PWR_3:
00309     case IIS2DLPC_CONT_LOW_PWR_2:
00310     case IIS2DLPC_SINGLE_LOW_PWR_4:
00311     case IIS2DLPC_SINGLE_LOW_PWR_3:
00312     case IIS2DLPC_SINGLE_LOW_PWR_2:
00313     case IIS2DLPC_HIGH_PERFORMANCE_LOW_NOISE:
00314     case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_4:
00315     case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_3:
00316     case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_2:
00317     case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_4:
00318     case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_3:
00319     case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_2:
00320       switch (full_scale)
00321       {
00322         case IIS2DLPC_2g:
00323           *sensitivity = IIS2DLPC_ACC_SENSITIVITY_FOR_FS_2G_OTHER_MODES;
00324            break;
00325 
00326         case IIS2DLPC_4g:
00327           *sensitivity = IIS2DLPC_ACC_SENSITIVITY_FOR_FS_4G_OTHER_MODES;
00328           break;
00329 
00330         case IIS2DLPC_8g:
00331           *sensitivity = IIS2DLPC_ACC_SENSITIVITY_FOR_FS_8G_OTHER_MODES;
00332            break;
00333 
00334         case IIS2DLPC_16g:
00335           *sensitivity = IIS2DLPC_ACC_SENSITIVITY_FOR_FS_16G_OTHER_MODES;
00336           break;
00337 
00338         default:
00339           *sensitivity = -1.0f;
00340           ret = 1;
00341           break;
00342       }
00343       break;
00344 
00345     default:
00346       *sensitivity = -1.0f;
00347       ret = 1;
00348       break;
00349   }
00350 
00351   return ret;
00352 }
00353 
00354 /**
00355  * @brief  Read raw data from IIS2DLPC Accelerometer
00356  * @param  value the pointer where the accelerometer raw data are stored
00357  * @retval 0 in case of success, an error code otherwise
00358  */
00359 int IIS2DLPCSensor::get_x_axes_raw(int16_t *value)
00360 {
00361   axis3bit16_t data_raw;
00362   iis2dlpc_mode_t mode;
00363   int32_t ret = 0;
00364 
00365   /* Read actual power mode selection from sensor. */
00366   if (iis2dlpc_power_mode_get(&_reg_ctx, &mode) != 0)
00367   {
00368     return 1;
00369   }
00370 
00371   /* Read raw data values. */
00372   if (iis2dlpc_acceleration_raw_get(&_reg_ctx, data_raw.u8bit) != 0)
00373   {
00374     return 1;
00375   }
00376 
00377   switch(mode)
00378   {
00379     case IIS2DLPC_CONT_LOW_PWR_12bit:
00380     case IIS2DLPC_SINGLE_LOW_PWR_12bit:
00381     case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_12bit:
00382     case IIS2DLPC_SINGLE_LOW_LOW_NOISE_PWR_12bit:
00383       /* Data format 12 bits. */
00384       value[0] = (data_raw.i16bit[0] / 16);
00385       value[1] = (data_raw.i16bit[1] / 16);
00386       value[2] = (data_raw.i16bit[2] / 16);
00387       break;
00388 
00389     case IIS2DLPC_HIGH_PERFORMANCE:
00390     case IIS2DLPC_CONT_LOW_PWR_4:
00391     case IIS2DLPC_CONT_LOW_PWR_3:
00392     case IIS2DLPC_CONT_LOW_PWR_2:
00393     case IIS2DLPC_SINGLE_LOW_PWR_4:
00394     case IIS2DLPC_SINGLE_LOW_PWR_3:
00395     case IIS2DLPC_SINGLE_LOW_PWR_2:
00396     case IIS2DLPC_HIGH_PERFORMANCE_LOW_NOISE:
00397     case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_4:
00398     case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_3:
00399     case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_2:
00400     case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_4:
00401     case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_3:
00402     case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_2:
00403       /* Data format 14 bits. */
00404       value[0] = (data_raw.i16bit[0] / 4);
00405       value[1] = (data_raw.i16bit[1] / 4);
00406       value[2] = (data_raw.i16bit[2] / 4);
00407       break;
00408 
00409     default:
00410       ret = 1;
00411       break;
00412   }
00413 
00414   return ret;
00415 }
00416 
00417 /**
00418  * @brief  Read IIS2DLPC Accelerometer output data rate
00419  * @param  odr the pointer to the output data rate
00420  * @retval 0 in case of success, an error code otherwise
00421  */
00422 int IIS2DLPCSensor::get_x_odr(float* odr)
00423 {
00424   int32_t ret = 0;
00425   iis2dlpc_odr_t odr_low_level;
00426   iis2dlpc_mode_t mode;
00427 
00428   /* Get current output data rate. */
00429   if (iis2dlpc_data_rate_get(&_reg_ctx, &odr_low_level) != 0)
00430   {
00431     return 1;
00432   }
00433 
00434   /* Read actual power mode selection from sensor. */
00435   if (iis2dlpc_power_mode_get(&_reg_ctx, &mode) != 0)
00436   {
00437     return 1;
00438   }
00439 
00440   switch (odr_low_level)
00441   {
00442     case IIS2DLPC_XL_ODR_OFF:
00443     case IIS2DLPC_XL_SET_SW_TRIG:
00444     case IIS2DLPC_XL_SET_PIN_TRIG:
00445       *odr = 0.0f;
00446       break;
00447 
00448     case IIS2DLPC_XL_ODR_1Hz6_LP_ONLY:
00449       switch (mode)
00450       {
00451         case IIS2DLPC_HIGH_PERFORMANCE:
00452         case IIS2DLPC_HIGH_PERFORMANCE_LOW_NOISE:
00453           *odr = 12.5f;
00454            break;
00455 
00456         case IIS2DLPC_CONT_LOW_PWR_4:
00457         case IIS2DLPC_CONT_LOW_PWR_3:
00458         case IIS2DLPC_CONT_LOW_PWR_2:
00459         case IIS2DLPC_CONT_LOW_PWR_12bit:
00460         case IIS2DLPC_SINGLE_LOW_PWR_4:
00461         case IIS2DLPC_SINGLE_LOW_PWR_3:
00462         case IIS2DLPC_SINGLE_LOW_PWR_2:
00463         case IIS2DLPC_SINGLE_LOW_PWR_12bit:
00464         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_4:
00465         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_3:
00466         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_2:
00467         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_12bit:
00468         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_4:
00469         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_3:
00470         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_2:
00471         case IIS2DLPC_SINGLE_LOW_LOW_NOISE_PWR_12bit:
00472           *odr = 1.6f;
00473           break;
00474 
00475         default:
00476           *odr = -1.0f;
00477           ret = 1;
00478           break;
00479       }
00480       break;
00481 
00482     case IIS2DLPC_XL_ODR_12Hz5:
00483       *odr = 12.5f;
00484       break;
00485 
00486     case IIS2DLPC_XL_ODR_25Hz:
00487       *odr = 25.0f;
00488       break;
00489 
00490     case IIS2DLPC_XL_ODR_50Hz:
00491       *odr = 50.0f;
00492       break;
00493 
00494     case IIS2DLPC_XL_ODR_100Hz:
00495       *odr = 100.0f;
00496       break;
00497 
00498     case IIS2DLPC_XL_ODR_200Hz:
00499       *odr = 200.0f;
00500       break;
00501 
00502     case IIS2DLPC_XL_ODR_400Hz:
00503       switch (mode)
00504       {
00505         case IIS2DLPC_HIGH_PERFORMANCE:
00506         case IIS2DLPC_HIGH_PERFORMANCE_LOW_NOISE:
00507           *odr = 400.0f;
00508            break;
00509 
00510         case IIS2DLPC_CONT_LOW_PWR_4:
00511         case IIS2DLPC_CONT_LOW_PWR_3:
00512         case IIS2DLPC_CONT_LOW_PWR_2:
00513         case IIS2DLPC_CONT_LOW_PWR_12bit:
00514         case IIS2DLPC_SINGLE_LOW_PWR_4:
00515         case IIS2DLPC_SINGLE_LOW_PWR_3:
00516         case IIS2DLPC_SINGLE_LOW_PWR_2:
00517         case IIS2DLPC_SINGLE_LOW_PWR_12bit:
00518         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_4:
00519         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_3:
00520         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_2:
00521         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_12bit:
00522         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_4:
00523         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_3:
00524         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_2:
00525         case IIS2DLPC_SINGLE_LOW_LOW_NOISE_PWR_12bit:
00526           *odr = 200.0f;
00527           break;
00528 
00529         default:
00530           *odr = -1.0f;
00531           ret = 1;
00532           break;
00533       }
00534       break;
00535 
00536     case IIS2DLPC_XL_ODR_800Hz:
00537       switch (mode)
00538       {
00539         case IIS2DLPC_HIGH_PERFORMANCE:
00540         case IIS2DLPC_HIGH_PERFORMANCE_LOW_NOISE:
00541           *odr = 800.0f;
00542            break;
00543 
00544         case IIS2DLPC_CONT_LOW_PWR_4:
00545         case IIS2DLPC_CONT_LOW_PWR_3:
00546         case IIS2DLPC_CONT_LOW_PWR_2:
00547         case IIS2DLPC_CONT_LOW_PWR_12bit:
00548         case IIS2DLPC_SINGLE_LOW_PWR_4:
00549         case IIS2DLPC_SINGLE_LOW_PWR_3:
00550         case IIS2DLPC_SINGLE_LOW_PWR_2:
00551         case IIS2DLPC_SINGLE_LOW_PWR_12bit:
00552         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_4:
00553         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_3:
00554         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_2:
00555         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_12bit:
00556         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_4:
00557         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_3:
00558         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_2:
00559         case IIS2DLPC_SINGLE_LOW_LOW_NOISE_PWR_12bit:
00560           *odr = 200.0f;
00561           break;
00562 
00563         default:
00564           *odr = -1.0f;
00565           ret = 1;
00566           break;
00567       }
00568       break;
00569 
00570     case IIS2DLPC_XL_ODR_1k6Hz:
00571       switch (mode)
00572       {
00573         case IIS2DLPC_HIGH_PERFORMANCE:
00574         case IIS2DLPC_HIGH_PERFORMANCE_LOW_NOISE:
00575           *odr = 1600.0f;
00576            break;
00577 
00578         case IIS2DLPC_CONT_LOW_PWR_4:
00579         case IIS2DLPC_CONT_LOW_PWR_3:
00580         case IIS2DLPC_CONT_LOW_PWR_2:
00581         case IIS2DLPC_CONT_LOW_PWR_12bit:
00582         case IIS2DLPC_SINGLE_LOW_PWR_4:
00583         case IIS2DLPC_SINGLE_LOW_PWR_3:
00584         case IIS2DLPC_SINGLE_LOW_PWR_2:
00585         case IIS2DLPC_SINGLE_LOW_PWR_12bit:
00586         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_4:
00587         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_3:
00588         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_2:
00589         case IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_12bit:
00590         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_4:
00591         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_3:
00592         case IIS2DLPC_SINGLE_LOW_PWR_LOW_NOISE_2:
00593         case IIS2DLPC_SINGLE_LOW_LOW_NOISE_PWR_12bit:
00594           *odr = 200.0f;
00595           break;
00596 
00597         default:
00598           *odr = -1.0f;
00599           ret = 1;
00600           break;
00601       }
00602       break;
00603 
00604     default:
00605       *odr = -1.0f;
00606       ret = 1;
00607       break;
00608   }
00609 
00610   return ret;
00611 }
00612 
00613 /**
00614  * @brief  Set IIS2DLPC Accelerometer output data rate
00615  * @param  odr the output data rate to be set
00616  * @retval 0 in case of success, an error code otherwise
00617  */
00618 int IIS2DLPCSensor::set_x_odr(float odr)
00619 { 
00620   return set_x_odr_with_mode(odr, IIS2DLPC_HIGH_PERFORMANCE_MODE, IIS2DLPC_LOW_NOISE_DISABLE);
00621 }
00622 
00623 /**
00624  * @brief  Set IIS2DLPC Accelerometer output data rate
00625  * @param  odr the output data rate to be set
00626  * @param  mode the operating mode to be used
00627  * @param  noise the low noise option
00628  * @retval 0 in case of success, an error code otherwise
00629  */
00630 int IIS2DLPCSensor::set_x_odr_with_mode(float odr, IIS2DLPC_Operating_Mode_t mode, IIS2DLPC_Low_Noise_t noise)
00631 {
00632   if(_x_is_enabled == 1)
00633   {
00634     if(set_x_odr_when_enabled(odr, mode, noise) != 0)
00635     {
00636       return 1;
00637     }
00638   }
00639   else
00640   {
00641     if(set_x_odr_when_disabled(odr, mode, noise) != 0)
00642     {
00643       return 1;
00644     }
00645   }
00646   
00647   return 0;
00648 }
00649 
00650 /**
00651  * @brief  Set IIS2DLPC Accelerometer output data rate when enabled
00652  * @param  odr the output data rate to be set
00653  * @param  mode the operating mode to be used
00654  * @param  noise the low noise option
00655  * @retval 0 in case of success, an error code otherwise
00656  */
00657 int IIS2DLPCSensor::set_x_odr_when_enabled(float odr, IIS2DLPC_Operating_Mode_t mode, IIS2DLPC_Low_Noise_t noise)
00658 {
00659   iis2dlpc_odr_t new_odr;
00660   iis2dlpc_mode_t new_power_mode;
00661 
00662   switch (mode)
00663   {
00664     case IIS2DLPC_HIGH_PERFORMANCE_MODE:
00665     default:
00666       switch (noise)
00667       {
00668         case IIS2DLPC_LOW_NOISE_DISABLE:
00669         default:
00670           new_power_mode = IIS2DLPC_HIGH_PERFORMANCE; 
00671           break;
00672         case IIS2DLPC_LOW_NOISE_ENABLE:
00673           new_power_mode = IIS2DLPC_HIGH_PERFORMANCE_LOW_NOISE;
00674           break;
00675       }
00676 
00677       /* If High Performance mode minimum ODR is 12.5Hz */
00678       if(odr < 12.5f)
00679       {
00680         odr = 12.5f;
00681       }
00682       break;
00683     case IIS2DLPC_LOW_POWER_MODE4:
00684       switch (noise)
00685       {
00686         case IIS2DLPC_LOW_NOISE_DISABLE:
00687         default:
00688           new_power_mode = IIS2DLPC_CONT_LOW_PWR_4; 
00689           break;
00690         case IIS2DLPC_LOW_NOISE_ENABLE:
00691           new_power_mode = IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_4;
00692           break;
00693       }
00694 
00695       /* If Low Power mode maximum ODR is 200Hz */
00696       if(odr > 200.0f)
00697       {
00698         odr = 200.0f;
00699       }
00700       break;
00701     case IIS2DLPC_LOW_POWER_MODE3:
00702       switch (noise)
00703       {
00704         case IIS2DLPC_LOW_NOISE_DISABLE:
00705         default:
00706           new_power_mode = IIS2DLPC_CONT_LOW_PWR_3; 
00707           break;
00708         case IIS2DLPC_LOW_NOISE_ENABLE:
00709           new_power_mode = IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_3;
00710           break;
00711       }
00712 
00713       /* If Low Power mode maximum ODR is 200Hz */
00714       if(odr > 200.0f)
00715       {
00716         odr = 200.0f;
00717       }
00718       break;
00719     case IIS2DLPC_LOW_POWER_MODE2:
00720       switch (noise)
00721       {
00722         case IIS2DLPC_LOW_NOISE_DISABLE:
00723         default:
00724           new_power_mode = IIS2DLPC_CONT_LOW_PWR_2; 
00725           break;
00726         case IIS2DLPC_LOW_NOISE_ENABLE:
00727           new_power_mode = IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_2;
00728           break;
00729       }
00730 
00731       /* If Low Power mode maximum ODR is 200Hz */
00732       if(odr > 200.0f)
00733       {
00734         odr = 200.0f;
00735       }
00736       break;
00737     case IIS2DLPC_LOW_POWER_MODE1:
00738       switch (noise)
00739       {
00740         case IIS2DLPC_LOW_NOISE_DISABLE:
00741         default:
00742           new_power_mode = IIS2DLPC_CONT_LOW_PWR_12bit; 
00743           break;
00744         case IIS2DLPC_LOW_NOISE_ENABLE:
00745           new_power_mode = IIS2DLPC_CONT_LOW_PWR_LOW_NOISE_12bit;
00746           break;
00747       }
00748 
00749       /* If Low Power mode maximum ODR is 200Hz */
00750       if(odr > 200.0f)
00751       {
00752         odr = 200.0f;
00753       }
00754       break;
00755   }
00756   
00757 
00758   new_odr = (odr <=    1.6f) ? IIS2DLPC_XL_ODR_1Hz6_LP_ONLY
00759           : (odr <=   12.5f) ? IIS2DLPC_XL_ODR_12Hz5
00760           : (odr <=   25.0f) ? IIS2DLPC_XL_ODR_25Hz
00761           : (odr <=   50.0f) ? IIS2DLPC_XL_ODR_50Hz
00762           : (odr <=  100.0f) ? IIS2DLPC_XL_ODR_100Hz
00763           : (odr <=  200.0f) ? IIS2DLPC_XL_ODR_200Hz
00764           : (odr <=  400.0f) ? IIS2DLPC_XL_ODR_400Hz
00765           : (odr <=  800.0f) ? IIS2DLPC_XL_ODR_800Hz
00766           :                    IIS2DLPC_XL_ODR_1k6Hz;
00767 
00768   /* Output data rate selection. */
00769   if (iis2dlpc_data_rate_set(&_reg_ctx, new_odr) != 0)
00770   {
00771     return 1;
00772   }
00773 
00774   /* Power mode selection. */
00775   if (iis2dlpc_power_mode_set(&_reg_ctx, new_power_mode) != 0)
00776   {
00777     return 1;
00778   }
00779 
00780   /* Store actual output data rate, operating mode and low noise. */
00781   _x_last_odr = odr;
00782   _x_last_operating_mode = mode;
00783   _x_last_noise = noise;
00784 
00785   return 0;
00786 }
00787 
00788 /**
00789  * @brief  Set IIS2DLPC Accelerometer output data rate when disabled
00790  * @param  odr the output data rate to be set
00791  * @param  mode the operating mode to be used
00792  * @param  noise the low noise option
00793  * @retval 0 in case of success, an error code otherwise
00794  */
00795 int IIS2DLPCSensor::set_x_odr_when_disabled(float odr, IIS2DLPC_Operating_Mode_t mode, IIS2DLPC_Low_Noise_t noise)
00796 {
00797   _x_last_operating_mode = mode;
00798   _x_last_noise = noise;
00799 
00800   _x_last_odr = (odr <=    1.6f) ?    1.6f
00801               : (odr <=   12.5f) ?   12.5f
00802               : (odr <=   25.0f) ?   25.0f
00803               : (odr <=   50.0f) ?   50.0f
00804               : (odr <=  100.0f) ?  100.0f
00805               : (odr <=  200.0f) ?  200.0f
00806               : (odr <=  400.0f) ?  400.0f
00807               : (odr <=  800.0f) ?  800.0f
00808               :                    1600.0f;
00809                                  
00810   return 0;
00811 }
00812 
00813 /**
00814  * @brief  Read IIS2DLPC Accelerometer full scale
00815  * @param  full_scale the pointer to the full scale
00816  * @retval 0 in case of success, an error code otherwise
00817  */
00818 int IIS2DLPCSensor::get_x_fs(float* full_scale)
00819 {
00820   int32_t ret = 0;
00821   iis2dlpc_fs_t fs_low_level;
00822 
00823   /* Read actual full scale selection from sensor. */
00824   if (iis2dlpc_full_scale_get(&_reg_ctx, &fs_low_level) != 0)
00825   {
00826     return 1;
00827   }
00828 
00829   switch (fs_low_level)
00830   {
00831     case IIS2DLPC_2g:
00832       *full_scale =  2;
00833       break;
00834 
00835     case IIS2DLPC_4g:
00836       *full_scale =  4;
00837       break;
00838 
00839     case IIS2DLPC_8g:
00840       *full_scale =  8;
00841       break;
00842 
00843     case IIS2DLPC_16g:
00844       *full_scale = 16;
00845       break;
00846 
00847     default:
00848       *full_scale = -1;
00849       ret = 1;
00850       break;
00851   }
00852 
00853   return ret;
00854 }
00855 
00856 /**
00857  * @brief  Set IIS2DLPC Accelerometer full scale
00858  * @param  full_scale the full scale to be set
00859  * @retval 0 in case of success, an error code otherwise
00860  */
00861 int IIS2DLPCSensor::set_x_fs(float full_scale)
00862 {
00863   iis2dlpc_fs_t new_fs;
00864 
00865   /* Seems like MISRA C-2012 rule 14.3a violation but only from single file statical analysis point of view because
00866      the parameter passed to the function is not known at the moment of analysis */
00867   new_fs = (full_scale <= 2) ? IIS2DLPC_2g
00868          : (full_scale <= 4) ? IIS2DLPC_4g
00869          : (full_scale <= 8) ? IIS2DLPC_8g
00870          :                    IIS2DLPC_16g;
00871 
00872   if (iis2dlpc_full_scale_set(&_reg_ctx, new_fs) != 0)
00873   {
00874     return 1;
00875   }
00876 
00877   return 0;
00878 }
00879 
00880 /**
00881  * @brief Enable the wake up detection for IIS2DLPC accelerometer sensor
00882  * @note  This function sets the IIS2DLPC accelerometer ODR to 200Hz and the IIS2DLPC accelerometer full scale to 2g
00883  * @retval 0 in case of success, an error code otherwise
00884  */
00885 int IIS2DLPCSensor::enable_wake_up_detection(void)
00886 {
00887   int32_t ret = 0;
00888   iis2dlpc_ctrl4_int1_pad_ctrl_t val;
00889 
00890   /* Output Data Rate selection */
00891   if (set_x_odr(200.0f) != 0)
00892   {
00893     return 1;
00894   }
00895 
00896   /* Full scale selection */
00897   if (set_x_fs(2) != 0)
00898   {
00899     return 1;
00900   }
00901 
00902   /* WAKE_DUR setting */
00903   if (iis2dlpc_wkup_dur_set(&_reg_ctx, 0x00) != 0)
00904   {
00905     return 1;
00906   }
00907 
00908   /* Set wake up threshold. */
00909   if (iis2dlpc_wkup_threshold_set(&_reg_ctx, 0x02) != 0)
00910   {
00911     return 1;
00912   }
00913 
00914   if (iis2dlpc_pin_int1_route_get(&_reg_ctx, &val) != 0)
00915   {
00916     return 1;
00917   }
00918 
00919   val.int1_wu = PROPERTY_ENABLE;
00920 
00921   if (iis2dlpc_pin_int1_route_set(&_reg_ctx, &val) != 0)
00922   {
00923     return 1;
00924   }
00925 
00926   return ret;
00927 }
00928 
00929 /**
00930  * @brief Disable the wake up detection for IIS2DLPC accelerometer sensor
00931  * @retval 0 in case of success, an error code otherwise
00932  */
00933 int IIS2DLPCSensor::disable_wake_up_detection(void)
00934 {
00935   iis2dlpc_ctrl4_int1_pad_ctrl_t ctrl4_int1_reg;
00936   iis2dlpc_ctrl5_int2_pad_ctrl_t ctrl5_int2_reg;
00937   iis2dlpc_ctrl_reg7_t ctrl_reg7;
00938 
00939   /* Disable wake up event on INT1 pin. */
00940   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0)
00941   {
00942     return 1;
00943   }
00944 
00945   ctrl4_int1_reg.int1_wu = PROPERTY_DISABLE;
00946 
00947   if (iis2dlpc_write_reg(&_reg_ctx, IIS2DLPC_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0)
00948   {
00949     return 1;
00950   }
00951 
00952   /* Read INT2 Sleep Change */
00953   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0)
00954   {
00955     return 1;
00956   }
00957 
00958   /*Disable Interrupts bit if none event is still enabled */
00959   if(ctrl5_int2_reg.int2_sleep_chg == 0 && ctrl4_int1_reg.int1_wu == 0 && ctrl4_int1_reg.int1_6d == 0)
00960   {
00961     if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0)
00962     {
00963       return 1;
00964     }
00965 
00966     ctrl_reg7.interrupts_enable = PROPERTY_DISABLE;
00967 
00968     if (iis2dlpc_write_reg(&_reg_ctx, IIS2DLPC_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0)
00969     {
00970       return 1;
00971     }
00972   }
00973 
00974   /* Reset wake up threshold. */
00975   if (iis2dlpc_wkup_threshold_set(&_reg_ctx, 0x00) != 0)
00976   {
00977     return 1;
00978   }
00979 
00980   /* WAKE_DUR setting */
00981   if (iis2dlpc_wkup_dur_set(&_reg_ctx, 0x00) != 0)
00982   {
00983     return 1;
00984   }
00985 
00986   return 0;
00987 }
00988 
00989 /**
00990  * @brief Set the wake up threshold for IIS2DLPC accelerometer sensor
00991  * @param thr the threshold to be set
00992  * @retval 0 in case of success, an error code otherwise
00993  */
00994 int IIS2DLPCSensor::set_wake_up_threshold(uint8_t thr)
00995 {
00996   /* Set wake up threshold. */
00997   if (iis2dlpc_wkup_threshold_set(&_reg_ctx, thr) != 0)
00998   {
00999     return 1;
01000   }
01001 
01002   return 0;
01003 }
01004 
01005 /**
01006  * @brief Set the wake up duration for IIS2DLPC accelerometer sensor
01007  * @param dur the duration to be set
01008  * @retval 0 in case of success, an error code otherwise
01009  */
01010 int IIS2DLPCSensor::set_wake_up_duration(uint8_t dur)
01011 {
01012   /* Set wake up duration. */
01013   if (iis2dlpc_wkup_dur_set(&_reg_ctx, dur) != 0)
01014   {
01015     return 1;
01016   }
01017 
01018   return 0;
01019 }
01020 
01021 /**
01022  * @brief Enable the inactivity detection for IIS2DLPC accelerometer sensor
01023  * @retval 0 in case of success, an error code otherwise
01024  */
01025 int IIS2DLPCSensor::enable_inactivity_detection(void)
01026 {
01027   int32_t ret = 0;
01028   iis2dlpc_ctrl5_int2_pad_ctrl_t val;
01029 
01030   /* Output Data Rate and Full scale must be selected externally */
01031 
01032   /* SLEEP_DUR setting */
01033   if (iis2dlpc_act_sleep_dur_set(&_reg_ctx, 0x01) != 0)
01034   {
01035     return 1;
01036   }
01037 
01038   /* Set wake up threshold. */
01039   if (iis2dlpc_wkup_threshold_set(&_reg_ctx, 0x02) != 0)
01040   {
01041     return 1;
01042   }
01043 
01044   /* Enable inactivity detection. */
01045   if (iis2dlpc_act_mode_set(&_reg_ctx, IIS2DLPC_DETECT_ACT_INACT) != 0)
01046   {
01047     return 1;
01048   }
01049 
01050   if (iis2dlpc_pin_int2_route_get(&_reg_ctx, &val) != 0)
01051   {
01052     return 1;
01053   }
01054 
01055   val.int2_sleep_chg = PROPERTY_ENABLE;
01056 
01057   if (iis2dlpc_pin_int2_route_set(&_reg_ctx, &val) != 0)
01058   {
01059     return 1;
01060   }
01061 
01062   return ret;
01063 }
01064 
01065 /**
01066  * @brief Disable the inactivity detection for IIS2DLPC accelerometer sensor
01067  * @retval 0 in case of success, an error code otherwise
01068  */
01069 int IIS2DLPCSensor::disable_inactivity_detection(void)
01070 {
01071   iis2dlpc_ctrl4_int1_pad_ctrl_t ctrl4_int1_reg;
01072   iis2dlpc_ctrl5_int2_pad_ctrl_t ctrl5_int2_reg;
01073   iis2dlpc_ctrl_reg7_t ctrl_reg7;
01074 
01075   /* Disable inactivity event on INT2 pin */
01076   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0)
01077   {
01078     return 1;
01079   }
01080 
01081   ctrl5_int2_reg.int2_sleep_chg = PROPERTY_DISABLE;
01082 
01083   if (iis2dlpc_write_reg(&_reg_ctx, IIS2DLPC_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0)
01084   {
01085     return 1;
01086   }
01087 
01088   /* Read INT1 Wake Up event and INT1 6D Orientation event */
01089   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0)
01090   {
01091     return 1;
01092   }
01093 
01094   /*Disable Interrupts bit if none event is still enabled */
01095   if(ctrl5_int2_reg.int2_sleep_chg == 0 && ctrl4_int1_reg.int1_wu == 0 && ctrl4_int1_reg.int1_6d == 0)
01096   {
01097     if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0)
01098     {
01099       return 1;
01100     }
01101 
01102     ctrl_reg7.interrupts_enable = PROPERTY_DISABLE;
01103 
01104     if (iis2dlpc_write_reg(&_reg_ctx, IIS2DLPC_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0)
01105     {
01106       return 1;
01107     }
01108   }
01109 
01110   /* Disable inactivity detection. */
01111   if (iis2dlpc_act_mode_set(&_reg_ctx, IIS2DLPC_NO_DETECTION) != 0)
01112   {
01113     return 1;
01114   }
01115 
01116   /* Reset wake up threshold. */
01117   if (iis2dlpc_wkup_threshold_set(&_reg_ctx, 0x00) != 0)
01118   {
01119     return 1;
01120   }
01121 
01122   /* SLEEP_DUR setting */
01123   if (iis2dlpc_act_sleep_dur_set(&_reg_ctx, 0x00) != 0)
01124   {
01125     return 1;
01126   }
01127 
01128   return 0;
01129 }
01130 
01131 /**
01132  * @brief Set the sleep duration for IIS2DLPC accelerometer sensor
01133  * @param dur the duration to be set
01134  * @retval 0 in case of success, an error code otherwise
01135  */
01136 int IIS2DLPCSensor::set_sleep_duration(uint8_t dur)
01137 {
01138   /* Set sleep duration. */
01139   if (iis2dlpc_act_sleep_dur_set(&_reg_ctx, dur) != 0)
01140   {
01141     return 1;
01142   }
01143 
01144   return 0;
01145 }
01146 
01147 /**
01148  * @brief Enable the 6D orientation detection for IIS2DLPC accelerometer sensor
01149  * @note  This function sets the IIS2DLPC accelerometer ODR to 200Hz and the IIS2DLPC accelerometer full scale to 2g
01150  * @retval 0 in case of success, an error code otherwise
01151  */
01152 int IIS2DLPCSensor::enable_6d_orientation(void)
01153 {
01154   int32_t ret = 0;
01155   iis2dlpc_ctrl4_int1_pad_ctrl_t val;
01156 
01157   /* Output Data Rate selection */
01158   if(set_x_odr(200.0f) == 1)
01159   {
01160     return 1;
01161   }
01162   
01163   /* Full scale selection. */
01164   if(set_x_fs(2.0f) == 1)
01165   {
01166     return 1;
01167   }
01168 
01169   /* 6D orientation threshold. */
01170   if (iis2dlpc_6d_threshold_set(&_reg_ctx, 2) != 0) /* 60 degrees */
01171   {
01172     return 1;
01173   }
01174 
01175   /* Enable 6D orientation event on INT1 pin */
01176   if (iis2dlpc_pin_int1_route_get(&_reg_ctx, &val) != 0)
01177   {
01178     return 1;
01179   }
01180 
01181   val.int1_6d = PROPERTY_ENABLE;
01182 
01183   if (iis2dlpc_pin_int1_route_set(&_reg_ctx, &val) != 0)
01184   {
01185     return 1;
01186   }
01187 
01188   return ret;
01189 }
01190 
01191 /**
01192  * @brief Disable the 6D orientation detection for IIS2DLPC accelerometer sensor
01193  * @retval 0 in case of success, an error code otherwise
01194  */
01195 int IIS2DLPCSensor::disable_6d_orientation(void)
01196 {
01197   iis2dlpc_ctrl4_int1_pad_ctrl_t ctrl4_int1_reg;
01198   iis2dlpc_ctrl5_int2_pad_ctrl_t ctrl5_int2_reg;
01199   iis2dlpc_ctrl_reg7_t ctrl_reg7;
01200 
01201   /* Disable 6D orientation event on INT1 pin */
01202   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0)
01203   {
01204     return 1;
01205   }
01206 
01207   ctrl4_int1_reg.int1_6d = PROPERTY_DISABLE;
01208 
01209   if (iis2dlpc_write_reg(&_reg_ctx, IIS2DLPC_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0)
01210   {
01211     return 1;
01212   }
01213 
01214   /* Read INT2 Sleep Change */
01215   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0)
01216   {
01217     return 1;
01218   }
01219 
01220   /*Disable Interrupts bit if none event is still enabled */
01221   if(ctrl5_int2_reg.int2_sleep_chg == 0 && ctrl4_int1_reg.int1_wu == 0 && ctrl4_int1_reg.int1_6d == 0)
01222   {
01223     if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0)
01224     {
01225       return 1;
01226     }
01227 
01228     ctrl_reg7.interrupts_enable = PROPERTY_DISABLE;
01229 
01230     if (iis2dlpc_write_reg(&_reg_ctx, IIS2DLPC_CTRL_REG7, (uint8_t *)&ctrl_reg7, 1) != 0)
01231     {
01232       return 1;
01233     }
01234   }
01235 
01236   /* Reset 6D orientation threshold. */
01237   if (iis2dlpc_6d_threshold_set(&_reg_ctx, 0) != 0)
01238   {
01239     return 1;
01240   }
01241 
01242   return 0;
01243 }
01244 
01245 /**
01246  * @brief Set the 6D orientation threshold for IIS2DLPC accelerometer sensor
01247  * @param thr the threshold to be set
01248  * @retval 0 in case of success, an error code otherwise
01249  */
01250 int IIS2DLPCSensor::set_6d_orientation_threshold(uint8_t thr)
01251 {
01252   if(thr > 3)
01253   {
01254     return 1;
01255   }
01256 
01257   if (iis2dlpc_6d_threshold_set(&_reg_ctx, thr) != 0)
01258   {
01259     return 1;
01260   }
01261 
01262   return 0;
01263 }
01264 
01265 /**
01266  * @brief Get the 6D orientation XL axis for IIS2DLPC accelerometer sensor
01267  * @param xl the pointer to the 6D orientation XL axis
01268  * @retval 0 in case of success, an error code otherwise
01269  */
01270 int IIS2DLPCSensor::get_6d_orientation_xl(uint8_t *xl)
01271 {
01272   iis2dlpc_sixd_src_t data;
01273 
01274   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_SIXD_SRC, (uint8_t *)&data, 1) != 0)
01275   {
01276     return 1;
01277   }
01278 
01279   *xl = data.xl;
01280 
01281   return 0;
01282 }
01283 
01284 /**
01285  * @brief Get the 6D orientation XH axis for IIS2DLPC accelerometer sensor
01286  * @param xh the pointer to the 6D orientation XH axis
01287  * @retval 0 in case of success, an error code otherwise
01288  */
01289 int IIS2DLPCSensor::get_6d_orientation_xh(uint8_t *xh)
01290 {
01291   iis2dlpc_sixd_src_t data;
01292 
01293   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_SIXD_SRC, (uint8_t *)&data, 1) != 0)
01294   {
01295     return 1;
01296   }
01297 
01298   *xh = data.xh;
01299 
01300   return 0;
01301 }
01302 
01303 /**
01304  * @brief Get the 6D orientation YL axis for IIS2DLPC accelerometer sensor
01305  * @param yl the pointer to the 6D orientation YL axis
01306  * @retval 0 in case of success, an error code otherwise
01307  */
01308 int IIS2DLPCSensor::get_6d_orientation_yl(uint8_t *yl)
01309 {
01310   iis2dlpc_sixd_src_t data;
01311 
01312   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_SIXD_SRC, (uint8_t *)&data, 1) != 0)
01313   {
01314     return 1;
01315   }
01316 
01317   *yl = data.yl;
01318 
01319   return 0;
01320 }
01321 
01322 /**
01323  * @brief Get the 6D orientation YH axis for IIS2DLPC accelerometer sensor
01324  * @param yh the pointer to the 6D orientation YH axis
01325  * @retval 0 in case of success, an error code otherwise
01326  */
01327 int IIS2DLPCSensor::get_6d_orientation_yh(uint8_t *yh)
01328 {
01329   iis2dlpc_sixd_src_t data;
01330 
01331   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_SIXD_SRC, (uint8_t *)&data, 1) != 0)
01332   {
01333     return 1;
01334   }
01335 
01336   *yh = data.yh;
01337 
01338   return 0;
01339 }
01340 
01341 /**
01342  * @brief Get the 6D orientation ZL axis for IIS2DLPC accelerometer sensor
01343  * @param zl the pointer to the 6D orientation ZL axis
01344  * @retval 0 in case of success, an error code otherwise
01345  */
01346 int IIS2DLPCSensor::get_6d_orientation_zl(uint8_t *zl)
01347 {
01348   iis2dlpc_sixd_src_t data;
01349 
01350   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_SIXD_SRC, (uint8_t *)&data, 1) != 0)
01351   {
01352     return 1;
01353   }
01354 
01355   *zl = data.zl;
01356 
01357   return 0;
01358 }
01359 
01360 /**
01361  * @brief Get the 6D orientation ZH axis for IIS2DLPC accelerometer sensor
01362  * @param zh the pointer to the 6D orientation ZH axis
01363  * @retval 0 in case of success, an error code otherwise
01364  */
01365 int IIS2DLPCSensor::get_6d_orientation_zh(uint8_t *zh)
01366 {
01367   iis2dlpc_sixd_src_t data;
01368 
01369   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_SIXD_SRC, (uint8_t *)&data, 1) != 0)
01370   {
01371     return 1;
01372   }
01373 
01374   *zh = data.zh;
01375 
01376   return 0;
01377 }
01378 
01379 /**
01380  * @brief Get the status of all hardware events for IIS2DLPC accelerometer sensor
01381  * @param status the pointer to the status of all hardware events
01382  * @retval 0 in case of success, an error code otherwise
01383  */
01384 int IIS2DLPCSensor::get_event_status(IIS2DLPC_Event_Status_t *status)
01385 {
01386   iis2dlpc_status_t status_reg;
01387   iis2dlpc_ctrl4_int1_pad_ctrl_t ctrl4_int1_reg;
01388   iis2dlpc_ctrl5_int2_pad_ctrl_t ctrl5_int2_reg;
01389 
01390   (void)memset((void *)status, 0x0, sizeof(IIS2DLPC_Event_Status_t));
01391 
01392   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_STATUS, (uint8_t *)&status_reg, 1) != 0)
01393   {
01394     return 1;
01395   }
01396 
01397   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL4_INT1_PAD_CTRL, (uint8_t *)&ctrl4_int1_reg, 1) != 0)
01398   {
01399     return 1;
01400   }
01401 
01402   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_CTRL5_INT2_PAD_CTRL, (uint8_t *)&ctrl5_int2_reg, 1) != 0)
01403   {
01404     return 1;
01405   }
01406 
01407   if (ctrl4_int1_reg.int1_wu == 1U)
01408   {
01409     if (status_reg.wu_ia == 1U)
01410     {
01411       status->WakeUpStatus = 1;
01412     }
01413   }
01414 
01415   if (ctrl4_int1_reg.int1_6d == 1U)
01416   {
01417     if (status_reg._6d_ia == 1U)
01418     {
01419       status->D6DOrientationStatus = 1;
01420     }
01421   }
01422 
01423   if (ctrl5_int2_reg.int2_sleep_chg == 1U)
01424   {
01425     if (status_reg.sleep_state == 1U)
01426     {
01427       status->SleepStatus = 1;
01428     }
01429   }
01430 
01431   return 0;
01432 }
01433 
01434 /**
01435  * @brief  Get the number of samples contained into the FIFO
01436  * @param  num_samples the number of samples contained into the FIFO
01437  * @retval 0 in case of success, an error code otherwise
01438  */
01439 int IIS2DLPCSensor::get_fifo_num_samples(uint16_t *num_samples)
01440 {
01441   iis2dlpc_fifo_samples_t fifo_samples;
01442 
01443   if (iis2dlpc_read_reg(&_reg_ctx, IIS2DLPC_FIFO_SAMPLES, (uint8_t *)&fifo_samples, 1) != 0)
01444   {
01445     return 1;
01446   }
01447 
01448   if(fifo_samples.diff == 0x20)
01449   {
01450     *num_samples = 32;
01451   }
01452   else
01453   {
01454     *num_samples = fifo_samples.diff;
01455   }
01456 
01457   return 0;
01458 }
01459 
01460 /**
01461  * @brief  Set the FIFO mode
01462  * @param  mode FIFO mode
01463  * @retval 0 in case of success, an error code otherwise
01464  */
01465 int IIS2DLPCSensor::set_fifo_mode(uint8_t mode)
01466 {
01467   int32_t ret = 0;
01468 
01469   /* Verify that the passed parameter contains one of the valid values. */
01470   switch ((iis2dlpc_fmode_t)mode)
01471   {
01472     case IIS2DLPC_BYPASS_MODE:
01473     case IIS2DLPC_FIFO_MODE:
01474     case IIS2DLPC_STREAM_TO_FIFO_MODE:
01475     case IIS2DLPC_BYPASS_TO_STREAM_MODE:
01476     case IIS2DLPC_STREAM_MODE:
01477       break;
01478 
01479     default:
01480       ret = 1;
01481       break;
01482   }
01483 
01484   if (ret == 1)
01485   {
01486     return ret;
01487   }
01488 
01489   if (iis2dlpc_fifo_mode_set(&_reg_ctx, (iis2dlpc_fmode_t)mode) != 0)
01490   {
01491     return 1;
01492   }
01493 
01494   return ret;
01495 }
01496 
01497 /**
01498  * @brief Read the data from register
01499  * @param reg register address
01500  * @param data register data
01501  * @retval 0 in case of success, an error code otherwise
01502  */
01503 int IIS2DLPCSensor::read_reg(uint8_t reg, uint8_t *data)
01504 {
01505 
01506   if (iis2dlpc_read_reg(&_reg_ctx, reg, data, 1) != 0)
01507   {
01508     return 1;
01509   }
01510 
01511   return 0;
01512 }
01513 
01514 /**
01515  * @brief Write the data to register
01516  * @param reg register address
01517  * @param data register data
01518  * @retval 0 in case of success, an error code otherwise
01519  */
01520 int IIS2DLPCSensor::write_reg(uint8_t reg, uint8_t data)
01521 {
01522 
01523   if (iis2dlpc_write_reg(&_reg_ctx, reg, &data, 1) != 0)
01524   {
01525     return 1;
01526   }
01527 
01528   return 0;
01529 }
01530 
01531 
01532 int32_t IIS2DLPC_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
01533 {
01534   return ((IIS2DLPCSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
01535 }
01536 
01537 int32_t IIS2DLPC_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
01538 {
01539   return ((IIS2DLPCSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
01540 }